state_object.go 8.0 KB

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