OSDN Git Service

change crossin tx multi sign to single (#593)
[bytom/vapor.git] / protocol / validation / tx.go
index bd493c5..a9613b5 100644 (file)
@@ -3,18 +3,16 @@ package validation
 import (
        "fmt"
        "math"
+       "runtime"
        "sync"
 
-       "github.com/vapor/config"
-       "github.com/vapor/consensus"
-       "github.com/vapor/errors"
-       "github.com/vapor/math/checked"
-       "github.com/vapor/protocol/bc"
-       "github.com/vapor/protocol/vm"
-)
-
-const (
-       validateWorkerNum = 32
+       "github.com/bytom/vapor/common"
+       "github.com/bytom/vapor/config"
+       "github.com/bytom/vapor/consensus"
+       "github.com/bytom/vapor/errors"
+       "github.com/bytom/vapor/math/checked"
+       "github.com/bytom/vapor/protocol/bc"
+       "github.com/bytom/vapor/protocol/vm"
 )
 
 // validate transaction error
@@ -41,6 +39,7 @@ var (
        ErrGasCalculate              = errors.New("gas usage calculate got a math error")
        ErrVotePubKey                = errors.New("invalid public key of vote")
        ErrVoteOutputAmount          = errors.New("invalid vote amount")
+       ErrVoteOutputAseet           = errors.New("incorrect asset_id while checking vote asset")
 )
 
 // GasState record the gas usage status
@@ -239,6 +238,7 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                if len(e.Vote) != 64 {
                        return ErrVotePubKey
                }
+
                vs2 := *vs
                vs2.sourcePos = 0
                if err = checkValidSrc(&vs2, e.Source); err != nil {
@@ -249,6 +249,10 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        return ErrVoteOutputAmount
                }
 
+               if *e.Source.Value.AssetId != *consensus.BTMAssetID {
+                       return ErrVoteOutputAseet
+               }
+
        case *bc.Retirement:
                vs2 := *vs
                vs2.sourcePos = 0
@@ -272,8 +276,12 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                }
 
                prog := &bc.Program{
-                       VmVersion: e.ControlProgram.VmVersion,
-                       Code:      config.FederationWScript(config.CommonConfig),
+                       VmVersion: e.AssetDefinition.IssuanceProgram.VmVersion,
+                       Code:      e.AssetDefinition.IssuanceProgram.Code,
+               }
+
+               if !common.IsOpenFederationIssueAsset(e.RawDefinitionByte) {
+                       prog.Code = config.FederationWScript(config.CommonConfig, vs.block.Height)
                }
 
                if _, err := vm.Verify(NewTxVMContext(vs, e, prog, e.WitnessArguments), consensus.ActiveNetParams.DefaultGasCredit); err != nil {
@@ -579,6 +587,16 @@ func checkTimeRange(tx *bc.Tx, block *bc.Block) error {
        return nil
 }
 
+func applySoftFork001(vs *validationState, err error) {
+       if err == nil || vs.block.Height < consensus.ActiveNetParams.SoftForkPoint[consensus.SoftFork001] {
+               return
+       }
+
+       if rootErr := errors.Root(err); rootErr == ErrVotePubKey || rootErr == ErrVoteOutputAmount || rootErr == ErrVoteOutputAseet {
+               vs.gasStatus.GasValid = false
+       }
+}
+
 // ValidateTx validates a transaction.
 func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
        gasStatus := &GasState{GasValid: false}
@@ -602,7 +620,10 @@ func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
                gasStatus: gasStatus,
                cache:     make(map[bc.Hash]error),
        }
-       return vs.gasStatus, checkValid(vs, tx.TxHeader)
+
+       err := checkValid(vs, tx.TxHeader)
+       applySoftFork001(vs, err)
+       return vs.gasStatus, err
 }
 
 type validateTxWork struct {
@@ -644,6 +665,7 @@ func validateTxWorker(workCh chan *validateTxWork, resultCh chan *ValidateTxResu
 // ValidateTxs validates txs in async mode
 func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult {
        txSize := len(txs)
+       validateWorkerNum := runtime.NumCPU()
        //init the goroutine validate worker
        var wg sync.WaitGroup
        workCh := make(chan *validateTxWork, txSize)