Skip to content
This repository was archived by the owner on Jul 13, 2022. It is now read-only.

Commit 340decd

Browse files
authored
1559 Txs (#690)
* initial commit with depedency upgrade and opts changes for 1559 txs * err linting fix * gas price declaration fix * linting fix * moved gas tip cap logic in separate method for better testing * change test endpoint in connection tests back to correct port * pr changes, name changes and change in logic for max price in config is below baseFee * additional tests and fix for setting maxFeePerGas when max gas price < baseFee * fix test endpoint for CI * linter fix
1 parent f0ed761 commit 340decd

File tree

4 files changed

+451
-145
lines changed

4 files changed

+451
-145
lines changed

connections/ethereum/connection.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *Connection) Connect() error {
6767
if c.http {
6868
rpcClient, err = rpc.DialHTTP(c.endpoint)
6969
} else {
70-
rpcClient, err = rpc.DialWebsocket(context.Background(), c.endpoint, "/ws")
70+
rpcClient, err = rpc.DialContext(context.Background(), c.endpoint)
7171
}
7272
if err != nil {
7373
return err
@@ -165,6 +165,38 @@ func (c *Connection) SafeEstimateGas(ctx context.Context) (*big.Int, error) {
165165
}
166166
}
167167

168+
func (c *Connection) EstimateGasLondon(ctx context.Context, baseFee *big.Int) (*big.Int, *big.Int, error) {
169+
var maxPriorityFeePerGas *big.Int
170+
var maxFeePerGas *big.Int
171+
172+
if c.maxGasPrice.Cmp(baseFee) < 0 {
173+
maxPriorityFeePerGas = big.NewInt(1)
174+
maxFeePerGas = new(big.Int).Add(baseFee, maxPriorityFeePerGas)
175+
return maxPriorityFeePerGas, maxFeePerGas, nil
176+
}
177+
178+
maxPriorityFeePerGas, err := c.conn.SuggestGasTipCap(context.TODO())
179+
if err != nil {
180+
return nil, nil, err
181+
}
182+
183+
maxFeePerGas = new(big.Int).Add(
184+
maxPriorityFeePerGas,
185+
new(big.Int).Mul(baseFee, big.NewInt(2)),
186+
)
187+
188+
if maxFeePerGas.Cmp(maxPriorityFeePerGas) < 0 {
189+
return nil, nil, fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", maxFeePerGas, maxPriorityFeePerGas)
190+
}
191+
192+
// Check we aren't exceeding our limit
193+
if maxFeePerGas.Cmp(c.maxGasPrice) == 1 {
194+
maxPriorityFeePerGas.Sub(c.maxGasPrice, baseFee)
195+
maxFeePerGas = c.maxGasPrice
196+
}
197+
return maxPriorityFeePerGas, maxFeePerGas, nil
198+
}
199+
168200
func multiplyGasPrice(gasEstimate *big.Int, gasMultiplier *big.Float) *big.Int {
169201

170202
gasEstimateFloat := new(big.Float).SetInt(gasEstimate)
@@ -183,12 +215,30 @@ func multiplyGasPrice(gasEstimate *big.Int, gasMultiplier *big.Float) *big.Int {
183215
func (c *Connection) LockAndUpdateOpts() error {
184216
c.optsLock.Lock()
185217

186-
gasPrice, err := c.SafeEstimateGas(context.TODO())
218+
head, err := c.conn.HeaderByNumber(context.TODO(), nil)
187219
if err != nil {
188-
c.optsLock.Unlock()
220+
c.UnlockOpts()
189221
return err
190222
}
191-
c.opts.GasPrice = gasPrice
223+
224+
if head.BaseFee != nil {
225+
c.opts.GasTipCap, c.opts.GasFeeCap, err = c.EstimateGasLondon(context.TODO(), head.BaseFee)
226+
if err != nil {
227+
c.UnlockOpts()
228+
return err
229+
}
230+
231+
// Both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) cannot be specified: https://github.com/ethereum/go-ethereum/blob/95bbd46eabc5d95d9fb2108ec232dd62df2f44ab/accounts/abi/bind/base.go#L254
232+
c.opts.GasPrice = nil
233+
} else {
234+
var gasPrice *big.Int
235+
gasPrice, err = c.SafeEstimateGas(context.TODO())
236+
if err != nil {
237+
c.UnlockOpts()
238+
return err
239+
}
240+
c.opts.GasPrice = gasPrice
241+
}
192242

193243
nonce, err := c.conn.PendingNonceAt(context.Background(), c.opts.From)
194244
if err != nil {

connections/ethereum/connection_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,103 @@ func TestConnection_SafeEstimateGasMax(t *testing.T) {
9797
t.Fatalf("Gas price should equal max. Suggested: %s Max: %s", price.String(), maxPrice.String())
9898
}
9999
}
100+
101+
func TestConnection_EstimateGasLondon(t *testing.T) {
102+
// Set TestEndpoint to Goerli endpoint when testing as the current Github CI doesn't use the London version of geth
103+
// Goerli commonly has a base fee of 7 gwei with maxPriorityFeePerGas of 4.999999993 gwei
104+
maxGasPrice := big.NewInt(100000000000)
105+
conn := NewConnection(TestEndpoint, false, AliceKp, log15.Root(), GasLimit, maxGasPrice, GasMultipler, "", "")
106+
err := conn.Connect()
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
defer conn.Close()
111+
112+
head, err := conn.conn.HeaderByNumber(context.Background(), nil)
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
117+
// This is here as the current dev network is an old version of geth and will keep the test failing on the CI
118+
if head.BaseFee != nil {
119+
_, suggestedGasFeeCap, err := conn.EstimateGasLondon(context.Background(), head.BaseFee)
120+
if err != nil {
121+
t.Fatal(err)
122+
}
123+
124+
if suggestedGasFeeCap.Cmp(maxGasPrice) >= 0 {
125+
t.Fatalf("Gas fee cap should be less than max gas price. Suggested: %s Max: %s", suggestedGasFeeCap.String(), maxGasPrice.String())
126+
}
127+
}
128+
}
129+
130+
func TestConnection_EstimateGasLondonMax(t *testing.T) {
131+
// Set TestEndpoint to Goerli endpoint when testing as the current Github CI doesn't use the London version of geth
132+
// Goerli commonly has a base fee of 7 gwei with maxPriorityFeePerGas of 4.999999993 gwei
133+
maxGasPrice := big.NewInt(100)
134+
conn := NewConnection(TestEndpoint, false, AliceKp, log15.Root(), GasLimit, maxGasPrice, GasMultipler, "", "")
135+
err := conn.Connect()
136+
if err != nil {
137+
t.Fatal(err)
138+
}
139+
defer conn.Close()
140+
141+
head, err := conn.conn.HeaderByNumber(context.Background(), nil)
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
146+
// This is here as the current dev network is an old version of geth and will keep the test failing on the CI
147+
if head.BaseFee != nil {
148+
suggestedGasTip, suggestedGasFeeCap, err := conn.EstimateGasLondon(context.Background(), head.BaseFee)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
maxPriorityFeePerGas := new(big.Int).Sub(maxGasPrice, head.BaseFee)
154+
if suggestedGasTip.Cmp(maxPriorityFeePerGas) != 0 {
155+
t.Fatalf("Gas tip cap should equal max - baseFee. Suggested: %s Max Tip: %s", suggestedGasTip.String(), maxPriorityFeePerGas.String())
156+
}
157+
158+
if suggestedGasFeeCap.Cmp(maxGasPrice) != 0 {
159+
t.Fatalf("Gas fee cap should equal max gas price. Suggested: %s Max: %s", suggestedGasFeeCap.String(), maxGasPrice.String())
160+
}
161+
162+
}
163+
}
164+
165+
func TestConnection_EstimateGasLondonMin(t *testing.T) {
166+
// Set TestEndpoint to Goerli endpoint when testing as the current Github CI doesn't use the London version of geth
167+
// Goerli commonly has a base fee of 7 gwei with maxPriorityFeePerGas of 4.999999993 gwei
168+
maxGasPrice := big.NewInt(1)
169+
conn := NewConnection(TestEndpoint, false, AliceKp, log15.Root(), GasLimit, maxGasPrice, GasMultipler, "", "")
170+
err := conn.Connect()
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
defer conn.Close()
175+
176+
head, err := conn.conn.HeaderByNumber(context.Background(), nil)
177+
if err != nil {
178+
t.Fatal(err)
179+
}
180+
181+
// This is here as the current dev network is an old version of geth and will keep the test failing on the CI
182+
if head.BaseFee != nil {
183+
suggestedGasTip, suggestedGasFeeCap, err := conn.EstimateGasLondon(context.Background(), head.BaseFee)
184+
if err != nil {
185+
t.Fatal(err)
186+
}
187+
188+
maxPriorityFeePerGas := big.NewInt(1)
189+
maxFeePerGas := new(big.Int).Add(head.BaseFee, maxPriorityFeePerGas)
190+
191+
if suggestedGasTip.Cmp(maxPriorityFeePerGas) != 0 {
192+
t.Fatalf("Gas tip cap should be equal to 1. Suggested: %s Max Tip: %s", suggestedGasTip.String(), maxPriorityFeePerGas)
193+
}
194+
195+
if suggestedGasFeeCap.Cmp(maxFeePerGas) != 0 {
196+
t.Fatalf("Gas fee cap should be 1 greater than the base fee. Suggested: %s Max: %s", suggestedGasFeeCap.String(), maxFeePerGas.String())
197+
}
198+
}
199+
}

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ require (
66
github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e
77
github.com/ChainSafe/chainbridge-utils v1.0.6
88
github.com/ChainSafe/log15 v1.0.0
9-
github.com/aristanetworks/goarista v0.0.0-20200609010056-95bcf8053598 // indirect
109
github.com/centrifuge/go-substrate-rpc-client v2.0.0+incompatible
1110
github.com/deckarep/golang-set v1.7.1 // indirect
12-
github.com/ethereum/go-ethereum v1.9.25
13-
github.com/gorilla/websocket v1.4.2 // indirect
11+
github.com/ethereum/go-ethereum v1.10.6
1412
github.com/prometheus/client_golang v1.4.1
15-
github.com/rs/cors v1.7.0 // indirect
1613
github.com/stretchr/testify v1.7.0
1714
github.com/urfave/cli/v2 v2.3.0
1815
)

0 commit comments

Comments
 (0)