mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-04 05:58:40 +00:00
core/state: address comments from marius
This commit is contained in:
parent
394fe783c6
commit
7088fe5956
3 changed files with 33 additions and 79 deletions
|
|
@ -35,24 +35,22 @@ type revision struct {
|
||||||
type journalMutationKind uint8
|
type journalMutationKind uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
journalMutationKindTouch journalMutationKind = iota + 1
|
// journalMutationKindNone is the zero value returned by mutation() for
|
||||||
|
// entries that don't carry a tracked account mutation. The accompanying
|
||||||
|
// bool is false in that case; callers must gate on it before using the
|
||||||
|
// kind.
|
||||||
|
journalMutationKindNone journalMutationKind = iota
|
||||||
|
journalMutationKindTouch
|
||||||
journalMutationKindCreate
|
journalMutationKindCreate
|
||||||
journalMutationKindSelfDestruct
|
journalMutationKindSelfDestruct
|
||||||
journalMutationKindBalance
|
journalMutationKindBalance
|
||||||
journalMutationKindNonce
|
journalMutationKindNonce
|
||||||
journalMutationKindCode
|
journalMutationKindCode
|
||||||
journalMutationKindStorage
|
journalMutationKindStorage
|
||||||
|
journalMutationKindCount // sentinel, must stay last
|
||||||
)
|
)
|
||||||
|
|
||||||
type journalMutationCounts struct {
|
type journalMutationCounts [journalMutationKindCount]int
|
||||||
touch int
|
|
||||||
create int
|
|
||||||
selfDestruct int
|
|
||||||
balance int
|
|
||||||
nonce int
|
|
||||||
code int
|
|
||||||
storage int
|
|
||||||
}
|
|
||||||
|
|
||||||
// journalMutationState tracks, per account, both the per-kind count of mutation
|
// journalMutationState tracks, per account, both the per-kind count of mutation
|
||||||
// entries currently present in the journal and the pre-tx value of each
|
// entries currently present in the journal and the pre-tx value of each
|
||||||
|
|
@ -116,49 +114,12 @@ func (s *journalMutationState) copy() *journalMutationState {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *journalMutationCounts) add(kind journalMutationKind) {
|
func (c *journalMutationCounts) add(kind journalMutationKind) {
|
||||||
switch kind {
|
c[kind]++
|
||||||
case journalMutationKindTouch:
|
|
||||||
c.touch++
|
|
||||||
case journalMutationKindCreate:
|
|
||||||
c.create++
|
|
||||||
case journalMutationKindSelfDestruct:
|
|
||||||
c.selfDestruct++
|
|
||||||
case journalMutationKindBalance:
|
|
||||||
c.balance++
|
|
||||||
case journalMutationKindNonce:
|
|
||||||
c.nonce++
|
|
||||||
case journalMutationKindCode:
|
|
||||||
c.code++
|
|
||||||
case journalMutationKindStorage:
|
|
||||||
c.storage++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *journalMutationCounts) remove(kind journalMutationKind) bool {
|
func (c *journalMutationCounts) remove(kind journalMutationKind) bool {
|
||||||
switch kind {
|
c[kind]--
|
||||||
case journalMutationKindTouch:
|
return c[kind] == 0
|
||||||
c.touch--
|
|
||||||
return c.touch == 0
|
|
||||||
case journalMutationKindCreate:
|
|
||||||
c.create--
|
|
||||||
return c.create == 0
|
|
||||||
case journalMutationKindSelfDestruct:
|
|
||||||
c.selfDestruct--
|
|
||||||
return c.selfDestruct == 0
|
|
||||||
case journalMutationKindBalance:
|
|
||||||
c.balance--
|
|
||||||
return c.balance == 0
|
|
||||||
case journalMutationKindNonce:
|
|
||||||
c.nonce--
|
|
||||||
return c.nonce == 0
|
|
||||||
case journalMutationKindCode:
|
|
||||||
c.code--
|
|
||||||
return c.code == 0
|
|
||||||
case journalMutationKindStorage:
|
|
||||||
c.storage--
|
|
||||||
return c.storage == 0
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// journalEntry is a modification entry in the state change journal that can be
|
// journalEntry is a modification entry in the state change journal that can be
|
||||||
|
|
@ -335,25 +296,18 @@ func (j *journal) copy() *journal {
|
||||||
for i := 0; i < j.length(); i++ {
|
for i := 0; i < j.length(); i++ {
|
||||||
entries = append(entries, j.entries[i].copy())
|
entries = append(entries, j.entries[i].copy())
|
||||||
}
|
}
|
||||||
|
mutations := make(map[common.Address]*journalMutationState, len(j.mutations))
|
||||||
|
for addr, state := range j.mutations {
|
||||||
|
mutations[addr] = state.copy()
|
||||||
|
}
|
||||||
return &journal{
|
return &journal{
|
||||||
entries: entries,
|
entries: entries,
|
||||||
mutations: copyMutationStates(j.mutations),
|
mutations: mutations,
|
||||||
validRevisions: slices.Clone(j.validRevisions),
|
validRevisions: slices.Clone(j.validRevisions),
|
||||||
nextRevisionId: j.nextRevisionId,
|
nextRevisionId: j.nextRevisionId,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyMutationStates(src map[common.Address]*journalMutationState) map[common.Address]*journalMutationState {
|
|
||||||
if src == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
dst := make(map[common.Address]*journalMutationState, len(src))
|
|
||||||
for addr, state := range src {
|
|
||||||
dst[addr] = state.copy()
|
|
||||||
}
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *journal) logChange(txHash common.Hash) {
|
func (j *journal) logChange(txHash common.Hash) {
|
||||||
j.append(addLogChange{txhash: txHash})
|
j.append(addLogChange{txhash: txHash})
|
||||||
}
|
}
|
||||||
|
|
@ -528,7 +482,7 @@ func (ch createContractChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch createContractChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch createContractChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch createContractChange) copy() journalEntry {
|
func (ch createContractChange) copy() journalEntry {
|
||||||
|
|
@ -636,7 +590,7 @@ func (ch transientStorageChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch transientStorageChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch transientStorageChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch transientStorageChange) copy() journalEntry {
|
func (ch transientStorageChange) copy() journalEntry {
|
||||||
|
|
@ -652,7 +606,7 @@ func (ch refundChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch refundChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch refundChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch refundChange) copy() journalEntry {
|
func (ch refundChange) copy() journalEntry {
|
||||||
|
|
@ -672,7 +626,7 @@ func (ch addLogChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch addLogChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch addLogChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch addLogChange) copy() journalEntry {
|
func (ch addLogChange) copy() journalEntry {
|
||||||
|
|
@ -695,7 +649,7 @@ func (ch accessListAddAccountChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch accessListAddAccountChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch accessListAddAccountChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch accessListAddAccountChange) copy() journalEntry {
|
func (ch accessListAddAccountChange) copy() journalEntry {
|
||||||
|
|
@ -709,7 +663,7 @@ func (ch accessListAddSlotChange) revert(s *StateDB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch accessListAddSlotChange) mutation() (common.Address, journalMutationKind, bool) {
|
func (ch accessListAddSlotChange) mutation() (common.Address, journalMutationKind, bool) {
|
||||||
return common.Address{}, 0, false
|
return common.Address{}, journalMutationKindNone, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch accessListAddSlotChange) copy() journalEntry {
|
func (ch accessListAddSlotChange) copy() journalEntry {
|
||||||
|
|
|
||||||
|
|
@ -75,17 +75,17 @@ func checkJournalInvariants(t *testing.T, j *journal) {
|
||||||
t.Fatalf("addr %x: counts=%+v want=%+v", addr, state.counts, *want)
|
t.Fatalf("addr %x: counts=%+v want=%+v", addr, state.counts, *want)
|
||||||
}
|
}
|
||||||
// First-touch *Set flags must mirror the live per-kind counts.
|
// First-touch *Set flags must mirror the live per-kind counts.
|
||||||
if state.balanceSet != (want.balance > 0) {
|
if state.balanceSet != (want[journalMutationKindBalance] > 0) {
|
||||||
t.Fatalf("addr %x: balanceSet=%v want=%v (balance count=%d)",
|
t.Fatalf("addr %x: balanceSet=%v want=%v (balance count=%d)",
|
||||||
addr, state.balanceSet, want.balance > 0, want.balance)
|
addr, state.balanceSet, want[journalMutationKindBalance] > 0, want[journalMutationKindBalance])
|
||||||
}
|
}
|
||||||
if state.nonceSet != (want.nonce > 0) {
|
if state.nonceSet != (want[journalMutationKindNonce] > 0) {
|
||||||
t.Fatalf("addr %x: nonceSet=%v want=%v (nonce count=%d)",
|
t.Fatalf("addr %x: nonceSet=%v want=%v (nonce count=%d)",
|
||||||
addr, state.nonceSet, want.nonce > 0, want.nonce)
|
addr, state.nonceSet, want[journalMutationKindNonce] > 0, want[journalMutationKindNonce])
|
||||||
}
|
}
|
||||||
if state.codeSet != (want.code > 0) {
|
if state.codeSet != (want[journalMutationKindCode] > 0) {
|
||||||
t.Fatalf("addr %x: codeSet=%v want=%v (code count=%d)",
|
t.Fatalf("addr %x: codeSet=%v want=%v (code count=%d)",
|
||||||
addr, state.codeSet, want.code > 0, want.code)
|
addr, state.codeSet, want[journalMutationKindCode] > 0, want[journalMutationKindCode])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -720,11 +720,11 @@ func TestJournalMutationTracking(t *testing.T) {
|
||||||
state.SetState(addr, key, common.Hash{0x3})
|
state.SetState(addr, key, common.Hash{0x3})
|
||||||
|
|
||||||
want := journalMutationCounts{
|
want := journalMutationCounts{
|
||||||
create: 1,
|
journalMutationKindCreate: 1,
|
||||||
balance: 1,
|
journalMutationKindBalance: 1,
|
||||||
nonce: 1,
|
journalMutationKindNonce: 1,
|
||||||
code: 1,
|
journalMutationKindCode: 1,
|
||||||
storage: 1,
|
journalMutationKindStorage: 1,
|
||||||
}
|
}
|
||||||
checkCounts := func(got *journalMutationState, label string) {
|
checkCounts := func(got *journalMutationState, label string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue