Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 5e077e5

Browse files
authored
Check codehash changes synchronously (#251)
1 parent 9c603ab commit 5e077e5

File tree

2 files changed

+21
-48
lines changed

2 files changed

+21
-48
lines changed

pkg/modules/checks/codehash.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,26 @@ package checks
33
import (
44
"github.com/ethereum/go-ethereum/common"
55
"github.com/ethereum/go-ethereum/crypto"
6-
"golang.org/x/sync/errgroup"
76
)
87

98
type codeHash struct {
109
Address common.Address `json:"address"`
1110
Hash common.Hash `json:"hash"`
1211
}
1312

14-
func getCodeHashAsync(addr common.Address, gc GetCodeFunc, c chan codeHash) func() error {
15-
return func() error {
13+
func getCodeHashes(ic []common.Address, gc GetCodeFunc) ([]codeHash, error) {
14+
ret := []codeHash{}
15+
16+
for _, addr := range ic {
1617
bytecode, err := gc(addr)
1718
if err != nil {
18-
c <- codeHash{}
19-
return err
19+
return ret, err
2020
}
2121

22-
ch := codeHash{
22+
ret = append(ret, codeHash{
2323
Address: addr,
2424
Hash: crypto.Keccak256Hash(bytecode),
25-
}
26-
c <- ch
27-
return nil
28-
}
29-
}
30-
31-
func getCodeHashes(ic []common.Address, gc GetCodeFunc) ([]codeHash, error) {
32-
g := new(errgroup.Group)
33-
c := make(chan codeHash)
34-
ret := []codeHash{}
35-
36-
for _, addr := range ic {
37-
g.Go(getCodeHashAsync(addr, gc, c))
38-
}
39-
for range ic {
40-
ret = append(ret, <-c)
41-
}
42-
if err := g.Wait(); err != nil {
43-
return ret, err
25+
})
4426
}
4527

4628
return ret, nil

pkg/modules/checks/standalone.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package checks
44

55
import (
66
"math/big"
7-
"sync"
87
"time"
98

109
"github.com/dgraph-io/badger/v3"
@@ -137,32 +136,24 @@ func (s *Standalone) SimulateOp() modules.UserOpHandlerFunc {
137136
func (s *Standalone) CodeHashes() modules.BatchHandlerFunc {
138137
return func(ctx *modules.BatchHandlerCtx) error {
139138
gc := getCodeWithEthClient(s.eth)
140-
g := new(errgroup.Group)
141-
mu := &sync.Mutex{}
142-
fn := func(i int, op *userop.UserOperation) func() error {
143-
return func() error {
144-
chs, err := getSavedCodeHashes(s.db, op.GetUserOpHash(ctx.EntryPoint, ctx.ChainID))
145-
if err != nil {
146-
return err
147-
}
148139

149-
changed, err := hasCodeHashChanges(chs, gc)
150-
if err != nil {
151-
return err
152-
}
153-
if changed {
154-
mu.Lock()
155-
ctx.MarkOpIndexForRemoval(i)
156-
mu.Unlock()
157-
}
158-
return nil
140+
end := len(ctx.Batch) - 1
141+
for i := end; i >= 0; i-- {
142+
op := ctx.Batch[i]
143+
chs, err := getSavedCodeHashes(s.db, op.GetUserOpHash(ctx.EntryPoint, ctx.ChainID))
144+
if err != nil {
145+
return err
159146
}
160-
}
161147

162-
for i, op := range ctx.Batch {
163-
g.Go(fn(i, op))
148+
changed, err := hasCodeHashChanges(chs, gc)
149+
if err != nil {
150+
return err
151+
}
152+
if changed {
153+
ctx.MarkOpIndexForRemoval(i)
154+
}
164155
}
165-
return g.Wait()
156+
return nil
166157
}
167158
}
168159

0 commit comments

Comments
 (0)