BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("3b71527c26bff856a0f247df4fce1b48780f1bd9fd55ba79fb637b0f2e897bb019c5449febf593032dd25b9027cea712c752104700e67d8813326b06d052bf00"),
}
wantHex := strings.Join([]string{
"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
"f3f896d605", // timestamp
"20", // commitment extensible field length
- "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "41403b71527c26bff856a0f247df4fce1b48780f1bd9fd55ba79fb637b0f2e897bb019c5449febf593032dd25b9027cea712c752104700e67d8813326b06d052bf00", // block witness
}, "")
gotHex := testutil.Serialize(t, blockHeader)
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
},
wantHex: strings.Join([]string{
"01", // serialization flags
"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
"e8b287d905", // timestamp
"20", // commitment extensible field length
- "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "41409dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709", // block witness
}, ""),
},
{
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("2c27ea6e848a1191f25a7f4a04deae1c5a191587e5ee61f92e408ab97dbd35c3ce613b08475f0baa300606c38695d1eb0c4b409939acaa28b82fbb87e7de3c0f"),
},
wantError: blockchain.ErrRange,
},
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("3dc3cc903772033972b9f3e48df330649c8e6beb3a4376e694b83dedb91da8692a32da3817edf1606cd5800a411f91316c96b2700a275d22c52d5fdc28e0fa03"),
},
wantError: blockchain.ErrRange,
},
Height: 9223372036854775807, // MaxInt64(9223372036854775807)
PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
Timestamp: 1528945000,
+ BlockWitness: testutil.MustDecodeHexString("ac6380a17685c48af4b0a0877d9d61e83b50bd95daa61083dd90031ae66d12d7a371c41cce24887d4d422202b747494bb0e7ca78567d6663be82b27714357407"),
+ },
+ wantHex: strings.Join([]string{
+ "01", // serialization flags
+ "01", // version
+ "ffffffffffffffff7f", // block height
+ "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
+ "e8b287d905", // timestamp
+ "20", // commitment extensible field length
+ "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
+ "4140ac6380a17685c48af4b0a0877d9d61e83b50bd95daa61083dd90031ae66d12d7a371c41cce24887d4d422202b747494bb0e7ca78567d6663be82b27714357407", // block witness
+ }, ""),
+ },
+ {
+ blockHeader: &BlockHeader{
+ Version: 1,
+ Height: 9223372036854775807, // MaxInt64(9223372036854775807)
+ PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
+ Timestamp: 1528945000,
+ BlockWitness: BlockWitness{},
},
wantHex: strings.Join([]string{
"01", // serialization flags
"e8b287d905", // timestamp
"20", // commitment extensible field length
"0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
+ "0100", // block witness
}, ""),
},
}
"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
"e8b287d905", // timestamp
"20", // commitment extensible field length
- "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "41409dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709", // block witness
}, ""),
wantBlockHeader: &BlockHeader{
Version: 1,
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
},
},
{
"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
"e8b287d905", // timestamp
"20", // commitment extensible field length
- "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "41409dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709", // block witness
}, ""),
wantBlockHeader: &BlockHeader{
Version: 1,
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
},
},
{
"e8b287d905", // timestamp
"20", // commitment extensible field length
"ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "0100", // block witness
}, ""),
wantError: errors.New("binary: varint overflows a 64-bit integer"),
},
"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
"e8b287d905", // timestamp
"20", // commitment extensible field length
- "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
+ "4140e0776a3cf17b3e0f8340caeee32a75d02ecc25cf20bee9e5c7503bca3b2703c3c61fdcb4211ed59b58eb025ac81e06b138d54b5d01ea4614dd0f65e641836900", // block witness
}, ""),
wantBlockHeader: &BlockHeader{
Version: 1,
BlockCommitment: BlockCommitment{
TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
},
+ BlockWitness: testutil.MustDecodeHexString("e0776a3cf17b3e0f8340caeee32a75d02ecc25cf20bee9e5c7503bca3b2703c3c61fdcb4211ed59b58eb025ac81e06b138d54b5d01ea4614dd0f65e641836900"),
},
},
}
"00", // timestamp
"20", // commitment extensible field length
"0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
- "00", // num transactions
+ "0100", // block witness
+ "00", // num transactions
}, ""),
hash: testutil.MustDecodeHash("42e74d130e5ab27e8a71b90e7de8c8e00ecfa77456070202ab8509f7b0ab49ae"),
},
"f3f896d605", // timestamp
"20", // commitment extensible field length
"ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
- "02", // num transactions
+ "0100", // block witness
+ "02", // num transactions
"07018e0502012a00056e6f6e6365a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f380f6173736574446566696e6974696f6e010f69737375616e636550726f6772616d020a617267756d656e7473310a617267756d656e74733201540152fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f03010c7370656e6450726f6772616d17020a617267756d656e7473330a617267756d656e74733401010029a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f01047472756500",
"07010001010b02096172626974726172790002010029ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f0104747275650001002affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f010566616c736500",
}, ""),
hash: testutil.MustDecodeHash("6076fc8a96b08a4842f4bdc805606e9775ce6dbe4e371e88c70b75ea4283e942"),
},
-
}
for i, test := range cases {
wantBlock Block
}{
{
- rawBlock: "03018b5f3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137d4f5e1e4054035a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a020701000101080206003132313731000101003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809df3b49a010116001437e1aec83a4e6587ca9609e4e5aa728db700744900070100020160015e4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e6302405760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed90130000840142084606f20ca7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad0140621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb030201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0d1e123011600144b61da45324299e40dacc255e2ea07dfce3a56d20001003e7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f4020116001437e1aec83a4e6587ca9609e4e5aa728db700744900",
+ rawBlock: "03018b5f3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137d4f5e1e4052035a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad20100020701000101080206003132313731000101003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809df3b49a010116001437e1aec83a4e6587ca9609e4e5aa728db700744900070100020160015e4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e6302405760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed90130000840142084606f20ca7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad0140621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb030201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0d1e123011600144b61da45324299e40dacc255e2ea07dfce3a56d20001003e7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f4020116001437e1aec83a4e6587ca9609e4e5aa728db700744900",
wantBlock: Block{
BlockHeader: BlockHeader{
Version: 1,
--- /dev/null
+package types
+
+import (
+ "bytes"
+ "encoding/hex"
+ "testing"
+
+ "github.com/bytom/bytom/encoding/blockchain"
+ "github.com/bytom/bytom/testutil"
+)
+
+func TestReadWriteBlockWitness(t *testing.T) {
+ cases := []struct {
+ name string
+ witness BlockWitness
+ hexString string
+ }{
+ {
+ name: "normal block witness",
+ witness: testutil.MustDecodeHexString("e0776a3cf17b3e0f8340caeee32a75d02ecc25cf20bee9e5c7503bca3b2703c3c61fdcb4211ed59b58eb025ac81e06b138d54b5d01ea4614dd0f65e641836900"),
+ hexString: "40e0776a3cf17b3e0f8340caeee32a75d02ecc25cf20bee9e5c7503bca3b2703c3c61fdcb4211ed59b58eb025ac81e06b138d54b5d01ea4614dd0f65e641836900",
+ },
+ {
+ name: "empty block witness",
+ witness: BlockWitness{},
+ hexString: "00",
+ },
+ }
+
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ buff := []byte{}
+ buffer := bytes.NewBuffer(buff)
+ if err := c.witness.writeTo(buffer); err != nil {
+ t.Fatal(err)
+ }
+
+ hexString := hex.EncodeToString(buffer.Bytes())
+ if hexString != c.hexString {
+ t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString)
+ }
+
+ blockWitness := &BlockWitness{}
+ if err := blockWitness.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
+ t.Fatal(err)
+ }
+
+ if !testutil.DeepEqual(*blockWitness, c.witness) {
+ t.Errorf("test read block commitment fail, got:%v, want:%v", *blockWitness, c.witness)
+ }
+ })
+ }
+}
+
+func TestBlockWitnessSet(t *testing.T) {
+ signatureLength := 64
+ cases := []struct {
+ name string
+ bw BlockWitness
+ data []byte
+ want BlockWitness
+ }{
+ {
+ name: "shorter than normal block witness length",
+ bw: make(BlockWitness, signatureLength-2),
+ data: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
+ want: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c12"),
+ },
+ {
+ name: "longer than normal block witness length",
+ bw: make(BlockWitness, signatureLength+2),
+ data: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
+ want: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c1277090000"),
+ },
+ {
+ name: "normal block witness",
+ bw: testutil.MustDecodeHexString("9dc8892df991e1d1110a5cb1bbfd57f2f5e3aa89464de50f9555c7575d9c2b21cf8f05b77b880d8ae4dd218efb15b775c32c9d77f9a2955d69dca9020c127709"),
+ data: testutil.MustDecodeHexString("2c27ea6e848a1191f25a7f4a04deae1c5a191587e5ee61f92e408ab97dbd35c3ce613b08475f0baa300606c38695d1eb0c4b409939acaa28b82fbb87e7de3c0f"),
+ want: testutil.MustDecodeHexString("2c27ea6e848a1191f25a7f4a04deae1c5a191587e5ee61f92e408ab97dbd35c3ce613b08475f0baa300606c38695d1eb0c4b409939acaa28b82fbb87e7de3c0f"),
+ },
+ }
+
+ for i, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ newbw := c.bw
+ newbw.Set(c.data)
+ if !testutil.DeepEqual(c.want, newbw) {
+ t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want)
+ }
+ })
+ }
+}