Skip to content

Commit 809f3dd

Browse files
authored
feat: improve gas estimation and contract (#167)
1 parent 31eb579 commit 809f3dd

6 files changed

Lines changed: 76 additions & 18 deletions

File tree

contracts/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build: build-abi build-bin build-abi-go
22

33
build-abi: src/**
44
mkdir -p abi
5-
forge inspect src/Simulator.sol:Simulator abi --json > abi/Simulator.json
5+
forge inspect contracts/src/Simulator.sol:Simulator abi --json > abi/Simulator.json
66

77
build-bin: src/**
88
forge build --extra-output-files bin --force

contracts/src/Simulator.sol

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,26 +319,33 @@ contract Simulator {
319319

320320
function run_blake2f(uint256 num_iterations) private {
321321
bytes32[2] memory h;
322-
h[0] = 0x48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5;
323-
h[1] = 0xd182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b;
322+
h[0] = 0xa1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8;
323+
h[1] = 0xa1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8a1b2c3d4e5f6a7b8;
324324

325325
bytes32[4] memory m;
326-
m[0] = 0x6162630000000000000000000000000000000000000000000000000000000000;
327-
m[1] = 0x0000000000000000000000000000000000000000000000000000000000000000;
328-
m[2] = 0x0000000000000000000000000000000000000000000000000000000000000000;
329-
m[3] = 0x0000000000000000000000000000000000000000000000000000000000000000;
326+
m[0] = 0xc3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2;
327+
m[1] = 0xc3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2;
328+
m[2] = 0xc3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2;
329+
m[3] = 0xc3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2c3d4e5f6a7b8c1d2;
330330

331331
bytes8[2] memory t;
332-
t[0] = 0x0300000000000000;
333-
t[1] = 0x0000000000000000;
332+
t[0] = 0x0000000000000000;
333+
t[1] = 0x0000000000ff00ff;
334334

335335
bool f = true;
336336

337337
for (uint256 i = 0; i < num_iterations; i++) {
338-
uint32 rounds = uint32(i);
339-
(bool ok,) =
340-
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
341-
require(ok, "Blake2f failed");
338+
uint32 rounds = 0xc00;
339+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
340+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
341+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
342+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
343+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
344+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
345+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
346+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
347+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
348+
address(9).staticcall(abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f));
342349
}
343350
}
344351

runner/network/consensus/sequencer_consensus.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ func (f *SequencerConsensusClient) generatePayloadAttributes(sequencerTxs [][]by
121121
var b8 eth.Bytes8
122122
copy(b8[:], eip1559.EncodeHolocene1559Params(50, 1))
123123

124-
timestamp := f.lastTimestamp + 1
124+
lastTimestamp := f.lastTimestamp
125+
126+
// if the last timestamp is more than 2 seconds in the past, set it to the current time
127+
if int64(lastTimestamp)-time.Now().Unix() < -2 {
128+
lastTimestamp = uint64(time.Now().Unix())
129+
}
130+
131+
timestamp := lastTimestamp + 1
125132

126133
number := uint64(0)
127134
time := uint64(0)

runner/payload/simulator/abi/Simulator.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runner/payload/simulator/simulatorstats/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package simulatorstats
22

33
import (
4+
"crypto/sha256"
5+
"encoding/json"
46
"fmt"
57
"maps"
68
"math"
@@ -222,6 +224,16 @@ type Stats struct {
222224
Precompiles OpcodeStats
223225
}
224226

227+
func (s *Stats) Hash() common.Hash {
228+
229+
data, err := json.Marshal(s)
230+
if err != nil {
231+
panic(err)
232+
}
233+
hash := sha256.Sum256(data)
234+
return common.Hash(hash[:])
235+
}
236+
225237
func (s *Stats) ToConfig() (*abi.SimulatorConfig, error) {
226238
rounded := s.Copy().Round()
227239
precompiles := make([]abi.PrecompileConfig, 0, len(rounded.Precompiles))

runner/payload/simulator/worker.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type Contract struct {
3939
type SimulatorPayloadDefinition = simulatorstats.StatsConfig
4040

4141
type simulatorPayloadWorker struct {
42-
log log.Logger
42+
log log.Logger
43+
gasUsedCache map[common.Hash]uint64
4344

4445
params benchtypes.RunParams
4546
chainID *big.Int
@@ -139,12 +140,22 @@ func NewSimulatorPayloadWorker(ctx context.Context, log log.Logger, elRPCURL str
139140

140141
// Create transactors for each caller
141142
transactors := make([]*bind.TransactOpts, numCallers)
143+
142144
for i, key := range callerKeys {
143145
transactor, err := bind.NewKeyedTransactorWithChainID(key, chainID)
144146
if err != nil {
145147
return nil, errors.Wrapf(err, "failed to create transactor for caller %d", i)
146148
}
147149
transactor.NoSend = true
150+
nonce, err := client.PendingNonceAt(context.Background(), crypto.PubkeyToAddress(key.PublicKey))
151+
if err != nil {
152+
return nil, errors.Wrapf(err, "failed to get nonce for caller %d", i)
153+
}
154+
transactor.Nonce = big.NewInt(int64(nonce))
155+
156+
transactor.GasFeeCap = new(big.Int).Mul(big.NewInt(1e9), big.NewInt(1))
157+
transactor.GasTipCap = big.NewInt(1)
158+
148159
transactors[i] = transactor
149160
}
150161

@@ -192,6 +203,7 @@ func NewSimulatorPayloadWorker(ctx context.Context, log log.Logger, elRPCURL str
192203
scaleFactor: scaleFactor,
193204
actualNumConfig: simulatorstats.NewStats(),
194205
numCallers: numCallers,
206+
gasUsedCache: make(map[common.Hash]uint64),
195207
}
196208

197209
return t, nil
@@ -469,6 +481,11 @@ func (t *simulatorPayloadWorker) fundCallerAccounts(ctx context.Context) error {
469481
// If using just the prefunded account, no funding transfer needed
470482
if t.numCallers == 1 && t.callerAddrs[0] == prefundAddr {
471483
t.log.Info("Using single caller (prefunded account)", "address", prefundAddr.Hex())
484+
pendingNonce, err := t.client.PendingNonceAt(ctx, prefundAddr)
485+
if err != nil {
486+
return errors.Wrap(err, "failed to get pending nonce for prefunded account")
487+
}
488+
t.transactors[0].Nonce = big.NewInt(int64(pendingNonce))
472489
return nil
473490
}
474491

@@ -562,21 +579,34 @@ func (t *simulatorPayloadWorker) sendTxs(ctx context.Context) error {
562579

563580
gas := t.params.GasLimit - 100_000
564581

582+
sendTxsStartTime := time.Now()
583+
565584
for i := uint64(0); i < uint64(math.Ceil(float64(t.numCallsPerBlock)*t.scaleFactor)); i++ {
566585
actual := t.actualNumConfig
567586
expected := t.payloadParams.Mul(float64(t.numCalls+1) * t.scaleFactor)
568587

569588
// Round-robin across callers
570589
callerIdx := t.currCallerIdx
571590
t.currCallerIdx = (t.currCallerIdx + 1) % t.numCallers
572-
573591
blockCounts := expected.Sub(actual).Round()
592+
593+
expectedGas, ok := t.gasUsedCache[blockCounts.Hash()]
594+
if ok {
595+
t.transactors[callerIdx].GasLimit = expectedGas
596+
} else {
597+
t.transactors[callerIdx].GasLimit = 0
598+
}
599+
574600
transferTx, err := t.createCallTx(t.transactors[callerIdx], t.callerKeys[callerIdx], blockCounts)
575601
if err != nil {
576602
t.log.Error("Failed to create transfer transaction", "err", err)
577603
return err
578604
}
579605

606+
t.gasUsedCache[blockCounts.Hash()] = transferTx.Gas()
607+
608+
t.transactors[callerIdx].Nonce = t.transactors[callerIdx].Nonce.Add(t.transactors[callerIdx].Nonce, big.NewInt(1))
609+
580610
gasUsed := transferTx.Gas()
581611
if gasUsed > gas {
582612
t.log.Warn("Gas used is greater than gas limit, stopping tx sending", "gasUsed", gasUsed, "gasLimit", t.params.GasLimit)
@@ -594,11 +624,13 @@ func (t *simulatorPayloadWorker) sendTxs(ctx context.Context) error {
594624
}
595625

596626
t.mempool.AddTransactions(txs)
627+
sendTxsDuration := time.Since(sendTxsStartTime)
628+
log.Info("Send transactions duration", "duration", sendTxsDuration, "numCalls", uint64(math.Ceil(float64(t.numCallsPerBlock)*t.scaleFactor)))
597629
return nil
598630
}
599631

600632
func (t *simulatorPayloadWorker) createCallTx(transactor *bind.TransactOpts, fromPriv *ecdsa.PrivateKey, config *simulatorstats.Stats) (*types.Transaction, error) {
601-
simulator, err := abi.NewSimulator(t.contractAddr, t.contractBackend)
633+
simulator, err := abi.NewSimulatorTransactor(t.contractAddr, t.contractBackend)
602634
if err != nil {
603635
return nil, errors.Wrap(err, "failed to create simulator transactor")
604636
}

0 commit comments

Comments
 (0)