Skip to content

Commit 702abdb

Browse files
authored
Merge pull request #2002 from CortexFoundation/dev
state operation v2
2 parents a6eca66 + 6384c74 commit 702abdb

File tree

8 files changed

+335
-266
lines changed

8 files changed

+335
-266
lines changed

core/state/journal.go

Lines changed: 126 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"math/big"
2121

2222
"github.com/CortexFoundation/CortexTheseus/common"
23+
"maps"
2324
)
2425

2526
// journalEntry is a modification entry in the state change journal that can be
@@ -30,6 +31,9 @@ type journalEntry interface {
3031

3132
// dirtied returns the Cortex address modified by this journal entry.
3233
dirtied() *common.Address
34+
35+
// copy returns a deep-copied journal entry.
36+
copy() journalEntry
3337
}
3438

3539
// journal contains the list of state modifications applied since the last state
@@ -84,22 +88,31 @@ func (j *journal) length() int {
8488
return len(j.entries)
8589
}
8690

91+
// copy returns a deep-copied journal.
92+
func (j *journal) copy() *journal {
93+
entries := make([]journalEntry, 0, j.length())
94+
for i := 0; i < j.length(); i++ {
95+
entries = append(entries, j.entries[i].copy())
96+
}
97+
return &journal{
98+
entries: entries,
99+
dirties: maps.Clone(j.dirties),
100+
}
101+
}
102+
87103
type (
88104
// Changes to the account trie.
89105
createObjectChange struct {
90106
account *common.Address
91107
}
92-
resetObjectChange struct {
93-
account *common.Address
94-
prev *stateObject
95-
prevdestruct bool
96-
prevAccount []byte
97-
prevStorage map[common.Hash][]byte
98108

99-
prevAccountOriginExist bool
100-
prevAccountOrigin []byte
101-
prevStorageOrigin map[common.Hash][]byte
109+
// createContractChange represents an account becoming a contract-account.
110+
// This event happens prior to executing initcode. The journal-event simply
111+
// manages the created-flag, in order to allow same-tx destruction.
112+
createContractChange struct {
113+
account common.Address
102114
}
115+
103116
selfDestructChange struct {
104117
account *common.Address
105118
prev bool // whether account had already self-destructed
@@ -147,6 +160,7 @@ type (
147160
touchChange struct {
148161
account *common.Address
149162
}
163+
150164
// Changes to the access list
151165
accessListAddAccountChange struct {
152166
address *common.Address
@@ -156,6 +170,7 @@ type (
156170
slot *common.Hash
157171
}
158172

173+
// Changes to transient storage
159174
transientStorageChange struct {
160175
account *common.Address
161176
key, prevalue common.Hash
@@ -164,34 +179,30 @@ type (
164179

165180
func (ch createObjectChange) revert(s *StateDB) {
166181
delete(s.stateObjects, *ch.account)
167-
delete(s.stateObjectsDirty, *ch.account)
168182
}
169183

170184
func (ch createObjectChange) dirtied() *common.Address {
171185
return ch.account
172186
}
173187

174-
func (ch resetObjectChange) revert(s *StateDB) {
175-
s.setStateObject(ch.prev)
176-
if !ch.prevdestruct {
177-
delete(s.stateObjectsDestruct, ch.prev.address)
178-
}
179-
if ch.prevAccount != nil {
180-
s.accounts[ch.prev.addrHash] = ch.prevAccount
181-
}
182-
if ch.prevStorage != nil {
183-
s.storages[ch.prev.addrHash] = ch.prevStorage
184-
}
185-
if ch.prevAccountOriginExist {
186-
s.accountsOrigin[ch.prev.address] = ch.prevAccountOrigin
187-
}
188-
if ch.prevStorageOrigin != nil {
189-
s.storagesOrigin[ch.prev.address] = ch.prevStorageOrigin
188+
func (ch createObjectChange) copy() journalEntry {
189+
return createObjectChange{
190+
account: ch.account,
190191
}
191192
}
192193

193-
func (ch resetObjectChange) dirtied() *common.Address {
194-
return ch.account
194+
func (ch createContractChange) revert(s *StateDB) {
195+
s.getStateObject(ch.account).newContract = false
196+
}
197+
198+
func (ch createContractChange) dirtied() *common.Address {
199+
return nil
200+
}
201+
202+
func (ch createContractChange) copy() journalEntry {
203+
return createContractChange{
204+
account: ch.account,
205+
}
195206
}
196207

197208
func (ch selfDestructChange) revert(s *StateDB) {
@@ -208,6 +219,16 @@ func (ch selfDestructChange) dirtied() *common.Address {
208219
return ch.account
209220
}
210221

222+
func (ch selfDestructChange) copy() journalEntry {
223+
return selfDestructChange{
224+
account: ch.account,
225+
prev: ch.prev,
226+
prevbalance: new(big.Int).Set(ch.prevbalance),
227+
prevupload: new(big.Int).Set(ch.prevupload),
228+
prevnum: new(big.Int).Set(ch.prevnum),
229+
}
230+
}
231+
211232
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
212233

213234
func (ch touchChange) revert(s *StateDB) {
@@ -217,6 +238,12 @@ func (ch touchChange) dirtied() *common.Address {
217238
return ch.account
218239
}
219240

241+
func (ch touchChange) copy() journalEntry {
242+
return touchChange{
243+
account: ch.account,
244+
}
245+
}
246+
220247
func (ch balanceChange) revert(s *StateDB) {
221248
s.getStateObject(*ch.account).setBalance(ch.prev)
222249
}
@@ -241,6 +268,13 @@ func (ch numChange) dirtied() *common.Address {
241268
return ch.account
242269
}
243270

271+
func (ch balanceChange) copy() journalEntry {
272+
return balanceChange{
273+
account: ch.account,
274+
prev: new(big.Int).Set(ch.prev),
275+
}
276+
}
277+
244278
func (ch nonceChange) revert(s *StateDB) {
245279
s.getStateObject(*ch.account).setNonce(ch.prev)
246280
}
@@ -249,6 +283,13 @@ func (ch nonceChange) dirtied() *common.Address {
249283
return ch.account
250284
}
251285

286+
func (ch nonceChange) copy() journalEntry {
287+
return nonceChange{
288+
account: ch.account,
289+
prev: ch.prev,
290+
}
291+
}
292+
252293
func (ch codeChange) revert(s *StateDB) {
253294
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
254295
}
@@ -257,6 +298,14 @@ func (ch codeChange) dirtied() *common.Address {
257298
return ch.account
258299
}
259300

301+
func (ch codeChange) copy() journalEntry {
302+
return codeChange{
303+
account: ch.account,
304+
prevhash: common.CopyBytes(ch.prevhash),
305+
prevcode: common.CopyBytes(ch.prevcode),
306+
}
307+
}
308+
260309
func (ch storageChange) revert(s *StateDB) {
261310
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
262311
}
@@ -265,8 +314,12 @@ func (ch storageChange) dirtied() *common.Address {
265314
return ch.account
266315
}
267316

268-
func (ch refundChange) revert(s *StateDB) {
269-
s.refund = ch.prev
317+
func (ch storageChange) copy() journalEntry {
318+
return storageChange{
319+
account: ch.account,
320+
key: ch.key,
321+
prevalue: ch.prevalue,
322+
}
270323
}
271324

272325
func (ch transientStorageChange) revert(s *StateDB) {
@@ -277,10 +330,28 @@ func (ch transientStorageChange) dirtied() *common.Address {
277330
return nil
278331
}
279332

333+
func (ch transientStorageChange) copy() journalEntry {
334+
return transientStorageChange{
335+
account: ch.account,
336+
key: ch.key,
337+
prevalue: ch.prevalue,
338+
}
339+
}
340+
341+
func (ch refundChange) revert(s *StateDB) {
342+
s.refund = ch.prev
343+
}
344+
280345
func (ch refundChange) dirtied() *common.Address {
281346
return nil
282347
}
283348

349+
func (ch refundChange) copy() journalEntry {
350+
return refundChange{
351+
prev: ch.prev,
352+
}
353+
}
354+
284355
func (ch addLogChange) revert(s *StateDB) {
285356
logs := s.logs[ch.txhash]
286357
if len(logs) == 1 {
@@ -295,6 +366,12 @@ func (ch addLogChange) dirtied() *common.Address {
295366
return nil
296367
}
297368

369+
func (ch addLogChange) copy() journalEntry {
370+
return addLogChange{
371+
txhash: ch.txhash,
372+
}
373+
}
374+
298375
func (ch addPreimageChange) revert(s *StateDB) {
299376
delete(s.preimages, ch.hash)
300377
}
@@ -303,6 +380,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
303380
return nil
304381
}
305382

383+
func (ch addPreimageChange) copy() journalEntry {
384+
return addPreimageChange{
385+
hash: ch.hash,
386+
}
387+
}
388+
306389
func (ch accessListAddAccountChange) revert(s *StateDB) {
307390
/*
308391
One important invariant here, is that whenever a (addr, slot) is added, if the
@@ -320,10 +403,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
320403
return nil
321404
}
322405

406+
func (ch accessListAddAccountChange) copy() journalEntry {
407+
return accessListAddAccountChange{
408+
address: ch.address,
409+
}
410+
}
411+
323412
func (ch accessListAddSlotChange) revert(s *StateDB) {
324413
s.accessList.DeleteSlot(*ch.address, *ch.slot)
325414
}
326415

327416
func (ch accessListAddSlotChange) dirtied() *common.Address {
328417
return nil
329418
}
419+
420+
func (ch accessListAddSlotChange) copy() journalEntry {
421+
return accessListAddSlotChange{
422+
address: ch.address,
423+
slot: ch.slot,
424+
}
425+
}

0 commit comments

Comments
 (0)