Skip to content

Commit 682154c

Browse files
chore(transaction): rename interface to backend.Geth and remove unused funcs
1 parent 1d61647 commit 682154c

File tree

9 files changed

+70
-162
lines changed

9 files changed

+70
-162
lines changed

pkg/node/chain.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/ethersphere/bee/v2/pkg/storage"
3333
"github.com/ethersphere/bee/v2/pkg/transaction"
3434
"github.com/ethersphere/bee/v2/pkg/transaction/wrapped"
35-
"github.com/ethersphere/go-sw3-abi/sw3abi"
3635
"github.com/prometheus/client_golang/prometheus"
3736
)
3837

@@ -347,19 +346,10 @@ type noOpChainBackend struct {
347346
chainID int64
348347
}
349348

350-
// BlockByNumber implements transaction.Backend.
351-
func (m *noOpChainBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
352-
return nil, postagecontract.ErrChainDisabled
353-
}
354-
355349
func (m noOpChainBackend) Metrics() []prometheus.Collector {
356350
return nil
357351
}
358352

359-
func (m noOpChainBackend) CodeAt(context.Context, common.Address, *big.Int) ([]byte, error) {
360-
return common.FromHex(sw3abi.SimpleSwapFactoryDeployedBinv0_6_9), nil
361-
}
362-
363353
func (m noOpChainBackend) CallContract(context.Context, ethereum.CallMsg, *big.Int) ([]byte, error) {
364354
return nil, errors.New("disabled chain backend")
365355
}

pkg/transaction/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
// Backend is the minimum of blockchain backend functions we need.
2121
type Backend interface {
22-
backend.Backend
22+
backend.Geth
2323
SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)
2424
}
2525

pkg/transaction/backend/backend.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ import (
1313
"github.com/ethereum/go-ethereum/core/types"
1414
)
1515

16-
// Backend is the interface that an ethclient.Client satisfies.
17-
type Backend interface {
16+
// Geth is the interface that an ethclient.Client satisfies.
17+
type Geth interface {
1818
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
19-
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
2019
BlockNumber(ctx context.Context) (uint64, error)
2120
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
2221
ChainID(ctx context.Context) (*big.Int, error)
2322
Close()
24-
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
2523
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
2624
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
2725
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)

pkg/transaction/backendmock/backend.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
)
1717

1818
type backendMock struct {
19-
codeAt func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
2019
callContract func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
2120
sendTransaction func(ctx context.Context, tx *types.Transaction) error
2221
suggestedFeeAndTip func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)
@@ -26,30 +25,18 @@ type backendMock struct {
2625
pendingNonceAt func(ctx context.Context, account common.Address) (uint64, error)
2726
transactionByHash func(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
2827
blockNumber func(ctx context.Context) (uint64, error)
29-
blockByNumber func(ctx context.Context, number *big.Int) (*types.Block, error)
3028
headerByNumber func(ctx context.Context, number *big.Int) (*types.Header, error)
3129
balanceAt func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
3230
nonceAt func(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
3331
}
3432

35-
func (m *backendMock) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
36-
if m.codeAt != nil {
37-
return m.codeAt(ctx, contract, blockNumber)
38-
}
39-
return nil, errors.New("not implemented")
40-
}
41-
4233
func (m *backendMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
4334
if m.callContract != nil {
4435
return m.callContract(ctx, call, blockNumber)
4536
}
4637
return nil, errors.New("not implemented")
4738
}
4839

49-
func (*backendMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
50-
return nil, errors.New("not implemented")
51-
}
52-
5340
func (m *backendMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
5441
if m.pendingNonceAt != nil {
5542
return m.pendingNonceAt(ctx, account)
@@ -82,10 +69,6 @@ func (*backendMock) FilterLogs(ctx context.Context, query ethereum.FilterQuery)
8269
return nil, errors.New("not implemented")
8370
}
8471

85-
func (*backendMock) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
86-
return nil, errors.New("not implemented")
87-
}
88-
8972
func (m *backendMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
9073
if m.transactionReceipt != nil {
9174
return m.transactionReceipt(ctx, txHash)
@@ -107,13 +90,6 @@ func (m *backendMock) BlockNumber(ctx context.Context) (uint64, error) {
10790
return 0, errors.New("not implemented")
10891
}
10992

110-
func (m *backendMock) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
111-
if m.blockByNumber != nil {
112-
return m.blockByNumber(ctx, number)
113-
}
114-
return nil, errors.New("not implemented")
115-
}
116-
11793
func (m *backendMock) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
11894
if m.headerByNumber != nil {
11995
return m.headerByNumber(ctx, number)
@@ -171,12 +147,6 @@ func WithCallContractFunc(f func(ctx context.Context, call ethereum.CallMsg, blo
171147
})
172148
}
173149

174-
func WithCodeAtFunc(f func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)) Option {
175-
return optionFunc(func(s *backendMock) {
176-
s.codeAt = f
177-
})
178-
}
179-
180150
func WithBalanceAt(f func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)) Option {
181151
return optionFunc(func(s *backendMock) {
182152
s.balanceAt = f
@@ -219,12 +189,6 @@ func WithTransactionByHashFunc(f func(ctx context.Context, txHash common.Hash) (
219189
})
220190
}
221191

222-
func WithBlockByNumberFunc(f func(ctx context.Context, number *big.Int) (*types.Block, error)) Option {
223-
return optionFunc(func(s *backendMock) {
224-
s.blockByNumber = f
225-
})
226-
}
227-
228192
func WithSendTransactionFunc(f func(ctx context.Context, tx *types.Transaction) error) Option {
229193
return optionFunc(func(s *backendMock) {
230194
s.sendTransaction = f

pkg/transaction/backendsimulation/backend.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package backendsimulation
77
import (
88
"context"
99
"errors"
10+
"maps"
1011
"math/big"
1112

1213
"github.com/ethereum/go-ethereum"
@@ -74,34 +75,18 @@ func (m *simulatedBackend) advanceBlock() {
7475
m.blockNumber = block.Number
7576

7677
if block.Receipts != nil {
77-
for hash, receipt := range block.Receipts {
78-
m.receipts[hash] = receipt
79-
}
78+
maps.Copy(m.receipts, block.Receipts)
8079
}
8180

8281
if block.NoncesAt != nil {
83-
for addr, nonce := range block.NoncesAt {
84-
m.noncesAt[addr] = nonce
85-
}
82+
maps.Copy(m.noncesAt, block.NoncesAt)
8683
}
8784
}
8885

89-
func (m *simulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
90-
return nil, errors.New("not implemented")
91-
}
92-
93-
func (m *simulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
94-
return nil, errors.New("not implemented")
95-
}
96-
9786
func (*simulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
9887
return nil, errors.New("not implemented")
9988
}
10089

101-
func (*simulatedBackend) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
102-
return nil, errors.New("not implemented")
103-
}
104-
10590
func (m *simulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
10691
return 0, errors.New("not implemented")
10792
}
@@ -122,10 +107,6 @@ func (*simulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQu
122107
return nil, errors.New("not implemented")
123108
}
124109

125-
func (*simulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
126-
return nil, errors.New("not implemented")
127-
}
128-
129110
func (m *simulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
130111
receipt, ok := m.receipts[txHash]
131112
if ok {

pkg/transaction/wrapped/fee.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2021 The Swarm Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package wrapped
6+
7+
import (
8+
"context"
9+
"errors"
10+
"fmt"
11+
"math/big"
12+
)
13+
14+
const (
15+
percentageDivisor = 100
16+
baseFeeMultiplier = 2
17+
)
18+
19+
var (
20+
ErrEIP1559NotSupported = errors.New("network does not appear to support EIP-1559 (no baseFee)")
21+
)
22+
23+
// SuggestedFeeAndTip calculates the recommended gasFeeCap and gasTipCap for a transaction.
24+
func (b *wrappedBackend) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
25+
if gasPrice != nil {
26+
// 1. gasFeeCap: The absolute maximum price per gas does not exceed the user's specified price.
27+
// 2. gasTipCap: The entire amount (gasPrice - baseFee) can be used as a priority fee.
28+
return new(big.Int).Set(gasPrice), new(big.Int).Set(gasPrice), nil
29+
}
30+
31+
gasTipCap, err := b.backend.SuggestGasTipCap(ctx)
32+
if err != nil {
33+
return nil, nil, fmt.Errorf("failed to suggest gas tip cap: %w", err)
34+
}
35+
36+
if boostPercent != 0 {
37+
// multiplier: 100 + boostPercent (e.g., 110 for 10% boost)
38+
multiplier := new(big.Int).Add(big.NewInt(int64(percentageDivisor)), big.NewInt(int64(boostPercent)))
39+
// gasTipCap = gasTipCap * (100 + boostPercent) / 100
40+
gasTipCap.Mul(gasTipCap, multiplier).Div(gasTipCap, big.NewInt(int64(percentageDivisor)))
41+
}
42+
43+
minimumTip := big.NewInt(b.minimumGasTipCap)
44+
if gasTipCap.Cmp(minimumTip) < 0 {
45+
gasTipCap.Set(minimumTip)
46+
}
47+
48+
latestBlockHeader, err := b.backend.HeaderByNumber(ctx, nil)
49+
if err != nil {
50+
return nil, nil, fmt.Errorf("failed to get latest block header: %w", err)
51+
}
52+
if latestBlockHeader == nil || latestBlockHeader.BaseFee == nil {
53+
return nil, nil, ErrEIP1559NotSupported
54+
}
55+
56+
// EIP-1559: gasFeeCap = (2 * baseFee) + gasTipCap
57+
gasFeeCap := new(big.Int).Mul(latestBlockHeader.BaseFee, big.NewInt(int64(baseFeeMultiplier)))
58+
gasFeeCap.Add(gasFeeCap, gasTipCap)
59+
60+
return gasFeeCap, gasTipCap, nil
61+
}

pkg/transaction/wrapped/metrics.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ type metrics struct {
1818
BlockNumberCalls prometheus.Counter
1919
BlockHeaderCalls prometheus.Counter
2020
BalanceCalls prometheus.Counter
21-
CodeAtCalls prometheus.Counter
2221
NonceAtCalls prometheus.Counter
2322
PendingNonceCalls prometheus.Counter
2423
CallContractCalls prometheus.Counter
@@ -27,7 +26,6 @@ type metrics struct {
2726
SendTransactionCalls prometheus.Counter
2827
FilterLogsCalls prometheus.Counter
2928
ChainIDCalls prometheus.Counter
30-
BlockByNumberCalls prometheus.Counter
3129
}
3230

3331
func newMetrics() metrics {
@@ -76,12 +74,6 @@ func newMetrics() metrics {
7674
Name: "calls_balance",
7775
Help: "Count of eth_getBalance rpc calls",
7876
}),
79-
CodeAtCalls: prometheus.NewCounter(prometheus.CounterOpts{
80-
Namespace: m.Namespace,
81-
Subsystem: subsystem,
82-
Name: "calls_code_at",
83-
Help: "Count of eth_getCode rpc calls",
84-
}),
8577
NonceAtCalls: prometheus.NewCounter(prometheus.CounterOpts{
8678
Namespace: m.Namespace,
8779
Subsystem: subsystem,
@@ -130,12 +122,6 @@ func newMetrics() metrics {
130122
Name: "calls_chain_id",
131123
Help: "Count of eth_chainId rpc calls",
132124
}),
133-
BlockByNumberCalls: prometheus.NewCounter(prometheus.CounterOpts{
134-
Namespace: m.Namespace,
135-
Subsystem: subsystem,
136-
Name: "calls_block_by_number",
137-
Help: "Count of eth_getBlockByNumber rpc calls",
138-
}),
139125
}
140126
}
141127

0 commit comments

Comments
 (0)