statedb.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package state provides a caching layer atop the Ethereum state trie.
  17. package state
  18. import (
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/logger"
  22. "github.com/ethereum/go-ethereum/logger/glog"
  23. "github.com/ethereum/go-ethereum/trie"
  24. )
  25. // StateDBs within the ethereum protocol are used to store anything
  26. // within the merkle trie. StateDBs take care of caching and storing
  27. // nested states. It's the general query interface to retrieve:
  28. // * Contracts
  29. // * Accounts
  30. type StateDB struct {
  31. db common.Database
  32. trie *trie.SecureTrie
  33. root common.Hash
  34. stateObjects map[string]*StateObject
  35. refund *big.Int
  36. thash, bhash common.Hash
  37. txIndex int
  38. logs map[common.Hash]Logs
  39. logSize uint
  40. }
  41. // Create a new state from a given trie
  42. func New(root common.Hash, db common.Database) *StateDB {
  43. trie := trie.NewSecure(root[:], db)
  44. return &StateDB{root: root, db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]Logs)}
  45. }
  46. func (self *StateDB) PrintRoot() {
  47. self.trie.Trie.PrintRoot()
  48. }
  49. func (self *StateDB) StartRecord(thash, bhash common.Hash, ti int) {
  50. self.thash = thash
  51. self.bhash = bhash
  52. self.txIndex = ti
  53. }
  54. func (self *StateDB) AddLog(log *Log) {
  55. log.TxHash = self.thash
  56. log.BlockHash = self.bhash
  57. log.TxIndex = uint(self.txIndex)
  58. log.Index = self.logSize
  59. self.logs[self.thash] = append(self.logs[self.thash], log)
  60. self.logSize++
  61. }
  62. func (self *StateDB) GetLogs(hash common.Hash) Logs {
  63. return self.logs[hash]
  64. }
  65. func (self *StateDB) Logs() Logs {
  66. var logs Logs
  67. for _, lgs := range self.logs {
  68. logs = append(logs, lgs...)
  69. }
  70. return logs
  71. }
  72. func (self *StateDB) Refund(gas *big.Int) {
  73. self.refund.Add(self.refund, gas)
  74. }
  75. /*
  76. * GETTERS
  77. */
  78. func (self *StateDB) HasAccount(addr common.Address) bool {
  79. return self.GetStateObject(addr) != nil
  80. }
  81. // Retrieve the balance from the given address or 0 if object not found
  82. func (self *StateDB) GetBalance(addr common.Address) *big.Int {
  83. stateObject := self.GetStateObject(addr)
  84. if stateObject != nil {
  85. return stateObject.balance
  86. }
  87. return common.Big0
  88. }
  89. func (self *StateDB) GetNonce(addr common.Address) uint64 {
  90. stateObject := self.GetStateObject(addr)
  91. if stateObject != nil {
  92. return stateObject.nonce
  93. }
  94. return 0
  95. }
  96. func (self *StateDB) GetCode(addr common.Address) []byte {
  97. stateObject := self.GetStateObject(addr)
  98. if stateObject != nil {
  99. return stateObject.code
  100. }
  101. return nil
  102. }
  103. func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
  104. stateObject := self.GetStateObject(a)
  105. if stateObject != nil {
  106. return stateObject.GetState(b)
  107. }
  108. return common.Hash{}
  109. }
  110. func (self *StateDB) IsDeleted(addr common.Address) bool {
  111. stateObject := self.GetStateObject(addr)
  112. if stateObject != nil {
  113. return stateObject.remove
  114. }
  115. return false
  116. }
  117. /*
  118. * SETTERS
  119. */
  120. func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  121. stateObject := self.GetOrNewStateObject(addr)
  122. if stateObject != nil {
  123. stateObject.AddBalance(amount)
  124. }
  125. }
  126. func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
  127. stateObject := self.GetOrNewStateObject(addr)
  128. if stateObject != nil {
  129. stateObject.SetNonce(nonce)
  130. }
  131. }
  132. func (self *StateDB) SetCode(addr common.Address, code []byte) {
  133. stateObject := self.GetOrNewStateObject(addr)
  134. if stateObject != nil {
  135. stateObject.SetCode(code)
  136. }
  137. }
  138. func (self *StateDB) SetState(addr common.Address, key common.Hash, value common.Hash) {
  139. stateObject := self.GetOrNewStateObject(addr)
  140. if stateObject != nil {
  141. stateObject.SetState(key, value)
  142. }
  143. }
  144. func (self *StateDB) Delete(addr common.Address) bool {
  145. stateObject := self.GetStateObject(addr)
  146. if stateObject != nil {
  147. stateObject.MarkForDeletion()
  148. stateObject.balance = new(big.Int)
  149. return true
  150. }
  151. return false
  152. }
  153. //
  154. // Setting, updating & deleting state object methods
  155. //
  156. // Update the given state object and apply it to state trie
  157. func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
  158. //addr := stateObject.Address()
  159. if len(stateObject.CodeHash()) > 0 {
  160. self.db.Put(stateObject.CodeHash(), stateObject.code)
  161. }
  162. addr := stateObject.Address()
  163. self.trie.Update(addr[:], stateObject.RlpEncode())
  164. }
  165. // Delete the given state object and delete it from the state trie
  166. func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
  167. stateObject.deleted = true
  168. addr := stateObject.Address()
  169. self.trie.Delete(addr[:])
  170. }
  171. // Retrieve a state object given my the address. Nil if not found
  172. func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObject) {
  173. stateObject = self.stateObjects[addr.Str()]
  174. if stateObject != nil {
  175. if stateObject.deleted {
  176. stateObject = nil
  177. }
  178. return stateObject
  179. }
  180. data := self.trie.Get(addr[:])
  181. if len(data) == 0 {
  182. return nil
  183. }
  184. stateObject = NewStateObjectFromBytes(addr, []byte(data), self.db)
  185. self.SetStateObject(stateObject)
  186. return stateObject
  187. }
  188. func (self *StateDB) SetStateObject(object *StateObject) {
  189. self.stateObjects[object.Address().Str()] = object
  190. }
  191. // Retrieve a state object or create a new state object if nil
  192. func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
  193. stateObject := self.GetStateObject(addr)
  194. if stateObject == nil || stateObject.deleted {
  195. stateObject = self.CreateAccount(addr)
  196. }
  197. return stateObject
  198. }
  199. // NewStateObject create a state object whether it exist in the trie or not
  200. func (self *StateDB) newStateObject(addr common.Address) *StateObject {
  201. if glog.V(logger.Core) {
  202. glog.Infof("(+) %x\n", addr)
  203. }
  204. stateObject := NewStateObject(addr, self.db)
  205. self.stateObjects[addr.Str()] = stateObject
  206. return stateObject
  207. }
  208. // Creates creates a new state object and takes ownership. This is different from "NewStateObject"
  209. func (self *StateDB) CreateAccount(addr common.Address) *StateObject {
  210. // Get previous (if any)
  211. so := self.GetStateObject(addr)
  212. // Create a new one
  213. newSo := self.newStateObject(addr)
  214. // If it existed set the balance to the new account
  215. if so != nil {
  216. newSo.balance = so.balance
  217. }
  218. return newSo
  219. }
  220. //
  221. // Setting, copying of the state methods
  222. //
  223. func (self *StateDB) Copy() *StateDB {
  224. state := New(common.Hash{}, self.db)
  225. state.trie = self.trie
  226. for k, stateObject := range self.stateObjects {
  227. state.stateObjects[k] = stateObject.Copy()
  228. }
  229. state.refund.Set(self.refund)
  230. for hash, logs := range self.logs {
  231. state.logs[hash] = make(Logs, len(logs))
  232. copy(state.logs[hash], logs)
  233. }
  234. state.logSize = self.logSize
  235. return state
  236. }
  237. func (self *StateDB) Set(state *StateDB) {
  238. self.trie = state.trie
  239. self.stateObjects = state.stateObjects
  240. self.refund = state.refund
  241. self.logs = state.logs
  242. self.logSize = state.logSize
  243. }
  244. func (s *StateDB) Root() common.Hash {
  245. return common.BytesToHash(s.trie.Root())
  246. }
  247. // Syncs the trie and all siblings
  248. func (s *StateDB) Sync() {
  249. // Sync all nested states
  250. for _, stateObject := range s.stateObjects {
  251. stateObject.trie.Commit()
  252. }
  253. s.trie.Commit()
  254. s.Empty()
  255. }
  256. func (self *StateDB) Empty() {
  257. self.stateObjects = make(map[string]*StateObject)
  258. self.refund = new(big.Int)
  259. }
  260. func (self *StateDB) Refunds() *big.Int {
  261. return self.refund
  262. }
  263. // SyncIntermediate updates the intermediate state and all mid steps
  264. func (self *StateDB) SyncIntermediate() {
  265. self.refund = new(big.Int)
  266. for _, stateObject := range self.stateObjects {
  267. if stateObject.dirty {
  268. if stateObject.remove {
  269. self.DeleteStateObject(stateObject)
  270. } else {
  271. stateObject.Update()
  272. self.UpdateStateObject(stateObject)
  273. }
  274. stateObject.dirty = false
  275. }
  276. }
  277. }
  278. // SyncObjects syncs the changed objects to the trie
  279. func (self *StateDB) SyncObjects() {
  280. self.trie = trie.NewSecure(self.root[:], self.db)
  281. self.refund = new(big.Int)
  282. for _, stateObject := range self.stateObjects {
  283. if stateObject.remove {
  284. self.DeleteStateObject(stateObject)
  285. } else {
  286. stateObject.Update()
  287. self.UpdateStateObject(stateObject)
  288. }
  289. stateObject.dirty = false
  290. }
  291. }
  292. // Debug stuff
  293. func (self *StateDB) CreateOutputForDiff() {
  294. for _, stateObject := range self.stateObjects {
  295. stateObject.CreateOutputForDiff()
  296. }
  297. }