OSDN Git Service

modify import path (#1805)
[bytom/bytom.git] / protocol / validation / tx_test.go
index c09d972..d143da7 100644 (file)
@@ -6,14 +6,14 @@ import (
 
        "github.com/davecgh/go-spew/spew"
 
-       "github.com/bytom/consensus"
-       "github.com/bytom/crypto/sha3pool"
-       "github.com/bytom/errors"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/vm"
-       "github.com/bytom/protocol/vm/vmutil"
-       "github.com/bytom/testutil"
+       "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/crypto/sha3pool"
+       "github.com/bytom/bytom/errors"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/vm"
+       "github.com/bytom/bytom/protocol/vm/vmutil"
+       "github.com/bytom/bytom/testutil"
 )
 
 func init() {
@@ -77,6 +77,22 @@ func TestGasStatus(t *testing.T) {
                },
                {
                        input: &GasState{
+                               GasLeft:  consensus.DefaultGasCredit,
+                               GasUsed:  0,
+                               BTMValue: 0,
+                       },
+                       output: &GasState{
+                               GasLeft:  200000,
+                               GasUsed:  0,
+                               BTMValue: math.MaxInt64,
+                       },
+                       f: func(input *GasState) error {
+                               return input.setGas(math.MaxInt64, 0)
+                       },
+                       err: nil,
+               },
+               {
+                       input: &GasState{
                                GasLeft:  10000,
                                GasUsed:  0,
                                BTMValue: 0,
@@ -109,6 +125,22 @@ func TestGasStatus(t *testing.T) {
                },
                {
                        input: &GasState{
+                               GasLeft:  -10000,
+                               GasUsed:  0,
+                               BTMValue: 0,
+                       },
+                       output: &GasState{
+                               GasLeft:  -10000,
+                               GasUsed:  0,
+                               BTMValue: 0,
+                       },
+                       f: func(input *GasState) error {
+                               return input.updateUsage(math.MaxInt64)
+                       },
+                       err: ErrGasCalculate,
+               },
+               {
+                       input: &GasState{
                                GasLeft:    1000,
                                GasUsed:    10,
                                StorageGas: 1000,
@@ -659,6 +691,7 @@ func TestTxValidation(t *testing.T) {
        }
 }
 
+// TestCoinbase test the coinbase transaction is valid (txtest#1016)
 func TestCoinbase(t *testing.T) {
        cp, _ := vmutil.DefaultCoinbaseProgram()
        retire, _ := vmutil.RetireProgram([]byte{})
@@ -851,6 +884,7 @@ func TestRuleAA(t *testing.T) {
 
 }
 
+// TestTimeRange test the checkTimeRange function (txtest#1004)
 func TestTimeRange(t *testing.T) {
        cases := []struct {
                timeRange uint64
@@ -903,7 +937,7 @@ func TestTimeRange(t *testing.T) {
 func TestStandardTx(t *testing.T) {
        fixture := sample(t, nil)
        tx := types.NewTx(*fixture.tx).Tx
-       
+
        cases := []struct {
                desc string
                f    func()
@@ -947,6 +981,51 @@ func TestStandardTx(t *testing.T) {
        }
 }
 
+func TestValidateTxVersion(t *testing.T) {
+       cases := []struct {
+               desc  string
+               block *bc.Block
+               err   error
+       }{
+               {
+                       desc: "tx version greater than 1 (txtest#1001)",
+                       block: &bc.Block{
+                               BlockHeader: &bc.BlockHeader{Version: 1},
+                               Transactions: []*bc.Tx{
+                                       {TxHeader: &bc.TxHeader{Version: 2}},
+                               },
+                       },
+                       err: ErrTxVersion,
+               },
+               {
+                       desc: "tx version equals 0 (txtest#1002)",
+                       block: &bc.Block{
+                               BlockHeader: &bc.BlockHeader{Version: 1},
+                               Transactions: []*bc.Tx{
+                                       {TxHeader: &bc.TxHeader{Version: 0}},
+                               },
+                       },
+                       err: ErrTxVersion,
+               },
+               {
+                       desc: "tx version equals max uint64 (txtest#1003)",
+                       block: &bc.Block{
+                               BlockHeader: &bc.BlockHeader{Version: 1},
+                               Transactions: []*bc.Tx{
+                                       {TxHeader: &bc.TxHeader{Version: math.MaxUint64}},
+                               },
+                       },
+                       err: ErrTxVersion,
+               },
+       }
+
+       for i, c := range cases {
+               if _, err := ValidateTx(c.block.Transactions[0], c.block); rootErr(err) != c.err {
+                       t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
+               }
+       }
+}
+
 // A txFixture is returned by sample (below) to produce a sample
 // transaction, which takes a separate, optional _input_ txFixture to
 // affect the transaction that's built. The components of the
@@ -999,7 +1078,7 @@ func sample(tb testing.TB, in *txFixture) *txFixture {
                result.issuanceProg = bc.Program{VmVersion: 1, Code: prog}
        }
        if len(result.issuanceArgs) == 0 {
-               result.issuanceArgs = [][]byte{[]byte{2}, []byte{3}}
+               result.issuanceArgs = [][]byte{{2}, {3}}
        }
        if len(result.assetDef) == 0 {
                result.assetDef = []byte{2}
@@ -1017,13 +1096,13 @@ func sample(tb testing.TB, in *txFixture) *txFixture {
                if err != nil {
                        tb.Fatal(err)
                }
-               args1 := [][]byte{[]byte{4}, []byte{5}}
+               args1 := [][]byte{{4}, {5}}
 
                cp2, err := vm.Assemble("ADD 13 NUMEQUAL")
                if err != nil {
                        tb.Fatal(err)
                }
-               args2 := [][]byte{[]byte{6}, []byte{7}}
+               args2 := [][]byte{{6}, {7}}
 
                result.txInputs = []*types.TxInput{
                        types.NewIssuanceInput([]byte{3}, 10, result.issuanceProg.Code, result.issuanceArgs, result.assetDef),