state_object.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/ethereum/go-ethereum/ethutil"
  8. "github.com/ethereum/go-ethereum/rlp"
  9. "github.com/ethereum/go-ethereum/trie"
  10. )
  11. type Code []byte
  12. func (self Code) String() string {
  13. return string(self) //strings.Join(Disassemble(self), " ")
  14. }
  15. type Storage map[string]*ethutil.Value
  16. func (self Storage) Copy() Storage {
  17. cpy := make(Storage)
  18. for key, value := range self {
  19. // XXX Do we need a 'value' copy or is this sufficient?
  20. cpy[key] = value
  21. }
  22. return cpy
  23. }
  24. type StateObject struct {
  25. db ethutil.Database
  26. // Address of the object
  27. address []byte
  28. // Shared attributes
  29. balance *big.Int
  30. codeHash []byte
  31. Nonce uint64
  32. // Contract related attributes
  33. State *StateDB
  34. Code Code
  35. InitCode Code
  36. storage Storage
  37. // Total gas pool is the total amount of gas currently
  38. // left if this object is the coinbase. Gas is directly
  39. // purchased of the coinbase.
  40. gasPool *big.Int
  41. // Mark for deletion
  42. // When an object is marked for deletion it will be delete from the trie
  43. // during the "update" phase of the state transition
  44. remove bool
  45. }
  46. func (self *StateObject) Reset() {
  47. self.storage = make(Storage)
  48. self.State.Reset()
  49. }
  50. func NewStateObject(addr []byte, db ethutil.Database) *StateObject {
  51. // This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
  52. address := ethutil.Address(addr)
  53. object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int)}
  54. object.State = New(nil, db) //New(trie.New(ethutil.Config.Db, ""))
  55. object.storage = make(Storage)
  56. object.gasPool = new(big.Int)
  57. return object
  58. }
  59. func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateObject {
  60. // TODO clean me up
  61. var extobject struct {
  62. Nonce uint64
  63. Balance *big.Int
  64. Root []byte
  65. CodeHash []byte
  66. }
  67. err := rlp.Decode(bytes.NewReader(data), &extobject)
  68. if err != nil {
  69. fmt.Println(err)
  70. return nil
  71. }
  72. object := &StateObject{address: address, db: db}
  73. //object.RlpDecode(data)
  74. object.Nonce = extobject.Nonce
  75. object.balance = extobject.Balance
  76. object.codeHash = extobject.CodeHash
  77. object.State = New(extobject.Root, db)
  78. object.storage = make(map[string]*ethutil.Value)
  79. object.gasPool = new(big.Int)
  80. object.Code, _ = db.Get(extobject.CodeHash)
  81. return object
  82. }
  83. func (self *StateObject) MarkForDeletion() {
  84. self.remove = true
  85. statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance)
  86. }
  87. func (c *StateObject) getAddr(addr []byte) *ethutil.Value {
  88. return ethutil.NewValueFromBytes([]byte(c.State.trie.Get(addr)))
  89. }
  90. func (c *StateObject) setAddr(addr []byte, value interface{}) {
  91. c.State.trie.Update(addr, ethutil.Encode(value))
  92. }
  93. func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value {
  94. return self.GetState(key.Bytes())
  95. }
  96. func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) {
  97. self.SetState(key.Bytes(), value)
  98. }
  99. func (self *StateObject) Storage() map[string]*ethutil.Value {
  100. return self.storage
  101. }
  102. func (self *StateObject) GetState(k []byte) *ethutil.Value {
  103. key := ethutil.LeftPadBytes(k, 32)
  104. value := self.storage[string(key)]
  105. if value == nil {
  106. value = self.getAddr(key)
  107. if !value.IsNil() {
  108. self.storage[string(key)] = value
  109. }
  110. }
  111. return value
  112. }
  113. func (self *StateObject) SetState(k []byte, value *ethutil.Value) {
  114. key := ethutil.LeftPadBytes(k, 32)
  115. self.storage[string(key)] = value.Copy()
  116. }
  117. func (self *StateObject) Sync() {
  118. for key, value := range self.storage {
  119. if value.Len() == 0 {
  120. self.State.trie.Delete([]byte(key))
  121. continue
  122. }
  123. self.setAddr([]byte(key), value)
  124. }
  125. self.storage = make(Storage)
  126. }
  127. func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
  128. if int64(len(c.Code)-1) < pc.Int64() {
  129. return ethutil.NewValue(0)
  130. }
  131. return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]})
  132. }
  133. func (c *StateObject) AddBalance(amount *big.Int) {
  134. c.SetBalance(new(big.Int).Add(c.balance, amount))
  135. statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount)
  136. }
  137. func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) }
  138. func (c *StateObject) SubBalance(amount *big.Int) {
  139. c.SetBalance(new(big.Int).Sub(c.balance, amount))
  140. statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.balance, amount)
  141. }
  142. func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) }
  143. func (c *StateObject) SetBalance(amount *big.Int) {
  144. c.balance = amount
  145. }
  146. func (self *StateObject) Balance() *big.Int { return self.balance }
  147. //
  148. // Gas setters and getters
  149. //
  150. // Return the gas back to the origin. Used by the Virtual machine or Closures
  151. func (c *StateObject) ReturnGas(gas, price *big.Int) {}
  152. func (c *StateObject) ConvertGas(gas, price *big.Int) error {
  153. total := new(big.Int).Mul(gas, price)
  154. if total.Cmp(c.balance) > 0 {
  155. return fmt.Errorf("insufficient amount: %v, %v", c.balance, total)
  156. }
  157. c.SubAmount(total)
  158. return nil
  159. }
  160. func (self *StateObject) SetGasPool(gasLimit *big.Int) {
  161. self.gasPool = new(big.Int).Set(gasLimit)
  162. statelogger.Debugf("%x: gas (+ %v)", self.Address(), self.gasPool)
  163. }
  164. func (self *StateObject) BuyGas(gas, price *big.Int) error {
  165. if self.gasPool.Cmp(gas) < 0 {
  166. return GasLimitError(self.gasPool, gas)
  167. }
  168. rGas := new(big.Int).Set(gas)
  169. rGas.Mul(rGas, price)
  170. self.AddAmount(rGas)
  171. return nil
  172. }
  173. func (self *StateObject) RefundGas(gas, price *big.Int) {
  174. self.gasPool.Add(self.gasPool, gas)
  175. rGas := new(big.Int).Set(gas)
  176. rGas.Mul(rGas, price)
  177. self.balance.Sub(self.balance, rGas)
  178. }
  179. func (self *StateObject) Copy() *StateObject {
  180. stateObject := NewStateObject(self.Address(), self.db)
  181. stateObject.balance.Set(self.balance)
  182. stateObject.codeHash = ethutil.CopyBytes(self.codeHash)
  183. stateObject.Nonce = self.Nonce
  184. if self.State != nil {
  185. stateObject.State = self.State.Copy()
  186. }
  187. stateObject.Code = ethutil.CopyBytes(self.Code)
  188. stateObject.InitCode = ethutil.CopyBytes(self.InitCode)
  189. stateObject.storage = self.storage.Copy()
  190. stateObject.gasPool.Set(self.gasPool)
  191. stateObject.remove = self.remove
  192. return stateObject
  193. }
  194. func (self *StateObject) Set(stateObject *StateObject) {
  195. *self = *stateObject
  196. }
  197. //
  198. // Attribute accessors
  199. //
  200. func (c *StateObject) N() *big.Int {
  201. return big.NewInt(int64(c.Nonce))
  202. }
  203. // Returns the address of the contract/account
  204. func (c *StateObject) Address() []byte {
  205. return c.address
  206. }
  207. // Returns the initialization Code
  208. func (c *StateObject) Init() Code {
  209. return c.InitCode
  210. }
  211. func (self *StateObject) Trie() *trie.Trie {
  212. return self.State.trie
  213. }
  214. func (self *StateObject) Root() []byte {
  215. return self.Trie().Root()
  216. }
  217. func (self *StateObject) SetCode(code []byte) {
  218. self.Code = code
  219. }
  220. //
  221. // Encoding
  222. //
  223. // State object encoding methods
  224. func (c *StateObject) RlpEncode() []byte {
  225. return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.Root(), c.CodeHash()})
  226. }
  227. func (c *StateObject) CodeHash() ethutil.Bytes {
  228. return crypto.Sha3(c.Code)
  229. }
  230. func (c *StateObject) RlpDecode(data []byte) {
  231. decoder := ethutil.NewValueFromBytes(data)
  232. c.Nonce = decoder.Get(0).Uint()
  233. c.balance = decoder.Get(1).BigInt()
  234. c.State = New(decoder.Get(2).Bytes(), c.db) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface()))
  235. c.storage = make(map[string]*ethutil.Value)
  236. c.gasPool = new(big.Int)
  237. c.codeHash = decoder.Get(3).Bytes()
  238. c.Code, _ = c.db.Get(c.codeHash)
  239. }
  240. // Storage change object. Used by the manifest for notifying changes to
  241. // the sub channels.
  242. type StorageState struct {
  243. StateAddress []byte
  244. Address []byte
  245. Value *big.Int
  246. }