Skip to content

Commit

Permalink
Revert "chore(cmd): remove --taiko.preconfirmationForwardingUrl flag (
Browse files Browse the repository at this point in the history
#362)"

This reverts commit 283fedd.
  • Loading branch information
davidtaikocha committed Jan 10, 2025
1 parent d954b86 commit 765520c
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func makeFullNode(ctx *cli.Context) *node.Node {
cfg.Eth.OverrideVerkle = &v
}

// CHANGE(taiko): set preconfirmation forwarding URL.
if ctx.IsSet(utils.PreconfirmationForwardingURLFlag.Name) {
cfg.Eth.PreconfirmationForwardingURL = ctx.String(utils.PreconfirmationForwardingURLFlag.Name)
}

backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

// CHANGE(TAIKO): register Taiko RPC APIs.
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func init() {
debug.Flags,
metricsFlags,
)
// CHANGE(taiko): append Taiko flags into the original GETH flags
app.Flags = append(app.Flags, utils.TaikoFlag, utils.PreconfirmationForwardingURLFlag)

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

Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/taiko_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ var (
Usage: "Taiko network",
Category: flags.TaikoCategory,
}
PreconfirmationForwardingURLFlag = &cli.StringFlag{
Name: "taiko.preconfirmationForwardingUrl",
Usage: "URL to forward RPC requests before confirmation",
Category: flags.TaikoCategory,
}
)

// RegisterTaikoAPIs initializes and registers the Taiko RPC APIs.
Expand Down
14 changes: 10 additions & 4 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ import (

// EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes
type EthAPIBackend struct {
extRPCEnabled bool
allowUnprotectedTxs bool
eth *Ethereum
gpo *gasprice.Oracle
extRPCEnabled bool
allowUnprotectedTxs bool
eth *Ethereum
gpo *gasprice.Oracle
preconfirmationForwardingURL string // CHANGE(taiko): add preconfirmation forwarding URL
}

// ChainConfig returns the active chain configuration.
Expand Down Expand Up @@ -431,3 +432,8 @@ func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, re
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
}

// GetPreconfirmationForwardingURL returns the URL to forward RPC requests before confirmation.
func (b *EthAPIBackend) GetPreconfirmationForwardingURL() string {
return b.preconfirmationForwardingURL
}
4 changes: 3 additions & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type Ethereum struct {
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)

shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully

PreconfirmationForwardingURL string // CHANGE(taiko): add preconfirmation forwarding URL
}

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

// CHANGE(taiko): set up the pre-confirmation forwarding URL
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil, config.PreconfirmationForwardingURL}
if eth.APIBackend.allowUnprotectedTxs {
log.Info("Unprotected transactions allowed")
}
Expand Down
3 changes: 3 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ type Config struct {

// OverrideVerkle (TODO: remove after the fork)
OverrideVerkle *uint64 `toml:",omitempty"`

// CHANGE(taiko): add preconfirmation forwarding URL
PreconfirmationForwardingURL string
}

// CreateConsensusEngine creates a consensus engine for the given chain config.
Expand Down
12 changes: 12 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,18 @@ func (api *TransactionAPI) FillTransaction(ctx context.Context, args Transaction
// SendRawTransaction will add the signed transaction to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce.
func (api *TransactionAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (common.Hash, error) {
// CHANGE(taiko): Forward the request to the preconf node if specified.
if forwardURL := api.b.GetPreconfirmationForwardingURL(); forwardURL != "" {
log.Info("Forwarding SendRawTransaction request", "forwardURL", forwardURL)
// Forward the raw transaction to the specified URL
h, err := forward[string](forwardURL, "eth_sendRawTransaction", []interface{}{input.String()})
if err == nil && h != nil {
return common.HexToHash(*h), nil
} else {
return common.Hash{}, err
}
}

tx := new(types.Transaction)
if err := tx.UnmarshalBinary(input); err != nil {
return common.Hash{}, err
Expand Down
5 changes: 5 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3400,3 +3400,8 @@ func testRPCResponseWithFile(t *testing.T, testid int, result interface{}, rpc s
func addressToHash(a common.Address) common.Hash {
return common.BytesToHash(a.Bytes())
}

// CHANGE(taiko): add preconfirmation forwarding URL
func (b testBackend) GetPreconfirmationForwardingURL() string {
return ""
}
3 changes: 3 additions & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type Backend interface {
SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
BloomStatus() (uint64, uint64)
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)

// CHANGE(taiko): add preconfirmation forwarding URL
GetPreconfirmationForwardingURL() string
}

func GetAPIs(apiBackend Backend) []rpc.API {
Expand Down
94 changes: 94 additions & 0 deletions internal/ethapi/taiko_preconf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ethapi

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/ethereum/go-ethereum/log"
)

type rpcRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
ID int `json:"id"`
}

type rpcResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result *json.RawMessage `json:"result"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
} `json:"error,omitempty"`
}

func forward[T any](forwardURL string, method string, params []interface{}) (*T, error) {
rpcReq := rpcRequest{
Jsonrpc: "2.0",
Method: method,
Params: params,
ID: 1,
}

jsonData, err := json.Marshal(rpcReq)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", forwardURL, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to forward transaction, status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var rpcResp rpcResponse

// Unmarshal the response into the struct
if err := json.Unmarshal(body, &rpcResp); err != nil {
return nil, err
}

// Check for errors in the response
if rpcResp.Error != nil {
err := fmt.Errorf("RPC error %d: %s", rpcResp.Error.Code, rpcResp.Error.Message)

log.Error("forwarded request error", "err", err, "method", method, "params", params)

return nil, fmt.Errorf("RPC error %d: %s", rpcResp.Error.Code, rpcResp.Error.Message)
}

if rpcResp.Result == nil {
log.Warn("forwarded request result is nil", "method", method)
return nil, nil
}

// Unmarshal the Result into the desired type
var result T
if err := json.Unmarshal(*rpcResp.Result, &result); err != nil {
return nil, err
}

return &result, nil
}
5 changes: 5 additions & 0 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,8 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
}

func (b *backendMock) Engine() consensus.Engine { return nil }

// CHANGE(taiko): add preconfirmation forwarding URL
func (b *backendMock) GetPreconfirmationForwardingURL() string {
return ""
}

0 comments on commit 765520c

Please sign in to comment.