Skip to content

Commit 0013387

Browse files
Switch to use dynamic gas price transactions in load generators (#32)
1 parent c496d31 commit 0013387

11 files changed

+29
-52
lines changed

load/app/app.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package app
1818

1919
import (
20-
"math/big"
21-
2220
"github.com/0xsoniclabs/norma/driver/rpc"
2321
"github.com/ethereum/go-ethereum/core/types"
2422
)
@@ -38,6 +36,6 @@ type Application interface {
3836
// User produces a stream of transactions to Generate traffic on the chain.
3937
// Implementations are not required to be thread-safe.
4038
type User interface {
41-
GenerateTx(currentGasPrice *big.Int) (*types.Transaction, error)
39+
GenerateTx() (*types.Transaction, error)
4240
GetSentTransactions() uint64
4341
}

load/app/app_mock.go

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

load/app/app_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ func testGenerator(t *testing.T, app app.Application, ctxt app.AppContext) {
9494
numTransactions := 10
9595
transactions := []*types.Transaction{}
9696
for range numTransactions {
97-
price, err := rpcClient.SuggestGasPrice(context.Background())
98-
if err != nil {
99-
return
100-
}
101-
tx, err := user.GenerateTx(price)
97+
tx, err := user.GenerateTx()
10298
if err != nil {
10399
t.Fatal(err)
104100
}

load/app/counter_app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type CounterUser struct {
120120
sentTxs atomic.Uint64
121121
}
122122

123-
func (g *CounterUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, error) {
123+
func (g *CounterUser) GenerateTx() (*types.Transaction, error) {
124124
// prepare tx data
125125
data, err := g.abi.Pack("incrementCounter")
126126
if err != nil || data == nil {
@@ -129,7 +129,7 @@ func (g *CounterUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction,
129129

130130
// prepare tx
131131
const gasLimit = 28036
132-
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, currentGasPrice, gasLimit)
132+
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, gasLimit)
133133
if err == nil {
134134
g.sentTxs.Add(1)
135135
}

load/app/erc20_app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ type ERC20User struct {
169169
sentTxs uint64
170170
}
171171

172-
func (g *ERC20User) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, error) {
172+
func (g *ERC20User) GenerateTx() (*types.Transaction, error) {
173173
// choose random recipient
174174
recipient := g.recipients[rand.Intn(len(g.recipients))]
175175

@@ -181,7 +181,7 @@ func (g *ERC20User) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, er
181181

182182
// prepare tx
183183
const gasLimit = 52000 // Transfer method call takes 51349 of gas
184-
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, currentGasPrice, gasLimit)
184+
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, gasLimit)
185185
if err == nil {
186186
atomic.AddUint64(&g.sentTxs, 1)
187187
}

load/app/helpers.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ package app
1919
import (
2020
"context"
2121
"fmt"
22+
"math/big"
23+
2224
"github.com/0xsoniclabs/norma/driver/rpc"
2325
"github.com/ethereum/go-ethereum/common"
2426
"github.com/ethereum/go-ethereum/core/types"
25-
"math/big"
2627
)
2728

28-
func createTx(from *Account, toAddress common.Address, value *big.Int, data []byte, gasPrice *big.Int, gasLimit uint64) (*types.Transaction, error) {
29-
tx := types.NewTx(&types.LegacyTx{
30-
Nonce: from.getNextNonce(),
31-
GasPrice: gasPrice,
32-
Gas: gasLimit,
33-
To: &toAddress,
34-
Value: value,
35-
Data: data,
29+
func createTx(from *Account, toAddress common.Address, value *big.Int, data []byte, gasLimit uint64) (*types.Transaction, error) {
30+
tx := types.NewTx(&types.DynamicFeeTx{
31+
Nonce: from.getNextNonce(),
32+
GasFeeCap: new(big.Int).Mul(big.NewInt(10_000), big.NewInt(1e9)),
33+
GasTipCap: big.NewInt(0),
34+
Gas: gasLimit,
35+
To: &toAddress,
36+
Value: value,
37+
Data: data,
3638
})
37-
return types.SignTx(tx, types.NewEIP155Signer(from.chainID), from.privateKey)
39+
return types.SignTx(tx, types.NewLondonSigner(from.chainID), from.privateKey)
3840
}
3941

4042
// GetGasPrice obtains optimal gasPrice for regular transactions

load/app/store_app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type StoreUser struct {
120120
sentTxs atomic.Uint64
121121
}
122122

123-
func (g *StoreUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, error) {
123+
func (g *StoreUser) GenerateTx() (*types.Transaction, error) {
124124
const updateSize = 260 // ~ 1 GB/minute new netto data at 1000 Tx/s
125125

126126
// prepare tx data -- since as single put is rather cheap, we use the 'fill' operation
@@ -136,7 +136,7 @@ func (g *StoreUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, er
136136

137137
// prepare tx
138138
const gasLimit = 52000 + 25000*updateSize // wild guess ...
139-
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, currentGasPrice, gasLimit)
139+
tx, err := createTx(g.sender, g.contract, big.NewInt(0), data, gasLimit)
140140
if err == nil {
141141
g.sentTxs.Add(1)
142142
}

load/app/uniswap_app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ type UniswapUser struct {
268268
sentTxs uint64
269269
}
270270

271-
func (g *UniswapUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction, error) {
271+
func (g *UniswapUser) GenerateTx() (*types.Transaction, error) {
272272
var data []byte
273273
var err error
274274

@@ -287,7 +287,7 @@ func (g *UniswapUser) GenerateTx(currentGasPrice *big.Int) (*types.Transaction,
287287
// prepare tx
288288
// swapExactTokensForTokens consumes 157571 for 2 tokens + cca 94314 for each additional token
289289
const gasLimit = 160_000 + (TokensInChain-2)*95000
290-
tx, err := createTx(g.sender, g.routerAddress, big.NewInt(0), data, currentGasPrice, gasLimit)
290+
tx, err := createTx(g.sender, g.routerAddress, big.NewInt(0), data, gasLimit)
291291
if err == nil {
292292
atomic.AddUint64(&g.sentTxs, 1)
293293
}

load/controller/controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestLoadGeneration_CanRealizeConstantTrafficShape(t *testing.T) {
7979
application.EXPECT().CreateUsers(gomock.Any(), 100).AnyTimes().Return(users, nil)
8080

8181
rpcClient.EXPECT().SuggestGasPrice(gomock.Any()).AnyTimes().Return(big.NewInt(0), nil)
82-
user.EXPECT().GenerateTx(gomock.Any()).AnyTimes().Return(&transaction, nil)
82+
user.EXPECT().GenerateTx().AnyTimes().Return(&transaction, nil)
8383

8484
clientFactory := app.NewMockRpcClientFactory(ctrl)
8585
clientFactory.EXPECT().DialRandomRpc().AnyTimes().Return(rpcClient, nil)

load/controller/generator.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,7 @@ import (
2525

2626
func runGeneratorLoop(user app.User, trigger <-chan struct{}, network driver.Network) {
2727
for range trigger {
28-
// retrieve gas price for each tx individually as the gasPrice can change by amount of time passed(larger db size),
29-
// or by new validator registration, etc.
30-
rpcClient, err := network.DialRandomRpc()
31-
if err != nil {
32-
log.Printf("generator loop; failed to dial random rpc; %v", err)
33-
continue
34-
}
35-
currentRegularGasPrice, err := app.GetGasPrice(rpcClient)
36-
if err != nil {
37-
log.Printf("generator loop; failed to get gas price; %v", err)
38-
continue
39-
}
40-
41-
tx, err := user.GenerateTx(currentRegularGasPrice)
28+
tx, err := user.GenerateTx()
4229
if err != nil {
4330
log.Printf("failed to generate tx; %v", err)
4431
} else {

load/controller/mocked_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controller
1818

1919
import (
2020
"context"
21-
"math/big"
2221
"testing"
2322
"time"
2423

@@ -40,22 +39,18 @@ func TestMockedTrafficGenerating(t *testing.T) {
4039
mockUser := app.NewMockUser(mockCtrl)
4140

4241
mockedRpcClient := rpc.NewMockRpcClient(mockCtrl)
43-
mockedRpcClient.EXPECT().SuggestGasPrice(gomock.Any()).Return(big.NewInt(0), nil)
4442
mockedRpcClient.EXPECT().Close()
4543

4644
appContext := app.NewMockAppContext(mockCtrl)
4745
appContext.EXPECT().GetClient().Return(mockedRpcClient).AnyTimes()
4846

4947
mockedNetwork := driver.NewMockNetwork(mockCtrl)
50-
mockedNetwork.EXPECT().DialRandomRpc().Return(mockedRpcClient, nil)
5148

5249
mockedApp := app.NewMockApplication(mockCtrl)
5350
mockedApp.EXPECT().CreateUsers(appContext, numUsers).Return([]app.User{mockUser, mockUser}, nil)
5451

5552
// app should be called 10-times to generate 10 txs
56-
mockedNetwork.EXPECT().DialRandomRpc().Return(mockedRpcClient, nil).MaxTimes(11)
57-
mockedRpcClient.EXPECT().SuggestGasPrice(gomock.Any()).Return(big.NewInt(0), nil).MaxTimes(11)
58-
mockUser.EXPECT().GenerateTx(gomock.Any()).Return(&demoTx, nil).MinTimes(5).MaxTimes(11)
53+
mockUser.EXPECT().GenerateTx().Return(&demoTx, nil).MinTimes(5).MaxTimes(11)
5954
// network should be called 10-times to send 10 txs
6055
mockedNetwork.EXPECT().SendTransaction(&demoTx).MinTimes(5).MaxTimes(11)
6156

0 commit comments

Comments
 (0)