statedb.go 6.6 KB

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