github.com/iotexproject/iotex-core@v1.14.1-rc1/action/actctx.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package action 7 8 import ( 9 "math/big" 10 ) 11 12 // AbstractAction is an abstract implementation of Action interface 13 type AbstractAction struct { 14 version uint32 15 chainID uint32 16 nonce uint64 17 gasLimit uint64 18 gasPrice *big.Int 19 } 20 21 // Version returns the version 22 func (act *AbstractAction) Version() uint32 { return act.version } 23 24 // ChainID returns the chainID 25 func (act *AbstractAction) ChainID() uint32 { return act.chainID } 26 27 // Nonce returns the nonce 28 func (act *AbstractAction) Nonce() uint64 { return act.nonce } 29 30 // SetNonce sets gaslimit 31 func (act *AbstractAction) SetNonce(val uint64) { 32 act.nonce = val 33 } 34 35 // GasLimit returns the gas limit 36 func (act *AbstractAction) GasLimit() uint64 { return act.gasLimit } 37 38 // SetGasLimit sets gaslimit 39 func (act *AbstractAction) SetGasLimit(val uint64) { 40 act.gasLimit = val 41 } 42 43 // GasPrice returns the gas price 44 func (act *AbstractAction) GasPrice() *big.Int { 45 p := &big.Int{} 46 if act.gasPrice == nil { 47 return p 48 } 49 return p.Set(act.gasPrice) 50 } 51 52 // SetGasPrice sets gaslimit 53 func (act *AbstractAction) SetGasPrice(val *big.Int) { 54 act.gasPrice = val 55 } 56 57 // BasicActionSize returns the basic size of action 58 func (act *AbstractAction) BasicActionSize() uint32 { 59 // VersionSizeInBytes + NonceSizeInBytes + GasSizeInBytes 60 size := 4 + 8 + 8 61 if act.gasPrice != nil && len(act.gasPrice.Bytes()) > 0 { 62 size += len(act.gasPrice.Bytes()) 63 } 64 65 return uint32(size) 66 } 67 68 // SetEnvelopeContext sets the struct according to input 69 func (act *AbstractAction) SetEnvelopeContext(elp Envelope) { 70 if act == nil { 71 return 72 } 73 act.version = elp.Version() 74 act.chainID = elp.ChainID() 75 act.nonce = elp.Nonce() 76 act.gasLimit = elp.GasLimit() 77 act.gasPrice = elp.GasPrice() 78 } 79 80 // SanityCheck validates the variables in the action 81 func (act *AbstractAction) SanityCheck() error { 82 // Reject execution of negative gas price 83 if act.GasPrice().Sign() < 0 { 84 return ErrNegativeValue 85 } 86 return nil 87 }