@@ -20,8 +20,7 @@ import (
2020 "bytes"
2121 "encoding/binary"
2222 "fmt"
23-
24- "github.com/ledgerwatch/erigon-lib/kv/dbutils"
23+ "sort"
2524
2625 "github.com/google/btree"
2726 "github.com/holiman/uint256"
@@ -30,6 +29,7 @@ import (
3029 libcommon "github.com/ledgerwatch/erigon-lib/common"
3130 "github.com/ledgerwatch/erigon-lib/common/length"
3231 "github.com/ledgerwatch/erigon-lib/kv"
32+ "github.com/ledgerwatch/erigon-lib/kv/dbutils"
3333 "github.com/ledgerwatch/erigon-lib/kv/kvcfg"
3434
3535 "github.com/ledgerwatch/erigon/core/state/historyv2read"
@@ -187,8 +187,23 @@ func (s *PlainState) ReadAccountData(address libcommon.Address) (*accounts.Accou
187187 if fromHistory {
188188 //restore codehash
189189 if records , ok := s .systemContractLookup [address ]; ok {
190- a .CodeHash = records [len (records )- 1 ].CodeHash
191- } else if a .Incarnation > 0 && a .IsEmptyCodeHash () {
190+ // Find the first system contract record after the block number
191+ p := sort .Search (len (records ), func (i int ) bool {
192+ return records [i ].BlockNumber > s .blockNr
193+ })
194+ if p > 0 {
195+ a .CodeHash = records [p - 1 ].CodeHash
196+ if s .trace {
197+ fmt .Printf ("ReadAccountData [%x] => (systemContractLookup) [nonce: %d, balance: %d, codeHash: %x]\n " , address , a .Nonce , & a .Balance , a .CodeHash )
198+ }
199+ return & a , nil
200+ }
201+ // All system contract records are after the block number
202+ if s .trace {
203+ fmt .Printf ("ReadAccountData [%x] systemContract was created after %d, at %d\n " , address , s .blockNr , records [0 ].BlockNumber )
204+ }
205+ }
206+ if a .Incarnation > 0 && a .IsEmptyCodeHash () {
192207 if codeHash , err1 := s .tx .GetOne (kv .PlainContractCode , dbutils .PlainGenerateStoragePrefix (address [:], a .Incarnation )); err1 == nil {
193208 if len (codeHash ) > 0 {
194209 a .CodeHash = libcommon .BytesToHash (codeHash )
0 commit comments