Skip to content

Commit 8c5ce11

Browse files
authored
eth/filters: send rpctransactions in pending-subscription (#26126)
This PR changes the pending tx subscription to return RPCTransaction types instead of normal Transaction objects. This will fix the inconsistencies with other tx returning API methods (i.e. getTransactionByHash), and also fill in the sender value for the tx. co-authored by @s1na
1 parent e34e540 commit 8c5ce11

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,14 @@ func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.Matche
915915
panic("not supported")
916916
}
917917

918+
func (fb *filterBackend) ChainConfig() *params.ChainConfig {
919+
panic("not supported")
920+
}
921+
922+
func (fb *filterBackend) CurrentHeader() *types.Header {
923+
panic("not supported")
924+
}
925+
918926
func nullSubscription() event.Subscription {
919927
return event.NewSubscription(func(quit <-chan struct{}) error {
920928
<-quit

eth/filters/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/common"
3030
"github.com/ethereum/go-ethereum/common/hexutil"
3131
"github.com/ethereum/go-ethereum/core/types"
32+
"github.com/ethereum/go-ethereum/internal/ethapi"
3233
"github.com/ethereum/go-ethereum/rpc"
3334
)
3435

@@ -147,15 +148,18 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
147148
go func() {
148149
txs := make(chan []*types.Transaction, 128)
149150
pendingTxSub := api.events.SubscribePendingTxs(txs)
151+
chainConfig := api.sys.backend.ChainConfig()
150152

151153
for {
152154
select {
153155
case txs := <-txs:
154156
// To keep the original behaviour, send a single tx hash in one notification.
155157
// TODO(rjl493456442) Send a batch of tx hashes in one notification
158+
latest := api.sys.backend.CurrentHeader()
156159
for _, tx := range txs {
157160
if fullTx != nil && *fullTx {
158-
notifier.Notify(rpcSub.ID, tx)
161+
rpcTx := ethapi.NewRPCPendingTransaction(tx, latest, chainConfig)
162+
notifier.Notify(rpcSub.ID, rpcTx)
159163
} else {
160164
notifier.Notify(rpcSub.ID, tx.Hash())
161165
}

eth/filters/filter_system.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/ethdb"
3434
"github.com/ethereum/go-ethereum/event"
3535
"github.com/ethereum/go-ethereum/log"
36+
"github.com/ethereum/go-ethereum/params"
3637
"github.com/ethereum/go-ethereum/rpc"
3738
lru "github.com/hashicorp/golang-lru"
3839
)
@@ -61,6 +62,8 @@ type Backend interface {
6162
GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error)
6263
PendingBlockAndReceipts() (*types.Block, types.Receipts)
6364

65+
CurrentHeader() *types.Header
66+
ChainConfig() *params.ChainConfig
6467
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
6568
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
6669
SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription

eth/filters/filter_system_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ type testBackend struct {
5050
chainFeed event.Feed
5151
}
5252

53+
func (b *testBackend) ChainConfig() *params.ChainConfig {
54+
panic("implement me")
55+
}
56+
57+
func (b *testBackend) CurrentHeader() *types.Header {
58+
panic("implement me")
59+
}
60+
5361
func (b *testBackend) ChainDb() ethdb.Database {
5462
return b.db
5563
}

internal/ethapi/api.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ func (s *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
171171
for account, txs := range pending {
172172
dump := make(map[string]*RPCTransaction)
173173
for _, tx := range txs {
174-
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
174+
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
175175
}
176176
content["pending"][account.Hex()] = dump
177177
}
178178
// Flatten the queued transactions
179179
for account, txs := range queue {
180180
dump := make(map[string]*RPCTransaction)
181181
for _, tx := range txs {
182-
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
182+
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
183183
}
184184
content["queued"][account.Hex()] = dump
185185
}
@@ -195,14 +195,14 @@ func (s *TxPoolAPI) ContentFrom(addr common.Address) map[string]map[string]*RPCT
195195
// Build the pending transactions
196196
dump := make(map[string]*RPCTransaction, len(pending))
197197
for _, tx := range pending {
198-
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
198+
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
199199
}
200200
content["pending"] = dump
201201

202202
// Build the queued transactions
203203
dump = make(map[string]*RPCTransaction, len(queue))
204204
for _, tx := range queue {
205-
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
205+
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, s.b.ChainConfig())
206206
}
207207
content["queued"] = dump
208208

@@ -1344,8 +1344,8 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
13441344
return result
13451345
}
13461346

1347-
// newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
1348-
func newRPCPendingTransaction(tx *types.Transaction, current *types.Header, config *params.ChainConfig) *RPCTransaction {
1347+
// NewRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
1348+
func NewRPCPendingTransaction(tx *types.Transaction, current *types.Header, config *params.ChainConfig) *RPCTransaction {
13491349
var baseFee *big.Int
13501350
blockNumber := uint64(0)
13511351
if current != nil {
@@ -1577,7 +1577,7 @@ func (s *TransactionAPI) GetTransactionByHash(ctx context.Context, hash common.H
15771577
}
15781578
// No finalized transaction, try to retrieve it from the pool
15791579
if tx := s.b.GetPoolTransaction(hash); tx != nil {
1580-
return newRPCPendingTransaction(tx, s.b.CurrentHeader(), s.b.ChainConfig()), nil
1580+
return NewRPCPendingTransaction(tx, s.b.CurrentHeader(), s.b.ChainConfig()), nil
15811581
}
15821582

15831583
// Transaction unknown, return as such
@@ -1847,7 +1847,7 @@ func (s *TransactionAPI) PendingTransactions() ([]*RPCTransaction, error) {
18471847
for _, tx := range pending {
18481848
from, _ := types.Sender(s.signer, tx)
18491849
if _, exists := accounts[from]; exists {
1850-
transactions = append(transactions, newRPCPendingTransaction(tx, curHeader, s.b.ChainConfig()))
1850+
transactions = append(transactions, NewRPCPendingTransaction(tx, curHeader, s.b.ChainConfig()))
18511851
}
18521852
}
18531853
return transactions, nil

internal/ethapi/backend.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"github.com/ethereum/go-ethereum/common"
2828
"github.com/ethereum/go-ethereum/consensus"
2929
"github.com/ethereum/go-ethereum/core"
30+
"github.com/ethereum/go-ethereum/core/bloombits"
3031
"github.com/ethereum/go-ethereum/core/state"
3132
"github.com/ethereum/go-ethereum/core/types"
3233
"github.com/ethereum/go-ethereum/core/vm"
33-
"github.com/ethereum/go-ethereum/eth/filters"
3434
"github.com/ethereum/go-ethereum/ethdb"
3535
"github.com/ethereum/go-ethereum/event"
3636
"github.com/ethereum/go-ethereum/params"
@@ -87,9 +87,15 @@ type Backend interface {
8787
ChainConfig() *params.ChainConfig
8888
Engine() consensus.Engine
8989

90+
// This is copied from filters.Backend
9091
// eth/filters needs to be initialized from this backend type, so methods needed by
9192
// it must also be included here.
92-
filters.Backend
93+
GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error)
94+
SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
95+
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
96+
SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
97+
BloomStatus() (uint64, uint64)
98+
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
9399
}
94100

95101
func GetAPIs(apiBackend Backend) []rpc.API {

0 commit comments

Comments
 (0)