OSDN Git Service

feat(warder): add warder backbone (#181)
[bytom/vapor.git] / protocol / validation / tx.go
index 8d36ea5..0d25751 100644 (file)
@@ -4,8 +4,8 @@ import (
        "fmt"
        "math"
 
+       "github.com/vapor/config"
        "github.com/vapor/consensus"
-       "github.com/vapor/consensus/segwit"
        "github.com/vapor/errors"
        "github.com/vapor/math/checked"
        "github.com/vapor/protocol/bc"
@@ -36,6 +36,8 @@ var (
        ErrUnbalanced                = errors.New("unbalanced asset amount between input and output")
        ErrOverGasCredit             = errors.New("all gas credit has been spend")
        ErrGasCalculate              = errors.New("gas usage calculate got a math error")
+       ErrVotePubKey                = errors.New("invalid public key of vote")
+       ErrVoteOutputAmount          = errors.New("invalid vote amount")
 )
 
 // GasState record the gas usage status
@@ -210,13 +212,33 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        }
                }
 
-       case *bc.Output:
+       case *bc.IntraChainOutput:
                vs2 := *vs
                vs2.sourcePos = 0
                if err = checkValidSrc(&vs2, e.Source); err != nil {
                        return errors.Wrap(err, "checking output source")
                }
 
+       case *bc.CrossChainOutput:
+               vs2 := *vs
+               vs2.sourcePos = 0
+               if err = checkValidSrc(&vs2, e.Source); err != nil {
+                       return errors.Wrap(err, "checking output source")
+               }
+
+       case *bc.VoteOutput:
+               if len(e.Vote) != 64 {
+                       return ErrVotePubKey
+               }
+               vs2 := *vs
+               vs2.sourcePos = 0
+               if err = checkValidSrc(&vs2, e.Source); err != nil {
+                       return errors.Wrap(err, "checking vote output source")
+               }
+               if e.Source.Value.Amount < consensus.MinVoteOutputAmount {
+                       return ErrVoteOutputAmount
+               }
+
        case *bc.Retirement:
                vs2 := *vs
                vs2.sourcePos = 0
@@ -224,31 +246,36 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        return errors.Wrap(err, "checking retirement source")
                }
 
-       case *bc.Issuance:
-               computedAssetID := e.WitnessAssetDefinition.ComputeAssetID()
-               if computedAssetID != *e.Value.AssetId {
-                       return errors.WithDetailf(ErrMismatchedAssetID, "asset ID is %x, issuance wants %x", computedAssetID.Bytes(), e.Value.AssetId.Bytes())
+       case *bc.CrossChainInput:
+               // check assetID
+               assetID := e.AssetDefinition.ComputeAssetID()
+               if *e.Value.AssetId != *consensus.BTMAssetID && *e.Value.AssetId != assetID {
+                       return errors.New("incorrect asset_id while checking CrossChainInput")
                }
 
-               gasLeft, err := vm.Verify(NewTxVMContext(vs, e, e.WitnessAssetDefinition.IssuanceProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
-               if err != nil {
-                       return errors.Wrap(err, "checking issuance program")
+               code := config.FederationProgrom(config.CommonConfig)
+               prog := &bc.Program{
+                       VmVersion: e.ControlProgram.VmVersion,
+                       Code:      code,
                }
-               if err = vs.gasStatus.updateUsage(gasLeft); err != nil {
-                       return err
+               _, err := vm.Verify(NewTxVMContext(vs, e, prog, e.WitnessArguments), consensus.DefaultGasCredit)
+               if err != nil {
+                       return errors.Wrap(err, "checking cross-chain input control program")
                }
 
-               destVS := *vs
-               destVS.destPos = 0
-               if err = checkValidDest(&destVS, e.WitnessDestination); err != nil {
-                       return errors.Wrap(err, "checking issuance destination")
+               vs2 := *vs
+               vs2.destPos = 0
+               if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
+                       return errors.Wrap(err, "checking cross-chain input destination")
                }
+               vs.gasStatus.StorageGas = 0
 
        case *bc.Spend:
                if e.SpentOutputId == nil {
                        return errors.Wrap(ErrMissingField, "spend without spent output ID")
                }
-               spentOutput, err := vs.tx.Output(*e.SpentOutputId)
+
+               spentOutput, err := vs.tx.IntraChainOutput(*e.SpentOutputId)
                if err != nil {
                        return errors.Wrap(err, "getting spend prevout")
                }
@@ -275,13 +302,53 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                                e.WitnessDestination.Value.AssetId.Bytes(),
                        )
                }
-
                vs2 := *vs
                vs2.destPos = 0
                if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
                        return errors.Wrap(err, "checking spend destination")
                }
 
+       case *bc.VetoInput:
+               if e.SpentOutputId == nil {
+                       return errors.Wrap(ErrMissingField, "vetoInput without vetoInput output ID")
+               }
+
+               voteOutput, err := vs.tx.VoteOutput(*e.SpentOutputId)
+               if err != nil {
+                       return errors.Wrap(err, "getting vetoInput prevout")
+               }
+               if len(voteOutput.Vote) != 64 {
+                       return ErrVotePubKey
+               }
+
+               gasLeft, err := vm.Verify(NewTxVMContext(vs, e, voteOutput.ControlProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
+               if err != nil {
+                       return errors.Wrap(err, "checking control program")
+               }
+               if err = vs.gasStatus.updateUsage(gasLeft); err != nil {
+                       return err
+               }
+
+               eq, err := voteOutput.Source.Value.Equal(e.WitnessDestination.Value)
+               if err != nil {
+                       return err
+               }
+               if !eq {
+                       return errors.WithDetailf(
+                               ErrMismatchedValue,
+                               "previous output is for %d unit(s) of %x, vetoInput wants %d unit(s) of %x",
+                               voteOutput.Source.Value.Amount,
+                               voteOutput.Source.Value.AssetId.Bytes(),
+                               e.WitnessDestination.Value.Amount,
+                               e.WitnessDestination.Value.AssetId.Bytes(),
+                       )
+               }
+               vs2 := *vs
+               vs2.destPos = 0
+               if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
+                       return errors.Wrap(err, "checking vetoInput destination")
+               }
+
        case *bc.Coinbase:
                if vs.block == nil || len(vs.block.Transactions) == 0 || vs.block.Transactions[0] != vs.tx {
                        return ErrWrongCoinbaseTransaction
@@ -333,15 +400,21 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
 
        var dest *bc.ValueDestination
        switch ref := e.(type) {
+       case *bc.VetoInput:
+               if vs.Position != 0 {
+                       return errors.Wrapf(ErrPosition, "invalid position %d for veto-input source", vs.Position)
+               }
+               dest = ref.WitnessDestination
+
        case *bc.Coinbase:
                if vs.Position != 0 {
                        return errors.Wrapf(ErrPosition, "invalid position %d for coinbase source", vs.Position)
                }
                dest = ref.WitnessDestination
 
-       case *bc.Issuance:
+       case *bc.CrossChainInput:
                if vs.Position != 0 {
-                       return errors.Wrapf(ErrPosition, "invalid position %d for issuance source", vs.Position)
+                       return errors.Wrapf(ErrPosition, "invalid position %d for cross-chain input source", vs.Position)
                }
                dest = ref.WitnessDestination
 
@@ -358,7 +431,7 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
                dest = ref.WitnessDestinations[vs.Position]
 
        default:
-               return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be coinbase, issuance, spend, or mux", e)
+               return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be coinbase, cross-chain input, spend, or mux", e)
        }
 
        if dest.Ref == nil || *dest.Ref != vstate.entryID {
@@ -398,7 +471,19 @@ func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
 
        var src *bc.ValueSource
        switch ref := e.(type) {
-       case *bc.Output:
+       case *bc.IntraChainOutput:
+               if vd.Position != 0 {
+                       return errors.Wrapf(ErrPosition, "invalid position %d for output destination", vd.Position)
+               }
+               src = ref.Source
+
+       case *bc.CrossChainOutput:
+               if vd.Position != 0 {
+                       return errors.Wrapf(ErrPosition, "invalid position %d for output destination", vd.Position)
+               }
+               src = ref.Source
+
+       case *bc.VoteOutput:
                if vd.Position != 0 {
                        return errors.Wrapf(ErrPosition, "invalid position %d for output destination", vd.Position)
                }
@@ -417,7 +502,7 @@ func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
                src = ref.Sources[vd.Position]
 
        default:
-               return errors.Wrapf(bc.ErrEntryType, "value destination is %T, should be output, retirement, or mux", e)
+               return errors.Wrapf(bc.ErrEntryType, "value destination is %T, should be intra-chain/cross-chain output, retirement, or mux", e)
        }
 
        if src.Ref == nil || *src.Ref != vs.entryID {
@@ -439,43 +524,12 @@ func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
        return nil
 }
 
-func checkStandardTx(tx *bc.Tx, blockHeight uint64) error {
+func checkInputID(tx *bc.Tx, blockHeight uint64) error {
        for _, id := range tx.InputIDs {
                if blockHeight >= ruleAA && id.IsZero() {
                        return ErrEmptyInputIDs
                }
        }
-
-       for _, id := range tx.GasInputIDs {
-               spend, err := tx.Spend(id)
-               if err != nil {
-                       continue
-               }
-               spentOutput, err := tx.Output(*spend.SpentOutputId)
-               if err != nil {
-                       return err
-               }
-
-               if !segwit.IsP2WScript(spentOutput.ControlProgram.Code) {
-                       return ErrNotStandardTx
-               }
-       }
-
-       for _, id := range tx.ResultIds {
-               e, ok := tx.Entries[*id]
-               if !ok {
-                       return errors.Wrapf(bc.ErrMissingEntry, "id %x", id.Bytes())
-               }
-
-               output, ok := e.(*bc.Output)
-               if !ok || *output.Source.Value.AssetId != *consensus.BTMAssetID {
-                       continue
-               }
-
-               if !segwit.IsP2WScript(output.ControlProgram.Code) {
-                       return ErrNotStandardTx
-               }
-       }
        return nil
 }
 
@@ -487,6 +541,7 @@ func checkTimeRange(tx *bc.Tx, block *bc.Block) error {
        if tx.TimeRange < block.Height {
                return ErrBadTimeRange
        }
+
        return nil
 }
 
@@ -502,7 +557,7 @@ func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
        if err := checkTimeRange(tx, block); err != nil {
                return gasStatus, err
        }
-       if err := checkStandardTx(tx, block.Height); err != nil {
+       if err := checkInputID(tx, block.Height); err != nil {
                return gasStatus, err
        }