Skip to content

Commit b8eaf48

Browse files
committed
core/state: move state log mechanism to a separate layer 30569
1 parent cb1c4b8 commit b8eaf48

File tree

24 files changed

+627
-165
lines changed

24 files changed

+627
-165
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/XinFinOrg/XDPoSChain/core/bloombits"
4444
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
4545
"github.com/XinFinOrg/XDPoSChain/core/state"
46-
"github.com/XinFinOrg/XDPoSChain/core/tracing"
4746
"github.com/XinFinOrg/XDPoSChain/core/types"
4847
"github.com/XinFinOrg/XDPoSChain/core/vm"
4948
"github.com/XinFinOrg/XDPoSChain/crypto"
@@ -738,7 +737,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
738737

739738
// Set infinite balance to the fake caller account.
740739
from := stateDB.GetOrNewStateObject(call.From)
741-
from.SetBalance(math.MaxBig256, tracing.BalanceChangeUnspecified)
740+
from.SetBalance(math.MaxBig256)
742741

743742
// Execute the call.
744743
msg := &core.Message{

consensus/XDPoS/engines/engine_v1/engine.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/XinFinOrg/XDPoSChain/consensus/misc/eip1559"
2222
"github.com/XinFinOrg/XDPoSChain/core/state"
2323
"github.com/XinFinOrg/XDPoSChain/core/types"
24+
"github.com/XinFinOrg/XDPoSChain/core/vm"
2425
"github.com/XinFinOrg/XDPoSChain/crypto"
2526
"github.com/XinFinOrg/XDPoSChain/ethdb"
2627
"github.com/XinFinOrg/XDPoSChain/log"
@@ -53,7 +54,7 @@ type XDPoS_v1 struct {
5354
signFn clique.SignerFn // Signer function to authorize hashes with
5455
lock sync.RWMutex // Protects the signer fields
5556

56-
HookReward func(chain consensus.ChainReader, state *state.StateDB, parentState *state.StateDB, header *types.Header) (map[string]interface{}, error)
57+
HookReward func(chain consensus.ChainReader, state vm.StateDB, parentState *state.StateDB, header *types.Header) (map[string]interface{}, error)
5758
HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error)
5859
HookPenaltyTIPSigning func(chain consensus.ChainReader, header *types.Header, candidate []common.Address) ([]common.Address, error)
5960
HookValidator func(header *types.Header, signers []common.Address) ([]byte, error)

consensus/misc/dao.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"errors"
2222
"math/big"
2323

24-
"github.com/XinFinOrg/XDPoSChain/core/state"
2524
"github.com/XinFinOrg/XDPoSChain/core/tracing"
2625
"github.com/XinFinOrg/XDPoSChain/core/types"
26+
"github.com/XinFinOrg/XDPoSChain/core/vm"
2727
"github.com/XinFinOrg/XDPoSChain/params"
2828
)
2929

@@ -73,15 +73,16 @@ func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header)
7373
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
7474
// rules, transferring all balances of a set of DAO accounts to a single refund
7575
// contract.
76-
func ApplyDAOHardFork(statedb *state.StateDB) {
76+
func ApplyDAOHardFork(statedb vm.StateDB) {
7777
// Retrieve the contract to refund balances into
7878
if !statedb.Exist(params.DAORefundContract) {
7979
statedb.CreateAccount(params.DAORefundContract)
8080
}
8181

8282
// Move every DAO account and extra-balance account funds into the refund contract
8383
for _, addr := range params.DAODrainList() {
84-
statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr), tracing.BalanceIncreaseDaoContract)
85-
statedb.SetBalance(addr, new(big.Int), tracing.BalanceDecreaseDaoAccount)
84+
balance := statedb.GetBalance(addr)
85+
statedb.AddBalance(params.DAORefundContract, balance, tracing.BalanceIncreaseDaoContract)
86+
statedb.SubBalance(addr, balance, tracing.BalanceDecreaseDaoAccount)
8687
}
8788
}

core/blockchain.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
17841784
if err != nil {
17851785
return it.index, events, coalescedLogs, err
17861786
}
1787-
statedb.SetLogger(bc.logger)
17881787

17891788
// If we have a followup block, run that against the current state to pre-cache
17901789
// transactions and probabilistically some of the account/storage trie nodes.

core/state/database.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ func NewDatabase(db ethdb.Database) Database {
125125
return NewDatabaseWithConfig(db, nil)
126126
}
127127

128+
// NewDatabaseForTesting is similar to NewDatabase, but it initializes the caching
129+
// db by using an ephemeral memory db with default config for testing.
130+
func NewDatabaseForTesting() Database {
131+
return NewDatabase(rawdb.NewMemoryDatabase())
132+
}
133+
128134
// NewDatabaseWithConfig creates a backing store for state. The returned database
129135
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
130136
// large memory cache.

core/state/journal.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,21 @@ func (j *journal) length() int {
8585
return len(j.entries)
8686
}
8787

88+
func (j *journal) createContract(addr common.Address) {
89+
j.append(createContractChange{account: addr})
90+
}
91+
8892
type (
8993
// Changes to the account trie.
9094
createObjectChange struct {
9195
account common.Address
9296
}
97+
// createContractChange represents an account becoming a contract-account.
98+
// This event happens prior to executing initcode. The journal-event simply
99+
// manages the created-flag, in order to allow same-tx destruction.
100+
createContractChange struct {
101+
account common.Address
102+
}
93103
resetObjectChange struct {
94104
account common.Address
95105
prev *stateObject
@@ -156,6 +166,14 @@ func (ch createObjectChange) dirtied() *common.Address {
156166
return &ch.account
157167
}
158168

169+
func (ch createContractChange) revert(s *StateDB) {
170+
s.getStateObject(ch.account).created = false
171+
}
172+
173+
func (ch createContractChange) dirtied() *common.Address {
174+
return nil
175+
}
176+
159177
func (ch resetObjectChange) revert(s *StateDB) {
160178
s.setStateObject(ch.prev)
161179
if !ch.prevdestruct {

core/state/state_object.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"time"
2626

2727
"github.com/XinFinOrg/XDPoSChain/common"
28-
"github.com/XinFinOrg/XDPoSChain/core/tracing"
2928
"github.com/XinFinOrg/XDPoSChain/core/types"
3029
"github.com/XinFinOrg/XDPoSChain/crypto"
3130
"github.com/XinFinOrg/XDPoSChain/rlp"
@@ -218,22 +217,21 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
218217
}
219218

220219
// SetState updates a value in account storage.
221-
func (s *stateObject) SetState(db Database, key, value common.Hash) {
222-
// If the new value is the same as old, don't set
220+
func (s *stateObject) SetState(db Database, key, value common.Hash) common.Hash {
221+
// If the new value is the same as old, don't set. Otherwise, track only the
222+
// dirty changes, supporting reverting all of it back to no change.
223223
prev := s.GetState(db, key)
224224
if prev == value {
225-
return
225+
return prev
226226
}
227227
// New value is different, update and journal the change
228228
s.db.journal.append(storageChange{
229229
account: s.address,
230230
key: key,
231231
prevalue: prev,
232232
})
233-
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
234-
s.db.logger.OnStorageChange(s.address, key, prev, value)
235-
}
236233
s.setState(key, value)
234+
return prev
237235
}
238236

239237
func (s *stateObject) setState(key, value common.Hash) {
@@ -340,36 +338,28 @@ func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) {
340338

341339
// AddBalance adds amount to s's balance.
342340
// It is used to add funds to the destination account of a transfer.
343-
func (s *stateObject) AddBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
341+
// returns the previous balance
342+
func (s *stateObject) AddBalance(amount *big.Int) *big.Int {
344343
// EIP161: We must check emptiness for the objects such that the account
345344
// clearing (0,0,0 objects) can take effect.
346345
if amount.Sign() == 0 {
347346
if s.empty() {
348347
s.touch()
349348
}
350-
return
349+
return new(big.Int).Set(s.Balance())
351350
}
352-
s.SetBalance(new(big.Int).Add(s.Balance(), amount), reason)
351+
return s.SetBalance(new(big.Int).Add(s.Balance(), amount))
353352
}
354353

355-
// SubBalance removes amount from s's balance.
356-
// It is used to remove funds from the origin account of a transfer.
357-
func (s *stateObject) SubBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
358-
if amount.Sign() == 0 {
359-
return
360-
}
361-
s.SetBalance(new(big.Int).Sub(s.Balance(), amount), reason)
362-
}
363-
364-
func (s *stateObject) SetBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
354+
// SetBalance sets the balance for the object, and returns the previous balance.
355+
func (s *stateObject) SetBalance(amount *big.Int) *big.Int {
356+
prev := new(big.Int).Set(s.data.Balance)
365357
s.db.journal.append(balanceChange{
366358
account: s.address,
367359
prev: new(big.Int).Set(s.data.Balance),
368360
})
369-
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
370-
s.db.logger.OnBalanceChange(s.address, s.Balance(), amount, reason)
371-
}
372361
s.setBalance(amount)
362+
return prev
373363
}
374364

375365
func (s *stateObject) setBalance(amount *big.Int) {
@@ -439,9 +429,6 @@ func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
439429
account: s.address,
440430
prevCode: prevCode,
441431
})
442-
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
443-
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), prevCode, codeHash, code)
444-
}
445432
s.setCode(codeHash, code)
446433
}
447434

@@ -456,9 +443,6 @@ func (s *stateObject) SetNonce(nonce uint64) {
456443
account: s.address,
457444
prev: s.data.Nonce,
458445
})
459-
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
460-
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
461-
}
462446
s.setNonce(nonce)
463447
}
464448

core/state/state_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/XinFinOrg/XDPoSChain/common"
2626
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
27-
"github.com/XinFinOrg/XDPoSChain/core/tracing"
2827
"github.com/XinFinOrg/XDPoSChain/core/types"
2928
"github.com/XinFinOrg/XDPoSChain/crypto"
3029
"github.com/XinFinOrg/XDPoSChain/ethdb"
@@ -49,11 +48,11 @@ func TestDump(t *testing.T) {
4948

5049
// generate a few entries
5150
obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01}))
52-
obj1.AddBalance(big.NewInt(22), tracing.BalanceChangeUnspecified)
51+
obj1.AddBalance(big.NewInt(22))
5352
obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02}))
5453
obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
5554
obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02}))
56-
obj3.SetBalance(big.NewInt(44), tracing.BalanceChangeUnspecified)
55+
obj3.SetBalance(big.NewInt(44))
5756

5857
// write some of them to the trie
5958
s.state.updateStateObject(obj1)
@@ -101,13 +100,13 @@ func TestIterativeDump(t *testing.T) {
101100

102101
// generate a few entries
103102
obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01}))
104-
obj1.AddBalance(big.NewInt(22), tracing.BalanceChangeUnspecified)
103+
obj1.AddBalance(big.NewInt(22))
105104
obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02}))
106105
obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
107106
obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02}))
108-
obj3.SetBalance(big.NewInt(44), tracing.BalanceChangeUnspecified)
107+
obj3.SetBalance(big.NewInt(44))
109108
obj4 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x00}))
110-
obj4.AddBalance(big.NewInt(1337), tracing.BalanceChangeUnspecified)
109+
obj4.AddBalance(big.NewInt(1337))
111110

112111
// write some of them to the trie
113112
s.state.updateStateObject(obj1)
@@ -203,7 +202,7 @@ func TestSnapshot2(t *testing.T) {
203202

204203
// db, trie are already non-empty values
205204
so0 := state.getStateObject(stateobjaddr0)
206-
so0.SetBalance(big.NewInt(42), tracing.BalanceChangeUnspecified)
205+
so0.SetBalance(big.NewInt(42))
207206
so0.SetNonce(43)
208207
so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
209208
so0.selfDestructed = false
@@ -215,7 +214,7 @@ func TestSnapshot2(t *testing.T) {
215214

216215
// and one with deleted == true
217216
so1 := state.getStateObject(stateobjaddr1)
218-
so1.SetBalance(big.NewInt(52), tracing.BalanceChangeUnspecified)
217+
so1.SetBalance(big.NewInt(52))
219218
so1.SetNonce(53)
220219
so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'})
221220
so1.selfDestructed = true

0 commit comments

Comments
 (0)