state_object.go 7.5 KB

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