statedb_test.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. // Copyright 2016 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
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math"
  22. "math/big"
  23. "math/rand"
  24. "reflect"
  25. "strings"
  26. "sync"
  27. "testing"
  28. "testing/quick"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/core/rawdb"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/private/engine"
  33. )
  34. // Tests that updating a state trie does not leak any database writes prior to
  35. // actually committing the state.
  36. func TestUpdateLeaks(t *testing.T) {
  37. // Create an empty state database
  38. db := rawdb.NewMemoryDatabase()
  39. state, _ := New(common.Hash{}, NewDatabase(db), nil)
  40. // Update it with some accounts
  41. for i := byte(0); i < 255; i++ {
  42. addr := common.BytesToAddress([]byte{i})
  43. state.AddBalance(addr, big.NewInt(int64(11*i)))
  44. state.SetNonce(addr, uint64(42*i))
  45. if i%2 == 0 {
  46. state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
  47. }
  48. if i%3 == 0 {
  49. state.SetCode(addr, []byte{i, i, i, i, i})
  50. }
  51. }
  52. root := state.IntermediateRoot(false)
  53. if err := state.Database().TrieDB().Commit(root, false, nil); err != nil {
  54. t.Errorf("can not commit trie %v to persistent database", root.Hex())
  55. }
  56. // Ensure that no data was leaked into the database
  57. it := db.NewIterator(nil, nil)
  58. for it.Next() {
  59. t.Errorf("State leaked into database: %x -> %x", it.Key(), it.Value())
  60. }
  61. it.Release()
  62. }
  63. // Tests that no intermediate state of an object is stored into the database,
  64. // only the one right before the commit.
  65. func TestIntermediateLeaks(t *testing.T) {
  66. // Create two state databases, one transitioning to the final state, the other final from the beginning
  67. transDb := rawdb.NewMemoryDatabase()
  68. finalDb := rawdb.NewMemoryDatabase()
  69. transState, _ := New(common.Hash{}, NewDatabase(transDb), nil)
  70. finalState, _ := New(common.Hash{}, NewDatabase(finalDb), nil)
  71. modify := func(state *StateDB, addr common.Address, i, tweak byte) {
  72. state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak)))
  73. state.SetNonce(addr, uint64(42*i+tweak))
  74. if i%2 == 0 {
  75. state.SetState(addr, common.Hash{i, i, i, 0}, common.Hash{})
  76. state.SetState(addr, common.Hash{i, i, i, tweak}, common.Hash{i, i, i, i, tweak})
  77. }
  78. if i%3 == 0 {
  79. state.SetCode(addr, []byte{i, i, i, i, i, tweak})
  80. }
  81. }
  82. // Modify the transient state.
  83. for i := byte(0); i < 255; i++ {
  84. modify(transState, common.Address{i}, i, 0)
  85. }
  86. // Write modifications to trie.
  87. transState.IntermediateRoot(false)
  88. // Overwrite all the data with new values in the transient database.
  89. for i := byte(0); i < 255; i++ {
  90. modify(transState, common.Address{i}, i, 99)
  91. modify(finalState, common.Address{i}, i, 99)
  92. }
  93. // Commit and cross check the databases.
  94. transRoot, err := transState.Commit(false)
  95. if err != nil {
  96. t.Fatalf("failed to commit transition state: %v", err)
  97. }
  98. if err = transState.Database().TrieDB().Commit(transRoot, false, nil); err != nil {
  99. t.Errorf("can not commit trie %v to persistent database", transRoot.Hex())
  100. }
  101. finalRoot, err := finalState.Commit(false)
  102. if err != nil {
  103. t.Fatalf("failed to commit final state: %v", err)
  104. }
  105. if err = finalState.Database().TrieDB().Commit(finalRoot, false, nil); err != nil {
  106. t.Errorf("can not commit trie %v to persistent database", finalRoot.Hex())
  107. }
  108. it := finalDb.NewIterator(nil, nil)
  109. for it.Next() {
  110. key, fvalue := it.Key(), it.Value()
  111. tvalue, err := transDb.Get(key)
  112. if err != nil {
  113. t.Errorf("entry missing from the transition database: %x -> %x", key, fvalue)
  114. }
  115. if !bytes.Equal(fvalue, tvalue) {
  116. t.Errorf("the value associate key %x is mismatch,: %x in transition database ,%x in final database", key, tvalue, fvalue)
  117. }
  118. }
  119. it.Release()
  120. it = transDb.NewIterator(nil, nil)
  121. for it.Next() {
  122. key, tvalue := it.Key(), it.Value()
  123. fvalue, err := finalDb.Get(key)
  124. if err != nil {
  125. t.Errorf("extra entry in the transition database: %x -> %x", key, it.Value())
  126. }
  127. if !bytes.Equal(fvalue, tvalue) {
  128. t.Errorf("the value associate key %x is mismatch,: %x in transition database ,%x in final database", key, tvalue, fvalue)
  129. }
  130. }
  131. }
  132. // TestCopy tests that copying a StateDB object indeed makes the original and
  133. // the copy independent of each other. This test is a regression test against
  134. // https://github.com/ethereum/go-ethereum/pull/15549.
  135. func TestCopy(t *testing.T) {
  136. // Create a random state test to copy and modify "independently"
  137. orig, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  138. for i := byte(0); i < 255; i++ {
  139. obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  140. obj.AddBalance(big.NewInt(int64(i)))
  141. orig.updateStateObject(obj)
  142. }
  143. orig.Finalise(false)
  144. // Copy the state
  145. copy := orig.Copy()
  146. // Copy the copy state
  147. ccopy := copy.Copy()
  148. // modify all in memory
  149. for i := byte(0); i < 255; i++ {
  150. origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  151. copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  152. ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  153. origObj.AddBalance(big.NewInt(2 * int64(i)))
  154. copyObj.AddBalance(big.NewInt(3 * int64(i)))
  155. ccopyObj.AddBalance(big.NewInt(4 * int64(i)))
  156. orig.updateStateObject(origObj)
  157. copy.updateStateObject(copyObj)
  158. ccopy.updateStateObject(copyObj)
  159. }
  160. // Finalise the changes on all concurrently
  161. finalise := func(wg *sync.WaitGroup, db *StateDB) {
  162. defer wg.Done()
  163. db.Finalise(true)
  164. }
  165. var wg sync.WaitGroup
  166. wg.Add(3)
  167. go finalise(&wg, orig)
  168. go finalise(&wg, copy)
  169. go finalise(&wg, ccopy)
  170. wg.Wait()
  171. // Verify that the three states have been updated independently
  172. for i := byte(0); i < 255; i++ {
  173. origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  174. copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  175. ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  176. if want := big.NewInt(3 * int64(i)); origObj.Balance().Cmp(want) != 0 {
  177. t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want)
  178. }
  179. if want := big.NewInt(4 * int64(i)); copyObj.Balance().Cmp(want) != 0 {
  180. t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, copyObj.Balance(), want)
  181. }
  182. if want := big.NewInt(5 * int64(i)); ccopyObj.Balance().Cmp(want) != 0 {
  183. t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, ccopyObj.Balance(), want)
  184. }
  185. }
  186. }
  187. func TestSnapshotRandom(t *testing.T) {
  188. config := &quick.Config{MaxCount: 1000}
  189. err := quick.Check((*snapshotTest).run, config)
  190. if cerr, ok := err.(*quick.CheckError); ok {
  191. test := cerr.In[0].(*snapshotTest)
  192. t.Errorf("%v:\n%s", test.err, test)
  193. } else if err != nil {
  194. t.Error(err)
  195. }
  196. }
  197. // A snapshotTest checks that reverting StateDB snapshots properly undoes all changes
  198. // captured by the snapshot. Instances of this test with pseudorandom content are created
  199. // by Generate.
  200. //
  201. // The test works as follows:
  202. //
  203. // A new state is created and all actions are applied to it. Several snapshots are taken
  204. // in between actions. The test then reverts each snapshot. For each snapshot the actions
  205. // leading up to it are replayed on a fresh, empty state. The behaviour of all public
  206. // accessor methods on the reverted state must match the return value of the equivalent
  207. // methods on the replayed state.
  208. type snapshotTest struct {
  209. addrs []common.Address // all account addresses
  210. actions []testAction // modifications to the state
  211. snapshots []int // actions indexes at which snapshot is taken
  212. err error // failure details are reported through this field
  213. }
  214. type testAction struct {
  215. name string
  216. fn func(testAction, *StateDB)
  217. args []int64
  218. noAddr bool
  219. }
  220. // newTestAction creates a random action that changes state.
  221. func newTestAction(addr common.Address, r *rand.Rand) testAction {
  222. actions := []testAction{
  223. {
  224. name: "SetBalance",
  225. fn: func(a testAction, s *StateDB) {
  226. s.SetBalance(addr, big.NewInt(a.args[0]))
  227. },
  228. args: make([]int64, 1),
  229. },
  230. {
  231. name: "AddBalance",
  232. fn: func(a testAction, s *StateDB) {
  233. s.AddBalance(addr, big.NewInt(a.args[0]))
  234. },
  235. args: make([]int64, 1),
  236. },
  237. {
  238. name: "SetNonce",
  239. fn: func(a testAction, s *StateDB) {
  240. s.SetNonce(addr, uint64(a.args[0]))
  241. },
  242. args: make([]int64, 1),
  243. },
  244. {
  245. name: "SetState",
  246. fn: func(a testAction, s *StateDB) {
  247. var key, val common.Hash
  248. binary.BigEndian.PutUint16(key[:], uint16(a.args[0]))
  249. binary.BigEndian.PutUint16(val[:], uint16(a.args[1]))
  250. s.SetState(addr, key, val)
  251. },
  252. args: make([]int64, 2),
  253. },
  254. {
  255. name: "SetCode",
  256. fn: func(a testAction, s *StateDB) {
  257. code := make([]byte, 16)
  258. binary.BigEndian.PutUint64(code, uint64(a.args[0]))
  259. binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
  260. s.SetCode(addr, code)
  261. },
  262. args: make([]int64, 2),
  263. },
  264. {
  265. name: "SetPrivacyMetadata",
  266. fn: func(a testAction, s *StateDB) {
  267. privFlag := engine.PrivacyFlagType((uint64(a.args[0])%2)*2 + 1) // the only possible values should be 1 and 3
  268. b := make([]byte, 8)
  269. binary.BigEndian.PutUint64(b, uint64(a.args[1]))
  270. hash := common.BytesToEncryptedPayloadHash(b)
  271. s.SetPrivacyMetadata(addr, &PrivacyMetadata{
  272. CreationTxHash: hash,
  273. PrivacyFlag: privFlag,
  274. })
  275. },
  276. args: make([]int64, 2),
  277. },
  278. {
  279. name: "CreateAccount",
  280. fn: func(a testAction, s *StateDB) {
  281. s.CreateAccount(addr)
  282. },
  283. },
  284. {
  285. name: "Suicide",
  286. fn: func(a testAction, s *StateDB) {
  287. s.Suicide(addr)
  288. },
  289. },
  290. {
  291. name: "AddRefund",
  292. fn: func(a testAction, s *StateDB) {
  293. s.AddRefund(uint64(a.args[0]))
  294. },
  295. args: make([]int64, 1),
  296. noAddr: true,
  297. },
  298. {
  299. name: "AddLog",
  300. fn: func(a testAction, s *StateDB) {
  301. data := make([]byte, 2)
  302. binary.BigEndian.PutUint16(data, uint16(a.args[0]))
  303. s.AddLog(&types.Log{Address: addr, Data: data})
  304. },
  305. args: make([]int64, 1),
  306. },
  307. {
  308. name: "AddPreimage",
  309. fn: func(a testAction, s *StateDB) {
  310. preimage := []byte{1}
  311. hash := common.BytesToHash(preimage)
  312. s.AddPreimage(hash, preimage)
  313. },
  314. args: make([]int64, 1),
  315. },
  316. {
  317. name: "AddAddressToAccessList",
  318. fn: func(a testAction, s *StateDB) {
  319. s.AddAddressToAccessList(addr)
  320. },
  321. },
  322. {
  323. name: "AddSlotToAccessList",
  324. fn: func(a testAction, s *StateDB) {
  325. s.AddSlotToAccessList(addr,
  326. common.Hash{byte(a.args[0])})
  327. },
  328. args: make([]int64, 1),
  329. },
  330. }
  331. action := actions[r.Intn(len(actions))]
  332. var nameargs []string
  333. if !action.noAddr {
  334. nameargs = append(nameargs, addr.Hex())
  335. }
  336. for i := range action.args {
  337. action.args[i] = rand.Int63n(100)
  338. nameargs = append(nameargs, fmt.Sprint(action.args[i]))
  339. }
  340. action.name += strings.Join(nameargs, ", ")
  341. return action
  342. }
  343. // Generate returns a new snapshot test of the given size. All randomness is
  344. // derived from r.
  345. func (*snapshotTest) Generate(r *rand.Rand, size int) reflect.Value {
  346. // Generate random actions.
  347. addrs := make([]common.Address, 50)
  348. for i := range addrs {
  349. addrs[i][0] = byte(i)
  350. }
  351. actions := make([]testAction, size)
  352. for i := range actions {
  353. addr := addrs[r.Intn(len(addrs))]
  354. actions[i] = newTestAction(addr, r)
  355. }
  356. // Generate snapshot indexes.
  357. nsnapshots := int(math.Sqrt(float64(size)))
  358. if size > 0 && nsnapshots == 0 {
  359. nsnapshots = 1
  360. }
  361. snapshots := make([]int, nsnapshots)
  362. snaplen := len(actions) / nsnapshots
  363. for i := range snapshots {
  364. // Try to place the snapshots some number of actions apart from each other.
  365. snapshots[i] = (i * snaplen) + r.Intn(snaplen)
  366. }
  367. return reflect.ValueOf(&snapshotTest{addrs, actions, snapshots, nil})
  368. }
  369. func (test *snapshotTest) String() string {
  370. out := new(bytes.Buffer)
  371. sindex := 0
  372. for i, action := range test.actions {
  373. if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
  374. fmt.Fprintf(out, "---- snapshot %d ----\n", sindex)
  375. sindex++
  376. }
  377. fmt.Fprintf(out, "%4d: %s\n", i, action.name)
  378. }
  379. return out.String()
  380. }
  381. func (test *snapshotTest) run() bool {
  382. // Run all actions and create snapshots.
  383. var (
  384. state, _ = New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  385. snapshotRevs = make([]int, len(test.snapshots))
  386. sindex = 0
  387. )
  388. for i, action := range test.actions {
  389. if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
  390. snapshotRevs[sindex] = state.Snapshot()
  391. sindex++
  392. }
  393. action.fn(action, state)
  394. }
  395. // Revert all snapshots in reverse order. Each revert must yield a state
  396. // that is equivalent to fresh state with all actions up the snapshot applied.
  397. for sindex--; sindex >= 0; sindex-- {
  398. checkstate, _ := New(common.Hash{}, state.Database(), nil)
  399. for _, action := range test.actions[:test.snapshots[sindex]] {
  400. action.fn(action, checkstate)
  401. }
  402. state.RevertToSnapshot(snapshotRevs[sindex])
  403. if err := test.checkEqual(state, checkstate); err != nil {
  404. test.err = fmt.Errorf("state mismatch after revert to snapshot %d\n%v", sindex, err)
  405. return false
  406. }
  407. }
  408. return true
  409. }
  410. // checkEqual checks that methods of state and checkstate return the same values.
  411. func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
  412. for _, addr := range test.addrs {
  413. var err error
  414. checkeq := func(op string, a, b interface{}) bool {
  415. if err == nil && !reflect.DeepEqual(a, b) {
  416. err = fmt.Errorf("got %s(%s) == %v, want %v", op, addr.Hex(), a, b)
  417. return false
  418. }
  419. return true
  420. }
  421. // Check basic accessor methods.
  422. checkeq("Exist", state.Exist(addr), checkstate.Exist(addr))
  423. checkeq("HasSuicided", state.HasSuicided(addr), checkstate.HasSuicided(addr))
  424. checkeq("GetBalance", state.GetBalance(addr), checkstate.GetBalance(addr))
  425. checkeq("GetNonce", state.GetNonce(addr), checkstate.GetNonce(addr))
  426. checkeq("GetCode", state.GetCode(addr), checkstate.GetCode(addr))
  427. checkeq("GetCodeHash", state.GetCodeHash(addr), checkstate.GetCodeHash(addr))
  428. checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
  429. statePM, _ := state.GetPrivacyMetadata(addr)
  430. checkStatePM, _ := checkstate.GetPrivacyMetadata(addr)
  431. checkeq("GetPrivacyMetadata", statePM, checkStatePM)
  432. // Check storage.
  433. if obj := state.getStateObject(addr); obj != nil {
  434. state.ForEachStorage(addr, func(key, value common.Hash) bool {
  435. return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
  436. })
  437. checkstate.ForEachStorage(addr, func(key, value common.Hash) bool {
  438. return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
  439. })
  440. }
  441. if err != nil {
  442. return err
  443. }
  444. }
  445. if state.GetRefund() != checkstate.GetRefund() {
  446. return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
  447. state.GetRefund(), checkstate.GetRefund())
  448. }
  449. if !reflect.DeepEqual(state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{})) {
  450. return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
  451. state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{}))
  452. }
  453. return nil
  454. }
  455. func TestTouchDelete(t *testing.T) {
  456. s := newStateTest()
  457. s.state.GetOrNewStateObject(common.Address{})
  458. root, _ := s.state.Commit(false)
  459. s.state, _ = New(root, s.state.db, s.state.snaps)
  460. snapshot := s.state.Snapshot()
  461. s.state.AddBalance(common.Address{}, new(big.Int))
  462. if len(s.state.journal.dirties) != 1 {
  463. t.Fatal("expected one dirty state object")
  464. }
  465. s.state.RevertToSnapshot(snapshot)
  466. if len(s.state.journal.dirties) != 0 {
  467. t.Fatal("expected no dirty state object")
  468. }
  469. }
  470. // TestCopyOfCopy tests that modified objects are carried over to the copy, and the copy of the copy.
  471. // See https://github.com/ethereum/go-ethereum/pull/15225#issuecomment-380191512
  472. func TestCopyOfCopy(t *testing.T) {
  473. state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  474. addr := common.HexToAddress("aaaa")
  475. state.SetBalance(addr, big.NewInt(42))
  476. if got := state.Copy().GetBalance(addr).Uint64(); got != 42 {
  477. t.Fatalf("1st copy fail, expected 42, got %v", got)
  478. }
  479. if got := state.Copy().Copy().GetBalance(addr).Uint64(); got != 42 {
  480. t.Fatalf("2nd copy fail, expected 42, got %v", got)
  481. }
  482. }
  483. // Tests a regression where committing a copy lost some internal meta information,
  484. // leading to corrupted subsequent copies.
  485. //
  486. // See https://github.com/ethereum/go-ethereum/issues/20106.
  487. func TestCopyCommitCopy(t *testing.T) {
  488. state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  489. // Create an account and check if the retrieved balance is correct
  490. addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
  491. skey := common.HexToHash("aaa")
  492. sval := common.HexToHash("bbb")
  493. state.SetBalance(addr, big.NewInt(42)) // Change the account trie
  494. state.SetCode(addr, []byte("hello")) // Change an external metadata
  495. state.SetState(addr, skey, sval) // Change the storage trie
  496. if balance := state.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  497. t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42)
  498. }
  499. if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  500. t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
  501. }
  502. if val := state.GetState(addr, skey); val != sval {
  503. t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
  504. }
  505. if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
  506. t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
  507. }
  508. // Copy the non-committed state database and check pre/post commit balance
  509. copyOne := state.Copy()
  510. if balance := copyOne.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  511. t.Fatalf("first copy pre-commit balance mismatch: have %v, want %v", balance, 42)
  512. }
  513. if code := copyOne.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  514. t.Fatalf("first copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
  515. }
  516. if val := copyOne.GetState(addr, skey); val != sval {
  517. t.Fatalf("first copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
  518. }
  519. if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
  520. t.Fatalf("first copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
  521. }
  522. copyOne.Commit(false)
  523. if balance := copyOne.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  524. t.Fatalf("first copy post-commit balance mismatch: have %v, want %v", balance, 42)
  525. }
  526. if code := copyOne.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  527. t.Fatalf("first copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
  528. }
  529. if val := copyOne.GetState(addr, skey); val != sval {
  530. t.Fatalf("first copy post-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
  531. }
  532. if val := copyOne.GetCommittedState(addr, skey); val != sval {
  533. t.Fatalf("first copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
  534. }
  535. // Copy the copy and check the balance once more
  536. copyTwo := copyOne.Copy()
  537. if balance := copyTwo.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  538. t.Fatalf("second copy balance mismatch: have %v, want %v", balance, 42)
  539. }
  540. if code := copyTwo.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  541. t.Fatalf("second copy code mismatch: have %x, want %x", code, []byte("hello"))
  542. }
  543. if val := copyTwo.GetState(addr, skey); val != sval {
  544. t.Fatalf("second copy non-committed storage slot mismatch: have %x, want %x", val, sval)
  545. }
  546. if val := copyTwo.GetCommittedState(addr, skey); val != sval {
  547. t.Fatalf("second copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
  548. }
  549. }
  550. // Tests a regression where committing a copy lost some internal meta information,
  551. // leading to corrupted subsequent copies.
  552. //
  553. // See https://github.com/ethereum/go-ethereum/issues/20106.
  554. func TestCopyCopyCommitCopy(t *testing.T) {
  555. state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  556. // Create an account and check if the retrieved balance is correct
  557. addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
  558. skey := common.HexToHash("aaa")
  559. sval := common.HexToHash("bbb")
  560. state.SetBalance(addr, big.NewInt(42)) // Change the account trie
  561. state.SetCode(addr, []byte("hello")) // Change an external metadata
  562. state.SetState(addr, skey, sval) // Change the storage trie
  563. if balance := state.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  564. t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42)
  565. }
  566. if code := state.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  567. t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
  568. }
  569. if val := state.GetState(addr, skey); val != sval {
  570. t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
  571. }
  572. if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
  573. t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
  574. }
  575. // Copy the non-committed state database and check pre/post commit balance
  576. copyOne := state.Copy()
  577. if balance := copyOne.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  578. t.Fatalf("first copy balance mismatch: have %v, want %v", balance, 42)
  579. }
  580. if code := copyOne.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  581. t.Fatalf("first copy code mismatch: have %x, want %x", code, []byte("hello"))
  582. }
  583. if val := copyOne.GetState(addr, skey); val != sval {
  584. t.Fatalf("first copy non-committed storage slot mismatch: have %x, want %x", val, sval)
  585. }
  586. if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
  587. t.Fatalf("first copy committed storage slot mismatch: have %x, want %x", val, common.Hash{})
  588. }
  589. // Copy the copy and check the balance once more
  590. copyTwo := copyOne.Copy()
  591. if balance := copyTwo.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  592. t.Fatalf("second copy pre-commit balance mismatch: have %v, want %v", balance, 42)
  593. }
  594. if code := copyTwo.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  595. t.Fatalf("second copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
  596. }
  597. if val := copyTwo.GetState(addr, skey); val != sval {
  598. t.Fatalf("second copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
  599. }
  600. if val := copyTwo.GetCommittedState(addr, skey); val != (common.Hash{}) {
  601. t.Fatalf("second copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
  602. }
  603. copyTwo.Commit(false)
  604. if balance := copyTwo.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  605. t.Fatalf("second copy post-commit balance mismatch: have %v, want %v", balance, 42)
  606. }
  607. if code := copyTwo.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  608. t.Fatalf("second copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
  609. }
  610. if val := copyTwo.GetState(addr, skey); val != sval {
  611. t.Fatalf("second copy post-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
  612. }
  613. if val := copyTwo.GetCommittedState(addr, skey); val != sval {
  614. t.Fatalf("second copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
  615. }
  616. // Copy the copy-copy and check the balance once more
  617. copyThree := copyTwo.Copy()
  618. if balance := copyThree.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
  619. t.Fatalf("third copy balance mismatch: have %v, want %v", balance, 42)
  620. }
  621. if code := copyThree.GetCode(addr); !bytes.Equal(code, []byte("hello")) {
  622. t.Fatalf("third copy code mismatch: have %x, want %x", code, []byte("hello"))
  623. }
  624. if val := copyThree.GetState(addr, skey); val != sval {
  625. t.Fatalf("third copy non-committed storage slot mismatch: have %x, want %x", val, sval)
  626. }
  627. if val := copyThree.GetCommittedState(addr, skey); val != sval {
  628. t.Fatalf("third copy committed storage slot mismatch: have %x, want %x", val, sval)
  629. }
  630. }
  631. // TestDeleteCreateRevert tests a weird state transition corner case that we hit
  632. // while changing the internals of StateDB. The workflow is that a contract is
  633. // self-destructed, then in a follow-up transaction (but same block) it's created
  634. // again and the transaction reverted.
  635. //
  636. // The original StateDB implementation flushed dirty objects to the tries after
  637. // each transaction, so this works ok. The rework accumulated writes in memory
  638. // first, but the journal wiped the entire state object on create-revert.
  639. func TestDeleteCreateRevert(t *testing.T) {
  640. // Create an initial state with a single contract
  641. state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil)
  642. addr := common.BytesToAddress([]byte("so"))
  643. state.SetBalance(addr, big.NewInt(1))
  644. root, _ := state.Commit(false)
  645. state, _ = New(root, state.db, state.snaps)
  646. // Simulate self-destructing in one transaction, then create-reverting in another
  647. state.Suicide(addr)
  648. state.Finalise(true)
  649. id := state.Snapshot()
  650. state.SetBalance(addr, big.NewInt(2))
  651. state.RevertToSnapshot(id)
  652. // Commit the entire state and make sure we don't crash and have the correct state
  653. root, _ = state.Commit(true)
  654. state, _ = New(root, state.db, state.snaps)
  655. if state.getStateObject(addr) != nil {
  656. t.Fatalf("self-destructed contract came alive")
  657. }
  658. }
  659. // TestMissingTrieNodes tests that if the StateDB fails to load parts of the trie,
  660. // the Commit operation fails with an error
  661. // If we are missing trie nodes, we should not continue writing to the trie
  662. func TestMissingTrieNodes(t *testing.T) {
  663. // Create an initial state with a few accounts
  664. memDb := rawdb.NewMemoryDatabase()
  665. db := NewDatabase(memDb)
  666. var root common.Hash
  667. state, _ := New(common.Hash{}, db, nil)
  668. addr := common.BytesToAddress([]byte("so"))
  669. {
  670. state.SetBalance(addr, big.NewInt(1))
  671. state.SetCode(addr, []byte{1, 2, 3})
  672. a2 := common.BytesToAddress([]byte("another"))
  673. state.SetBalance(a2, big.NewInt(100))
  674. state.SetCode(a2, []byte{1, 2, 4})
  675. root, _ = state.Commit(false)
  676. t.Logf("root: %x", root)
  677. // force-flush
  678. state.Database().TrieDB().Cap(0)
  679. }
  680. // Create a new state on the old root
  681. state, _ = New(root, db, nil)
  682. // Now we clear out the memdb
  683. it := memDb.NewIterator(nil, nil)
  684. for it.Next() {
  685. k := it.Key()
  686. // Leave the root intact
  687. if !bytes.Equal(k, root[:]) {
  688. t.Logf("key: %x", k)
  689. memDb.Delete(k)
  690. }
  691. }
  692. balance := state.GetBalance(addr)
  693. // The removed elem should lead to it returning zero balance
  694. if exp, got := uint64(0), balance.Uint64(); got != exp {
  695. t.Errorf("expected %d, got %d", exp, got)
  696. }
  697. // Modify the state
  698. state.SetBalance(addr, big.NewInt(2))
  699. root, err := state.Commit(false)
  700. if err == nil {
  701. t.Fatalf("expected error, got root :%x", root)
  702. }
  703. }
  704. func TestStateDBAccessList(t *testing.T) {
  705. // Some helpers
  706. addr := func(a string) common.Address {
  707. return common.HexToAddress(a)
  708. }
  709. slot := func(a string) common.Hash {
  710. return common.HexToHash(a)
  711. }
  712. memDb := rawdb.NewMemoryDatabase()
  713. db := NewDatabase(memDb)
  714. state, _ := New(common.Hash{}, db, nil)
  715. state.accessList = newAccessList()
  716. verifyAddrs := func(astrings ...string) {
  717. t.Helper()
  718. // convert to common.Address form
  719. var addresses []common.Address
  720. var addressMap = make(map[common.Address]struct{})
  721. for _, astring := range astrings {
  722. address := addr(astring)
  723. addresses = append(addresses, address)
  724. addressMap[address] = struct{}{}
  725. }
  726. // Check that the given addresses are in the access list
  727. for _, address := range addresses {
  728. if !state.AddressInAccessList(address) {
  729. t.Fatalf("expected %x to be in access list", address)
  730. }
  731. }
  732. // Check that only the expected addresses are present in the acesslist
  733. for address := range state.accessList.addresses {
  734. if _, exist := addressMap[address]; !exist {
  735. t.Fatalf("extra address %x in access list", address)
  736. }
  737. }
  738. }
  739. verifySlots := func(addrString string, slotStrings ...string) {
  740. if !state.AddressInAccessList(addr(addrString)) {
  741. t.Fatalf("scope missing address/slots %v", addrString)
  742. }
  743. var address = addr(addrString)
  744. // convert to common.Hash form
  745. var slots []common.Hash
  746. var slotMap = make(map[common.Hash]struct{})
  747. for _, slotString := range slotStrings {
  748. s := slot(slotString)
  749. slots = append(slots, s)
  750. slotMap[s] = struct{}{}
  751. }
  752. // Check that the expected items are in the access list
  753. for i, s := range slots {
  754. if _, slotPresent := state.SlotInAccessList(address, s); !slotPresent {
  755. t.Fatalf("input %d: scope missing slot %v (address %v)", i, s, addrString)
  756. }
  757. }
  758. // Check that no extra elements are in the access list
  759. index := state.accessList.addresses[address]
  760. if index >= 0 {
  761. stateSlots := state.accessList.slots[index]
  762. for s := range stateSlots {
  763. if _, slotPresent := slotMap[s]; !slotPresent {
  764. t.Fatalf("scope has extra slot %v (address %v)", s, addrString)
  765. }
  766. }
  767. }
  768. }
  769. state.AddAddressToAccessList(addr("aa")) // 1
  770. state.AddSlotToAccessList(addr("bb"), slot("01")) // 2,3
  771. state.AddSlotToAccessList(addr("bb"), slot("02")) // 4
  772. verifyAddrs("aa", "bb")
  773. verifySlots("bb", "01", "02")
  774. // Make a copy
  775. stateCopy1 := state.Copy()
  776. if exp, got := 4, state.journal.length(); exp != got {
  777. t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
  778. }
  779. // same again, should cause no journal entries
  780. state.AddSlotToAccessList(addr("bb"), slot("01"))
  781. state.AddSlotToAccessList(addr("bb"), slot("02"))
  782. state.AddAddressToAccessList(addr("aa"))
  783. if exp, got := 4, state.journal.length(); exp != got {
  784. t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
  785. }
  786. // some new ones
  787. state.AddSlotToAccessList(addr("bb"), slot("03")) // 5
  788. state.AddSlotToAccessList(addr("aa"), slot("01")) // 6
  789. state.AddSlotToAccessList(addr("cc"), slot("01")) // 7,8
  790. state.AddAddressToAccessList(addr("cc"))
  791. if exp, got := 8, state.journal.length(); exp != got {
  792. t.Fatalf("journal length mismatch: have %d, want %d", got, exp)
  793. }
  794. verifyAddrs("aa", "bb", "cc")
  795. verifySlots("aa", "01")
  796. verifySlots("bb", "01", "02", "03")
  797. verifySlots("cc", "01")
  798. // now start rolling back changes
  799. state.journal.revert(state, 7)
  800. if _, ok := state.SlotInAccessList(addr("cc"), slot("01")); ok {
  801. t.Fatalf("slot present, expected missing")
  802. }
  803. verifyAddrs("aa", "bb", "cc")
  804. verifySlots("aa", "01")
  805. verifySlots("bb", "01", "02", "03")
  806. state.journal.revert(state, 6)
  807. if state.AddressInAccessList(addr("cc")) {
  808. t.Fatalf("addr present, expected missing")
  809. }
  810. verifyAddrs("aa", "bb")
  811. verifySlots("aa", "01")
  812. verifySlots("bb", "01", "02", "03")
  813. state.journal.revert(state, 5)
  814. if _, ok := state.SlotInAccessList(addr("aa"), slot("01")); ok {
  815. t.Fatalf("slot present, expected missing")
  816. }
  817. verifyAddrs("aa", "bb")
  818. verifySlots("bb", "01", "02", "03")
  819. state.journal.revert(state, 4)
  820. if _, ok := state.SlotInAccessList(addr("bb"), slot("03")); ok {
  821. t.Fatalf("slot present, expected missing")
  822. }
  823. verifyAddrs("aa", "bb")
  824. verifySlots("bb", "01", "02")
  825. state.journal.revert(state, 3)
  826. if _, ok := state.SlotInAccessList(addr("bb"), slot("02")); ok {
  827. t.Fatalf("slot present, expected missing")
  828. }
  829. verifyAddrs("aa", "bb")
  830. verifySlots("bb", "01")
  831. state.journal.revert(state, 2)
  832. if _, ok := state.SlotInAccessList(addr("bb"), slot("01")); ok {
  833. t.Fatalf("slot present, expected missing")
  834. }
  835. verifyAddrs("aa", "bb")
  836. state.journal.revert(state, 1)
  837. if state.AddressInAccessList(addr("bb")) {
  838. t.Fatalf("addr present, expected missing")
  839. }
  840. verifyAddrs("aa")
  841. state.journal.revert(state, 0)
  842. if state.AddressInAccessList(addr("aa")) {
  843. t.Fatalf("addr present, expected missing")
  844. }
  845. if got, exp := len(state.accessList.addresses), 0; got != exp {
  846. t.Fatalf("expected empty, got %d", got)
  847. }
  848. if got, exp := len(state.accessList.slots), 0; got != exp {
  849. t.Fatalf("expected empty, got %d", got)
  850. }
  851. // Check the copy
  852. // Make a copy
  853. state = stateCopy1
  854. verifyAddrs("aa", "bb")
  855. verifySlots("bb", "01", "02")
  856. if got, exp := len(state.accessList.addresses), 2; got != exp {
  857. t.Fatalf("expected empty, got %d", got)
  858. }
  859. if got, exp := len(state.accessList.slots), 1; got != exp {
  860. t.Fatalf("expected empty, got %d", got)
  861. }
  862. }
  863. // Quorum
  864. func TestStorageRoot(t *testing.T) {
  865. var (
  866. mem = rawdb.NewMemoryDatabase()
  867. db = NewDatabase(mem)
  868. state, _ = New(common.Hash{}, db, nil)
  869. addr = common.Address{1}
  870. key = common.Hash{1}
  871. value = common.Hash{42}
  872. empty = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  873. )
  874. so := state.GetOrNewStateObject(addr)
  875. emptyRoot := so.storageRoot(db)
  876. if emptyRoot != empty {
  877. t.Errorf("Invalid empty storage root, expected %x, got %x", empty, emptyRoot)
  878. }
  879. // add a bit of state
  880. so.SetState(db, key, value)
  881. state.Commit(false)
  882. root := so.storageRoot(db)
  883. expected := common.HexToHash("63511abd258fa907afa30cb118b53744a4f49055bb3f531da512c6b866fc2ffb")
  884. if expected != root {
  885. t.Errorf("Invalid storage root, expected %x, got %x", expected, root)
  886. }
  887. }
  888. // End Quorum
  889. // Quorum - Privacy Enhancements
  890. func TestPrivacyMetadataIsSavedOnStateDbCommit(t *testing.T) {
  891. ethDb := rawdb.NewMemoryDatabase()
  892. stateDb := NewDatabase(ethDb)
  893. state, _ := New(common.Hash{}, stateDb, nil)
  894. addr := common.Address{1}
  895. state.CreateAccount(addr)
  896. state.SetNonce(addr, uint64(1))
  897. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  898. PrivacyFlag: engine.PrivacyFlagPartyProtection,
  899. CreationTxHash: common.EncryptedPayloadHash{1},
  900. })
  901. privMetaData, _ := state.GetCommittedStatePrivacyMetadata(addr)
  902. if privMetaData != nil {
  903. t.Errorf("privacy metadata should not have been stored before commit")
  904. }
  905. state.Commit(false)
  906. privMetaData, _ = state.GetCommittedStatePrivacyMetadata(addr)
  907. if privMetaData == nil {
  908. t.Errorf("privacy metadata should have been stored during commit")
  909. }
  910. }
  911. func TestPrivacyMetadataIsUpdatedOnAccountReCreateWithDifferentPrivacyMetadata(t *testing.T) {
  912. ethDb := rawdb.NewMemoryDatabase()
  913. stateDb := NewDatabase(ethDb)
  914. state, _ := New(common.Hash{}, stateDb, nil)
  915. addr := common.Address{1}
  916. state.CreateAccount(addr)
  917. state.SetNonce(addr, uint64(1))
  918. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  919. PrivacyFlag: engine.PrivacyFlagPartyProtection,
  920. CreationTxHash: common.EncryptedPayloadHash{1},
  921. })
  922. state.Commit(false)
  923. privMetaData, _ := state.GetCommittedStatePrivacyMetadata(addr)
  924. if privMetaData == nil {
  925. t.Errorf("privacy metadata should have been stored during commit")
  926. }
  927. state.CreateAccount(addr)
  928. state.SetNonce(addr, uint64(1))
  929. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  930. PrivacyFlag: engine.PrivacyFlagStateValidation,
  931. CreationTxHash: common.EncryptedPayloadHash{1},
  932. })
  933. state.Commit(false)
  934. privMetaData, _ = state.GetCommittedStatePrivacyMetadata(addr)
  935. if privMetaData == nil {
  936. t.Errorf("privacy metadata should have been updated during commit")
  937. } else if privMetaData.PrivacyFlag != engine.PrivacyFlagStateValidation {
  938. t.Errorf("privacy metadata should have StateValidation as the the privacy flag")
  939. }
  940. }
  941. func TestPrivacyMetadataIsRemovedOnAccountSuicide(t *testing.T) {
  942. ethDb := rawdb.NewMemoryDatabase()
  943. stateDb := NewDatabase(ethDb)
  944. state, _ := New(common.Hash{}, stateDb, nil)
  945. addr := common.Address{1}
  946. state.CreateAccount(addr)
  947. state.SetNonce(addr, uint64(1))
  948. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  949. PrivacyFlag: engine.PrivacyFlagPartyProtection,
  950. CreationTxHash: common.EncryptedPayloadHash{1},
  951. })
  952. state.Commit(false)
  953. privMetaData, _ := state.GetCommittedStatePrivacyMetadata(addr)
  954. if privMetaData == nil {
  955. t.Errorf("privacy metadata should have been stored during commit")
  956. }
  957. state.Suicide(addr)
  958. state.Commit(false)
  959. privMetaData, _ = state.GetCommittedStatePrivacyMetadata(addr)
  960. if privMetaData != nil {
  961. t.Errorf("privacy metadata should have been deleted during account suicide")
  962. }
  963. }
  964. func TestPrivacyMetadataChangesAreRolledBackOnRevert(t *testing.T) {
  965. ethDb := rawdb.NewMemoryDatabase()
  966. stateDb := NewDatabase(ethDb)
  967. state, _ := New(common.Hash{}, stateDb, nil)
  968. addr := common.Address{1}
  969. state.CreateAccount(addr)
  970. state.SetNonce(addr, uint64(1))
  971. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  972. PrivacyFlag: engine.PrivacyFlagPartyProtection,
  973. CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("one")),
  974. })
  975. state.Commit(false)
  976. privMetaData, _ := state.GetCommittedStatePrivacyMetadata(addr)
  977. if privMetaData == nil {
  978. t.Errorf("privacy metadata should have been stored during commit")
  979. }
  980. // update privacy metadata
  981. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  982. PrivacyFlag: engine.PrivacyFlagStateValidation,
  983. CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("two")),
  984. })
  985. // record the snapshot
  986. snapshot := state.Snapshot()
  987. privMetaData, _ = state.GetPrivacyMetadata(addr)
  988. if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("two")) {
  989. t.Errorf("current privacy metadata creation tx hash does not match the expected value")
  990. }
  991. // update the metadata
  992. state.SetPrivacyMetadata(addr, &PrivacyMetadata{
  993. PrivacyFlag: engine.PrivacyFlagStateValidation,
  994. CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("three")),
  995. })
  996. privMetaData, _ = state.GetPrivacyMetadata(addr)
  997. if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("three")) {
  998. t.Errorf("current privacy metadata creation tx hash does not match the expected value")
  999. }
  1000. // revert to snapshot
  1001. state.RevertToSnapshot(snapshot)
  1002. privMetaData, _ = state.GetPrivacyMetadata(addr)
  1003. if privMetaData.CreationTxHash != common.BytesToEncryptedPayloadHash([]byte("two")) {
  1004. t.Errorf("current privacy metadata creation tx hash does not match the expected value")
  1005. }
  1006. }
  1007. // End Quorum - Privacy Enhancements