mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-09 07:36:43 +00:00
[NOD-1256] Optimize PrepareBlockForTest (#861)
* [NOD-1256] Optimize PrepareBlockForTest * [NOD-1256] Remove redundant comment
This commit is contained in:
parent
91f4ed9825
commit
22dc3f998f
@ -51,14 +51,14 @@ func TestBlueBlockWindow(t *testing.T) {
|
|||||||
expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"D", "C"},
|
parents: []string{"C", "D"},
|
||||||
id: "E",
|
id: "E",
|
||||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"D", "C"},
|
parents: []string{"C", "D"},
|
||||||
id: "F",
|
id: "F",
|
||||||
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"A"},
|
parents: []string{"A"},
|
||||||
@ -73,37 +73,37 @@ func TestBlueBlockWindow(t *testing.T) {
|
|||||||
{
|
{
|
||||||
parents: []string{"H", "F"},
|
parents: []string{"H", "F"},
|
||||||
id: "I",
|
id: "I",
|
||||||
expectedWindowWithGenesisPadding: []string{"F", "D", "C", "B", "A", "A", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"F", "C", "D", "B", "A", "A", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"I"},
|
parents: []string{"I"},
|
||||||
id: "J",
|
id: "J",
|
||||||
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "C", "B", "A", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"I", "F", "C", "D", "B", "A", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"J"},
|
parents: []string{"J"},
|
||||||
id: "K",
|
id: "K",
|
||||||
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "C", "B", "A", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "D", "B", "A", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"K"},
|
parents: []string{"K"},
|
||||||
id: "L",
|
id: "L",
|
||||||
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "C", "B", "A", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "D", "B", "A", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"L"},
|
parents: []string{"L"},
|
||||||
id: "M",
|
id: "M",
|
||||||
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "C", "B", "A", "A"},
|
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "D", "B", "A", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"M"},
|
parents: []string{"M"},
|
||||||
id: "N",
|
id: "N",
|
||||||
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "C", "B", "A"},
|
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "D", "B", "A"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
parents: []string{"N"},
|
parents: []string{"N"},
|
||||||
id: "O",
|
id: "O",
|
||||||
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "C", "B"},
|
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "D", "B"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func TestGHOSTDAG(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
k: 3,
|
k: 3,
|
||||||
expectedReds: []string{"F", "G", "H", "I", "N", "P"},
|
expectedReds: []string{"F", "G", "H", "I", "O", "P"},
|
||||||
dagData: []*testBlockData{
|
dagData: []*testBlockData{
|
||||||
{
|
{
|
||||||
parents: []string{"A"},
|
parents: []string{"A"},
|
||||||
@ -167,7 +167,7 @@ func TestGHOSTDAG(t *testing.T) {
|
|||||||
id: "T",
|
id: "T",
|
||||||
expectedScore: 13,
|
expectedScore: 13,
|
||||||
expectedSelectedParent: "S",
|
expectedSelectedParent: "S",
|
||||||
expectedBlues: []string{"S", "O", "Q"},
|
expectedBlues: []string{"S", "Q", "N"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -245,12 +245,29 @@ func opTrueAddress(prefix util.Bech32Prefix) (util.Address, error) {
|
|||||||
|
|
||||||
// PrepareBlockForTest generates a block with the proper merkle roots, coinbase transaction etc. This function is used for test purposes only
|
// PrepareBlockForTest generates a block with the proper merkle roots, coinbase transaction etc. This function is used for test purposes only
|
||||||
func PrepareBlockForTest(dag *BlockDAG, parentHashes []*daghash.Hash, transactions []*domainmessage.MsgTx) (*domainmessage.MsgBlock, error) {
|
func PrepareBlockForTest(dag *BlockDAG, parentHashes []*daghash.Hash, transactions []*domainmessage.MsgTx) (*domainmessage.MsgBlock, error) {
|
||||||
newVirtual, err := GetVirtualFromParentsForTest(dag, parentHashes)
|
parents := newBlockSet()
|
||||||
|
for _, hash := range parentHashes {
|
||||||
|
parent, ok := dag.index.LookupNode(hash)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.Errorf("parent %s was not found", hash)
|
||||||
|
}
|
||||||
|
parents.add(parent)
|
||||||
|
}
|
||||||
|
node, _ := dag.newBlockNode(nil, parents)
|
||||||
|
|
||||||
|
_, selectedParentPastUTXO, txsAcceptanceData, err := dag.pastUTXO(node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
oldVirtual := SetVirtualForTest(dag, newVirtual)
|
|
||||||
defer SetVirtualForTest(dag, oldVirtual)
|
calculatedAccepetedIDMerkleRoot := calculateAcceptedIDMerkleRoot(txsAcceptanceData)
|
||||||
|
|
||||||
|
multiset, err := node.calcMultiset(dag, txsAcceptanceData, selectedParentPastUTXO)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
calculatedMultisetHash := daghash.Hash(*multiset.Finalize())
|
||||||
|
|
||||||
OpTrueAddr, err := opTrueAddress(dag.Params.Prefix)
|
OpTrueAddr, err := opTrueAddress(dag.Params.Prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -265,7 +282,12 @@ func PrepareBlockForTest(dag *BlockDAG, parentHashes []*daghash.Hash, transactio
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
blockTransactions[0], err = dag.NextCoinbaseFromAddress(OpTrueAddr, coinbasePayloadExtraData)
|
coinbasePayloadScriptPubKey, err := txscript.PayToAddrScript(OpTrueAddr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
blockTransactions[0], err = node.expectedCoinbaseTransaction(dag, txsAcceptanceData, coinbasePayloadScriptPubKey, coinbasePayloadExtraData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -285,14 +307,31 @@ func PrepareBlockForTest(dag *BlockDAG, parentHashes []*daghash.Hash, transactio
|
|||||||
return subnetworkid.Less(&blockTransactions[i].MsgTx().SubnetworkID, &blockTransactions[j].MsgTx().SubnetworkID)
|
return subnetworkid.Less(&blockTransactions[i].MsgTx().SubnetworkID, &blockTransactions[j].MsgTx().SubnetworkID)
|
||||||
})
|
})
|
||||||
|
|
||||||
block, err := dag.BlockForMining(blockTransactions)
|
// Create a new block ready to be solved.
|
||||||
|
hashMerkleTree := BuildHashMerkleTreeStore(blockTransactions)
|
||||||
|
|
||||||
|
var msgBlock domainmessage.MsgBlock
|
||||||
|
for _, tx := range blockTransactions {
|
||||||
|
msgBlock.AddTransaction(tx.MsgTx())
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := dag.calcNextBlockVersion(node.selectedParent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
block.Header.Timestamp = dag.NextBlockMinimumTime()
|
|
||||||
block.Header.Bits = dag.NextRequiredDifficulty(block.Header.Timestamp)
|
|
||||||
|
|
||||||
return block, nil
|
timestamp := node.parents.bluest().PastMedianTime(dag)
|
||||||
|
msgBlock.Header = domainmessage.BlockHeader{
|
||||||
|
Version: version,
|
||||||
|
ParentHashes: parentHashes,
|
||||||
|
HashMerkleRoot: hashMerkleTree.Root(),
|
||||||
|
AcceptedIDMerkleRoot: calculatedAccepetedIDMerkleRoot,
|
||||||
|
UTXOCommitment: &calculatedMultisetHash,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Bits: dag.requiredDifficulty(node.parents.bluest(), timestamp),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &msgBlock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareAndProcessBlockForTest prepares a block that points to the given parent
|
// PrepareAndProcessBlockForTest prepares a block that points to the given parent
|
||||||
|
Loading…
x
Reference in New Issue
Block a user