github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/integration/block_integration_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"github.com/bytom/bytom/config"
     5  	"github.com/bytom/bytom/consensus"
     6  	"github.com/bytom/bytom/database"
     7  	"github.com/bytom/bytom/database/storage"
     8  	"github.com/bytom/bytom/protocol/bc"
     9  	"github.com/bytom/bytom/protocol/bc/types"
    10  	"github.com/bytom/bytom/protocol/state"
    11  	"github.com/bytom/bytom/testutil"
    12  )
    13  
    14  var blockMap map[int][]*attachBlock
    15  
    16  type attachBlock struct {
    17  	block *types.Block
    18  }
    19  
    20  func init() {
    21  	consensus.ActiveNetParams = consensus.SoloNetParams
    22  
    23  	blockMap = map[int][]*attachBlock{
    24  		0: {
    25  			{
    26  				block: config.GenesisBlock(),
    27  			},
    28  		},
    29  		// 0 号的hash不会变
    30  		1: {
    31  			{
    32  				block: &types.Block{
    33  					BlockHeader: types.BlockHeader{
    34  						Height:            1,
    35  						Version:           1,
    36  						Timestamp:         1556431597,
    37  						PreviousBlockHash: testutil.MustDecodeHash("6f62777fab457d134aa55d29197ea5874988627d8777f6a14ed032a2f06dcc2f"),
    38  					},
    39  					Transactions: []*types.Tx{
    40  						types.NewTx(types.TxData{
    41  							Version:        1,
    42  							SerializedSize: 77,
    43  							TimeRange:      0,
    44  							Inputs: []*types.TxInput{
    45  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0031")),
    46  							},
    47  							Outputs: []*types.TxOutput{
    48  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41250000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
    49  							},
    50  						}),
    51  					},
    52  				},
    53  			},
    54  			{
    55  				block: &types.Block{
    56  					BlockHeader: types.BlockHeader{
    57  						Height:            1,
    58  						Version:           1,
    59  						Timestamp:         1556431697,
    60  						PreviousBlockHash: testutil.MustDecodeHash("6f62777fab457d134aa55d29197ea5874988627d8777f6a14ed032a2f06dcc2f"),
    61  					},
    62  					Transactions: []*types.Tx{
    63  						types.NewTx(types.TxData{
    64  							Version:        1,
    65  							SerializedSize: 77,
    66  							TimeRange:      0,
    67  							Inputs: []*types.TxInput{
    68  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0031")),
    69  							},
    70  							Outputs: []*types.TxOutput{
    71  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41250000000, testutil.MustDecodeHexString("00143d05e891b165b165afefa2e861e83a9745f80d8c"), [][]byte{}),
    72  							},
    73  						}),
    74  					},
    75  				},
    76  			},
    77  		},
    78  		2: {
    79  			//the below blocks's previous block is blockMap[1][0]
    80  			{
    81  				block: &types.Block{
    82  					BlockHeader: types.BlockHeader{
    83  						Height:            2,
    84  						Version:           1,
    85  						Timestamp:         1556431604,
    86  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
    87  					},
    88  					Transactions: []*types.Tx{
    89  						types.NewTx(types.TxData{
    90  							Version:        1,
    91  							SerializedSize: 77,
    92  							TimeRange:      0,
    93  							Inputs: []*types.TxInput{
    94  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
    95  							},
    96  							Outputs: []*types.TxOutput{
    97  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41250000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
    98  							},
    99  						}),
   100  					},
   101  				},
   102  			},
   103  			// with spend btm transaction
   104  			{
   105  				block: &types.Block{
   106  					BlockHeader: types.BlockHeader{
   107  						Height:            2,
   108  						Version:           1,
   109  						Timestamp:         1556431604,
   110  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   111  					},
   112  					Transactions: []*types.Tx{
   113  						types.NewTx(types.TxData{
   114  							Version:        1,
   115  							SerializedSize: 77,
   116  							TimeRange:      0,
   117  							Inputs: []*types.TxInput{
   118  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   119  							},
   120  							Outputs: []*types.TxOutput{
   121  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   122  							},
   123  						}),
   124  						types.NewTx(types.TxData{
   125  							Version:   1,
   126  							TimeRange: 0,
   127  							Inputs: []*types.TxInput{
   128  								types.NewSpendInput(
   129  									[][]byte{
   130  										testutil.MustDecodeHexString("7b4082c9d745c3f07dd07afb1f987960d2ef3ea2486741c3f3184751485f77d046df6670eba21020fcf9c7987c0c938384320dc21b0e116c62ae2597cb1fe109"),
   131  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   132  									},
   133  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   134  									*consensus.BTMAssetID, 10000000000, 0,
   135  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   136  									[][]byte{},
   137  								),
   138  							},
   139  							Outputs: []*types.TxOutput{
   140  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("00143d05e891b165b165afefa2e861e83a9745f80d8c"), [][]byte{}),
   141  							},
   142  						}),
   143  					},
   144  				},
   145  			},
   146  			// with btm retire transaction
   147  			{
   148  				block: &types.Block{
   149  					BlockHeader: types.BlockHeader{
   150  						Height:            2,
   151  						Version:           1,
   152  						Timestamp:         1556431607,
   153  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   154  					},
   155  					Transactions: []*types.Tx{
   156  						types.NewTx(types.TxData{
   157  							Version:        1,
   158  							SerializedSize: 77,
   159  							TimeRange:      0,
   160  							Inputs: []*types.TxInput{
   161  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   162  							},
   163  							Outputs: []*types.TxOutput{
   164  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   165  							},
   166  						}),
   167  						types.NewTx(types.TxData{
   168  							Version:   1,
   169  							TimeRange: 0,
   170  							Inputs: []*types.TxInput{
   171  								types.NewSpendInput(
   172  									[][]byte{
   173  										testutil.MustDecodeHexString("302035b362d80419cfed12cfc7d33d2ff7638c589ee2cf6573eb14b4d8cb4a63d7d1302589666dd6d1cd08185dbb2842407f3980bc2564705eda15680c984c05"),
   174  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   175  									},
   176  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   177  									*consensus.BTMAssetID, 10000000000, 0,
   178  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   179  									[][]byte{},
   180  								),
   181  							},
   182  							Outputs: []*types.TxOutput{
   183  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("6a"), [][]byte{}), // retire
   184  							},
   185  						}),
   186  					},
   187  				},
   188  			},
   189  			// with issuance transaction
   190  			{
   191  				block: &types.Block{
   192  					BlockHeader: types.BlockHeader{
   193  						Height:            2,
   194  						Version:           1,
   195  						Timestamp:         1556431607,
   196  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   197  					},
   198  					Transactions: []*types.Tx{
   199  						types.NewTx(types.TxData{
   200  							Version:        1,
   201  							SerializedSize: 77,
   202  							TimeRange:      0,
   203  							Inputs: []*types.TxInput{
   204  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   205  							},
   206  							Outputs: []*types.TxOutput{
   207  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   208  							},
   209  						}),
   210  						types.NewTx(types.TxData{
   211  							Version:   1,
   212  							TimeRange: 0,
   213  							Inputs: []*types.TxInput{
   214  								types.NewSpendInput(
   215  									[][]byte{
   216  										testutil.MustDecodeHexString("46cbb829b6a5bb9fc436c8e51bcbd9f0b3ed99ce97b2e0fac28879b4202c5a9eccaae39a4d18584f789a9427af3a2f09ff0360fb187e46ef172146a9b957ef0c"),
   217  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   218  									},
   219  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   220  									*consensus.BTMAssetID, 10000000000, 0,
   221  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   222  									[][]byte{},
   223  								),
   224  								types.NewIssuanceInput(
   225  									testutil.MustDecodeHexString("fd0aec4229deb281"),
   226  									10000000000,
   227  									testutil.MustDecodeHexString("ae20f25e8b73ffbc3a42300a43279fdf612d79e1936a6c614fc05a5adec9bba42dcd5151ad"),
   228  									[][]byte{testutil.MustDecodeHexString("df9fabf4636904e017eefb7cdf2b4f08e29efbd4cfc41fe5b01a453191f0913489b19ad74272145824e92bd4843e91140cc5d1a6256f84981d1437ed4566a60b")},
   229  									testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
   230  								),
   231  							},
   232  							Outputs: []*types.TxOutput{
   233  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   234  								types.NewOriginalTxOutput(testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   235  							},
   236  						}),
   237  					},
   238  				},
   239  			},
   240  			// with issuance transaction but status fail is true
   241  			{
   242  				block: &types.Block{
   243  					BlockHeader: types.BlockHeader{
   244  						Height:            2,
   245  						Version:           1,
   246  						Timestamp:         1556431607,
   247  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   248  					},
   249  					Transactions: []*types.Tx{
   250  						types.NewTx(types.TxData{
   251  							Version:        1,
   252  							SerializedSize: 77,
   253  							TimeRange:      0,
   254  							Inputs: []*types.TxInput{
   255  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   256  							},
   257  							Outputs: []*types.TxOutput{
   258  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   259  							},
   260  						}),
   261  						types.NewTx(types.TxData{
   262  							Version:   1,
   263  							TimeRange: 0,
   264  							Inputs: []*types.TxInput{
   265  								types.NewSpendInput(
   266  									[][]byte{
   267  										testutil.MustDecodeHexString("46cbb829b6a5bb9fc436c8e51bcbd9f0b3ed99ce97b2e0fac28879b4202c5a9eccaae39a4d18584f789a9427af3a2f09ff0360fb187e46ef172146a9b957ef0c"),
   268  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   269  									},
   270  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   271  									*consensus.BTMAssetID, 10000000000, 0,
   272  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   273  									[][]byte{},
   274  								),
   275  								types.NewIssuanceInput(
   276  									testutil.MustDecodeHexString("fd0aec4229deb281"),
   277  									10000000000,
   278  									testutil.MustDecodeHexString("ae20f25e8b73ffbc3a42300a43279fdf612d79e1936a6c614fc05a5adec9bba42dcd5151ad"),
   279  									// invalid signature
   280  									[][]byte{testutil.MustDecodeHexString("df9fabf4636904e017eefb7cdf2b4f08e29efbd4cfc41fe5b01a453191f0913489b19ad74272145824e92bd4843e91140cc5d1a6256f84981d1437ed4566a60c")},
   281  									testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
   282  								),
   283  							},
   284  							Outputs: []*types.TxOutput{
   285  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   286  								types.NewOriginalTxOutput(testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   287  							},
   288  						}),
   289  					},
   290  				},
   291  			},
   292  			// with non btm transaction
   293  			{
   294  				block: &types.Block{
   295  					BlockHeader: types.BlockHeader{
   296  						Height:            2,
   297  						Version:           1,
   298  						Timestamp:         1556431607,
   299  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   300  					},
   301  					Transactions: []*types.Tx{
   302  						types.NewTx(types.TxData{
   303  							Version:        1,
   304  							SerializedSize: 77,
   305  							TimeRange:      0,
   306  							Inputs: []*types.TxInput{
   307  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   308  							},
   309  							Outputs: []*types.TxOutput{
   310  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   311  							},
   312  						}),
   313  						types.NewTx(types.TxData{
   314  							Version:   1,
   315  							TimeRange: 0,
   316  							Inputs: []*types.TxInput{
   317  								types.NewSpendInput(
   318  									[][]byte{
   319  										testutil.MustDecodeHexString("afc4e24f0e0383e3fd78af3de189be3913faddbbd8cac8a8c9316bf9eb0866e83df3618cf4c7b4d091a79968a16377d422cbd8011f1f5e75ba201e173b68ad02"),
   320  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   321  									},
   322  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   323  									*consensus.BTMAssetID, 10000000000, 0,
   324  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   325  									[][]byte{},
   326  								),
   327  								types.NewSpendInput(
   328  									[][]byte{
   329  										testutil.MustDecodeHexString("cd6fb451102db667341438f20dbeabd30b343ed08d89625a8e27e82478e89ddea9e7d51f8a4036e0cc2602ac5fae0bdbfda025a0e2c12e3ddc8100b62461670b"),
   330  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   331  									},
   332  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f22"),
   333  									testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, 1,
   334  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   335  									[][]byte{},
   336  								),
   337  							},
   338  							Outputs: []*types.TxOutput{
   339  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   340  								types.NewOriginalTxOutput(testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   341  							},
   342  						}),
   343  					},
   344  				},
   345  			},
   346  			// with non btm transaction but status fail is true
   347  			{
   348  				block: &types.Block{
   349  					BlockHeader: types.BlockHeader{
   350  						Height:            2,
   351  						Version:           1,
   352  						Timestamp:         1556431607,
   353  						PreviousBlockHash: testutil.MustDecodeHash("0311998e27abc1c2f5cc1f86b1aca5e7dd3ca65d63359e0c7539c40207923e10"),
   354  					},
   355  					Transactions: []*types.Tx{
   356  						types.NewTx(types.TxData{
   357  							Version:        1,
   358  							SerializedSize: 77,
   359  							TimeRange:      0,
   360  							Inputs: []*types.TxInput{
   361  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0032")),
   362  							},
   363  							Outputs: []*types.TxOutput{
   364  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41350000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   365  							},
   366  						}),
   367  						types.NewTx(types.TxData{
   368  							Version:   1,
   369  							TimeRange: 0,
   370  							Inputs: []*types.TxInput{
   371  								types.NewSpendInput(
   372  									[][]byte{
   373  										testutil.MustDecodeHexString("afc4e24f0e0383e3fd78af3de189be3913faddbbd8cac8a8c9316bf9eb0866e83df3618cf4c7b4d091a79968a16377d422cbd8011f1f5e75ba201e173b68ad02"),
   374  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   375  									},
   376  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21"),
   377  									*consensus.BTMAssetID, 10000000000, 0,
   378  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   379  									[][]byte{},
   380  								),
   381  								types.NewSpendInput(
   382  									// invalid signature
   383  									[][]byte{
   384  										testutil.MustDecodeHexString("cd6fb451102db667341438f20dbeabd30b343ed08d89625a8e27e82478e89ddea9e7d51f8a4036e0cc2602ac5fae0bdbfda025a0e2c12e3ddc8100b62461670c"),
   385  										testutil.MustDecodeHexString("33b05e00e19cb2bdbc8a6a67b4f1e03fc265534bcfc7641b305c8204fb486f79"),
   386  									},
   387  									testutil.MustDecodeHash("28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f22"),
   388  									testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, 1,
   389  									testutil.MustDecodeHexString("0014cade6dd7cbe2ea2b8ab90dfb8756dda4ba1624bc"),
   390  									[][]byte{},
   391  								),
   392  							},
   393  							Outputs: []*types.TxOutput{
   394  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 9900000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   395  								types.NewOriginalTxOutput(testutil.MustDecodeAsset("641ccb49dd38df9921a55e020d40a2323589c36ab5557f8a249ee01cc09d1836"), 10000000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   396  							},
   397  						}),
   398  					},
   399  				},
   400  			},
   401  		},
   402  		3: {
   403  			// the previous block is blockMap[2][0]
   404  			{
   405  				block: &types.Block{
   406  					BlockHeader: types.BlockHeader{
   407  						Height:            3,
   408  						Version:           1,
   409  						Timestamp:         1556431640,
   410  						PreviousBlockHash: testutil.MustDecodeHash("d96091fb7784af594980012cadb05ad717d45603eab2b2105a2735ae5bb3aca3"),
   411  					},
   412  					Transactions: []*types.Tx{
   413  						types.NewTx(types.TxData{
   414  							Version:        1,
   415  							SerializedSize: 77,
   416  							TimeRange:      0,
   417  							Inputs: []*types.TxInput{
   418  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0033")),
   419  							},
   420  							Outputs: []*types.TxOutput{
   421  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41250000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   422  							},
   423  						}),
   424  					},
   425  				},
   426  			},
   427  			// the previous block is blockMap[2][2]
   428  			{
   429  				block: &types.Block{
   430  					BlockHeader: types.BlockHeader{
   431  						Height:            3,
   432  						Version:           1,
   433  						Timestamp:         1556431640,
   434  						PreviousBlockHash: testutil.MustDecodeHash("222356873e67eacf3fa38ddb736c467471c8aa91f65686d28a96f7d39f8278e7"),
   435  					},
   436  					Transactions: []*types.Tx{
   437  						types.NewTx(types.TxData{
   438  							Version:        1,
   439  							SerializedSize: 77,
   440  							TimeRange:      0,
   441  							Inputs: []*types.TxInput{
   442  								types.NewCoinbaseInput(testutil.MustDecodeHexString("0033")),
   443  							},
   444  							Outputs: []*types.TxOutput{
   445  								types.NewOriginalTxOutput(*consensus.BTMAssetID, 41250000000, testutil.MustDecodeHexString("0014024bb9bfc639bdac292ff9ceb41b5c6f5a970eab"), [][]byte{}),
   446  							},
   447  						}),
   448  					},
   449  				},
   450  			},
   451  		},
   452  	}
   453  
   454  	mustPostProcessBlock()
   455  }
   456  
   457  func createStoreItems(mainChainIndexes []int, attachBlocks []*attachBlock, extralItem ...*storeItem) storeItems {
   458  	var items storeItems
   459  	for _, item := range extralItem {
   460  		items = append(items, item)
   461  	}
   462  
   463  	mainChainIndexMap := make(map[int]interface{})
   464  	for _, index := range mainChainIndexes {
   465  		mainChainIndexMap[index] = nil
   466  	}
   467  
   468  	for i, attachBlock := range attachBlocks {
   469  		block := attachBlock.block
   470  		blockHash := block.Hash()
   471  		items = append(items, &storeItem{
   472  			val: block,
   473  		})
   474  		items = append(items, &storeItem{
   475  			key: database.CalcBlockHeaderKey(&blockHash),
   476  			val: block.BlockHeader,
   477  		})
   478  		if _, ok := mainChainIndexMap[i]; !ok {
   479  			continue
   480  		}
   481  
   482  		for i, tx := range block.Transactions {
   483  			for _, input := range tx.Inputs {
   484  				if _, ok := input.TypedInput.(*types.SpendInput); !ok {
   485  					continue
   486  				}
   487  				spendOutputID, err := input.SpentOutputID()
   488  				if err != nil {
   489  					panic(err)
   490  				}
   491  				index := spendUTXO(spendOutputID, items, block.Height)
   492  				items = append(items[0:index], items[index+1:]...)
   493  			}
   494  			for j, output := range tx.Outputs {
   495  				if output.ControlProgram[0] == 0x6a {
   496  					continue
   497  				}
   498  				utxoType := storage.NormalUTXOType
   499  				if i == 0 {
   500  					utxoType = storage.CoinbaseUTXOType
   501  				}
   502  				items = append(items, &storeItem{key: database.CalcUtxoKey(tx.Tx.ResultIds[j]),
   503  					val: &storage.UtxoEntry{Type: utxoType, BlockHeight: block.Height, Spent: false},
   504  				})
   505  			}
   506  		}
   507  	}
   508  
   509  	lastIndex := mainChainIndexes[len(mainChainIndexes)-1]
   510  	betBlock := attachBlocks[lastIndex].block
   511  	bestBlockHash := betBlock.Hash()
   512  	items = append(items, &storeItem{
   513  		key: database.BlockStoreKey,
   514  		val: &state.BlockStoreState{Height: betBlock.Height, Hash: &bestBlockHash},
   515  	})
   516  	return items
   517  }
   518  
   519  func hashPtr(hash bc.Hash) *bc.Hash {
   520  	return &hash
   521  }
   522  
   523  func mustPostProcessBlock() {
   524  	for _, blocks := range blockMap {
   525  		for _, attachBlock := range blocks {
   526  			mustCalcMerkleRootHash(attachBlock)
   527  			mustFillTransactionSize(attachBlock.block)
   528  			sortSpendOutputID(attachBlock.block)
   529  		}
   530  	}
   531  }
   532  
   533  func mustCalcMerkleRootHash(attachBlock *attachBlock) {
   534  	bcBlock := types.MapBlock(attachBlock.block)
   535  	merkleRoot, err := types.TxMerkleRoot(bcBlock.Transactions)
   536  	if err != nil {
   537  		panic("fail on calc genesis tx merkel root")
   538  	}
   539  
   540  	attachBlock.block.TransactionsMerkleRoot = merkleRoot
   541  }
   542  
   543  func mustFillTransactionSize(block *types.Block) {
   544  	for _, tx := range block.Transactions {
   545  		bytes, err := tx.MarshalText()
   546  		if err != nil {
   547  			panic(err)
   548  		}
   549  		tx.TxData.SerializedSize = uint64(len(bytes) / 2)
   550  		tx.Tx.SerializedSize = uint64(len(bytes) / 2)
   551  	}
   552  }
   553  
   554  func spendUTXO(spendOutputID bc.Hash, items storeItems, blockHeight uint64) int {
   555  	for i, item := range items {
   556  		utxo, ok := item.val.(*storage.UtxoEntry)
   557  		if !ok {
   558  			continue
   559  		}
   560  		if string(database.CalcUtxoKey(&spendOutputID)) != string(item.key) {
   561  			continue
   562  		}
   563  		if utxo.Spent || (utxo.Type == storage.CoinbaseUTXOType && utxo.BlockHeight+consensus.CoinbasePendingBlockNumber > blockHeight) {
   564  			panic("utxo can not be use")
   565  		}
   566  		utxo.Spent = true
   567  		return i
   568  	}
   569  	panic("can not find available utxo")
   570  }
   571  
   572  func createStoreEntries(mainChainIndexes []int, attachBlocks []*attachBlock, extralItem ...*storeItem) []storeEntry {
   573  	storeItems := createStoreItems(mainChainIndexes, attachBlocks, extralItem...)
   574  	var storeEntries []storeEntry
   575  	for _, item := range storeItems {
   576  		entrys, err := serialItem(item)
   577  		if err != nil {
   578  			panic(err)
   579  		}
   580  
   581  		storeEntries = append(storeEntries, entrys...)
   582  	}
   583  
   584  	return storeEntries
   585  }