github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/validation/tx_scene_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/bytom/bytom/consensus"
     8  	"github.com/bytom/bytom/protocol/bc"
     9  	"github.com/bytom/bytom/protocol/bc/types"
    10  	"github.com/bytom/bytom/protocol/vm"
    11  )
    12  
    13  func TestValidateTx(t *testing.T) {
    14  	cases := []struct {
    15  		desc     string
    16  		txData   *types.TxData
    17  		gasValid bool
    18  		err      error
    19  	}{
    20  		{
    21  			desc: "single utxo, single sign, non asset, btm stanard transaction",
    22  			txData: &types.TxData{
    23  				Version:        1,
    24  				SerializedSize: 331,
    25  				Inputs: []*types.TxInput{
    26  					types.NewSpendInput(
    27  						[][]byte{
    28  							mustDecodeString("298fbf48459480914e19a0fc20440b095bd7f38d9f01c56bfc904b4ed4967a7b73f1fc4919f23a7806eeb834a89f8ce696500f4528e8f7bf29c8ee1f38a91e01"),
    29  							mustDecodeString("5a260070d967d894a9c4a6e16670c2881ed4c225e12d93b0707156e71fce5bfd"),
    30  						},
    31  						bc.Hash{V0: 3485387979411255237, V1: 15603105575416882039, V2: 5974145557334619041, V3: 16513948410238218452},
    32  						*consensus.BTMAssetID, 21819700000, 0, mustDecodeString("001411ef7695d46e1f9288d996c3daa6ff4d956ac355")),
    33  				},
    34  				Outputs: []*types.TxOutput{
    35  					types.NewTxOutput(*consensus.BTMAssetID, 11818900000, mustDecodeString("001415c956112c2b46354690e36051803cc9d5a8f26b")),
    36  					types.NewTxOutput(*consensus.BTMAssetID, 10000000000, mustDecodeString("00149c9dd93184cc34ac5d47c145c5af3df852235aad")),
    37  				},
    38  			},
    39  			gasValid: true,
    40  			err:      nil,
    41  		},
    42  		{
    43  			desc: "multi utxo, single sign, non asset, btm stanard transaction",
    44  			txData: &types.TxData{
    45  				Version:        1,
    46  				SerializedSize: 595,
    47  				Inputs: []*types.TxInput{
    48  					types.NewSpendInput(
    49  						[][]byte{
    50  							mustDecodeString("d488321eff213793fb685749a65b945b4d32f08774c27461e0dda265580e9d4582f4b210756b7f8a5b4a64bde531076e92244e12c145c9b54012134cebf9e402"),
    51  							mustDecodeString("ca85ea98011ddd592d1f081ebd2a91ac0f4238784222ed85b9d95aeb654f1cf1"),
    52  						},
    53  						bc.Hash{V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994},
    54  						*consensus.BTMAssetID, 11818900000, 0, mustDecodeString("0014e6e1f8b11f1cfb7609037003b90f64837afd272c")),
    55  					types.NewSpendInput(
    56  						[][]byte{
    57  							mustDecodeString("5d528bdb13b93c26245dc90c1fe51265555eb22a34fa013649db9aa874eb7770c6c4016320017224efdecf5fee39b682151f881f82c2c7195fe444ac5966140e"),
    58  							mustDecodeString("563cb0eedf2a2891926dfaa0b9ac20913c67a066517f06b1f77c5ab527a8a8c4"),
    59  						},
    60  						bc.Hash{V0: 13464118406972499748, V1: 5083224803004805715, V2: 16263625389659454272, V3: 9428032044180324575},
    61  						*consensus.BTMAssetID, 99439999900, 2, mustDecodeString("001419f79910f29df2ef80ec10d24c78e2009ed19302")),
    62  				},
    63  				Outputs: []*types.TxOutput{
    64  					types.NewTxOutput(*consensus.BTMAssetID, 1818900000, mustDecodeString("00145931e1b7b65897f47845ac08fc136e0c0a4ff166")),
    65  					types.NewTxOutput(*consensus.BTMAssetID, 89439999900, mustDecodeString("0014ca1f877c2787f746a4473adac932171dd18d55d7")),
    66  					types.NewTxOutput(*consensus.BTMAssetID, 19900000000, mustDecodeString("00145ade29df622cc68d0473aa1a20fb89690451c66e")),
    67  				},
    68  			},
    69  			gasValid: true,
    70  			err:      nil,
    71  		},
    72  		{
    73  			desc: "multi utxo, single sign, non asset, btm stanard transaction, insufficient gas",
    74  			txData: &types.TxData{
    75  				Version:        1,
    76  				SerializedSize: 595,
    77  				Inputs: []*types.TxInput{
    78  					types.NewSpendInput(
    79  						[][]byte{
    80  							mustDecodeString("4a8bf559f3c334ad23ed0aadab22dd3a4a8260488b1632dee16f75cac5c0ade674f2938776459414ab4d4e43622290507ff750a3fb563a25ee9a72386bfbe207"),
    81  							mustDecodeString("ca85ea98011ddd592d1f081ebd2a91ac0f4238784222ed85b9d95aeb654f1cf1"),
    82  						},
    83  						bc.Hash{V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994},
    84  						*consensus.BTMAssetID, 11818900000, 0, mustDecodeString("0014e6e1f8b11f1cfb7609037003b90f64837afd272c")),
    85  					types.NewSpendInput(
    86  						[][]byte{
    87  							mustDecodeString("b4f6876a97c8e6bd7e038b476fb6fd07cdd6cfcf7d661dfab796b5e2c777b3de166495de4fba2aa154af844ed6a3d51c26742241edb0d5d107fc52dfff0f6305"),
    88  							mustDecodeString("e5966eee4092eeefdd805b06f2ad368bb9392edec20998993ebe2a929052c1ce"),
    89  						},
    90  						bc.Hash{V0: 17091584763764411831, V1: 2315724244669489432, V2: 4322938623810388342, V3: 11167378497724951792},
    91  						*consensus.BTMAssetID, 99960000000, 1, mustDecodeString("0014cfbccfac5018ad4b4bfbcb1fab834e3c85037460")),
    92  				},
    93  				Outputs: []*types.TxOutput{
    94  					types.NewTxOutput(*consensus.BTMAssetID, 1818900000, mustDecodeString("00144b5637cc25b188136f440484f210541fa2a7ce64")),
    95  					types.NewTxOutput(*consensus.BTMAssetID, 89960000000, mustDecodeString("0014c7271a69dba57331b36221118dfeb1b1793933df")),
    96  					types.NewTxOutput(*consensus.BTMAssetID, 20000000000, mustDecodeString("0014447e597c1c326ad1a639f8023d3f87ae22a4e049")),
    97  				},
    98  			},
    99  			gasValid: false,
   100  			err:      vm.ErrRunLimitExceeded,
   101  		},
   102  		{
   103  			desc: "single utxo, multi sign, non asset, btm stanard transaction",
   104  			txData: &types.TxData{
   105  				Version:        1,
   106  				SerializedSize: 396,
   107  				Inputs: []*types.TxInput{
   108  					types.NewSpendInput(
   109  						[][]byte{
   110  							mustDecodeString("abc55905b5c477f424ea5ce88bbd00376f18f525850b7b74f54e94e7999edbe5ded9e9f5d8f1319470e9a38540bbaa6bbe67aacc8227c898ae30b9ac15f8dc0b"),
   111  							mustDecodeString("ae203f56f71972918585ece56a21f77c3e9101ce14c75038b65454e10960266cceba20c9927f289b57c647578d07904a9d34597079d80e300df023a26658a770f611545152ad"),
   112  						},
   113  						bc.Hash{V0: 6970879411704044573, V1: 10086395903308657573, V2: 10107608596190358115, V3: 8645856247221333302},
   114  						*consensus.BTMAssetID, 89220000000, 1, mustDecodeString("0020ff726649e34c921ff61a97090fc62054f339597acfc710197bb0133e18a19c5c")),
   115  				},
   116  				Outputs: []*types.TxOutput{
   117  					types.NewTxOutput(*consensus.BTMAssetID, 79220000000, mustDecodeString("00206205ec178dc1ac6ea05ea01bb0fcda6aa978173026fa75204a101bdad7bd6b48")),
   118  					types.NewTxOutput(*consensus.BTMAssetID, 9900000000, mustDecodeString("0014414eb62abda9a9191f9cba5d7e38d92f3e91e268")),
   119  				},
   120  			},
   121  			gasValid: true,
   122  			err:      nil,
   123  		},
   124  		{
   125  			desc: "single utxo, retire, non asset, btm stanard transaction",
   126  			txData: &types.TxData{
   127  				Version:        1,
   128  				SerializedSize: 309,
   129  				Inputs: []*types.TxInput{
   130  					types.NewSpendInput(
   131  						[][]byte{
   132  							mustDecodeString("f0009a0fa67238f6dfbb208282f509fb460531f43f74809e0226af2ff064607fad8a2506779e717a5f7848bbc3abdfa724148a9df46426027f201a4dfec27809"),
   133  							mustDecodeString("ca85ea98011ddd592d1f081ebd2a91ac0f4238784222ed85b9d95aeb654f1cf1"),
   134  						},
   135  						bc.Hash{V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994},
   136  						*consensus.BTMAssetID, 11818900000, 0, mustDecodeString("0014e6e1f8b11f1cfb7609037003b90f64837afd272c")),
   137  				},
   138  				Outputs: []*types.TxOutput{
   139  					types.NewTxOutput(*consensus.BTMAssetID, 11718900000, mustDecodeString("0014085a02ecdf934a56343aa59a3dec9d9feb86ee43")),
   140  					types.NewTxOutput(*consensus.BTMAssetID, 90000000, []byte{byte(vm.OP_FAIL)}),
   141  				},
   142  			},
   143  			gasValid: true,
   144  			err:      nil,
   145  		},
   146  		{
   147  			desc: "single utxo, single sign, issuance, spend, retire, btm stanard transaction, gas sufficient",
   148  			txData: &types.TxData{
   149  				Version:        1,
   150  				SerializedSize: 601,
   151  				Inputs: []*types.TxInput{
   152  					types.NewSpendInput(
   153  						[][]byte{
   154  							mustDecodeString("8aab6052cb935384ac8fcbd4c0857cbce2e19825a002635d0b242757f17e5fdd148d83eb3837baf91754bf539cd08e29f66975f4bc9843ac00e280f228026105"),
   155  							mustDecodeString("ca85ea98011ddd592d1f081ebd2a91ac0f4238784222ed85b9d95aeb654f1cf1"),
   156  						},
   157  						bc.Hash{V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994},
   158  						*consensus.BTMAssetID, 11818900000, 0, mustDecodeString("0014e6e1f8b11f1cfb7609037003b90f64837afd272c")),
   159  					types.NewIssuanceInput(
   160  						mustDecodeString("fd0aec4229deb281"),
   161  						10000000000,
   162  						mustDecodeString("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"),
   163  						[][]byte{
   164  							mustDecodeString("e8f301f7bd3b1e4ca853b15559b3a253a4f5f9c7efba233ab0f6896bec23adc6a816c350e08f6b8ac5bc23eb5720173f9190805328af581f34a7fe561358d100"),
   165  						},
   166  						mustDecodeString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
   167  					),
   168  				},
   169  				Outputs: []*types.TxOutput{
   170  					types.NewTxOutput(*consensus.BTMAssetID, 1818900000, mustDecodeString("00147d6b00edfbbc758a5da6130a5fa1a4cfec8422c3")),
   171  					types.NewTxOutput(*consensus.BTMAssetID, 9900000000, []byte{byte(vm.OP_FAIL)}),
   172  					types.NewTxOutput(bc.AssetID{V0: 8879089148261671560, V1: 16875272676673176923, V2: 14627348561007036053, V3: 5774520766896450836}, 10000000000, mustDecodeString("0014447e597c1c326ad1a639f8023d3f87ae22a4e049")),
   173  				},
   174  			},
   175  			gasValid: true,
   176  			err:      nil,
   177  		},
   178  		{
   179  			desc: "single utxo, single sign, issuance, spend, retire, btm stanard transaction, gas insufficient",
   180  			txData: &types.TxData{
   181  				Version:        1,
   182  				SerializedSize: 601,
   183  				Inputs: []*types.TxInput{
   184  					types.NewSpendInput(
   185  						[][]byte{
   186  							mustDecodeString("23ca3a6f8474b1b9ab8b77fcf3cf3fd9dfa761dff4e5d8551a72307dc065cd19100f3ca9fcca4df2f8842b71dba2fd29b73c1b06b3d8bddc2a71e8cc18842a04"),
   187  							mustDecodeString("ca85ea98011ddd592d1f081ebd2a91ac0f4238784222ed85b9d95aeb654f1cf1"),
   188  						},
   189  						bc.Hash{V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994},
   190  						*consensus.BTMAssetID, 11818900000, 0, mustDecodeString("0014e6e1f8b11f1cfb7609037003b90f64837afd272c")),
   191  					types.NewIssuanceInput(
   192  						mustDecodeString("4b6afc9344c3ce63"),
   193  						10000000000,
   194  						mustDecodeString("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"),
   195  						[][]byte{
   196  							mustDecodeString("e8f301f7bd3b1e4ca85f1f8acda3a91fb73e717c096b8b82b2c7ed9d25170c0f9fcd9b5e8039094bd1174886f1b5428272eb6c2af03946bf3c2037a4b499c77107b94b96a92088a0d0d3b15559b3a253a4f5f9c7efba233ab0f6896bec23adc6a816c350e08f6b8ac5bc23eb5720173f9190805328af581f34a7fe561358d100"),
   197  						},
   198  						mustDecodeString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
   199  					),
   200  				},
   201  				Outputs: []*types.TxOutput{
   202  					types.NewTxOutput(*consensus.BTMAssetID, 1818900000, mustDecodeString("001482b7991d64d001009b673ffe3ca2b35eab14f142")),
   203  					types.NewTxOutput(*consensus.BTMAssetID, 10000000000, []byte{byte(vm.OP_FAIL)}),
   204  					types.NewTxOutput(bc.AssetID{V0: 8879089148261671560, V1: 16875272676673176923, V2: 14627348561007036053, V3: 5774520766896450836}, 10000000000, mustDecodeString("0014447e597c1c326ad1a639f8023d3f87ae22a4e049")),
   205  				},
   206  			},
   207  			gasValid: false,
   208  			err:      vm.ErrRunLimitExceeded,
   209  		},
   210  		{
   211  			desc: "btm stanard transaction check signature is not passed",
   212  			txData: &types.TxData{
   213  				Version:        1,
   214  				SerializedSize: 331,
   215  				Inputs: []*types.TxInput{
   216  					types.NewSpendInput(
   217  						[][]byte{
   218  							mustDecodeString("298fbf48459480914e19a0fc20440b095bd7f38d9f01c56bfc904b4ed4967a7b73f1fc4919f23a7806eeb834a89f8ce696500f4528e8f7bf29c8ee1f38a91e02"),
   219  							mustDecodeString("5a260070d967d894a9c4a6e16670c2881ed4c225e12d93b0707156e71fce5bfd"),
   220  						},
   221  						bc.Hash{V0: 3485387979411255237, V1: 15603105575416882039, V2: 5974145557334619041, V3: 16513948410238218452},
   222  						*consensus.BTMAssetID, 21819700000, 0, mustDecodeString("001411ef7695d46e1f9288d996c3daa6ff4d956ac355")),
   223  				},
   224  				Outputs: []*types.TxOutput{
   225  					types.NewTxOutput(*consensus.BTMAssetID, 11818900000, mustDecodeString("001415c956112c2b46354690e36051803cc9d5a8f26b")),
   226  					types.NewTxOutput(*consensus.BTMAssetID, 10000000000, mustDecodeString("00149c9dd93184cc34ac5d47c145c5af3df852235aad")),
   227  				},
   228  			},
   229  			gasValid: false,
   230  			err:      vm.ErrFalseVMResult,
   231  		},
   232  		{
   233  			desc: "non btm stanard transaction",
   234  			txData: &types.TxData{
   235  				Version:        1,
   236  				SerializedSize: 508,
   237  				Inputs: []*types.TxInput{
   238  					types.NewSpendInput(
   239  						[][]byte{mustDecodeString("585f298f2396c3b1be616b6eb48b21c7ec2b25fa4daf7256e970e0a55658c04cbcb406ed41e6b184732187daf0627ea805b24098785c80979edf4d4fc2b8100c")},
   240  						bc.Hash{V0: 13727785470566991667, V1: 17422390991613608658, V2: 10016033157382430074, V3: 8274310611876171875},
   241  						bc.AssetID{V0: 986236576456443635, V1: 13806502593573493203, V2: 9657495453304566675, V3: 15226142438973879401},
   242  						1000,
   243  						1,
   244  						mustDecodeString("206dbca07ff0a6025612c835423daadd4460c3a2ed9a65622ba8025dfd3388238c7403ae7cac00c0")),
   245  					types.NewSpendInput(
   246  						[][]byte{
   247  							mustDecodeString("4ef8f5a377c166b9fb4efa221894f06194b6b7bc277e613ad75b442929a417bb278ee347586e8f06b20c9b759263c981f03d00253f49753fde88dc8b39ccb10e"),
   248  							mustDecodeString("1381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed9"),
   249  						},
   250  						bc.Hash{V0: 5430419158397285610, V1: 15989125147582690097, V2: 3140150800656736345, V3: 4704385074037173738},
   251  						*consensus.BTMAssetID, 9800000000, 2, mustDecodeString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e")),
   252  				},
   253  				Outputs: []*types.TxOutput{
   254  					types.NewTxOutput(
   255  						bc.AssetID{V0: 986236576456443635, V1: 13806502593573493203, V2: 9657495453304566675, V3: 15226142438973879401},
   256  						1000,
   257  						mustDecodeString("001437e1aec83a4e6587ca9609e4e5aa728db7007449")),
   258  					types.NewTxOutput(*consensus.BTMAssetID, 9750000000, mustDecodeString("0014ec75fda5c727cb0d41137ab62afbf9070a405744")),
   259  				},
   260  			},
   261  			gasValid: true,
   262  			err:      nil,
   263  		},
   264  	}
   265  
   266  	for i, c := range cases {
   267  		gasStatus, err := ValidateTx(types.MapTx(c.txData), mockBlock())
   268  		if rootErr(err) != c.err {
   269  			t.Errorf("case #%d (%s) got error %s, want %s; validationState is:\n", i, c.desc, err, c.err)
   270  		}
   271  		if c.gasValid != gasStatus.GasValid {
   272  			t.Errorf("#%d got GasValid %t, want %t", i, gasStatus.GasValid, c.gasValid)
   273  		}
   274  	}
   275  }
   276  
   277  func mustDecodeString(hexString string) []byte {
   278  	bytes, err := hex.DecodeString(hexString)
   279  	if err != nil {
   280  		panic(err)
   281  	}
   282  	return bytes
   283  }