statedb.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // go-ethereum 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. "bytes"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/trie"
  25. )
  26. // StateDBs within the ethereum protocol are used to store anything
  27. // within the merkle trie. StateDBs take care of caching and storing
  28. // nested states. It's the general query interface to retrieve:
  29. // * Contracts
  30. // * Accounts
  31. type StateDB struct {
  32. db common.Database
  33. trie *trie.SecureTrie
  34. root common.Hash
  35. stateObjects map[string]*StateObject
  36. refund *big.Int
  37. thash, bhash common.Hash
  38. txIndex int
  39. logs map[common.Hash]Logs
  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. self.logs[self.thash] = append(self.logs[self.thash], log)
  59. }
  60. func (self *StateDB) GetLogs(hash common.Hash) Logs {
  61. return self.logs[hash]
  62. }
  63. func (self *StateDB) Logs() Logs {
  64. var logs Logs
  65. for _, lgs := range self.logs {
  66. logs = append(logs, lgs...)
  67. }
  68. return logs
  69. }
  70. func (self *StateDB) Refund(gas *big.Int) {
  71. self.refund.Add(self.refund, gas)
  72. }
  73. /*
  74. * GETTERS
  75. */
  76. func (self *StateDB) HasAccount(addr common.Address) bool {
  77. return self.GetStateObject(addr) != nil
  78. }
  79. // Retrieve the balance from the given address or 0 if object not found
  80. func (self *StateDB) GetBalance(addr common.Address) *big.Int {
  81. stateObject := self.GetStateObject(addr)
  82. if stateObject != nil {
  83. return stateObject.balance
  84. }
  85. return common.Big0
  86. }
  87. func (self *StateDB) GetNonce(addr common.Address) uint64 {
  88. stateObject := self.GetStateObject(addr)
  89. if stateObject != nil {
  90. return stateObject.nonce
  91. }
  92. return 0
  93. }
  94. func (self *StateDB) GetCode(addr common.Address) []byte {
  95. stateObject := self.GetStateObject(addr)
  96. if stateObject != nil {
  97. return stateObject.code
  98. }
  99. return nil
  100. }
  101. func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
  102. stateObject := self.GetStateObject(a)
  103. if stateObject != nil {
  104. return stateObject.GetState(b)
  105. }
  106. return common.Hash{}
  107. }
  108. func (self *StateDB) IsDeleted(addr common.Address) bool {
  109. stateObject := self.GetStateObject(addr)
  110. if stateObject != nil {
  111. return stateObject.remove
  112. }
  113. return false
  114. }
  115. /*
  116. * SETTERS
  117. */
  118. func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  119. stateObject := self.GetOrNewStateObject(addr)
  120. if stateObject != nil {
  121. stateObject.AddBalance(amount)
  122. }
  123. }
  124. func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
  125. stateObject := self.GetOrNewStateObject(addr)
  126. if stateObject != nil {
  127. stateObject.SetNonce(nonce)
  128. }
  129. }
  130. func (self *StateDB) SetCode(addr common.Address, code []byte) {
  131. stateObject := self.GetOrNewStateObject(addr)
  132. if stateObject != nil {
  133. stateObject.SetCode(code)
  134. }
  135. }
  136. func (self *StateDB) SetState(addr common.Address, key common.Hash, value common.Hash) {
  137. stateObject := self.GetOrNewStateObject(addr)
  138. if stateObject != nil {
  139. stateObject.SetState(key, value)
  140. }
  141. }
  142. func (self *StateDB) Delete(addr common.Address) bool {
  143. stateObject := self.GetStateObject(addr)
  144. if stateObject != nil {
  145. stateObject.MarkForDeletion()
  146. stateObject.balance = new(big.Int)
  147. return true
  148. }
  149. return false
  150. }
  151. //
  152. // Setting, updating & deleting state object methods
  153. //
  154. // Update the given state object and apply it to state trie
  155. func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
  156. //addr := stateObject.Address()
  157. if len(stateObject.CodeHash()) > 0 {
  158. self.db.Put(stateObject.CodeHash(), stateObject.code)
  159. }
  160. addr := stateObject.Address()
  161. self.trie.Update(addr[:], stateObject.RlpEncode())
  162. }
  163. // Delete the given state object and delete it from the state trie
  164. func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
  165. addr := stateObject.Address()
  166. self.trie.Delete(addr[:])
  167. //delete(self.stateObjects, addr.Str())
  168. }
  169. // Retrieve a state object given my the address. Nil if not found
  170. func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
  171. //addr = common.Address(addr)
  172. stateObject := self.stateObjects[addr.Str()]
  173. if stateObject != nil {
  174. return stateObject
  175. }
  176. data := self.trie.Get(addr[:])
  177. if len(data) == 0 {
  178. return nil
  179. }
  180. stateObject = NewStateObjectFromBytes(addr, []byte(data), self.db)
  181. self.SetStateObject(stateObject)
  182. return stateObject
  183. }
  184. func (self *StateDB) SetStateObject(object *StateObject) {
  185. self.stateObjects[object.Address().Str()] = object
  186. }
  187. // Retrieve a state object or create a new state object if nil
  188. func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
  189. stateObject := self.GetStateObject(addr)
  190. if stateObject == nil {
  191. stateObject = self.CreateAccount(addr)
  192. }
  193. return stateObject
  194. }
  195. // NewStateObject create a state object whether it exist in the trie or not
  196. func (self *StateDB) newStateObject(addr common.Address) *StateObject {
  197. if glog.V(logger.Core) {
  198. glog.Infof("(+) %x\n", addr)
  199. }
  200. stateObject := NewStateObject(addr, self.db)
  201. self.stateObjects[addr.Str()] = stateObject
  202. return stateObject
  203. }
  204. // Creates creates a new state object and takes ownership. This is different from "NewStateObject"
  205. func (self *StateDB) CreateAccount(addr common.Address) *StateObject {
  206. // Get previous (if any)
  207. so := self.GetStateObject(addr)
  208. // Create a new one
  209. newSo := self.newStateObject(addr)
  210. // If it existed set the balance to the new account
  211. if so != nil {
  212. newSo.balance = so.balance
  213. }
  214. return newSo
  215. }
  216. //
  217. // Setting, copying of the state methods
  218. //
  219. func (s *StateDB) Cmp(other *StateDB) bool {
  220. return bytes.Equal(s.trie.Root(), other.trie.Root())
  221. }
  222. func (self *StateDB) Copy() *StateDB {
  223. state := New(common.Hash{}, self.db)
  224. state.trie = self.trie
  225. for k, stateObject := range self.stateObjects {
  226. state.stateObjects[k] = stateObject.Copy()
  227. }
  228. state.refund.Set(self.refund)
  229. for hash, logs := range self.logs {
  230. state.logs[hash] = make(Logs, len(logs))
  231. copy(state.logs[hash], logs)
  232. }
  233. return state
  234. }
  235. func (self *StateDB) Set(state *StateDB) {
  236. self.trie = state.trie
  237. self.stateObjects = state.stateObjects
  238. self.refund = state.refund
  239. self.logs = state.logs
  240. }
  241. func (s *StateDB) Root() common.Hash {
  242. return common.BytesToHash(s.trie.Root())
  243. }
  244. func (s *StateDB) Trie() *trie.SecureTrie {
  245. return s.trie
  246. }
  247. // Resets the trie and all siblings
  248. func (s *StateDB) Reset() {
  249. s.trie.Reset()
  250. // Reset all nested states
  251. for _, stateObject := range s.stateObjects {
  252. stateObject.Reset()
  253. }
  254. s.Empty()
  255. }
  256. // Syncs the trie and all siblings
  257. func (s *StateDB) Sync() {
  258. // Sync all nested states
  259. for _, stateObject := range s.stateObjects {
  260. stateObject.trie.Commit()
  261. }
  262. s.trie.Commit()
  263. s.Empty()
  264. }
  265. func (self *StateDB) Empty() {
  266. self.stateObjects = make(map[string]*StateObject)
  267. self.refund = new(big.Int)
  268. }
  269. func (self *StateDB) Refunds() *big.Int {
  270. return self.refund
  271. }
  272. // SyncIntermediate updates the intermediate state and all mid steps
  273. func (self *StateDB) SyncIntermediate() {
  274. self.refund = new(big.Int)
  275. for _, stateObject := range self.stateObjects {
  276. if stateObject.dirty {
  277. if stateObject.remove {
  278. self.DeleteStateObject(stateObject)
  279. } else {
  280. stateObject.Update()
  281. self.UpdateStateObject(stateObject)
  282. }
  283. stateObject.dirty = false
  284. }
  285. }
  286. }
  287. // SyncObjects syncs the changed objects to the trie
  288. func (self *StateDB) SyncObjects() {
  289. self.trie = trie.NewSecure(self.root[:], self.db)
  290. self.refund = new(big.Int)
  291. for _, stateObject := range self.stateObjects {
  292. if stateObject.remove {
  293. self.DeleteStateObject(stateObject)
  294. } else {
  295. stateObject.Update()
  296. self.UpdateStateObject(stateObject)
  297. }
  298. stateObject.dirty = false
  299. }
  300. }
  301. // Debug stuff
  302. func (self *StateDB) CreateOutputForDiff() {
  303. for _, stateObject := range self.stateObjects {
  304. stateObject.CreateOutputForDiff()
  305. }
  306. }