state_transition.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/core/types"
  6. "github.com/ethereum/go-ethereum/state"
  7. "github.com/ethereum/go-ethereum/vm"
  8. )
  9. /*
  10. * The State transitioning model
  11. *
  12. * A state transition is a change made when a transaction is applied to the current world state
  13. * The state transitioning model does all all the necessary work to work out a valid new state root.
  14. * 1) Nonce handling
  15. * 2) Pre pay / buy gas of the coinbase (miner)
  16. * 3) Create a new state object if the recipient is \0*32
  17. * 4) Value transfer
  18. * == If contract creation ==
  19. * 4a) Attempt to run transaction data
  20. * 4b) If valid, use result as code for the new state object
  21. * == end ==
  22. * 5) Run Script section
  23. * 6) Derive new state root
  24. */
  25. type StateTransition struct {
  26. coinbase, receiver []byte
  27. tx *types.Transaction
  28. gas, gasPrice *big.Int
  29. value *big.Int
  30. data []byte
  31. state *state.State
  32. block *types.Block
  33. cb, rec, sen *state.StateObject
  34. }
  35. func NewStateTransition(coinbase *state.StateObject, tx *types.Transaction, state *state.State, block *types.Block) *StateTransition {
  36. return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil}
  37. }
  38. func (self *StateTransition) Coinbase() *state.StateObject {
  39. if self.cb != nil {
  40. return self.cb
  41. }
  42. self.cb = self.state.GetOrNewStateObject(self.coinbase)
  43. return self.cb
  44. }
  45. func (self *StateTransition) Sender() *state.StateObject {
  46. if self.sen != nil {
  47. return self.sen
  48. }
  49. self.sen = self.state.GetOrNewStateObject(self.tx.Sender())
  50. return self.sen
  51. }
  52. func (self *StateTransition) Receiver() *state.StateObject {
  53. if self.tx != nil && self.tx.CreatesContract() {
  54. return nil
  55. }
  56. if self.rec != nil {
  57. return self.rec
  58. }
  59. self.rec = self.state.GetOrNewStateObject(self.tx.Recipient)
  60. return self.rec
  61. }
  62. func (self *StateTransition) UseGas(amount *big.Int) error {
  63. if self.gas.Cmp(amount) < 0 {
  64. return OutOfGasError()
  65. }
  66. self.gas.Sub(self.gas, amount)
  67. return nil
  68. }
  69. func (self *StateTransition) AddGas(amount *big.Int) {
  70. self.gas.Add(self.gas, amount)
  71. }
  72. func (self *StateTransition) BuyGas() error {
  73. var err error
  74. sender := self.Sender()
  75. if sender.Balance().Cmp(self.tx.GasValue()) < 0 {
  76. return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance())
  77. }
  78. coinbase := self.Coinbase()
  79. err = coinbase.BuyGas(self.tx.Gas, self.tx.GasPrice)
  80. if err != nil {
  81. return err
  82. }
  83. self.AddGas(self.tx.Gas)
  84. sender.SubAmount(self.tx.GasValue())
  85. return nil
  86. }
  87. func (self *StateTransition) RefundGas() {
  88. coinbase, sender := self.Coinbase(), self.Sender()
  89. coinbase.RefundGas(self.gas, self.tx.GasPrice)
  90. // Return remaining gas
  91. remaining := new(big.Int).Mul(self.gas, self.tx.GasPrice)
  92. sender.AddAmount(remaining)
  93. }
  94. func (self *StateTransition) preCheck() (err error) {
  95. var (
  96. tx = self.tx
  97. sender = self.Sender()
  98. )
  99. // Make sure this transaction's nonce is correct
  100. if sender.Nonce != tx.Nonce {
  101. return NonceError(tx.Nonce, sender.Nonce)
  102. }
  103. // Pre-pay gas / Buy gas of the coinbase account
  104. if err = self.BuyGas(); err != nil {
  105. return err
  106. }
  107. return nil
  108. }
  109. func (self *StateTransition) TransitionState() (err error) {
  110. statelogger.Debugf("(~) %x\n", self.tx.Hash())
  111. // XXX Transactions after this point are considered valid.
  112. if err = self.preCheck(); err != nil {
  113. return
  114. }
  115. var (
  116. tx = self.tx
  117. sender = self.Sender()
  118. receiver *state.StateObject
  119. )
  120. defer self.RefundGas()
  121. // Increment the nonce for the next transaction
  122. sender.Nonce += 1
  123. // Transaction gas
  124. if err = self.UseGas(vm.GasTx); err != nil {
  125. return
  126. }
  127. // Pay data gas
  128. var dgas int64
  129. for _, byt := range self.data {
  130. if byt != 0 {
  131. dgas += vm.GasData.Int64()
  132. } else {
  133. dgas += 1 // This is 1/5. If GasData changes this fails
  134. }
  135. }
  136. if err = self.UseGas(big.NewInt(dgas)); err != nil {
  137. return
  138. }
  139. var ret []byte
  140. vmenv := NewEnv(self.state, self.tx, self.block)
  141. var ref vm.ClosureRef
  142. if tx.CreatesContract() {
  143. self.rec = MakeContract(tx, self.state)
  144. ret, err, ref = vmenv.Create(sender, receiver.Address(), self.tx.Data, self.gas, self.gasPrice, self.value)
  145. ref.SetCode(ret)
  146. } else {
  147. ret, err = vmenv.Call(self.Sender(), self.Receiver().Address(), self.tx.Data, self.gas, self.gasPrice, self.value)
  148. }
  149. if err != nil {
  150. statelogger.Debugln(err)
  151. }
  152. return
  153. }
  154. // Converts an transaction in to a state object
  155. func MakeContract(tx *types.Transaction, state *state.State) *state.StateObject {
  156. addr := tx.CreationAddress(state)
  157. contract := state.GetOrNewStateObject(addr)
  158. contract.InitCode = tx.Data
  159. return contract
  160. }