Skip to content

Commit 63e8dcd

Browse files
committed
test: StateDB propagation
1 parent 5ca8376 commit 63e8dcd

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

libevm/precompiles/parallel/parallel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646
type Handler[Result any] interface {
4747
BeforeBlock(*types.Header)
4848
Gas(*types.Transaction) (gas uint64, process bool)
49-
Process(index int, tx *types.Transaction, sdb libevm.StateReader) Result
49+
Process(sdb libevm.StateReader, index int, tx *types.Transaction) Result
5050
}
5151

5252
// A Processor orchestrates dispatch and collection of results from a [Handler].
@@ -133,7 +133,7 @@ func (p *Processor[R]) worker() {
133133
return
134134
}
135135

136-
r := p.handler.Process(w.index, w.tx, sdb)
136+
r := p.handler.Process(sdb, w.index, w.tx)
137137
p.results[w.index] <- result[R]{
138138
tx: w.tx.Hash(),
139139
val: &r,

libevm/precompiles/parallel/parallel_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ func TestMain(m *testing.M) {
4848
}
4949

5050
type reverser struct {
51-
extra []byte
52-
addr common.Address
53-
gas uint64
51+
headerExtra []byte
52+
addr common.Address
53+
stateKey common.Hash
54+
gas uint64
5455
}
5556

5657
func (r *reverser) BeforeBlock(h *types.Header) {
57-
r.extra = h.Extra
58+
r.headerExtra = slices.Clone(h.Extra)
5859
}
5960

6061
func (r *reverser) Gas(tx *types.Transaction) (uint64, bool) {
@@ -64,20 +65,25 @@ func (r *reverser) Gas(tx *types.Transaction) (uint64, bool) {
6465
return r.gas, true
6566
}
6667

67-
func reverserOutput(data, extra []byte) []byte {
68-
out := append(slices.Clone(data), extra...)
68+
func reverserOutput(txData []byte, state common.Hash, extra []byte) []byte {
69+
out := slices.Concat(txData, state[:], extra)
6970
slices.Reverse(out)
7071
return out
7172
}
7273

73-
func (r *reverser) Process(i int, tx *types.Transaction, _ libevm.StateReader) []byte {
74-
return reverserOutput(tx.Data(), r.extra)
74+
func (r *reverser) Process(sdb libevm.StateReader, i int, tx *types.Transaction) []byte {
75+
return reverserOutput(
76+
tx.Data(),
77+
sdb.GetTransientState(r.addr, r.stateKey),
78+
r.headerExtra,
79+
)
7580
}
7681

7782
func TestProcessor(t *testing.T) {
7883
handler := &reverser{
79-
addr: common.Address{'r', 'e', 'v', 'e', 'r', 's', 'e'},
80-
gas: 1e6,
84+
addr: common.Address{'r', 'e', 'v', 'e', 'r', 's', 'e'},
85+
stateKey: common.Hash{'k', 'e', 'y'},
86+
gas: 1e6,
8187
}
8288
p := New(handler, 8)
8389
t.Cleanup(p.Close)
@@ -128,6 +134,8 @@ func TestProcessor(t *testing.T) {
128134
}
129135

130136
_, _, sdb := ethtest.NewEmptyStateDB(t)
137+
stateVal := common.Hash{'s', 't', 'a', 't', 'e'}
138+
sdb.SetTransientState(handler.addr, handler.stateKey, stateVal)
131139

132140
for _, tt := range tests {
133141
t.Run("", func(t *testing.T) {
@@ -175,7 +183,7 @@ func TestProcessor(t *testing.T) {
175183

176184
var want []byte
177185
if wantOK {
178-
want = reverserOutput(tx.Data(), extra)
186+
want = reverserOutput(tx.Data(), stateVal, extra)
179187
}
180188

181189
got, gotOK := p.Result(i)
@@ -292,7 +300,7 @@ func TestIntegration(t *testing.T) {
292300
wantR.Logs = []*types.Log{{
293301
TxHash: tx.Hash(),
294302
TxIndex: ui,
295-
Data: reverserOutput(data, nil),
303+
Data: reverserOutput(data, common.Hash{}, nil),
296304
}}
297305
}
298306
want = append(want, wantR)

0 commit comments

Comments
 (0)