statedb.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package state
  2. import (
  3. "bytes"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/logger"
  7. "github.com/ethereum/go-ethereum/trie"
  8. )
  9. var statelogger = logger.NewLogger("STATE")
  10. // StateDBs within the ethereum protocol are used to store anything
  11. // within the merkle trie. StateDBs take care of caching and storing
  12. // nested states. It's the general query interface to retrieve:
  13. // * Contracts
  14. // * Accounts
  15. type StateDB struct {
  16. db common.Database
  17. trie *trie.SecureTrie
  18. stateObjects map[string]*StateObject
  19. refund map[string]*big.Int
  20. logs Logs
  21. }
  22. // Create a new state from a given trie
  23. func New(root common.Hash, db common.Database) *StateDB {
  24. trie := trie.NewSecure(root[:], db)
  25. return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int)}
  26. }
  27. func (self *StateDB) EmptyLogs() {
  28. self.logs = nil
  29. }
  30. func (self *StateDB) AddLog(log Log) {
  31. self.logs = append(self.logs, log)
  32. }
  33. func (self *StateDB) Logs() Logs {
  34. return self.logs
  35. }
  36. func (self *StateDB) Refund(address common.Address, gas *big.Int) {
  37. addr := address.Str()
  38. if self.refund[addr] == nil {
  39. self.refund[addr] = new(big.Int)
  40. }
  41. self.refund[addr].Add(self.refund[addr], gas)
  42. }
  43. // Retrieve the balance from the given address or 0 if object not found
  44. func (self *StateDB) GetBalance(addr common.Address) *big.Int {
  45. stateObject := self.GetStateObject(addr)
  46. if stateObject != nil {
  47. return stateObject.balance
  48. }
  49. return common.Big0
  50. }
  51. func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  52. stateObject := self.GetStateObject(addr)
  53. if stateObject != nil {
  54. stateObject.AddBalance(amount)
  55. }
  56. }
  57. func (self *StateDB) GetNonce(addr common.Address) uint64 {
  58. stateObject := self.GetStateObject(addr)
  59. if stateObject != nil {
  60. return stateObject.nonce
  61. }
  62. return 0
  63. }
  64. func (self *StateDB) GetCode(addr common.Address) []byte {
  65. stateObject := self.GetStateObject(addr)
  66. if stateObject != nil {
  67. return stateObject.code
  68. }
  69. return nil
  70. }
  71. func (self *StateDB) GetState(a common.Address, b common.Hash) []byte {
  72. stateObject := self.GetStateObject(a)
  73. if stateObject != nil {
  74. return stateObject.GetState(b).Bytes()
  75. }
  76. return nil
  77. }
  78. func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
  79. stateObject := self.GetStateObject(addr)
  80. if stateObject != nil {
  81. stateObject.SetNonce(nonce)
  82. }
  83. }
  84. func (self *StateDB) SetCode(addr common.Address, code []byte) {
  85. stateObject := self.GetStateObject(addr)
  86. if stateObject != nil {
  87. stateObject.SetCode(code)
  88. }
  89. }
  90. func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
  91. stateObject := self.GetStateObject(addr)
  92. if stateObject != nil {
  93. stateObject.SetState(key, common.NewValue(value))
  94. }
  95. }
  96. func (self *StateDB) Delete(addr common.Address) bool {
  97. stateObject := self.GetStateObject(addr)
  98. if stateObject != nil {
  99. stateObject.MarkForDeletion()
  100. stateObject.balance = new(big.Int)
  101. return true
  102. }
  103. return false
  104. }
  105. func (self *StateDB) IsDeleted(addr common.Address) bool {
  106. stateObject := self.GetStateObject(addr)
  107. if stateObject != nil {
  108. return stateObject.remove
  109. }
  110. return false
  111. }
  112. //
  113. // Setting, updating & deleting state object methods
  114. //
  115. // Update the given state object and apply it to state trie
  116. func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
  117. //addr := stateObject.Address()
  118. if len(stateObject.CodeHash()) > 0 {
  119. self.db.Put(stateObject.CodeHash(), stateObject.code)
  120. }
  121. addr := stateObject.Address()
  122. self.trie.Update(addr[:], stateObject.RlpEncode())
  123. }
  124. // Delete the given state object and delete it from the state trie
  125. func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
  126. addr := stateObject.Address()
  127. self.trie.Delete(addr[:])
  128. delete(self.stateObjects, addr.Str())
  129. }
  130. // Retrieve a state object given my the address. Nil if not found
  131. func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
  132. //addr = common.Address(addr)
  133. stateObject := self.stateObjects[addr.Str()]
  134. if stateObject != nil {
  135. return stateObject
  136. }
  137. data := self.trie.Get(addr[:])
  138. if len(data) == 0 {
  139. return nil
  140. }
  141. stateObject = NewStateObjectFromBytes(addr, []byte(data), self.db)
  142. self.SetStateObject(stateObject)
  143. return stateObject
  144. }
  145. func (self *StateDB) SetStateObject(object *StateObject) {
  146. self.stateObjects[object.Address().Str()] = object
  147. }
  148. // Retrieve a state object or create a new state object if nil
  149. func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
  150. stateObject := self.GetStateObject(addr)
  151. if stateObject == nil {
  152. stateObject = self.NewStateObject(addr)
  153. }
  154. return stateObject
  155. }
  156. // Create a state object whether it exist in the trie or not
  157. func (self *StateDB) NewStateObject(addr common.Address) *StateObject {
  158. //addr = common.Address(addr)
  159. statelogger.Debugf("(+) %x\n", addr)
  160. stateObject := NewStateObject(addr, self.db)
  161. self.stateObjects[addr.Str()] = stateObject
  162. return stateObject
  163. }
  164. // Deprecated
  165. func (self *StateDB) GetAccount(addr common.Address) *StateObject {
  166. return self.GetOrNewStateObject(addr)
  167. }
  168. //
  169. // Setting, copying of the state methods
  170. //
  171. func (s *StateDB) Cmp(other *StateDB) bool {
  172. return bytes.Equal(s.trie.Root(), other.trie.Root())
  173. }
  174. func (self *StateDB) Copy() *StateDB {
  175. state := New(common.Hash{}, self.db)
  176. state.trie = self.trie.Copy()
  177. for k, stateObject := range self.stateObjects {
  178. state.stateObjects[k] = stateObject.Copy()
  179. }
  180. for addr, refund := range self.refund {
  181. state.refund[addr] = new(big.Int).Set(refund)
  182. }
  183. logs := make(Logs, len(self.logs))
  184. copy(logs, self.logs)
  185. state.logs = logs
  186. return state
  187. }
  188. func (self *StateDB) Set(state *StateDB) {
  189. self.trie = state.trie
  190. self.stateObjects = state.stateObjects
  191. self.refund = state.refund
  192. self.logs = state.logs
  193. }
  194. func (s *StateDB) Root() common.Hash {
  195. return common.BytesToHash(s.trie.Root())
  196. }
  197. func (s *StateDB) Trie() *trie.SecureTrie {
  198. return s.trie
  199. }
  200. // Resets the trie and all siblings
  201. func (s *StateDB) Reset() {
  202. s.trie.Reset()
  203. // Reset all nested states
  204. for _, stateObject := range s.stateObjects {
  205. if stateObject.State == nil {
  206. continue
  207. }
  208. stateObject.Reset()
  209. }
  210. s.Empty()
  211. }
  212. // Syncs the trie and all siblings
  213. func (s *StateDB) Sync() {
  214. // Sync all nested states
  215. for _, stateObject := range s.stateObjects {
  216. if stateObject.State == nil {
  217. continue
  218. }
  219. stateObject.State.Sync()
  220. }
  221. s.trie.Commit()
  222. s.Empty()
  223. }
  224. func (self *StateDB) Empty() {
  225. self.stateObjects = make(map[string]*StateObject)
  226. self.refund = make(map[string]*big.Int)
  227. }
  228. func (self *StateDB) Refunds() map[string]*big.Int {
  229. return self.refund
  230. }
  231. func (self *StateDB) Update(gasUsed *big.Int) {
  232. self.refund = make(map[string]*big.Int)
  233. for _, stateObject := range self.stateObjects {
  234. if stateObject.dirty {
  235. if stateObject.remove {
  236. self.DeleteStateObject(stateObject)
  237. } else {
  238. stateObject.Sync()
  239. self.UpdateStateObject(stateObject)
  240. }
  241. stateObject.dirty = false
  242. }
  243. }
  244. }
  245. // Debug stuff
  246. func (self *StateDB) CreateOutputForDiff() {
  247. for _, stateObject := range self.stateObjects {
  248. stateObject.CreateOutputForDiff()
  249. }
  250. }