kaspad/mining/test_utils.go
Ori Newman 0aa7c430e0 Dev 364 implement the dag fee structure (#181)
* [DEV-364] Add fee transactions validation

* [DEV-364] make NextBlockFeeTransactions for creating block templates

* [DEV-364] apply coinbase rules to fee transaction is some cases

* [DEV-364] Add comments

* [DEV-364] put getTXO as separate function

* [DEV-364] Make getParentsFeeData a separate function

* [DEV-364] fix calculateFees

* [DEV-364] force maximum sequence for fee transactions

* [DEV-364] add TestValidateFeeTransactions

* [DEV-364] change fee transaction to be one tx per block rather than one tx for each blue

* [DEV-364] fix tests

* [DEV-364] Use constants instead of inline numbers
2019-02-11 17:33:55 +02:00

97 lines
2.6 KiB
Go

package mining
// This file functions are not considered safe for regular use, and should be used for test purposes only.
import (
"fmt"
"time"
"github.com/daglabs/btcd/dagconfig"
"github.com/daglabs/btcd/blockdag"
"github.com/daglabs/btcd/dagconfig/daghash"
"github.com/daglabs/btcd/txscript"
"github.com/daglabs/btcd/util"
"github.com/daglabs/btcd/wire"
)
// fakeTxSource is a simple implementation of TxSource interface
type fakeTxSource struct {
txDescs []*TxDesc
}
func (txs *fakeTxSource) LastUpdated() time.Time {
return time.Unix(0, 0)
}
func (txs *fakeTxSource) MiningDescs() []*TxDesc {
return txs.txDescs
}
func (txs *fakeTxSource) HaveTransaction(txID *daghash.TxID) bool {
for _, desc := range txs.txDescs {
if *desc.Tx.ID() == *txID {
return true
}
}
return false
}
// PrepareBlockForTest generates a block with the proper merkle roots, coinbase/fee transactions etc. This function is used for test purposes only
func PrepareBlockForTest(dag *blockdag.BlockDAG, params *dagconfig.Params, parentHashes []daghash.Hash, transactions []*wire.MsgTx) (*wire.MsgBlock, error) {
newVirtual, err := blockdag.GetVirtualFromParentsForTest(dag, parentHashes)
if err != nil {
return nil, err
}
oldVirtual := blockdag.SetVirtualForTest(dag, newVirtual)
defer blockdag.SetVirtualForTest(dag, oldVirtual)
policy := Policy{
BlockMaxSize: 50000,
BlockPrioritySize: 750000,
TxMinFreeFee: util.Amount(0),
}
txSource := &fakeTxSource{
txDescs: make([]*TxDesc, len(transactions)),
}
for i, tx := range transactions {
txSource.txDescs[i] = &TxDesc{
Tx: util.NewTx(tx),
}
}
blockTemplateGenerator := NewBlkTmplGenerator(&policy,
params, txSource, dag, blockdag.NewMedianTime(), txscript.NewSigCache(100000))
template, err := blockTemplateGenerator.NewBlockTemplate(nil)
if err != nil {
return nil, err
}
// In order of creating deterministic coinbase tx ids.
blockTemplateGenerator.UpdateExtraNonce(template.Block, dag.Height()+1, GenerateDeterministicExtraNonceForTest())
for _, tx := range transactions {
found := false
for _, blockTx := range template.Block.Transactions {
if blockTx.TxHash() == tx.TxHash() {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("tx %v wasn't found in the block", tx.TxHash())
}
}
return template.Block, nil
}
// GenerateDeterministicExtraNonceForTest returns a unique deterministic extra nonce for coinbase data, in order to create unique coinbase transactions.
func GenerateDeterministicExtraNonceForTest() uint64 {
extraNonceForTest++
return extraNonceForTest
}
var extraNonceForTest = uint64(0)