github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/types/block_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/davecgh/go-spew/spew"
    11  
    12  	"github.com/bytom/bytom/consensus"
    13  	"github.com/bytom/bytom/encoding/blockchain"
    14  	"github.com/bytom/bytom/protocol/bc"
    15  	"github.com/bytom/bytom/testutil"
    16  )
    17  
    18  func TestBlock(t *testing.T) {
    19  	cases := []struct {
    20  		block *Block
    21  		hex   string
    22  		hash  bc.Hash
    23  	}{
    24  		{
    25  			block: &Block{
    26  				BlockHeader: BlockHeader{
    27  					Version: 1,
    28  					Height:  1,
    29  				},
    30  				Transactions: []*Tx{},
    31  			},
    32  			hex: strings.Join([]string{
    33  				"03", // serialization flags
    34  				"01", // version
    35  				"01", // block height
    36  				"0000000000000000000000000000000000000000000000000000000000000000", // prev block hash
    37  				"00",   // timestamp
    38  				"20",   // commitment extensible field length
    39  				"0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
    40  				"0100", // block witness
    41  				"0100", // sup links
    42  				"00",   // num transactions
    43  			}, ""),
    44  			hash: testutil.MustDecodeHash("42e74d130e5ab27e8a71b90e7de8c8e00ecfa77456070202ab8509f7b0ab49ae"),
    45  		},
    46  		{
    47  			block: &Block{
    48  				BlockHeader: BlockHeader{
    49  					Version:           1,
    50  					Height:            432234,
    51  					PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
    52  					Timestamp:         1522908275,
    53  					BlockCommitment: BlockCommitment{
    54  						TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
    55  					},
    56  				},
    57  				Transactions: []*Tx{
    58  					NewTx(TxData{
    59  						Version:        1,
    60  						SerializedSize: uint64(284),
    61  						TimeRange:      654,
    62  						Inputs: []*TxInput{
    63  							NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
    64  							NewSpendInput([][]byte{[]byte("arguments3"), []byte("arguments4")}, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 254354, 3, []byte("spendProgram"), [][]byte{[]byte("stateData")}),
    65  						},
    66  						Outputs: []*TxOutput{
    67  							NewOriginalTxOutput(testutil.MustDecodeAsset("a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf"), 254354, []byte("true"), [][]byte{[]byte("stateData")}),
    68  						},
    69  					}),
    70  					NewTx(TxData{
    71  						Version:        1,
    72  						SerializedSize: uint64(132),
    73  						Inputs: []*TxInput{
    74  							NewCoinbaseInput([]byte("arbitrary")),
    75  						},
    76  						Outputs: []*TxOutput{
    77  							NewOriginalTxOutput(*consensus.BTMAssetID, 254354, []byte("true"), [][]byte{[]byte("stateData")}),
    78  							NewOriginalTxOutput(*consensus.BTMAssetID, 254354, []byte("false"), [][]byte{[]byte("stateData")}),
    79  						},
    80  					}),
    81  				},
    82  			},
    83  			hex: strings.Join([]string{
    84  				"03",     // serialization flags
    85  				"01",     // version
    86  				"eab01a", // block height
    87  				"c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
    88  				"f3f896d605", // timestamp
    89  				"20",         // commitment extensible field length
    90  				"ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
    91  				"0100", // block witness
    92  				"0100", // sup links
    93  				"02",   // num transactions
    94  				"07018e0502012a00056e6f6e6365a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f380f6173736574446566696e6974696f6e010f69737375616e636550726f6772616d020a617267756d656e7473310a617267756d656e747332015f015dfad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f03010c7370656e6450726f6772616d010973746174654461746117020a617267756d656e7473330a617267756d656e74733401010034a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f010474727565010973746174654461746100",
    95  				"07010001010b02096172626974726172790002010034ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f010474727565010973746174654461746100010035ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f010566616c7365010973746174654461746100",
    96  			}, ""),
    97  			hash: testutil.MustDecodeHash("6076fc8a96b08a4842f4bdc805606e9775ce6dbe4e371e88c70b75ea4283e942"),
    98  		},
    99  	}
   100  
   101  	for i, test := range cases {
   102  		got := testutil.Serialize(t, test.block)
   103  		want, err := hex.DecodeString(test.hex)
   104  		if err != nil {
   105  			t.Fatal(err)
   106  		}
   107  
   108  		if !bytes.Equal(got, want) {
   109  			t.Errorf("test %d: bytes = %x want %x", i, got, want)
   110  		}
   111  
   112  		blockHash := test.block.Hash()
   113  		if blockHash != test.hash {
   114  			t.Errorf("test %d: hash = %s want %s", i, blockHash.String(), test.hash.String())
   115  		}
   116  
   117  		blockJSON, err := json.Marshal(test.block)
   118  		if err != nil {
   119  			t.Errorf("test %d: error marshaling block to json: %s", i, err)
   120  		}
   121  
   122  		blockFromJSON := Block{}
   123  		if err := json.Unmarshal(blockJSON, &blockFromJSON); err != nil {
   124  			t.Errorf("test %d: error unmarshaling block from json: %s", i, err)
   125  		}
   126  		if !testutil.DeepEqual(*test.block, blockFromJSON) {
   127  			t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(blockFromJSON), spew.Sdump(*test.block))
   128  		}
   129  	}
   130  }
   131  
   132  func TestReadFrom(t *testing.T) {
   133  	btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
   134  
   135  	cases := []struct {
   136  		rawBlock  string
   137  		wantBlock Block
   138  	}{
   139  		{
   140  			rawBlock: "03018b5f3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137d4f5e1e4054035a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a01000100020701000101080206003132313731000101003fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809df3b49a010116001437e1aec83a4e6587ca9609e4e5aa728db70074490000070100020161015f4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e006302405760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed90130000840142084606f20ca7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad0140621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb030201003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0d1e123011600144b61da45324299e40dacc255e2ea07dfce3a56d2000001003f7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f4020116001437e1aec83a4e6587ca9609e4e5aa728db70074490000",
   141  			wantBlock: Block{
   142  				BlockHeader: BlockHeader{
   143  					Version:           1,
   144  					Height:            12171,
   145  					PreviousBlockHash: testutil.MustDecodeHash("3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137"),
   146  					Timestamp:         1553496788,
   147  					BlockCommitment: BlockCommitment{
   148  						TransactionsMerkleRoot: testutil.MustDecodeHash("35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad2"),
   149  					},
   150  				},
   151  				Transactions: []*Tx{
   152  					{
   153  						TxData: TxData{
   154  							Version:        1,
   155  							SerializedSize: 83,
   156  							TimeRange:      0,
   157  							Inputs: []*TxInput{
   158  								NewCoinbaseInput(testutil.MustDecodeHexString("003132313731")),
   159  							},
   160  							Outputs: []*TxOutput{
   161  								NewOriginalTxOutput(btmAssetID, 41450000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449"), nil),
   162  							},
   163  						},
   164  					},
   165  					{
   166  						TxData: TxData{
   167  							Version:        1,
   168  							SerializedSize: 565,
   169  							TimeRange:      0,
   170  							Inputs: []*TxInput{
   171  								NewSpendInput(
   172  									[][]byte{
   173  										testutil.MustDecodeHexString("5760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104"),
   174  										testutil.MustDecodeHexString("1381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed9"),
   175  									},
   176  									testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
   177  									btmAssetID,
   178  									9800000000,
   179  									2,
   180  									testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
   181  									nil,
   182  								),
   183  								NewIssuanceInput(
   184  									testutil.MustDecodeHexString("40142084606f20ca"),
   185  									100000000000,
   186  									testutil.MustDecodeHexString("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"),
   187  									[][]byte{testutil.MustDecodeHexString("621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb03")},
   188  									testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
   189  								),
   190  							},
   191  							Outputs: []*TxOutput{
   192  								NewOriginalTxOutput(btmAssetID, 9600000000, testutil.MustDecodeHexString("00144b61da45324299e40dacc255e2ea07dfce3a56d2"), nil),
   193  								NewOriginalTxOutput(testutil.MustDecodeAsset("7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad14"), 100000000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449"), nil),
   194  							},
   195  						},
   196  					},
   197  				},
   198  			},
   199  		},
   200  	}
   201  
   202  	for _, c := range cases {
   203  		blockBytes, err := hex.DecodeString(c.rawBlock)
   204  		if err != nil {
   205  			t.Fatal(err)
   206  		}
   207  
   208  		block := &Block{}
   209  		if err := block.readFrom(blockchain.NewReader(blockBytes)); err != nil {
   210  			t.Fatal(err)
   211  		}
   212  
   213  		for _, tx := range c.wantBlock.Transactions {
   214  			tx.Tx = MapTx(&tx.TxData)
   215  		}
   216  
   217  		if !testutil.DeepEqual(*block, c.wantBlock) {
   218  			t.Errorf("test block read from fail, got:%v, want:%v", *block, c.wantBlock)
   219  		}
   220  	}
   221  }