Skip to content

Commit 765520c

Browse files
committed
Revert "chore(cmd): remove --taiko.preconfirmationForwardingUrl flag (#362)"
This reverts commit 283fedd.
1 parent d954b86 commit 765520c

File tree

11 files changed

+147
-5
lines changed

11 files changed

+147
-5
lines changed

cmd/geth/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ func makeFullNode(ctx *cli.Context) *node.Node {
192192
cfg.Eth.OverrideVerkle = &v
193193
}
194194

195+
// CHANGE(taiko): set preconfirmation forwarding URL.
196+
if ctx.IsSet(utils.PreconfirmationForwardingURLFlag.Name) {
197+
cfg.Eth.PreconfirmationForwardingURL = ctx.String(utils.PreconfirmationForwardingURLFlag.Name)
198+
}
199+
195200
backend, eth := utils.RegisterEthService(stack, &cfg.Eth)
196201

197202
// CHANGE(TAIKO): register Taiko RPC APIs.

cmd/geth/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ func init() {
260260
debug.Flags,
261261
metricsFlags,
262262
)
263+
// CHANGE(taiko): append Taiko flags into the original GETH flags
264+
app.Flags = append(app.Flags, utils.TaikoFlag, utils.PreconfirmationForwardingURLFlag)
263265

264266
flags.AutoEnvVars(app.Flags, "GETH")
265267

cmd/utils/taiko_flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ var (
1818
Usage: "Taiko network",
1919
Category: flags.TaikoCategory,
2020
}
21+
PreconfirmationForwardingURLFlag = &cli.StringFlag{
22+
Name: "taiko.preconfirmationForwardingUrl",
23+
Usage: "URL to forward RPC requests before confirmation",
24+
Category: flags.TaikoCategory,
25+
}
2126
)
2227

2328
// RegisterTaikoAPIs initializes and registers the Taiko RPC APIs.

eth/api_backend.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ import (
4444

4545
// EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes
4646
type EthAPIBackend struct {
47-
extRPCEnabled bool
48-
allowUnprotectedTxs bool
49-
eth *Ethereum
50-
gpo *gasprice.Oracle
47+
extRPCEnabled bool
48+
allowUnprotectedTxs bool
49+
eth *Ethereum
50+
gpo *gasprice.Oracle
51+
preconfirmationForwardingURL string // CHANGE(taiko): add preconfirmation forwarding URL
5152
}
5253

5354
// ChainConfig returns the active chain configuration.
@@ -431,3 +432,8 @@ func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, re
431432
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
432433
return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
433434
}
435+
436+
// GetPreconfirmationForwardingURL returns the URL to forward RPC requests before confirmation.
437+
func (b *EthAPIBackend) GetPreconfirmationForwardingURL() string {
438+
return b.preconfirmationForwardingURL
439+
}

eth/backend.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type Ethereum struct {
9696
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
9797

9898
shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully
99+
100+
PreconfirmationForwardingURL string // CHANGE(taiko): add preconfirmation forwarding URL
99101
}
100102

101103
// New creates a new Ethereum object (including the initialisation of the common Ethereum object),
@@ -255,7 +257,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
255257
eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData))
256258

257259
// CHANGE(taiko): set up the pre-confirmation forwarding URL
258-
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
260+
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil, config.PreconfirmationForwardingURL}
259261
if eth.APIBackend.allowUnprotectedTxs {
260262
log.Info("Unprotected transactions allowed")
261263
}

eth/ethconfig/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ type Config struct {
157157

158158
// OverrideVerkle (TODO: remove after the fork)
159159
OverrideVerkle *uint64 `toml:",omitempty"`
160+
161+
// CHANGE(taiko): add preconfirmation forwarding URL
162+
PreconfirmationForwardingURL string
160163
}
161164

162165
// CreateConsensusEngine creates a consensus engine for the given chain config.

internal/ethapi/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,18 @@ func (api *TransactionAPI) FillTransaction(ctx context.Context, args Transaction
19741974
// SendRawTransaction will add the signed transaction to the transaction pool.
19751975
// The sender is responsible for signing the transaction and using the correct nonce.
19761976
func (api *TransactionAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (common.Hash, error) {
1977+
// CHANGE(taiko): Forward the request to the preconf node if specified.
1978+
if forwardURL := api.b.GetPreconfirmationForwardingURL(); forwardURL != "" {
1979+
log.Info("Forwarding SendRawTransaction request", "forwardURL", forwardURL)
1980+
// Forward the raw transaction to the specified URL
1981+
h, err := forward[string](forwardURL, "eth_sendRawTransaction", []interface{}{input.String()})
1982+
if err == nil && h != nil {
1983+
return common.HexToHash(*h), nil
1984+
} else {
1985+
return common.Hash{}, err
1986+
}
1987+
}
1988+
19771989
tx := new(types.Transaction)
19781990
if err := tx.UnmarshalBinary(input); err != nil {
19791991
return common.Hash{}, err

internal/ethapi/api_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,3 +3400,8 @@ func testRPCResponseWithFile(t *testing.T, testid int, result interface{}, rpc s
34003400
func addressToHash(a common.Address) common.Hash {
34013401
return common.BytesToHash(a.Bytes())
34023402
}
3403+
3404+
// CHANGE(taiko): add preconfirmation forwarding URL
3405+
func (b testBackend) GetPreconfirmationForwardingURL() string {
3406+
return ""
3407+
}

internal/ethapi/backend.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ type Backend interface {
9696
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
9797
BloomStatus() (uint64, uint64)
9898
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
99+
100+
// CHANGE(taiko): add preconfirmation forwarding URL
101+
GetPreconfirmationForwardingURL() string
99102
}
100103

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

internal/ethapi/taiko_preconf.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ethapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
10+
"github.com/ethereum/go-ethereum/log"
11+
)
12+
13+
type rpcRequest struct {
14+
Jsonrpc string `json:"jsonrpc"`
15+
Method string `json:"method"`
16+
Params []interface{} `json:"params"`
17+
ID int `json:"id"`
18+
}
19+
20+
type rpcResponse struct {
21+
Jsonrpc string `json:"jsonrpc"`
22+
ID int `json:"id"`
23+
Result *json.RawMessage `json:"result"`
24+
Error *struct {
25+
Code int `json:"code"`
26+
Message string `json:"message"`
27+
Data string `json:"data"`
28+
} `json:"error,omitempty"`
29+
}
30+
31+
func forward[T any](forwardURL string, method string, params []interface{}) (*T, error) {
32+
rpcReq := rpcRequest{
33+
Jsonrpc: "2.0",
34+
Method: method,
35+
Params: params,
36+
ID: 1,
37+
}
38+
39+
jsonData, err := json.Marshal(rpcReq)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
req, err := http.NewRequest("POST", forwardURL, bytes.NewBuffer(jsonData))
45+
if err != nil {
46+
return nil, err
47+
}
48+
req.Header.Set("Content-Type", "application/json")
49+
50+
client := &http.Client{}
51+
resp, err := client.Do(req)
52+
if err != nil {
53+
return nil, err
54+
}
55+
defer resp.Body.Close()
56+
57+
if resp.StatusCode != http.StatusOK {
58+
return nil, fmt.Errorf("failed to forward transaction, status code: %d", resp.StatusCode)
59+
}
60+
61+
body, err := io.ReadAll(resp.Body)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
var rpcResp rpcResponse
67+
68+
// Unmarshal the response into the struct
69+
if err := json.Unmarshal(body, &rpcResp); err != nil {
70+
return nil, err
71+
}
72+
73+
// Check for errors in the response
74+
if rpcResp.Error != nil {
75+
err := fmt.Errorf("RPC error %d: %s", rpcResp.Error.Code, rpcResp.Error.Message)
76+
77+
log.Error("forwarded request error", "err", err, "method", method, "params", params)
78+
79+
return nil, fmt.Errorf("RPC error %d: %s", rpcResp.Error.Code, rpcResp.Error.Message)
80+
}
81+
82+
if rpcResp.Result == nil {
83+
log.Warn("forwarded request result is nil", "method", method)
84+
return nil, nil
85+
}
86+
87+
// Unmarshal the Result into the desired type
88+
var result T
89+
if err := json.Unmarshal(*rpcResp.Result, &result); err != nil {
90+
return nil, err
91+
}
92+
93+
return &result, nil
94+
}

internal/ethapi/transaction_args_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,8 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
405405
}
406406

407407
func (b *backendMock) Engine() consensus.Engine { return nil }
408+
409+
// CHANGE(taiko): add preconfirmation forwarding URL
410+
func (b *backendMock) GetPreconfirmationForwardingURL() string {
411+
return ""
412+
}

0 commit comments

Comments
 (0)