Skip to content

Commit c9eabea

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

File tree

24 files changed

+628
-166
lines changed

24 files changed

+628
-166
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
@@ -1775,7 +1775,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
17751775
if err != nil {
17761776
return it.index, events, coalescedLogs, err
17771777
}
1778-
statedb.SetLogger(bc.logger)
17791778

17801779
// If we have a followup block, run that against the current state to pre-cache
17811780
// 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
@@ -84,11 +84,21 @@ func (j *journal) length() int {
8484
return len(j.entries)
8585
}
8686

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

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

core/state/state_object.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"time"
2525

2626
"github.com/XinFinOrg/XDPoSChain/common"
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/rlp"
@@ -222,22 +221,21 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
222221
}
223222

224223
// SetState updates a value in account storage.
225-
func (s *stateObject) SetState(db Database, key, value common.Hash) {
226-
// If the new value is the same as old, don't set
224+
func (s *stateObject) SetState(db Database, key, value common.Hash) common.Hash {
225+
// If the new value is the same as old, don't set. Otherwise, track only the
226+
// dirty changes, supporting reverting all of it back to no change.
227227
prev := s.GetState(db, key)
228228
if prev == value {
229-
return
229+
return prev
230230
}
231231
// New value is different, update and journal the change
232232
s.db.journal.append(storageChange{
233233
account: &s.address,
234234
key: key,
235235
prevalue: prev,
236236
})
237-
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
238-
s.db.logger.OnStorageChange(s.address, key, prev, value)
239-
}
240237
s.setState(key, value)
238+
return prev
241239
}
242240

243241
func (s *stateObject) setState(key, value common.Hash) {
@@ -344,36 +342,28 @@ func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) {
344342

345343
// AddBalance adds amount to s's balance.
346344
// It is used to add funds to the destination account of a transfer.
347-
func (s *stateObject) AddBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
345+
// returns the previous balance
346+
func (s *stateObject) AddBalance(amount *big.Int) *big.Int {
348347
// EIP161: We must check emptiness for the objects such that the account
349348
// clearing (0,0,0 objects) can take effect.
350349
if amount.Sign() == 0 {
351350
if s.empty() {
352351
s.touch()
353352
}
354-
return
353+
return new(big.Int).Set(s.Balance())
355354
}
356-
s.SetBalance(new(big.Int).Add(s.Balance(), amount), reason)
355+
return s.SetBalance(new(big.Int).Add(s.Balance(), amount))
357356
}
358357

359-
// SubBalance removes amount from s's balance.
360-
// It is used to remove funds from the origin account of a transfer.
361-
func (s *stateObject) SubBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
362-
if amount.Sign() == 0 {
363-
return
364-
}
365-
s.SetBalance(new(big.Int).Sub(s.Balance(), amount), reason)
366-
}
367-
368-
func (s *stateObject) SetBalance(amount *big.Int, reason tracing.BalanceChangeReason) {
358+
// SetBalance sets the balance for the object, and returns the previous balance.
359+
func (s *stateObject) SetBalance(amount *big.Int) *big.Int {
360+
prev := new(big.Int).Set(s.data.Balance)
369361
s.db.journal.append(balanceChange{
370362
account: &s.address,
371-
prev: new(big.Int).Set(s.data.Balance),
363+
prev: new(big.Int).Set(prev),
372364
})
373-
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
374-
s.db.logger.OnBalanceChange(s.address, s.Balance(), amount, reason)
375-
}
376365
s.setBalance(amount)
366+
return prev
377367
}
378368

379369
func (s *stateObject) setBalance(amount *big.Int) {
@@ -444,9 +434,6 @@ func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
444434
prevhash: s.CodeHash(),
445435
prevcode: prevcode,
446436
})
447-
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
448-
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), prevcode, codeHash, code)
449-
}
450437
s.setCode(codeHash, code)
451438
}
452439

@@ -461,9 +448,6 @@ func (s *stateObject) SetNonce(nonce uint64) {
461448
account: &s.address,
462449
prev: s.data.Nonce,
463450
})
464-
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
465-
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
466-
}
467451
s.setNonce(nonce)
468452
}
469453

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)