Skip to content

Commit

Permalink
Fix codehash override index error (#11959)
Browse files Browse the repository at this point in the history
`sort.Search` returns 0 for when a valid record in
`systemContractLookup` was not found, and when we subtract 1 from it, it
leads to an invalid index and a crash.

Fixes #11936
  • Loading branch information
shohamc1 authored Sep 16, 2024
1 parent 2118fbf commit ec969d5
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions core/state/plain_readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
"bytes"
"encoding/binary"
"fmt"

"github.com/ledgerwatch/erigon-lib/kv/dbutils"
"sort"

"github.com/google/btree"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/core/state/historyv2read"
"github.com/ledgerwatch/erigon/core/types/accounts"
Expand Down Expand Up @@ -186,10 +187,7 @@ func (s *PlainState) ReadAccountData(address libcommon.Address) (*accounts.Accou
if fromHistory {
//restore codehash
if records, ok := s.systemContractLookup[address]; ok {
p := sort.Search(len(records), func(i int) bool {
return records[i].BlockNumber > s.blockNr
})
a.CodeHash = records[p-1].CodeHash
a.CodeHash = records[len(records)-1].CodeHash
} else if a.Incarnation > 0 && a.IsEmptyCodeHash() {
if codeHash, err1 := s.tx.GetOne(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], a.Incarnation)); err1 == nil {
if len(codeHash) > 0 {
Expand Down

0 comments on commit ec969d5

Please sign in to comment.