Implement FeeUnit SatoshisPerKW (#1455)

This commit is contained in:
Ben Carman 2020-05-26 06:11:49 -05:00 committed by GitHub
parent c6aae0dbf9
commit f2d98514b7
2 changed files with 28 additions and 0 deletions

View File

@ -30,6 +30,12 @@ class FeeUnitTest extends BitcoinSUnitTest {
assert(feeRate.calc(tx) == Satoshis(906))
}
it must "calculate the correct fee with a SatoshisPerKW fee rate" in {
val feeRate = SatoshisPerKW(Satoshis(3700))
assert(feeRate.calc(tx) == Satoshis(2416))
}
it must "have symmetry for SatoshisPerByte and SatoshisPerVirtualByte with BaseTransactions" in {
val baseTx = BaseTransaction(
"020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000")

View File

@ -82,3 +82,25 @@ object SatoshisPerVirtualByte {
val zero: SatoshisPerVirtualByte = SatoshisPerVirtualByte(CurrencyUnits.zero)
val one: SatoshisPerVirtualByte = SatoshisPerVirtualByte(Satoshis.one)
}
/**
* Weight is used to indicate how 'expensive' the transaction is on the blockchain.
* This use to be a simple calculation before segwit (BIP141). Each byte in the transaction
* counted as 4 'weight' units. Now with segwit, the
* [[org.bitcoins.core.protocol.transaction.TransactionWitness TransactionWitness]]
* is counted as 1 weight unit per byte,
* while other parts of the transaction (outputs, inputs, locktime etc) count as 4 weight units.
* As we add more witness versions, this may be subject to change.
* [[https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#Transaction_size_calculations BIP 141]]
* [[https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/consensus/validation.h#L96]]
*/
case class SatoshisPerKW(currencyUnit: CurrencyUnit) extends BitcoinFeeUnit {
override def calc(tx: Transaction): CurrencyUnit =
Satoshis((tx.weight * toLong / 1000))
}
object SatoshisPerKW {
val zero: SatoshisPerKW = SatoshisPerKW(CurrencyUnits.zero)
val one: SatoshisPerKW = SatoshisPerKW(Satoshis.one)
}