-
Notifications
You must be signed in to change notification settings - Fork 21.4k
internal/ethapi: add eth_SendRawTransactionSync #32830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 14 commits
ca42c98
a33a1c9
0684a33
e7a48a5
e3fa487
1e697d9
4c5beb4
a463b32
3a3c46a
3d083d0
862c07e
91be12e
960d6c3
4c075f6
2ed437e
3aa3538
455f09d
1d662b5
3ab8ec8
409eea4
eed426c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,13 @@ | |
package ethclient | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common" | ||
|
@@ -696,6 +698,42 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er | |
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data)) | ||
} | ||
|
||
// SendRawTransactionSync submits a signed tx and waits for a receipt (or until | ||
// the optional timeout elapses on the server side). If timeout == 0, the server | ||
// uses its default. | ||
func (ec *Client) SendRawTransactionSync( | ||
ctx context.Context, | ||
tx *types.Transaction, | ||
timeout time.Duration, | ||
) (*types.Receipt, error) { | ||
var buf bytes.Buffer | ||
if err := tx.EncodeRLP(&buf); err != nil { | ||
|
||
return nil, err | ||
} | ||
return ec.SendRawTransactionSyncRaw(ctx, buf.Bytes(), timeout) | ||
} | ||
|
||
// SendRawTransactionSyncRaw is the low-level variant that takes the raw RLP. | ||
func (ec *Client) SendRawTransactionSyncRaw( | ||
ctx context.Context, | ||
rawTx []byte, | ||
timeout time.Duration, | ||
) (*types.Receipt, error) { | ||
var out *types.Receipt | ||
|
||
// Build params: raw bytes as hex, plus optional timeout as hexutil.Uint64 | ||
params := []any{hexutil.Bytes(rawTx)} | ||
if timeout > 0 { | ||
t := hexutil.Uint64(timeout.Milliseconds()) | ||
params = append(params, t) | ||
} | ||
|
||
if err := ec.c.CallContext(ctx, &out, "eth_sendRawTransactionSync", params...); err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
// RevertErrorData returns the 'revert reason' data of a contract call. | ||
// | ||
// This can be used with CallContract and EstimateGas, and only when the server is Geth. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1652,6 +1652,93 @@ func (api *TransactionAPI) SendRawTransaction(ctx context.Context, input hexutil | |
return SubmitTransaction(ctx, api.b, tx) | ||
} | ||
|
||
type ReceiptWithTx struct { | ||
Receipt *types.Receipt | ||
Transaction *types.Transaction | ||
} | ||
|
||
// SendRawTransactionSync will add the signed transaction to the transaction pool | ||
// and wait until the transaction has been included in a block and return the receipt, or the timeout. | ||
func (api *TransactionAPI) SendRawTransactionSync(ctx context.Context, input hexutil.Bytes, timeoutMs *hexutil.Uint64) (map[string]interface{}, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah it's annoying that the spec specifies the timeout in ms. In other places we use string like |
||
tx := new(types.Transaction) | ||
if err := tx.UnmarshalBinary(input); err != nil { | ||
return nil, err | ||
} | ||
hash, err := SubmitTransaction(ctx, api.b, tx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
maxTimeout := api.b.RPCTxSyncMaxTimeout() | ||
defaultTimeout := api.b.RPCTxSyncDefaultTimeout() | ||
|
||
timeout := defaultTimeout | ||
if timeoutMs != nil && *timeoutMs > 0 { | ||
req := time.Duration(*timeoutMs) * time.Millisecond | ||
if req > maxTimeout { | ||
timeout = maxTimeout | ||
} else { | ||
timeout = req | ||
} | ||
} | ||
|
||
receiptCtx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
// Fast path. | ||
if r, err := api.GetTransactionReceipt(receiptCtx, hash); err == nil && r != nil { | ||
return r, nil | ||
} | ||
|
||
// Subscribe to receipt stream (filtered to this tx) | ||
receipts := make(chan []*ReceiptWithTx, 1) | ||
sub := api.b.SubscribeTransactionReceipts([]common.Hash{hash}, receipts) | ||
defer sub.Unsubscribe() | ||
|
||
subErrCh := sub.Err() | ||
|
||
for { | ||
select { | ||
case <-receiptCtx.Done(): | ||
// Upstream cancellation -> bubble it; otherwise emit our timeout error | ||
if err := ctx.Err(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If deadline has passed it will still be returned here as an error. You can check the error against DeadlineExceeded to catch that case and return the proper error code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed in 3ab8ec8 |
||
return nil, err | ||
} | ||
return nil, &txSyncTimeoutError{ | ||
msg: fmt.Sprintf("The transaction was added to the transaction pool but wasn't processed in %v.", timeout), | ||
hash: hash, | ||
} | ||
|
||
case err, ok := <-subErrCh: | ||
if !ok || err == nil { | ||
// subscription closed; disable this case | ||
|
||
subErrCh = nil | ||
continue | ||
} | ||
return nil, err | ||
|
||
case batch := <-receipts: | ||
for _, rwt := range batch { | ||
if rwt == nil || rwt.Receipt == nil || rwt.Receipt.TxHash != hash { | ||
continue | ||
} | ||
|
||
if rwt.Receipt.BlockNumber != nil && rwt.Receipt.BlockHash != (common.Hash{}) { | ||
return MarshalReceipt( | ||
rwt.Receipt, | ||
rwt.Receipt.BlockHash, | ||
rwt.Receipt.BlockNumber.Uint64(), | ||
api.signer, | ||
rwt.Transaction, | ||
int(rwt.Receipt.TransactionIndex), | ||
), nil | ||
} | ||
return api.GetTransactionReceipt(receiptCtx, hash) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Sign calculates an ECDSA signature for: | ||
// keccak256("\x19Ethereum Signed Message:\n" + len(message) + message). | ||
// | ||
|
Uh oh!
There was an error while loading. Please reload this page.