From: muscle_boy Date: Tue, 19 Feb 2019 08:11:20 +0000 (+0800) Subject: validate block unit test (#1576) X-Git-Tag: 2.0.0-alpha~89^2~46 X-Git-Url: http://git.sourceforge.jp/view?a=commitdiff_plain;h=4dd6e6075a92963e27ccbff5b6372e02b8ff0214;p=bytom%2Fbytom.git validate block unit test (#1576) --- diff --git a/protocol/validation/block_test.go b/protocol/validation/block_test.go index aac57d86..ab0d2d2e 100644 --- a/protocol/validation/block_test.go +++ b/protocol/validation/block_test.go @@ -9,6 +9,7 @@ import ( "github.com/bytom/protocol/bc/types" "github.com/bytom/protocol/state" "github.com/bytom/testutil" + "github.com/bytom/protocol/vm/vmutil" ) func TestCheckBlockTime(t *testing.T) { @@ -188,3 +189,84 @@ func TestValidateBlockHeader(t *testing.T) { } } } + +func TestValidateMerkleRoot(t *testing.T) { + // add (hash, seed) --> (tensority hash) to the tensority cache for avoid + // real matrix calculate cost. + tensority.AIHash.AddCache(&bc.Hash{V0: 0}, &bc.Hash{}, testutil.MaxHash) + tensority.AIHash.AddCache(&bc.Hash{V0: 1}, &bc.Hash{}, testutil.MinHash) + tensority.AIHash.AddCache(&bc.Hash{V0: 1}, consensus.InitialSeed, testutil.MinHash) + + cp, _ := vmutil.DefaultCoinbaseProgram() + cases := []struct { + desc string + block *bc.Block + parent *state.BlockNode + err error + }{ + { + desc: "The calculated transaction merkel root hash is not equals to the hash of the block header", + block: &bc.Block{ + ID: bc.Hash{V0: 1}, + BlockHeader: &bc.BlockHeader{ + Height: 1, + Timestamp: 1523352601, + PreviousBlockId: &bc.Hash{V0: 0}, + Bits: 2305843009214532812, + TransactionsRoot: &bc.Hash{V0: 1}, + }, + Transactions: []*bc.Tx{ + types.MapTx(&types.TxData{ + SerializedSize: 1, + Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)}, + Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 41250000000, cp)}, + }), + }, + }, + parent: &state.BlockNode{ + Height: 0, + Timestamp: 1523352600, + Hash: bc.Hash{V0: 0}, + Seed: &bc.Hash{V1: 1}, + Bits: 2305843009214532812, + }, + err: errMismatchedMerkleRoot, + }, + { + desc: "The calculated transaction status merkel root hash is not equals to the hash of the block header", + block: &bc.Block{ + ID: bc.Hash{V0: 1}, + BlockHeader: &bc.BlockHeader{ + Height: 1, + Timestamp: 1523352601, + PreviousBlockId: &bc.Hash{V0: 0}, + Bits: 2305843009214532812, + TransactionsRoot: &bc.Hash{V0: 6294987741126419124, V1: 12520373106916389157, V2: 5040806596198303681, V3: 1151748423853876189}, + TransactionStatusHash: &bc.Hash{V0: 1}, + }, + Transactions: []*bc.Tx{ + types.MapTx(&types.TxData{ + SerializedSize: 1, + Inputs: []*types.TxInput{types.NewCoinbaseInput(nil)}, + Outputs: []*types.TxOutput{types.NewTxOutput(*consensus.BTMAssetID, 41250000000, cp)}, + }), + }, + }, + parent: &state.BlockNode{ + Height: 0, + Timestamp: 1523352600, + Hash: bc.Hash{V0: 0}, + Seed: &bc.Hash{V1: 1}, + Bits: 2305843009214532812, + }, + err: errMismatchedMerkleRoot, + }, + } + + for i, c := range cases { + err := ValidateBlock(c.block, c.parent) + if rootErr(err) != c.err { + t.Errorf("case #%d (%s) got error %s, want %s", i, c.desc, err, c.err) + } + } +}