statedb.go 8.8 KB

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