@@ -20,8 +20,7 @@ import (
20
20
"bytes"
21
21
"encoding/binary"
22
22
"fmt"
23
-
24
- "github.com/ledgerwatch/erigon-lib/kv/dbutils"
23
+ "sort"
25
24
26
25
"github.com/google/btree"
27
26
"github.com/holiman/uint256"
@@ -30,6 +29,7 @@ import (
30
29
libcommon "github.com/ledgerwatch/erigon-lib/common"
31
30
"github.com/ledgerwatch/erigon-lib/common/length"
32
31
"github.com/ledgerwatch/erigon-lib/kv"
32
+ "github.com/ledgerwatch/erigon-lib/kv/dbutils"
33
33
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
34
34
35
35
"github.com/ledgerwatch/erigon/core/state/historyv2read"
@@ -187,8 +187,23 @@ func (s *PlainState) ReadAccountData(address libcommon.Address) (*accounts.Accou
187
187
if fromHistory {
188
188
//restore codehash
189
189
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 () {
192
207
if codeHash , err1 := s .tx .GetOne (kv .PlainContractCode , dbutils .PlainGenerateStoragePrefix (address [:], a .Incarnation )); err1 == nil {
193
208
if len (codeHash ) > 0 {
194
209
a .CodeHash = libcommon .BytesToHash (codeHash )
0 commit comments