mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
core/state: polish code
This commit is contained in:
parent
d6ee4d95d2
commit
542b83ce19
3 changed files with 51 additions and 47 deletions
|
|
@ -63,26 +63,26 @@ func newJournal() *journal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset clears the journal, after this operation the journal can be used
|
// reset clears the journal, after this operation the journal can be used
|
||||||
// anew. It is semantically similar to calling 'newJournal', but the underlying
|
// as new. It is semantically similar to calling 'newJournal', but the underlying
|
||||||
// slices can be reused
|
// slices can be reused.
|
||||||
func (j *journal) Reset() {
|
func (j *journal) reset() {
|
||||||
j.entries = j.entries[:0]
|
j.entries = j.entries[:0]
|
||||||
j.validRevisions = j.validRevisions[:0]
|
j.validRevisions = j.validRevisions[:0]
|
||||||
clear(j.dirties)
|
clear(j.dirties)
|
||||||
j.nextRevisionId = 0
|
j.nextRevisionId = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Snapshot returns an identifier for the current revision of the state.
|
// snapshot returns an identifier for the current revision of the state.
|
||||||
func (j *journal) Snapshot() int {
|
func (j *journal) snapshot() int {
|
||||||
id := j.nextRevisionId
|
id := j.nextRevisionId
|
||||||
j.nextRevisionId++
|
j.nextRevisionId++
|
||||||
j.validRevisions = append(j.validRevisions, revision{id, j.length()})
|
j.validRevisions = append(j.validRevisions, revision{id, j.length()})
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertToSnapshot reverts all state changes made since the given revision.
|
// revertToSnapshot reverts all state changes made since the given revision.
|
||||||
func (j *journal) RevertToSnapshot(revid int, s *StateDB) {
|
func (j *journal) revertToSnapshot(revid int, s *StateDB) {
|
||||||
// Find the snapshot in the stack of valid snapshots.
|
// Find the snapshot in the stack of valid snapshots.
|
||||||
idx := sort.Search(len(j.validRevisions), func(i int) bool {
|
idx := sort.Search(len(j.validRevisions), func(i int) bool {
|
||||||
return j.validRevisions[i].id >= revid
|
return j.validRevisions[i].id >= revid
|
||||||
|
|
@ -148,30 +148,23 @@ func (j *journal) copy() *journal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) AccessListAddAccount(addr common.Address) {
|
func (j *journal) logChange(txHash common.Hash) {
|
||||||
j.append(accessListAddAccountChange{&addr})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *journal) AccessListAddSlot(addr common.Address, slot common.Hash) {
|
|
||||||
j.append(accessListAddSlotChange{
|
|
||||||
address: &addr,
|
|
||||||
slot: &slot,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *journal) Log(txHash common.Hash) {
|
|
||||||
j.append(addLogChange{txhash: txHash})
|
j.append(addLogChange{txhash: txHash})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) Create(addr common.Address) {
|
func (j *journal) createObject(addr common.Address) {
|
||||||
j.append(createObjectChange{account: &addr})
|
j.append(createObjectChange{account: &addr})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) Destruct(addr common.Address) {
|
func (j *journal) createContract(addr common.Address) {
|
||||||
|
j.append(createContractChange{account: addr})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *journal) destruct(addr common.Address) {
|
||||||
j.append(selfDestructChange{account: &addr})
|
j.append(selfDestructChange{account: &addr})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) SetStorage(addr common.Address, key, prev, origin common.Hash) {
|
func (j *journal) storageChange(addr common.Address, key, prev, origin common.Hash) {
|
||||||
j.append(storageChange{
|
j.append(storageChange{
|
||||||
account: &addr,
|
account: &addr,
|
||||||
key: key,
|
key: key,
|
||||||
|
|
@ -180,7 +173,7 @@ func (j *journal) SetStorage(addr common.Address, key, prev, origin common.Hash)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) SetTransientState(addr common.Address, key, prev common.Hash) {
|
func (j *journal) transientStateChange(addr common.Address, key, prev common.Hash) {
|
||||||
j.append(transientStorageChange{
|
j.append(transientStorageChange{
|
||||||
account: &addr,
|
account: &addr,
|
||||||
key: key,
|
key: key,
|
||||||
|
|
@ -188,29 +181,29 @@ func (j *journal) SetTransientState(addr common.Address, key, prev common.Hash)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) RefundChange(previous uint64) {
|
func (j *journal) refundChange(previous uint64) {
|
||||||
j.append(refundChange{prev: previous})
|
j.append(refundChange{prev: previous})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) BalanceChange(addr common.Address, previous *uint256.Int) {
|
func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) {
|
||||||
j.append(balanceChange{
|
j.append(balanceChange{
|
||||||
account: &addr,
|
account: &addr,
|
||||||
prev: previous.Clone(),
|
prev: previous.Clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) SetCode(address common.Address) {
|
func (j *journal) codeChange(address common.Address) {
|
||||||
j.append(codeChange{account: &address})
|
j.append(codeChange{account: &address})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) NonceChange(address common.Address, prev uint64) {
|
func (j *journal) nonceChange(address common.Address, prev uint64) {
|
||||||
j.append(nonceChange{
|
j.append(nonceChange{
|
||||||
account: &address,
|
account: &address,
|
||||||
prev: prev,
|
prev: prev,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) Touch(address common.Address) {
|
func (j *journal) touchChange(address common.Address) {
|
||||||
j.append(touchChange{
|
j.append(touchChange{
|
||||||
account: &address,
|
account: &address,
|
||||||
})
|
})
|
||||||
|
|
@ -221,6 +214,17 @@ func (j *journal) Touch(address common.Address) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j *journal) accessListAddAccount(addr common.Address) {
|
||||||
|
j.append(accessListAddAccountChange{&addr})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *journal) accessListAddSlot(addr common.Address, slot common.Hash) {
|
||||||
|
j.append(accessListAddSlotChange{
|
||||||
|
address: &addr,
|
||||||
|
slot: &slot,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Changes to the account trie.
|
// Changes to the account trie.
|
||||||
createObjectChange struct {
|
createObjectChange struct {
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ func (s *stateObject) markSelfdestructed() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateObject) touch() {
|
func (s *stateObject) touch() {
|
||||||
s.db.journal.Touch(s.address)
|
s.db.journal.touchChange(s.address)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getTrie returns the associated storage trie. The trie will be opened if it's
|
// getTrie returns the associated storage trie. The trie will be opened if it's
|
||||||
|
|
@ -244,7 +244,7 @@ func (s *stateObject) SetState(key, value common.Hash) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// New value is different, update and journal the change
|
// New value is different, update and journal the change
|
||||||
s.db.journal.SetStorage(s.address, key, prev, origin)
|
s.db.journal.storageChange(s.address, key, prev, origin)
|
||||||
s.setState(key, value, origin)
|
s.setState(key, value, origin)
|
||||||
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
|
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
|
||||||
s.db.logger.OnStorageChange(s.address, key, prev, value)
|
s.db.logger.OnStorageChange(s.address, key, prev, value)
|
||||||
|
|
@ -498,7 +498,7 @@ func (s *stateObject) SubBalance(amount *uint256.Int, reason tracing.BalanceChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateObject) SetBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
|
func (s *stateObject) SetBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
|
||||||
s.db.journal.BalanceChange(s.address, s.data.Balance)
|
s.db.journal.balanceChange(s.address, s.data.Balance)
|
||||||
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
|
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
|
||||||
s.db.logger.OnBalanceChange(s.address, s.Balance().ToBig(), amount.ToBig(), reason)
|
s.db.logger.OnBalanceChange(s.address, s.Balance().ToBig(), amount.ToBig(), reason)
|
||||||
}
|
}
|
||||||
|
|
@ -574,7 +574,7 @@ func (s *stateObject) CodeSize() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
||||||
s.db.journal.SetCode(s.address)
|
s.db.journal.codeChange(s.address)
|
||||||
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
|
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
|
||||||
// TODO remove prevcode from this callback
|
// TODO remove prevcode from this callback
|
||||||
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)
|
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)
|
||||||
|
|
@ -589,7 +589,7 @@ func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stateObject) SetNonce(nonce uint64) {
|
func (s *stateObject) SetNonce(nonce uint64) {
|
||||||
s.db.journal.NonceChange(s.address, s.data.Nonce)
|
s.db.journal.nonceChange(s.address, s.data.Nonce)
|
||||||
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
|
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
|
||||||
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
|
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -248,7 +248,7 @@ func (s *StateDB) Error() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StateDB) AddLog(log *types.Log) {
|
func (s *StateDB) AddLog(log *types.Log) {
|
||||||
s.journal.Log(s.thash)
|
s.journal.logChange(s.thash)
|
||||||
|
|
||||||
log.TxHash = s.thash
|
log.TxHash = s.thash
|
||||||
log.TxIndex = uint(s.txIndex)
|
log.TxIndex = uint(s.txIndex)
|
||||||
|
|
@ -293,14 +293,14 @@ func (s *StateDB) Preimages() map[common.Hash][]byte {
|
||||||
|
|
||||||
// AddRefund adds gas to the refund counter
|
// AddRefund adds gas to the refund counter
|
||||||
func (s *StateDB) AddRefund(gas uint64) {
|
func (s *StateDB) AddRefund(gas uint64) {
|
||||||
s.journal.RefundChange(s.refund)
|
s.journal.refundChange(s.refund)
|
||||||
s.refund += gas
|
s.refund += gas
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubRefund removes gas from the refund counter.
|
// SubRefund removes gas from the refund counter.
|
||||||
// This method will panic if the refund counter goes below zero
|
// This method will panic if the refund counter goes below zero
|
||||||
func (s *StateDB) SubRefund(gas uint64) {
|
func (s *StateDB) SubRefund(gas uint64) {
|
||||||
s.journal.RefundChange(s.refund)
|
s.journal.refundChange(s.refund)
|
||||||
if gas > s.refund {
|
if gas > s.refund {
|
||||||
panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
|
panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
|
||||||
}
|
}
|
||||||
|
|
@ -508,7 +508,7 @@ func (s *StateDB) SelfDestruct(addr common.Address) {
|
||||||
// If it is already marked as self-destructed, we do not need to add it
|
// If it is already marked as self-destructed, we do not need to add it
|
||||||
// for journalling a second time.
|
// for journalling a second time.
|
||||||
if !stateObject.selfDestructed {
|
if !stateObject.selfDestructed {
|
||||||
s.journal.Destruct(addr)
|
s.journal.destruct(addr)
|
||||||
stateObject.markSelfdestructed()
|
stateObject.markSelfdestructed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -531,7 +531,7 @@ func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash)
|
||||||
if prev == value {
|
if prev == value {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.journal.SetTransientState(addr, key, prev)
|
s.journal.transientStateChange(addr, key, prev)
|
||||||
s.setTransientState(addr, key, value)
|
s.setTransientState(addr, key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -650,7 +650,7 @@ func (s *StateDB) getOrNewStateObject(addr common.Address) *stateObject {
|
||||||
// existing account with the given address, otherwise it will be silently overwritten.
|
// existing account with the given address, otherwise it will be silently overwritten.
|
||||||
func (s *StateDB) createObject(addr common.Address) *stateObject {
|
func (s *StateDB) createObject(addr common.Address) *stateObject {
|
||||||
obj := newObject(s, addr, nil)
|
obj := newObject(s, addr, nil)
|
||||||
s.journal.Create(addr)
|
s.journal.createObject(addr)
|
||||||
s.setStateObject(obj)
|
s.setStateObject(obj)
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
@ -672,7 +672,7 @@ func (s *StateDB) CreateContract(addr common.Address) {
|
||||||
obj := s.getStateObject(addr)
|
obj := s.getStateObject(addr)
|
||||||
if !obj.newContract {
|
if !obj.newContract {
|
||||||
obj.newContract = true
|
obj.newContract = true
|
||||||
s.journal.append(createContractChange{account: addr})
|
s.journal.createContract(addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -741,12 +741,12 @@ func (s *StateDB) Copy() *StateDB {
|
||||||
|
|
||||||
// Snapshot returns an identifier for the current revision of the state.
|
// Snapshot returns an identifier for the current revision of the state.
|
||||||
func (s *StateDB) Snapshot() int {
|
func (s *StateDB) Snapshot() int {
|
||||||
return s.journal.Snapshot()
|
return s.journal.snapshot()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertToSnapshot reverts all state changes made since the given revision.
|
// RevertToSnapshot reverts all state changes made since the given revision.
|
||||||
func (s *StateDB) RevertToSnapshot(revid int) {
|
func (s *StateDB) RevertToSnapshot(revid int) {
|
||||||
s.journal.RevertToSnapshot(revid, s)
|
s.journal.revertToSnapshot(revid, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRefund returns the current value of the refund counter.
|
// GetRefund returns the current value of the refund counter.
|
||||||
|
|
@ -960,7 +960,7 @@ func (s *StateDB) SetTxContext(thash common.Hash, ti int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StateDB) clearJournalAndRefund() {
|
func (s *StateDB) clearJournalAndRefund() {
|
||||||
s.journal.Reset()
|
s.journal.reset()
|
||||||
s.refund = 0
|
s.refund = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1387,7 +1387,7 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
|
||||||
// AddAddressToAccessList adds the given address to the access list
|
// AddAddressToAccessList adds the given address to the access list
|
||||||
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
|
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
|
||||||
if s.accessList.AddAddress(addr) {
|
if s.accessList.AddAddress(addr) {
|
||||||
s.journal.AccessListAddAccount(addr)
|
s.journal.accessListAddAccount(addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1399,10 +1399,10 @@ func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
|
||||||
// scope of 'address' without having the 'address' become already added
|
// scope of 'address' without having the 'address' become already added
|
||||||
// to the access list (via call-variant, create, etc).
|
// to the access list (via call-variant, create, etc).
|
||||||
// Better safe than sorry, though
|
// Better safe than sorry, though
|
||||||
s.journal.AccessListAddAccount(addr)
|
s.journal.accessListAddAccount(addr)
|
||||||
}
|
}
|
||||||
if slotMod {
|
if slotMod {
|
||||||
s.journal.AccessListAddSlot(addr, slot)
|
s.journal.accessListAddSlot(addr, slot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue