OSDN Git Service

V0.1 votetx utxo (#73)
[bytom/vapor.git] / protocol / validation / tx.go
index e81fe11..9225eb2 100644 (file)
@@ -4,7 +4,6 @@ import (
        "fmt"
        "math"
 
-       "github.com/vapor/claim"
        "github.com/vapor/consensus"
        "github.com/vapor/consensus/segwit"
        "github.com/vapor/errors"
@@ -13,11 +12,14 @@ import (
        "github.com/vapor/protocol/vm"
 )
 
+const ruleAA = 142500
+
 // validate transaction error
 var (
        ErrTxVersion                 = errors.New("invalid transaction version")
        ErrWrongTransactionSize      = errors.New("invalid transaction size")
        ErrBadTimeRange              = errors.New("invalid transaction time range")
+       ErrEmptyInputIDs             = errors.New("got the empty InputIDs")
        ErrNotStandardTx             = errors.New("not standard transaction")
        ErrWrongCoinbaseTransaction  = errors.New("wrong coinbase transaction")
        ErrWrongCoinbaseAsset        = errors.New("wrong coinbase assetID")
@@ -140,11 +142,6 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
        case *bc.Mux:
                parity := make(map[bc.AssetID]int64)
                for i, src := range e.Sources {
-                       _, ok := vs.tx.Entries[*src.Ref]
-                       if !ok {
-                               return errors.Wrapf(bc.ErrMissingEntry, "entry for bytom input %x not found", *src.Ref)
-                       }
-
                        if src.Value.Amount > math.MaxInt64 {
                                return errors.WithDetailf(ErrOverflow, "amount %d exceeds maximum value 2^63", src.Value.Amount)
                        }
@@ -185,6 +182,7 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        if !ok {
                                return errors.Wrapf(bc.ErrMissingEntry, "entry for bytom input %x not found", BTMInputID)
                        }
+
                        vs2 := *vs
                        vs2.entryID = BTMInputID
                        if err := checkValid(&vs2, e); err != nil {
@@ -200,10 +198,8 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        }
                }
 
-               if len(vs.tx.GasInputIDs) > 0 {
-                       if err := vs.gasStatus.setGasValid(); err != nil {
-                               return err
-                       }
+               if err := vs.gasStatus.setGasValid(); err != nil {
+                       return err
                }
 
                for i, src := range e.Sources {
@@ -214,49 +210,71 @@ 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.Retirement:
+       case *bc.CrossChainOutput:
                vs2 := *vs
                vs2.sourcePos = 0
                if err = checkValidSrc(&vs2, e.Source); err != nil {
-                       return errors.Wrap(err, "checking retirement source")
+                       return errors.Wrap(err, "checking output 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.VoteOutput:
+               vs2 := *vs
+               vs2.sourcePos = 0
+               if err = checkValidSrc(&vs2, e.Source); err != nil {
+                       return errors.Wrap(err, "checking output source")
                }
 
-               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")
+       case *bc.Retirement:
+               vs2 := *vs
+               vs2.sourcePos = 0
+               if err = checkValidSrc(&vs2, e.Source); err != nil {
+                       return errors.Wrap(err, "checking retirement source")
                }
-               if err = vs.gasStatus.updateUsage(gasLeft); err != nil {
-                       return err
+
+       case *bc.CrossChainInput:
+               _, err := vm.Verify(NewTxVMContext(vs, e, e.ControlProgram, 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")
                }
 
        case *bc.Spend:
                if e.SpentOutputId == nil {
                        return errors.Wrap(ErrMissingField, "spend without spent output ID")
                }
-               spentOutput, err := vs.tx.Output(*e.SpentOutputId)
+               var (
+                       controlProgram *bc.Program
+                       value          *bc.AssetAmount
+               )
+               entryOutput, err := vs.tx.Entry(*e.SpentOutputId)
                if err != nil {
                        return errors.Wrap(err, "getting spend prevout")
                }
-               gasLeft, err := vm.Verify(NewTxVMContext(vs, e, spentOutput.ControlProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
+
+               switch output := entryOutput.(type) {
+               case *bc.IntraChainOutput:
+                       controlProgram = output.ControlProgram
+                       value = output.Source.Value
+               case *bc.VoteOutput:
+                       controlProgram = output.ControlProgram
+                       value = output.Source.Value
+               default:
+                       return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", e.SpentOutputId.Bytes(), entryOutput)
+               }
+
+               gasLeft, err := vm.Verify(NewTxVMContext(vs, e, controlProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
                if err != nil {
                        return errors.Wrap(err, "checking control program")
                }
@@ -264,7 +282,7 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        return err
                }
 
-               eq, err := spentOutput.Source.Value.Equal(e.WitnessDestination.Value)
+               eq, err := value.Equal(e.WitnessDestination.Value)
                if err != nil {
                        return err
                }
@@ -272,13 +290,12 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        return errors.WithDetailf(
                                ErrMismatchedValue,
                                "previous output is for %d unit(s) of %x, spend wants %d unit(s) of %x",
-                               spentOutput.Source.Value.Amount,
-                               spentOutput.Source.Value.AssetId.Bytes(),
+                               value.Amount,
+                               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 {
@@ -303,51 +320,8 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
                        return errors.Wrap(err, "checking coinbase destination")
                }
+               vs.gasStatus.StorageGas = 0
 
-               // special case for coinbase transaction, it's valid unit all the verify has been passed
-               vs.gasStatus.GasValid = true
-       case *bc.Claim:
-               // 对交易的合法性进行验证
-               if e.SpentOutputId == nil {
-                       return errors.Wrap(ErrMissingField, "spend without spent output ID")
-               }
-               spentOutput, err := vs.tx.Output(*e.SpentOutputId)
-               if err != nil {
-                       return errors.Wrap(err, "getting spend prevout")
-               }
-               stack := e.GetPeginwitness()
-               if len(stack) < 5 || stack[1] == nil || spentOutput.Source == nil {
-
-                       return errors.New("pegin-no-witness")
-               }
-
-               // 根据claim链类型选择验证类型
-               validation := &claim.BytomClaimValidation{}
-               if err := validation.IsValidPeginWitness(stack, *spentOutput); err != nil {
-                       return err
-               }
-
-               eq, err := spentOutput.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, spend wants %d unit(s) of %x",
-                               spentOutput.Source.Value.Amount,
-                               spentOutput.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 spend destination")
-               }
-               vs.gasStatus.GasValid = true
        default:
                return fmt.Errorf("entry has unexpected type %T", e)
        }
@@ -385,9 +359,9 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
                }
                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
 
@@ -402,13 +376,9 @@ func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
                        return errors.Wrapf(ErrPosition, "invalid position %d for %d-destination mux source", vs.Position, len(ref.WitnessDestinations))
                }
                dest = ref.WitnessDestinations[vs.Position]
-       case *bc.Claim:
-               if vs.Position != 0 {
-                       return errors.Wrapf(ErrPosition, "invalid position %d for coinbase source", vs.Position)
-               }
-               dest = ref.WitnessDestination
+
        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 {
@@ -438,7 +408,7 @@ func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
                return errors.Wrap(ErrMissingField, "missing ref on value destination")
        }
        if vd.Value == nil || vd.Value.AssetId == nil {
-               return errors.Wrap(ErrMissingField, "missing value on value source")
+               return errors.Wrap(ErrMissingField, "missing value on value destination")
        }
 
        e, ok := vs.tx.Entries[*vd.Ref]
@@ -448,7 +418,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)
                }
@@ -467,7 +449,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 {
@@ -489,34 +471,34 @@ func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
        return nil
 }
 
-func checkStandardTx(tx *bc.Tx) error {
+func checkStandardTx(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 {
-                       return err
+                       continue
                }
-               spentOutput, err := tx.Output(*spend.SpentOutputId)
+
+               code := []byte{}
+               outputEntry, err := tx.Entry(*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())
+               switch output := outputEntry.(type) {
+               case *bc.IntraChainOutput:
+                       code = output.ControlProgram.Code
+               case *bc.VoteOutput:
+                       code = output.ControlProgram.Code
+               default:
+                       return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), outputEntry)
                }
 
-               output, ok := e.(*bc.Output)
-               if !ok || *output.Source.Value.AssetId != *consensus.BTMAssetID {
-                       continue
-               }
-
-               if !segwit.IsP2WScript(output.ControlProgram.Code) {
+               if !segwit.IsP2WScript(code) {
                        return ErrNotStandardTx
                }
        }
@@ -531,6 +513,7 @@ func checkTimeRange(tx *bc.Tx, block *bc.Block) error {
        if tx.TimeRange < block.Height {
                return ErrBadTimeRange
        }
+
        return nil
 }
 
@@ -546,9 +529,10 @@ func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
        if err := checkTimeRange(tx, block); err != nil {
                return gasStatus, err
        }
-       if err := checkStandardTx(tx); err != nil {
+       if err := checkStandardTx(tx, block.Height); err != nil {
                return gasStatus, err
        }
+
        vs := &validationState{
                block:     block,
                tx:        tx,