Skip to content

Commit 866bb86

Browse files
committed
refactor: readability improvements
1 parent 63e8dcd commit 866bb86

File tree

3 files changed

+54
-62
lines changed

3 files changed

+54
-62
lines changed

core/vm/preprocess.libevm_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929

3030
"github.com/ava-labs/libevm/common"
3131
"github.com/ava-labs/libevm/core"
32-
"github.com/ava-labs/libevm/core/rawdb"
33-
"github.com/ava-labs/libevm/core/state"
3432
"github.com/ava-labs/libevm/core/types"
3533
"github.com/ava-labs/libevm/core/vm"
3634
"github.com/ava-labs/libevm/crypto"
@@ -148,12 +146,7 @@ func TestChargePreprocessingGas(t *testing.T) {
148146
t.Logf("Extra gas charge: %d", tt.charge)
149147

150148
t.Run("ApplyTransaction", func(t *testing.T) {
151-
sdb, err := state.New(
152-
types.EmptyRootHash,
153-
state.NewDatabase(rawdb.NewMemoryDatabase()),
154-
nil,
155-
)
156-
require.NoError(t, err, "state.New(types.EmptyRootHash, [memory db], nil)")
149+
_, _, sdb := ethtest.NewEmptyStateDB(t)
157150
sdb.SetTxContext(tx.Hash(), i)
158151
sdb.SetBalance(eoa, new(uint256.Int).SetAllOne())
159152
sdb.SetNonce(eoa, tx.Nonce())

libevm/precompiles/parallel/parallel.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ type result[T any] struct {
6969
val *T
7070
}
7171

72-
// A stateDBSharer allows concurrent workers to make copies of a primary
73-
// database. When the `nextAvailable` channel is closed, all workers call
74-
// [state.StateDB.Copy] then signal completion on the [sync.WaitGroup]. The
75-
// channel is replaced for each round of distribution.
76-
type stateDBSharer struct {
77-
nextAvailable chan struct{}
78-
primary *state.StateDB
79-
mu sync.Mutex
80-
workers int
81-
wg sync.WaitGroup
82-
}
83-
8472
// New constructs a new [Processor] with the specified number of concurrent
8573
// workers. [Processor.Close] must be called after the final call to
8674
// [Processor.FinishBlock] to avoid leaking goroutines.
@@ -107,6 +95,29 @@ func New[R any](h Handler[R], workers int) *Processor[R] {
10795
return p
10896
}
10997

98+
// A stateDBSharer allows concurrent workers to make copies of a primary
99+
// database. When the `nextAvailable` channel is closed, all workers call
100+
// [state.StateDB.Copy] then signal completion on the [sync.WaitGroup]. The
101+
// channel is replaced for each round of distribution.
102+
type stateDBSharer struct {
103+
nextAvailable chan struct{}
104+
primary *state.StateDB
105+
mu sync.Mutex
106+
workers int
107+
wg sync.WaitGroup
108+
}
109+
110+
func (s *stateDBSharer) distribute(sdb *state.StateDB) {
111+
s.primary = sdb // no need to Copy() as each worker does it
112+
113+
ch := s.nextAvailable // already copied by [Processor.worker], which is waiting for it to close
114+
s.nextAvailable = make(chan struct{}) // will be copied, ready for the next distribution
115+
116+
s.wg.Add(s.workers)
117+
close(ch)
118+
s.wg.Wait()
119+
}
120+
110121
func (p *Processor[R]) worker() {
111122
defer p.workers.Done()
112123

@@ -192,25 +203,14 @@ func (p *Processor[R]) StartBlock(b *types.Block, rules params.Rules, sdb *state
192203
return nil
193204
}
194205

195-
func (s *stateDBSharer) distribute(sdb *state.StateDB) {
196-
s.primary = sdb // no need to Copy() as each worker does it
197-
198-
ch := s.nextAvailable
199-
s.nextAvailable = make(chan struct{}) // already copied by each worker
200-
201-
s.wg.Add(s.workers)
202-
close(ch)
203-
s.wg.Wait()
204-
}
205-
206206
// FinishBlock returns the [Processor] to a state ready for the next block. A
207207
// return from FinishBlock guarantees that all dispatched work from the
208208
// respective call to [Processor.StartBlock] has been completed.
209209
func (p *Processor[R]) FinishBlock(b *types.Block) {
210210
for i := range len(b.Transactions()) {
211211
// Every result channel is guaranteed to have some value in its buffer
212212
// because [Processor.BeforeBlock] either sends a nil *R or it
213-
// dispatches a job that will send a non-nil *R.
213+
// dispatches a job, which will send a non-nil *R.
214214
tx := (<-p.results[i]).tx
215215
delete(p.txGas, tx)
216216
}
@@ -243,7 +243,7 @@ func (p *Processor[R]) Result(i int) (R, bool) {
243243
return *r.val, true
244244
}
245245

246-
func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules) (process bool, err error) {
246+
func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules) (process bool, retErr error) {
247247
// An explicit 0 is necessary to avoid [Processor.PreprocessingGasCharge]
248248
// returning [ErrTxUnknown].
249249
p.txGas[tx.Hash()] = 0
@@ -253,7 +253,7 @@ func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules)
253253
return false, nil
254254
}
255255
defer func() {
256-
if process && err == nil {
256+
if process && retErr == nil {
257257
p.txGas[tx.Hash()] = cost
258258
}
259259
}()
@@ -262,11 +262,12 @@ func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules)
262262
if err != nil {
263263
return false, fmt.Errorf("calculating intrinsic gas of %v: %v", tx.Hash(), err)
264264
}
265-
266-
// This could only overflow if the gas limit was insufficient to cover
267-
// the intrinsic cost, which would have invalidated it for inclusion.
268-
left := tx.Gas() - spent
269-
return left >= cost, nil
265+
if spent > tx.Gas() {
266+
// If this happens then consensus has a bug because the tx shouldn't
267+
// have been included. We include the check, however, for completeness.
268+
return false, core.ErrIntrinsicGas
269+
}
270+
return tx.Gas()-spent >= cost, nil
270271
}
271272

272273
func txIntrinsicGas(tx *types.Transaction, rules *params.Rules) (uint64, error) {

libevm/precompiles/parallel/parallel_test.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,39 @@ func TestMain(m *testing.M) {
4747
goleak.VerifyTestMain(m, goleak.IgnoreCurrent())
4848
}
4949

50-
type reverser struct {
50+
type concat struct {
5151
headerExtra []byte
5252
addr common.Address
5353
stateKey common.Hash
5454
gas uint64
5555
}
5656

57-
func (r *reverser) BeforeBlock(h *types.Header) {
58-
r.headerExtra = slices.Clone(h.Extra)
57+
func (c *concat) BeforeBlock(h *types.Header) {
58+
c.headerExtra = slices.Clone(h.Extra)
5959
}
6060

61-
func (r *reverser) Gas(tx *types.Transaction) (uint64, bool) {
62-
if to := tx.To(); to == nil || *to != r.addr {
63-
return 0, false
61+
func (c *concat) Gas(tx *types.Transaction) (uint64, bool) {
62+
if to := tx.To(); to != nil && *to == c.addr {
63+
return c.gas, true
6464
}
65-
return r.gas, true
65+
return 0, false
6666
}
6767

68-
func reverserOutput(txData []byte, state common.Hash, extra []byte) []byte {
69-
out := slices.Concat(txData, state[:], extra)
70-
slices.Reverse(out)
71-
return out
68+
func concatOutput(txData []byte, state common.Hash, extra []byte) []byte {
69+
return slices.Concat(txData, state[:], extra)
7270
}
7371

74-
func (r *reverser) Process(sdb libevm.StateReader, i int, tx *types.Transaction) []byte {
75-
return reverserOutput(
72+
func (c *concat) Process(sdb libevm.StateReader, i int, tx *types.Transaction) []byte {
73+
return concatOutput(
7674
tx.Data(),
77-
sdb.GetTransientState(r.addr, r.stateKey),
78-
r.headerExtra,
75+
sdb.GetTransientState(c.addr, c.stateKey),
76+
c.headerExtra,
7977
)
8078
}
8179

8280
func TestProcessor(t *testing.T) {
83-
handler := &reverser{
84-
addr: common.Address{'r', 'e', 'v', 'e', 'r', 's', 'e'},
81+
handler := &concat{
82+
addr: common.Address{'c', 'o', 'n', 'c', 'a', 't'},
8583
stateKey: common.Hash{'k', 'e', 'y'},
8684
gas: 1e6,
8785
}
@@ -164,7 +162,7 @@ func TestProcessor(t *testing.T) {
164162

165163
data := binary.BigEndian.AppendUint64(nil, uint64(i))
166164
gas, err := intrinsicGas(data, types.AccessList{}, &handler.addr, &rules)
167-
require.NoError(t, err, "core.IntrinsicGas(%#x, nil, false, true, true, true)", data)
165+
require.NoError(t, err, "core.IntrinsicGas(%#x, nil, false, ...)", data)
168166

169167
txs[i] = types.NewTx(&types.LegacyTx{
170168
To: &to,
@@ -183,7 +181,7 @@ func TestProcessor(t *testing.T) {
183181

184182
var want []byte
185183
if wantOK {
186-
want = reverserOutput(tx.Data(), stateVal, extra)
184+
want = concatOutput(tx.Data(), stateVal, extra)
187185
}
188186

189187
got, gotOK := p.Result(i)
@@ -210,8 +208,8 @@ func (h *vmHooks) PreprocessingGasCharge(tx common.Hash) (uint64, error) {
210208

211209
func TestIntegration(t *testing.T) {
212210
const handlerGas = 500
213-
handler := &reverser{
214-
addr: common.Address{'r', 'e', 'v', 'e', 'r', 's', 'e'},
211+
handler := &concat{
212+
addr: common.Address{'c', 'o', 'n', 'c', 'a', 't'},
215213
gas: handlerGas,
216214
}
217215
sut := New(handler, 8)
@@ -277,7 +275,7 @@ func TestIntegration(t *testing.T) {
277275
data := []byte("hello, world")
278276

279277
gas, err := intrinsicGas(data, types.AccessList{}, &addr, &rules)
280-
require.NoError(t, err, "core.IntrinsicGas(%#x, nil, false, false, false, false)", data)
278+
require.NoError(t, err, "core.IntrinsicGas(%#x, nil, false, ...)", data)
281279
if addr == handler.addr {
282280
gas += handlerGas
283281
}
@@ -300,7 +298,7 @@ func TestIntegration(t *testing.T) {
300298
wantR.Logs = []*types.Log{{
301299
TxHash: tx.Hash(),
302300
TxIndex: ui,
303-
Data: reverserOutput(data, common.Hash{}, nil),
301+
Data: concatOutput(data, common.Hash{}, nil),
304302
}}
305303
}
306304
want = append(want, wantR)

0 commit comments

Comments
 (0)