9 "github.com/bytom/bytom/consensus"
10 "github.com/bytom/bytom/consensus/segwit"
11 "github.com/bytom/bytom/errors"
12 "github.com/bytom/bytom/math/checked"
13 "github.com/bytom/bytom/protocol/bc"
14 "github.com/bytom/bytom/protocol/vm"
19 // validate transaction error
21 ErrTxVersion = errors.New("invalid transaction version")
22 ErrWrongTransactionSize = errors.New("invalid transaction size")
23 ErrBadTimeRange = errors.New("invalid transaction time range")
24 ErrEmptyInputIDs = errors.New("got the empty InputIDs")
25 ErrNotStandardTx = errors.New("not standard transaction")
26 ErrWrongCoinbaseTransaction = errors.New("wrong coinbase transaction")
27 ErrWrongCoinbaseAsset = errors.New("wrong coinbase assetID")
28 ErrCoinbaseArbitraryOversize = errors.New("coinbase arbitrary size is larger than limit")
29 ErrEmptyResults = errors.New("transaction has no results")
30 ErrMismatchedAssetID = errors.New("mismatched assetID")
31 ErrMismatchedPosition = errors.New("mismatched value source/dest position")
32 ErrMismatchedReference = errors.New("mismatched reference")
33 ErrMismatchedValue = errors.New("mismatched value")
34 ErrMissingField = errors.New("missing required field")
35 ErrNoSource = errors.New("no source for value")
36 ErrOverflow = errors.New("arithmetic overflow/underflow")
37 ErrPosition = errors.New("invalid source or destination position")
38 ErrUnbalanced = errors.New("unbalanced asset amount between input and output")
39 ErrOverGasCredit = errors.New("all gas credit has been spend")
40 ErrGasCalculate = errors.New("gas usage calculate got a math error")
43 // GasState record the gas usage status
44 type GasState struct {
52 func (g *GasState) setGas(BTMValue int64, txSize int64) error {
54 return errors.Wrap(ErrGasCalculate, "input BTM is negative")
57 g.BTMValue = uint64(BTMValue)
60 if g.GasLeft, ok = checked.DivInt64(BTMValue, consensus.VMGasRate); !ok {
61 return errors.Wrap(ErrGasCalculate, "setGas calc gas amount")
64 if g.GasLeft > consensus.MaxGasAmount {
65 g.GasLeft = consensus.MaxGasAmount
68 if g.StorageGas, ok = checked.MulInt64(txSize, consensus.StorageGasRate); !ok {
69 return errors.Wrap(ErrGasCalculate, "setGas calc tx storage gas")
74 func (g *GasState) setGasValid() error {
76 if g.GasLeft, ok = checked.SubInt64(g.GasLeft, g.StorageGas); !ok || g.GasLeft < 0 {
77 return errors.Wrap(ErrGasCalculate, "setGasValid calc gasLeft")
80 if g.GasUsed, ok = checked.AddInt64(g.GasUsed, g.StorageGas); !ok {
81 return errors.Wrap(ErrGasCalculate, "setGasValid calc gasUsed")
88 func (g *GasState) updateUsage(gasLeft int64) error {
90 return errors.Wrap(ErrGasCalculate, "updateUsage input negative gas")
93 if gasUsed, ok := checked.SubInt64(g.GasLeft, gasLeft); ok {
97 return errors.Wrap(ErrGasCalculate, "updateUsage calc gas diff")
100 if !g.GasValid && (g.GasUsed > consensus.DefaultGasCredit || g.StorageGas > g.GasLeft) {
101 return ErrOverGasCredit
106 // validationState contains the context that must propagate through
107 // the transaction graph when validating entries.
108 type validationState struct {
112 entryID bc.Hash // The ID of the nearest enclosing entry
113 sourcePos uint64 // The source position, for validate ValueSources
114 destPos uint64 // The destination position, for validate ValueDestinations
115 cache map[bc.Hash]error // Memoized per-entry validation results
118 func checkValid(vs *validationState, e bc.Entry) (err error) {
120 entryID := bc.EntryID(e)
121 if err, ok = vs.cache[entryID]; ok {
126 vs.cache[entryID] = err
129 switch e := e.(type) {
131 for i, resID := range e.ResultIds {
132 resultEntry := vs.tx.Entries[*resID]
135 if err = checkValid(&vs2, resultEntry); err != nil {
136 return errors.Wrapf(err, "checking result %d", i)
140 if e.Version == 1 && len(e.ResultIds) == 0 {
141 return ErrEmptyResults
145 parity := make(map[bc.AssetID]int64)
146 for i, src := range e.Sources {
147 if src.Value.Amount > math.MaxInt64 {
148 return errors.WithDetailf(ErrOverflow, "amount %d exceeds maximum value 2^63", src.Value.Amount)
150 sum, ok := checked.AddInt64(parity[*src.Value.AssetId], int64(src.Value.Amount))
152 return errors.WithDetailf(ErrOverflow, "adding %d units of asset %x from mux source %d to total %d overflows int64", src.Value.Amount, src.Value.AssetId.Bytes(), i, parity[*src.Value.AssetId])
154 parity[*src.Value.AssetId] = sum
157 for i, dest := range e.WitnessDestinations {
158 sum, ok := parity[*dest.Value.AssetId]
160 return errors.WithDetailf(ErrNoSource, "mux destination %d, asset %x, has no corresponding source", i, dest.Value.AssetId.Bytes())
162 if dest.Value.Amount > math.MaxInt64 {
163 return errors.WithDetailf(ErrOverflow, "amount %d exceeds maximum value 2^63", dest.Value.Amount)
165 diff, ok := checked.SubInt64(sum, int64(dest.Value.Amount))
167 return errors.WithDetailf(ErrOverflow, "subtracting %d units of asset %x from mux destination %d from total %d underflows int64", dest.Value.Amount, dest.Value.AssetId.Bytes(), i, sum)
169 parity[*dest.Value.AssetId] = diff
172 for assetID, amount := range parity {
173 if assetID == *consensus.BTMAssetID {
174 if err = vs.gasStatus.setGas(amount, int64(vs.tx.SerializedSize)); err != nil {
177 } else if amount != 0 {
178 return errors.WithDetailf(ErrUnbalanced, "asset %x sources - destinations = %d (should be 0)", assetID.Bytes(), amount)
182 for _, BTMInputID := range vs.tx.GasInputIDs {
183 e, ok := vs.tx.Entries[BTMInputID]
185 return errors.Wrapf(bc.ErrMissingEntry, "entry for bytom input %x not found", BTMInputID)
189 vs2.entryID = BTMInputID
190 if err := checkValid(&vs2, e); err != nil {
191 return errors.Wrap(err, "checking gas input")
195 for i, dest := range e.WitnessDestinations {
197 vs2.destPos = uint64(i)
198 if err = checkValidDest(&vs2, dest); err != nil {
199 return errors.Wrapf(err, "checking mux destination %d", i)
203 if err := vs.gasStatus.setGasValid(); err != nil {
207 for i, src := range e.Sources {
209 vs2.sourcePos = uint64(i)
210 if err = checkValidSrc(&vs2, src); err != nil {
211 return errors.Wrapf(err, "checking mux source %d", i)
218 if err = checkValidSrc(&vs2, e.Source); err != nil {
219 return errors.Wrap(err, "checking output source")
225 if err = checkValidSrc(&vs2, e.Source); err != nil {
226 return errors.Wrap(err, "checking retirement source")
230 computedAssetID := e.WitnessAssetDefinition.ComputeAssetID()
231 if computedAssetID != *e.Value.AssetId {
232 return errors.WithDetailf(ErrMismatchedAssetID, "asset ID is %x, issuance wants %x", computedAssetID.Bytes(), e.Value.AssetId.Bytes())
235 gasLeft, err := vm.Verify(NewTxVMContext(vs, e, e.WitnessAssetDefinition.IssuanceProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
237 return errors.Wrap(err, "checking issuance program")
239 if err = vs.gasStatus.updateUsage(gasLeft); err != nil {
245 if err = checkValidDest(&destVS, e.WitnessDestination); err != nil {
246 return errors.Wrap(err, "checking issuance destination")
250 if e.SpentOutputId == nil {
251 return errors.Wrap(ErrMissingField, "spend without spent output ID")
253 spentOutput, err := vs.tx.Output(*e.SpentOutputId)
255 return errors.Wrap(err, "getting spend prevout")
258 gasLeft, err := vm.Verify(NewTxVMContext(vs, e, spentOutput.ControlProgram, e.WitnessArguments), vs.gasStatus.GasLeft)
260 return errors.Wrap(err, "checking control program")
262 if err = vs.gasStatus.updateUsage(gasLeft); err != nil {
266 eq, err := spentOutput.Source.Value.Equal(e.WitnessDestination.Value)
271 return errors.WithDetailf(
273 "previous output is for %d unit(s) of %x, spend wants %d unit(s) of %x",
274 spentOutput.Source.Value.Amount,
275 spentOutput.Source.Value.AssetId.Bytes(),
276 e.WitnessDestination.Value.Amount,
277 e.WitnessDestination.Value.AssetId.Bytes(),
283 if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
284 return errors.Wrap(err, "checking spend destination")
288 if vs.block == nil || len(vs.block.Transactions) == 0 || vs.block.Transactions[0] != vs.tx {
289 return ErrWrongCoinbaseTransaction
292 if *e.WitnessDestination.Value.AssetId != *consensus.BTMAssetID {
293 return ErrWrongCoinbaseAsset
296 if e.Arbitrary != nil && len(e.Arbitrary) > consensus.CoinbaseArbitrarySizeLimit {
297 return ErrCoinbaseArbitraryOversize
302 if err = checkValidDest(&vs2, e.WitnessDestination); err != nil {
303 return errors.Wrap(err, "checking coinbase destination")
305 vs.gasStatus.StorageGas = 0
308 return fmt.Errorf("entry has unexpected type %T", e)
314 func checkValidSrc(vstate *validationState, vs *bc.ValueSource) error {
316 return errors.Wrap(ErrMissingField, "empty value source")
319 return errors.Wrap(ErrMissingField, "missing ref on value source")
321 if vs.Value == nil || vs.Value.AssetId == nil {
322 return errors.Wrap(ErrMissingField, "missing value on value source")
325 e, ok := vstate.tx.Entries[*vs.Ref]
327 return errors.Wrapf(bc.ErrMissingEntry, "entry for value source %x not found", vs.Ref.Bytes())
331 vstate2.entryID = *vs.Ref
332 if err := checkValid(&vstate2, e); err != nil {
333 return errors.Wrap(err, "checking value source")
336 var dest *bc.ValueDestination
337 switch ref := e.(type) {
339 if vs.Position != 0 {
340 return errors.Wrapf(ErrPosition, "invalid position %d for coinbase source", vs.Position)
342 dest = ref.WitnessDestination
345 if vs.Position != 0 {
346 return errors.Wrapf(ErrPosition, "invalid position %d for issuance source", vs.Position)
348 dest = ref.WitnessDestination
351 if vs.Position != 0 {
352 return errors.Wrapf(ErrPosition, "invalid position %d for spend source", vs.Position)
354 dest = ref.WitnessDestination
357 if vs.Position >= uint64(len(ref.WitnessDestinations)) {
358 return errors.Wrapf(ErrPosition, "invalid position %d for %d-destination mux source", vs.Position, len(ref.WitnessDestinations))
360 dest = ref.WitnessDestinations[vs.Position]
363 return errors.Wrapf(bc.ErrEntryType, "value source is %T, should be coinbase, issuance, spend, or mux", e)
366 if dest.Ref == nil || *dest.Ref != vstate.entryID {
367 return errors.Wrapf(ErrMismatchedReference, "value source for %x has disagreeing destination %x", vstate.entryID.Bytes(), dest.Ref.Bytes())
370 if dest.Position != vstate.sourcePos {
371 return errors.Wrapf(ErrMismatchedPosition, "value source position %d disagrees with %d", dest.Position, vstate.sourcePos)
374 eq, err := dest.Value.Equal(vs.Value)
376 return errors.Sub(ErrMissingField, err)
379 return errors.Wrapf(ErrMismatchedValue, "source value %v disagrees with %v", dest.Value, vs.Value)
385 func checkValidDest(vs *validationState, vd *bc.ValueDestination) error {
387 return errors.Wrap(ErrMissingField, "empty value destination")
390 return errors.Wrap(ErrMissingField, "missing ref on value destination")
392 if vd.Value == nil || vd.Value.AssetId == nil {
393 return errors.Wrap(ErrMissingField, "missing value on value destination")
396 e, ok := vs.tx.Entries[*vd.Ref]
398 return errors.Wrapf(bc.ErrMissingEntry, "entry for value destination %x not found", vd.Ref.Bytes())
401 var src *bc.ValueSource
402 switch ref := e.(type) {
404 if vd.Position != 0 {
405 return errors.Wrapf(ErrPosition, "invalid position %d for output destination", vd.Position)
410 if vd.Position != 0 {
411 return errors.Wrapf(ErrPosition, "invalid position %d for retirement destination", vd.Position)
416 if vd.Position >= uint64(len(ref.Sources)) {
417 return errors.Wrapf(ErrPosition, "invalid position %d for %d-source mux destination", vd.Position, len(ref.Sources))
419 src = ref.Sources[vd.Position]
422 return errors.Wrapf(bc.ErrEntryType, "value destination is %T, should be output, retirement, or mux", e)
425 if src.Ref == nil || *src.Ref != vs.entryID {
426 return errors.Wrapf(ErrMismatchedReference, "value destination for %x has disagreeing source %x", vs.entryID.Bytes(), src.Ref.Bytes())
429 if src.Position != vs.destPos {
430 return errors.Wrapf(ErrMismatchedPosition, "value destination position %d disagrees with %d", src.Position, vs.destPos)
433 eq, err := src.Value.Equal(vd.Value)
435 return errors.Sub(ErrMissingField, err)
438 return errors.Wrapf(ErrMismatchedValue, "destination value %v disagrees with %v", src.Value, vd.Value)
444 func checkStandardTx(tx *bc.Tx, blockHeight uint64) error {
445 for _, id := range tx.InputIDs {
446 if blockHeight >= ruleAA && id.IsZero() {
447 return ErrEmptyInputIDs
451 for _, id := range tx.GasInputIDs {
452 spend, err := tx.Spend(id)
456 spentOutput, err := tx.Output(*spend.SpentOutputId)
461 if !segwit.IsP2WScript(spentOutput.ControlProgram.Code) {
462 return ErrNotStandardTx
466 for _, id := range tx.ResultIds {
467 e, ok := tx.Entries[*id]
469 return errors.Wrapf(bc.ErrMissingEntry, "id %x", id.Bytes())
472 output, ok := e.(*bc.Output)
473 if !ok || *output.Source.Value.AssetId != *consensus.BTMAssetID {
477 if !segwit.IsP2WScript(output.ControlProgram.Code) {
478 return ErrNotStandardTx
484 func checkTimeRange(tx *bc.Tx, block *bc.Block) error {
485 if tx.TimeRange == 0 {
489 if tx.TimeRange < block.Height {
490 return ErrBadTimeRange
495 // ValidateTx validates a transaction.
496 func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
497 gasStatus := &GasState{GasValid: false}
498 if block.Version == 1 && tx.Version != 1 {
499 return gasStatus, errors.WithDetailf(ErrTxVersion, "block version %d, transaction version %d", block.Version, tx.Version)
501 if tx.SerializedSize == 0 {
502 return gasStatus, ErrWrongTransactionSize
504 if err := checkTimeRange(tx, block); err != nil {
505 return gasStatus, err
507 if err := checkStandardTx(tx, block.Height); err != nil {
508 return gasStatus, err
511 vs := &validationState{
515 gasStatus: gasStatus,
516 cache: make(map[bc.Hash]error),
518 return vs.gasStatus, checkValid(vs, tx.TxHeader)
521 type validateTxWork struct {
527 // ValidateTxResult is the result of async tx validate
528 type ValidateTxResult struct {
534 // GetGasState return the gasStatus
535 func (r *ValidateTxResult) GetGasState() *GasState {
539 // GetError return the err
540 func (r *ValidateTxResult) GetError() error {
544 func validateTxWorker(workCh chan *validateTxWork, resultCh chan *ValidateTxResult, wg *sync.WaitGroup) {
545 for work := range workCh {
546 gasStatus, err := ValidateTx(work.tx, work.block)
547 resultCh <- &ValidateTxResult{i: work.i, gasStatus: gasStatus, err: err}
552 // ValidateTxs validates txs in async mode
553 func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult {
555 validateWorkerNum := runtime.NumCPU()
556 //init the goroutine validate worker
557 var wg sync.WaitGroup
558 workCh := make(chan *validateTxWork, txSize)
559 resultCh := make(chan *ValidateTxResult, txSize)
560 for i := 0; i <= validateWorkerNum && i < txSize; i++ {
562 go validateTxWorker(workCh, resultCh, &wg)
566 for i, tx := range txs {
567 workCh <- &validateTxWork{i: i, tx: tx, block: block}
571 //collect validate results
572 results := make([]*ValidateTxResult, txSize)
573 for i := 0; i < txSize; i++ {
575 results[result.i] = result