OSDN Git Service

add free gas (#295)
[bytom/vapor.git] / protocol / validation / tx_test.go
index 465cb60..60b34ab 100644 (file)
@@ -34,7 +34,7 @@ func TestGasStatus(t *testing.T) {
                                BTMValue: 0,
                        },
                        output: &GasState{
-                               GasLeft:  10000 / consensus.VMGasRate,
+                               GasLeft:  10000/consensus.ActiveNetParams.VMGasRate + consensus.ActiveNetParams.DefaultGasCredit,
                                GasUsed:  0,
                                BTMValue: 10000,
                        },
@@ -61,7 +61,7 @@ func TestGasStatus(t *testing.T) {
                },
                {
                        input: &GasState{
-                               GasLeft:  consensus.DefaultGasCredit,
+                               GasLeft:  consensus.ActiveNetParams.DefaultGasCredit,
                                GasUsed:  0,
                                BTMValue: 0,
                        },
@@ -77,7 +77,7 @@ func TestGasStatus(t *testing.T) {
                },
                {
                        input: &GasState{
-                               GasLeft:  consensus.DefaultGasCredit,
+                               GasLeft:  consensus.ActiveNetParams.DefaultGasCredit,
                                GasUsed:  0,
                                BTMValue: 0,
                        },
@@ -231,8 +231,8 @@ func TestOverflow(t *testing.T) {
                txInputs := make([]*types.TxInput, 0, len(inputs))
                txOutputs := make([]*types.TxOutput, 0, len(outputs))
 
-               for _, amount := range inputs {
-                       txInput := types.NewSpendInput(nil, *sourceID, *consensus.BTMAssetID, amount, 0, ctrlProgram)
+               for i, amount := range inputs {
+                       txInput := types.NewSpendInput(nil, *sourceID, *consensus.BTMAssetID, amount, uint64(i), ctrlProgram)
                        txInputs = append(txInputs, txInput)
                }
 
@@ -366,15 +366,6 @@ func TestTxValidation(t *testing.T) {
                {
                        desc: "unbalanced mux amounts",
                        f: func() {
-                               mux.Sources[0].Value.Amount++
-                               iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
-                               iss.WitnessDestination.Value.Amount++
-                       },
-                       err: ErrUnbalanced,
-               },
-               {
-                       desc: "unbalanced mux amounts",
-                       f: func() {
                                mux.WitnessDestinations[0].Value.Amount++
                        },
                        err: ErrUnbalanced,
@@ -388,15 +379,6 @@ func TestTxValidation(t *testing.T) {
                        err: nil,
                },
                {
-                       desc: "overflowing mux source amounts",
-                       f: func() {
-                               mux.Sources[0].Value.Amount = math.MaxInt64
-                               iss := tx.Entries[*mux.Sources[0].Ref].(*bc.Issuance)
-                               iss.WitnessDestination.Value.Amount = math.MaxInt64
-                       },
-                       err: ErrOverflow,
-               },
-               {
                        desc: "underflowing mux destination amounts",
                        f: func() {
                                mux.WitnessDestinations[0].Value.Amount = math.MaxInt64
@@ -448,21 +430,6 @@ func TestTxValidation(t *testing.T) {
                        err: ErrMismatchedReference,
                },
                {
-                       desc: "mismatched input dest and mux source",
-                       f: func() {
-                               fixture2 := sample(t, fixture)
-                               tx2 := types.NewTx(*fixture2.tx).Tx
-                               input2ID := tx2.InputIDs[2]
-                               input2 := tx2.Entries[input2ID].(*bc.Spend)
-                               dest2Ref := input2.WitnessDestination.Ref
-                               dest2 := tx2.Entries[*dest2Ref].(*bc.Mux)
-                               tx.Entries[*dest2Ref] = dest2
-                               tx.Entries[input2ID] = input2
-                               mux.Sources[0].Ref = &input2ID
-                       },
-                       err: ErrMismatchedReference,
-               },
-               {
                        desc: "invalid mux destination position",
                        f: func() {
                                mux.WitnessDestinations[0].Position = 1
@@ -497,14 +464,6 @@ func TestTxValidation(t *testing.T) {
                        },
                },
                {
-                       desc: "issuance program failure",
-                       f: func() {
-                               iss := txIssuance(t, tx, 0)
-                               iss.WitnessArguments[0] = []byte{}
-                       },
-                       err: vm.ErrFalseVMResult,
-               },
-               {
                        desc: "spend control program failure",
                        f: func() {
                                spend := txSpend(t, tx, 1)
@@ -541,7 +500,7 @@ func TestTxValidation(t *testing.T) {
                        err: bc.ErrMissingEntry,
                },
                {
-                       desc: "no gas spend input",
+                       desc: "normal check with no gas spend input",
                        f: func() {
                                spendID := mux.Sources[len(mux.Sources)-1].Ref
                                delete(tx.Entries, *spendID)
@@ -549,7 +508,7 @@ func TestTxValidation(t *testing.T) {
                                tx.GasInputIDs = nil
                                vs.gasStatus.GasLeft = 0
                        },
-                       err: vm.ErrRunLimitExceeded,
+                       err: nil,
                },
                {
                        desc: "no gas spend input, but set gas left, so it's ok",
@@ -574,24 +533,6 @@ func TestTxValidation(t *testing.T) {
                        err: ErrMismatchedValue,
                },
                {
-                       desc: "mismatched witness asset destination",
-                       f: func() {
-                               issuanceID := mux.Sources[0].Ref
-                               issuance := tx.Entries[*issuanceID].(*bc.Issuance)
-                               issuance.WitnessAssetDefinition.Data = &bc.Hash{V0: 9999}
-                       },
-                       err: ErrMismatchedAssetID,
-               },
-               {
-                       desc: "issuance witness position greater than length of mux sources",
-                       f: func() {
-                               issuanceID := mux.Sources[0].Ref
-                               issuance := tx.Entries[*issuanceID].(*bc.Issuance)
-                               issuance.WitnessDestination.Position = uint64(len(mux.Sources) + 1)
-                       },
-                       err: ErrPosition,
-               },
-               {
                        desc: "normal coinbase tx",
                        f: func() {
                                addCoinbase(consensus.BTMAssetID, 100000, nil)
@@ -616,7 +557,7 @@ func TestTxValidation(t *testing.T) {
                {
                        desc: "coinbase arbitrary size out of limit",
                        f: func() {
-                               arbitrary := make([]byte, consensus.CoinbaseArbitrarySizeLimit+1)
+                               arbitrary := make([]byte, consensus.ActiveNetParams.CoinbaseArbitrarySizeLimit+1)
                                addCoinbase(consensus.BTMAssetID, 100000, arbitrary)
                        },
                        err: ErrCoinbaseArbitraryOversize,
@@ -771,8 +712,8 @@ func TestCoinbase(t *testing.T) {
                                        types.MapTx(&types.TxData{
                                                SerializedSize: 1,
                                                Inputs: []*types.TxInput{
-                                                       types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
                                                        types.NewCoinbaseInput(nil),
+                                                       types.NewSpendInput([][]byte{}, *newHash(8), *consensus.BTMAssetID, 100000000, 0, cp),
                                                },
                                                Outputs: []*types.TxOutput{
                                                        types.NewIntraChainOutput(*consensus.BTMAssetID, 888, cp),
@@ -868,7 +809,7 @@ func TestTimeRange(t *testing.T) {
        block := &bc.Block{
                BlockHeader: &bc.BlockHeader{
                        Height:    333,
-                       Timestamp: 1521625823,
+                       Timestamp: 1521625823000,
                },
        }
 
@@ -891,53 +832,6 @@ 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()
-               err  error
-       }{
-               {
-                       desc: "normal standard tx",
-                       err:  nil,
-               },
-               {
-                       desc: "not standard tx in spend input",
-                       f: func() {
-                               inputID := tx.GasInputIDs[0]
-                               spend := tx.Entries[inputID].(*bc.Spend)
-                               spentOutput, err := tx.IntraChainOutput(*spend.SpentOutputId)
-                               if err != nil {
-                                       t.Fatal(err)
-                               }
-                               spentOutput.ControlProgram = &bc.Program{Code: []byte{0}}
-                       },
-                       err: ErrNotStandardTx,
-               },
-               {
-                       desc: "not standard tx in output",
-                       f: func() {
-                               outputID := tx.ResultIds[0]
-                               output := tx.Entries[*outputID].(*bc.IntraChainOutput)
-                               output.ControlProgram = &bc.Program{Code: []byte{0}}
-                       },
-                       err: ErrNotStandardTx,
-               },
-       }
-
-       for i, c := range cases {
-               if c.f != nil {
-                       c.f()
-               }
-               if err := checkStandardTx(tx, 0); err != c.err {
-                       t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
-               }
-       }
-}
-
 func TestValidateTxVersion(t *testing.T) {
        cases := []struct {
                desc  string
@@ -1041,8 +935,7 @@ func sample(tb testing.TB, in *txFixture) *txFixture {
                result.assetDef = []byte{2}
        }
        if result.assetID.IsZero() {
-               refdatahash := hashData(result.assetDef)
-               result.assetID = bc.ComputeAssetID(result.issuanceProg.Code, result.issuanceProg.VmVersion, &refdatahash)
+               result.assetID = bc.AssetID{V0: 9999}
        }
 
        if result.txVersion == 0 {
@@ -1062,7 +955,7 @@ func sample(tb testing.TB, in *txFixture) *txFixture {
                args2 := [][]byte{{6}, {7}}
 
                result.txInputs = []*types.TxInput{
-                       types.NewIssuanceInput([]byte{3}, 10, result.issuanceProg.Code, result.issuanceArgs, result.assetDef),
+                       types.NewSpendInput(nil, *newHash(9), result.assetID, 10, 0, []byte{byte(vm.OP_TRUE)}),
                        types.NewSpendInput(args1, *newHash(5), result.assetID, 20, 0, cp1),
                        types.NewSpendInput(args2, *newHash(8), result.assetID, 40, 0, cp2),
                }
@@ -1129,15 +1022,6 @@ func newAssetID(n byte) *bc.AssetID {
        return &a
 }
 
-func txIssuance(t *testing.T, tx *bc.Tx, index int) *bc.Issuance {
-       id := tx.InputIDs[index]
-       res, err := tx.Issuance(id)
-       if err != nil {
-               t.Fatal(err)
-       }
-       return res
-}
-
 func txSpend(t *testing.T, tx *bc.Tx, index int) *bc.Spend {
        id := tx.InputIDs[index]
        res, err := tx.Spend(id)