kaspad/util/testtools/testtools.go
Dan Aharoni ea6f7a28c2 [NOD-420] Process delayed blocks (#529)
* [NOD-420] Delay blocks with valid timestamp (non-delayed) that point to a delayed block.

* [NOD-420] Mark block as requested when setting as delayed.

* [NOD-420] Merge master; Use dag.timeSource.AdjustedTime() instead of time.Now;

* [NOD-420] Return nil when not expecting an error

* [NOD-420] Initialise delyaed blocks mapping

* [NOD-420] Trigger delayed blocks processing every time we process a block.

* [NOD-420] Hold the read lock in processDelayedBlocks

* [NOD-420] Add delayed blocks heap sorted by their process time so we could process them in order.

* [NOD-420] Update debug log

* [NOD-420] Fix process blocks loop

* [NOD-420] Add comment

* [NOD-420] Log error message

* [NOD-420] Implement peek method for delayed block heap. extract delayed block processing to another  function.

* [NOD-420] Trigger process delayed blocks only in process block

* [NOD-420] Move delayed block addition to process block

* [NOD-420] Use process block to make sure we fully process the delayed block and deal with orphans.

* [NOD-420] Unexport functions when not needed; Return isDelayed boolean from ProcessBlock instead of the delay duration

* [NOd-420] Remove redundant delayedBlocksLock

* [NOD-420] Resolve merge conflict; Return delay 0 instead of boolean

* [NOD-420] Do not treat delayed block as orphan

* [NOD-420] Make sure block is not processed if we have already sa delayed.

* [NOD-420] Process delayed block if parent is delayed to make sure it would not be treated as orphan.

* [NOD-420] Rename variable

* [NOD-420] Rename function. Move maxDelayOfParents to process.go

* [NOD-420] Fix typo

* [NOD-420] Handle errors from processDelayedBlocks properly

* [NOD-420] Return default values if err != nil from dag.addDelayedBlock

* [NOD-420] Return default values if err != nil from dag.addDelayedBlock in another place

Co-authored-by: Svarog <feanorr@gmail.com>
2020-01-08 15:28:52 +02:00

97 lines
2.9 KiB
Go

package testtools
import (
"github.com/kaspanet/kaspad/dagconfig"
"github.com/pkg/errors"
"github.com/kaspanet/kaspad/mining"
"github.com/kaspanet/kaspad/util/daghash"
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/txscript"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/subnetworkid"
"github.com/kaspanet/kaspad/wire"
)
// RegisterSubnetworkForTest is used to register network on DAG with specified gas limit
func RegisterSubnetworkForTest(dag *blockdag.BlockDAG, params *dagconfig.Params, gasLimit uint64) (*subnetworkid.SubnetworkID, error) {
buildNextBlock := func(parentHashes []*daghash.Hash, txs []*wire.MsgTx) (*util.Block, error) {
msgBlock, err := mining.PrepareBlockForTest(dag, params, parentHashes, txs, false)
if err != nil {
return nil, err
}
return util.NewBlock(msgBlock), nil
}
addBlockToDAG := func(block *util.Block) error {
isOrphan, isDelayed, err := dag.ProcessBlock(block, blockdag.BFNoPoWCheck)
if err != nil {
return err
}
if isDelayed {
return errors.Errorf("ProcessBlock: block is is too far in the future")
}
if isOrphan {
return errors.Errorf("ProcessBlock: unexpected returned orphan block")
}
return nil
}
// Create a block in order to fund later transactions
fundsBlock, err := buildNextBlock(dag.TipHashes(), []*wire.MsgTx{})
if err != nil {
return nil, errors.Errorf("could not build funds block: %s", err)
}
err = addBlockToDAG(fundsBlock)
if err != nil {
return nil, errors.Errorf("could not add funds block to DAG: %s", err)
}
fundsBlockCbTx := fundsBlock.Transactions()[0].MsgTx()
// Create a block with a valid subnetwork registry transaction
signatureScript, err := txscript.PayToScriptHashSignatureScript(blockdag.OpTrueScript, nil)
if err != nil {
return nil, errors.Errorf("Failed to build signature script: %s", err)
}
txIn := &wire.TxIn{
PreviousOutpoint: *wire.NewOutpoint(fundsBlockCbTx.TxID(), 0),
Sequence: wire.MaxTxInSequenceNum,
SignatureScript: signatureScript,
}
scriptPubKey, err := txscript.PayToScriptHashScript(blockdag.OpTrueScript)
if err != nil {
return nil, err
}
txOut := &wire.TxOut{
ScriptPubKey: scriptPubKey,
Value: fundsBlockCbTx.TxOut[0].Value,
}
registryTx := wire.NewRegistryMsgTx(1, []*wire.TxIn{txIn}, []*wire.TxOut{txOut}, gasLimit)
// Add it to the DAG
registryBlock, err := buildNextBlock([]*daghash.Hash{fundsBlock.Hash()}, []*wire.MsgTx{registryTx})
if err != nil {
return nil, errors.Errorf("could not build registry block: %s", err)
}
err = addBlockToDAG(registryBlock)
if err != nil {
return nil, errors.Errorf("could not add registry block to DAG: %s", err)
}
// Build a subnetwork ID from the registry transaction
subnetworkID, err := blockdag.TxToSubnetworkID(registryTx)
if err != nil {
return nil, errors.Errorf("could not build subnetwork ID: %s", err)
}
return subnetworkID, nil
}