eth/filters: Wrap Transaction with RPCTransaction

Raw Transaction objects don't serialize the "from" field in JSON
(because it's calculated from the signature). This wraps it in
an RPCTransaction object, which makes sure the from field gets
included.
This commit is contained in:
Austin Roberts 2020-04-23 09:48:40 -05:00
parent 24eb6797e2
commit b7f218671c

View file

@ -5,6 +5,8 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc"
)
@ -15,10 +17,37 @@ type dropNotification struct {
}
type rejectNotification struct {
Tx *types.Transaction `json:"tx"`
Tx *ethapi.RPCTransaction `json:"tx"`
Reason string `json:"reason"`
}
// newRPCTransaction returns a transaction that will serialize to the RPC
// representation, with the given location metadata set (if available).
func newRPCPendingTransaction(tx *types.Transaction) *ethapi.RPCTransaction {
var signer types.Signer = types.FrontierSigner{}
if tx.Protected() {
signer = types.NewEIP155Signer(tx.ChainId())
}
from, _ := types.Sender(signer, tx)
v, r, s := tx.RawSignatureValues()
result := &ethapi.RPCTransaction{
From: from,
Gas: hexutil.Uint64(tx.Gas()),
GasPrice: (*hexutil.Big)(tx.GasPrice()),
Hash: tx.Hash(),
Input: hexutil.Bytes(tx.Data()),
Nonce: hexutil.Uint64(tx.Nonce()),
To: tx.To(),
Value: (*hexutil.Big)(tx.Value()),
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
}
return result
}
// DroppedTransactions send a notification each time a transaction is dropped from the mempool
func (api *PublicFilterAPI) DroppedTransactions(ctx context.Context) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
@ -71,7 +100,7 @@ func (api *PublicFilterAPI) RejectedTransactions(ctx context.Context) (*rpc.Subs
if d.Reason != nil {
reason = d.Reason.Error()
}
notifier.Notify(rpcSub.ID, &rejectNotification{Tx: d.Tx, Reason: reason})
notifier.Notify(rpcSub.ID, &rejectNotification{Tx: newRPCPendingTransaction(d.Tx), Reason: reason})
case <-rpcSub.Err():
rejectedSub.Unsubscribe()
return