From 705d3f6b071f29952c673a9ce401127ba7228c1a Mon Sep 17 00:00:00 2001 From: Paladz Date: Tue, 13 Mar 2018 17:10:32 +0800 Subject: [PATCH] Hash static len (#422) * edit hash uint64 method * edit unit test --- config/genesis.go | 2 +- protocol/bc/asset_test.go | 4 ++-- protocol/bc/entry.go | 5 ++++- protocol/bc/legacy/block_test.go | 2 +- protocol/bc/legacy/transaction_test.go | 10 +++++----- protocol/bc/legacy/tx_test.go | 4 ++-- protocol/bc/merkle_test.go | 6 +++--- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/config/genesis.go b/config/genesis.go index 33c409ff..b204e0c8 100644 --- a/config/genesis.go +++ b/config/genesis.go @@ -47,7 +47,7 @@ func GenerateGenesisBlock() *legacy.Block { BlockHeader: legacy.BlockHeader{ Version: 1, Height: 0, - Nonce: 4216080, + Nonce: 4216083, Timestamp: 1516788453, BlockCommitment: legacy.BlockCommitment{ TransactionsMerkleRoot: merkleRoot, diff --git a/protocol/bc/asset_test.go b/protocol/bc/asset_test.go index c5b6c4a3..77566a04 100644 --- a/protocol/bc/asset_test.go +++ b/protocol/bc/asset_test.go @@ -12,8 +12,8 @@ func TestComputeAssetID(t *testing.T) { assetID := ComputeAssetID(issuanceScript, &initialBlockHash, 1, &EmptyStringHash) unhashed := append([]byte{}, initialBlockHash.Bytes()...) - unhashed = append(unhashed, 0x01) // vmVersion - unhashed = append(unhashed, 0x01) // length of issuanceScript + unhashed = append(unhashed, []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}...) // vmVersion + unhashed = append(unhashed, 0x01) // length of issuanceScript unhashed = append(unhashed, issuanceScript...) unhashed = append(unhashed, EmptyStringHash.Bytes()...) want := NewAssetID(sha3.Sum256(unhashed)) diff --git a/protocol/bc/entry.go b/protocol/bc/entry.go index 525ac0f8..48c4662d 100644 --- a/protocol/bc/entry.go +++ b/protocol/bc/entry.go @@ -1,6 +1,7 @@ package bc import ( + "encoding/binary" "fmt" "io" "reflect" @@ -83,7 +84,9 @@ func writeForHash(w io.Writer, c interface{}) error { _, err := w.Write([]byte{v}) return errors.Wrap(err, "writing byte for hash") case uint64: - _, err := blockchain.WriteVarint63(w, v) + buf := [8]byte{} + binary.LittleEndian.PutUint64(buf[:], v) + _, err := w.Write(buf[:]) return errors.Wrapf(err, "writing uint64 (%d) for hash", v) case []byte: _, err := blockchain.WriteVarstr31(w, v) diff --git a/protocol/bc/legacy/block_test.go b/protocol/bc/legacy/block_test.go index 5281f0b5..f498e44b 100644 --- a/protocol/bc/legacy/block_test.go +++ b/protocol/bc/legacy/block_test.go @@ -126,7 +126,7 @@ func TestEmptyBlock(t *testing.T) { t.Errorf("empty block header bytes = %x want %x", got, want) } - wantHash := mustDecodeHash("a950a33eb49913c06c0c5c0aa643bb55c5cd898f3b54e91971f07ca2123d7204") + wantHash := mustDecodeHash("9609d2e45760f34cbc6c6d948c3fb9b6d7b61552d9d17fdd5b7d0cb5d2e67244") if h := block.Hash(); h != wantHash { t.Errorf("got block hash %x, want %x", h.Bytes(), wantHash.Bytes()) } diff --git a/protocol/bc/legacy/transaction_test.go b/protocol/bc/legacy/transaction_test.go index 17e17053..3e5a18ac 100644 --- a/protocol/bc/legacy/transaction_test.go +++ b/protocol/bc/legacy/transaction_test.go @@ -57,7 +57,7 @@ func TestTransaction(t *testing.T) { "00" + // common witness extensible string length "00" + // inputs count "00"), // outputs count - hash: mustDecodeHash("d7b066ef9e03e9ccea87b6d6769c8d0477bf33554f5fcdee3d5d22fa1be2f2f5"), + hash: mustDecodeHash("b28048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea7d60"), }, { tx: NewTx(TxData{ @@ -70,8 +70,8 @@ func TestTransaction(t *testing.T) { NewTxOutput(bc.AssetID{}, 1000000000000, []byte{1}, []byte("output")), }, }), - hex: ("0701000001012b00030a0908a9b2b6c5394888ab5396f583ae484b8459486b14268e2bef1b637440335eb6c180a094a58d1d05696e7075742903deff1d4319d67baa10a6d26c1fea9c3e8d30e33474efee1a610a9bb49d758d000101010103010203010129000000000000000000000000000000000000000000000000000000000000000080a094a58d1d010101066f757470757400"), // reference data - hash: mustDecodeHash("7603c9dbd98381883f96b9b90d37cedab93879fe03591ceec6c7933e096fc158"), + hex: ("0701000001012b00030a0908fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680a094a58d1d05696e7075742903deff1d4319d67baa10a6d26c1fea9c3e8d30e33474efee1a610a9bb49d758d000101010103010203010129000000000000000000000000000000000000000000000000000000000000000080a094a58d1d010101066f757470757400"), // reference data + hash: mustDecodeHash("9e7584bd2d436b92cfeb2dbb8b12a3750d18e65a8d7b4b1be7d9062b2b8a91d4"), }, { tx: NewTx(TxData{ @@ -85,8 +85,8 @@ func TestTransaction(t *testing.T) { NewTxOutput(assetID, 400000000000, []byte{2}, nil), }, }), - hex: ("0701000001016c016add385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292000000000000000000000000000000000000000000000000000000000000000080a094a58d1d01010101000000000000000000000000000000000000000000000000000000000000000005696e7075740100020129a9b2b6c5394888ab5396f583ae484b8459486b14268e2bef1b637440335eb6c180e0a596bb1101010100000129a9b2b6c5394888ab5396f583ae484b8459486b14268e2bef1b637440335eb6c180c0ee8ed20b0101020000"), // output 1, output witness - hash: mustDecodeHash("e588d0e8ed6fe123550f84f327072be954d9cb023ae8cef7e74faca429f8f091"), + hex: ("0701000001016c016add385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292000000000000000000000000000000000000000000000000000000000000000080a094a58d1d01010101000000000000000000000000000000000000000000000000000000000000000005696e7075740100020129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680e0a596bb1101010100000129fa48ca4e0150f83fbf26cf83211d136313cde98601a667d999ab9cc27b723d4680c0ee8ed20b0101020000"), // output 1, output witness + hash: mustDecodeHash("32b28e965343b7f910a97a61ea84a1add318531dc6b099c98a22e171ee00f8d9"), }, } for i, test := range cases { diff --git a/protocol/bc/legacy/tx_test.go b/protocol/bc/legacy/tx_test.go index 8ca1d5fd..adbfcc55 100644 --- a/protocol/bc/legacy/tx_test.go +++ b/protocol/bc/legacy/tx_test.go @@ -15,11 +15,11 @@ func TestTxHashes(t *testing.T) { }{ { txdata: &TxData{}, - hash: mustDecodeHash("e52b4bc1367e750d88953b78eeb4c8d352504133a04129f53c099cd03e500f22"), + hash: mustDecodeHash("202f9971e4b61016d3d771f7a3506c0fc0a850c76c8469ae13e87f58d11fbb1a"), }, { txdata: sampleTx(), - hash: mustDecodeHash("cee0721776e50e7788e8e727e3635856053a651eec4766f7b4c86c60547d2884"), // todo: verify this value, + hash: mustDecodeHash("9685f51eae04b5f7687d358af6550165e5436825e8f22552fa6b5fa36a4c6c4c"), // todo: verify this value, }, } diff --git a/protocol/bc/merkle_test.go b/protocol/bc/merkle_test.go index 5d61bd09..1080b7ae 100644 --- a/protocol/bc/merkle_test.go +++ b/protocol/bc/merkle_test.go @@ -20,7 +20,7 @@ func TestMerkleRoot(t *testing.T) { []byte("00000"), }, }, - want: mustDecodeHash("323f847f14a3e1dca5a6b84c5daaf84a92a892ef847a76731cf2ada283687e16"), + want: mustDecodeHash("dd26282725467a18bf98ed14e022da9493436dd09372cfeae13080cbaaded00f"), }, { witnesses: [][][]byte{ [][]byte{ @@ -32,7 +32,7 @@ func TestMerkleRoot(t *testing.T) { []byte("111111"), }, }, - want: mustDecodeHash("ed6ad6889d65bb7d917d9de969e559af6bfbc002f4ff0d72b99a2cf0e5cc15f0"), + want: mustDecodeHash("a112fb9aea40e6d8b2e9f443ec326d49bea7b61cd616b4bddeb9ebb010e76bf5"), }, { witnesses: [][][]byte{ [][]byte{ @@ -45,7 +45,7 @@ func TestMerkleRoot(t *testing.T) { []byte("222222"), }, }, - want: mustDecodeHash("ed6ad6889d65bb7d917d9de969e559af6bfbc002f4ff0d72b99a2cf0e5cc15f0"), + want: mustDecodeHash("a112fb9aea40e6d8b2e9f443ec326d49bea7b61cd616b4bddeb9ebb010e76bf5"), }} for _, c := range cases { -- 2.11.0