Skip to content

Commit

Permalink
ibs: store logs in array instead of map (#11977)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Sep 17, 2024
1 parent 293503e commit 85e2ac3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
29 changes: 19 additions & 10 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type IntraBlockState struct {
refund uint64

txIndex int
logs map[int][]*types.Log
logs []types.Logs
logSize uint

// Per-transaction access list
Expand All @@ -106,12 +106,12 @@ func New(stateReader StateReader) *IntraBlockState {
stateObjects: map[libcommon.Address]*stateObject{},
stateObjectsDirty: map[libcommon.Address]struct{}{},
nilAccounts: map[libcommon.Address]struct{}{},
logs: map[int][]*types.Log{},
logs: []types.Logs{},
journal: newJournal(),
accessList: newAccessList(),
transientStorage: newTransientStorage(),
balanceInc: map[libcommon.Address]*BalanceIncrease{},
txIndex: -1,
txIndex: 0,
//trace: true,
}
}
Expand Down Expand Up @@ -155,22 +155,28 @@ func (sdb *IntraBlockState) Reset() {
//clear(sdb.stateObjects)
sdb.stateObjectsDirty = make(map[libcommon.Address]struct{})
//clear(sdb.stateObjectsDirty)
sdb.logs = make(map[int][]*types.Log)
sdb.logs = sdb.logs[:0]
sdb.balanceInc = make(map[libcommon.Address]*BalanceIncrease)
//clear(sdb.balanceInc)
sdb.txIndex = -1
sdb.txIndex = 0
sdb.logSize = 0
}

func (sdb *IntraBlockState) AddLog(log2 *types.Log) {
sdb.journal.append(addLogChange{txIndex: sdb.txIndex})
log2.TxIndex = uint(sdb.txIndex)
log2.Index = sdb.logSize
sdb.logs[sdb.txIndex] = append(sdb.logs[sdb.txIndex], log2)
sdb.logSize++
for len(sdb.logs) <= sdb.txIndex {
sdb.logs = append(sdb.logs, nil)
}
sdb.logs[sdb.txIndex] = append(sdb.logs[sdb.txIndex], log2)
}

func (sdb *IntraBlockState) GetLogs(txIndex int, txnHash libcommon.Hash, blockNumber uint64, blockHash libcommon.Hash) []*types.Log {
func (sdb *IntraBlockState) GetLogs(txIndex int, txnHash libcommon.Hash, blockNumber uint64, blockHash libcommon.Hash) types.Logs {
if txIndex >= len(sdb.logs) {
return nil
}
logs := sdb.logs[txIndex]
for _, l := range logs {
l.TxHash = txnHash
Expand All @@ -182,12 +188,15 @@ func (sdb *IntraBlockState) GetLogs(txIndex int, txnHash libcommon.Hash, blockNu

// GetRawLogs - is like GetLogs, but allow postpone calculation of `txn.Hash()`.
// Example: if you need filter logs and only then set `txn.Hash()` for filtered logs - then no reason to calc for all transactions.
func (sdb *IntraBlockState) GetRawLogs(txIndex int) []*types.Log {
func (sdb *IntraBlockState) GetRawLogs(txIndex int) types.Logs {
if txIndex >= len(sdb.logs) {
return nil
}
return sdb.logs[txIndex]
}

func (sdb *IntraBlockState) Logs() []*types.Log {
var logs []*types.Log
func (sdb *IntraBlockState) Logs() types.Logs {
var logs types.Logs
for _, lgs := range sdb.logs {
logs = append(logs, lgs...)
}
Expand Down
9 changes: 4 additions & 5 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,10 @@ func (ch refundChange) dirtied() *libcommon.Address {
}

func (ch addLogChange) revert(s *IntraBlockState) {
logs := s.logs[ch.txIndex]
if len(logs) == 1 {
delete(s.logs, ch.txIndex)
} else {
s.logs[ch.txIndex] = logs[:len(logs)-1]
txnLogs := s.logs[ch.txIndex]
s.logs[ch.txIndex] = txnLogs[:len(txnLogs)-1] // revert 1 log
if len(s.logs[ch.txIndex]) == 0 {
s.logs = s.logs[:len(s.logs)-1] // revert txn
}
s.logSize--
}
Expand Down
1 change: 1 addition & 0 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func (t *StateTest) RunNoVerify(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Co
func MakePreState(rules *chain.Rules, tx kv.RwTx, accounts types.GenesisAlloc, blockNr uint64) (*state.IntraBlockState, error) {
r := rpchelper.NewLatestStateReader(tx)
statedb := state.New(r)
statedb.SetTxContext(0)
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
statedb.SetNonce(addr, a.Nonce)
Expand Down

0 comments on commit 85e2ac3

Please sign in to comment.