Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
force recheck
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Feb 1, 2024
1 parent 4751e4e commit 1f5b812
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
5 changes: 5 additions & 0 deletions cosmos/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func readConfigFromAppOptsParser(parser AppOptionsParser) (*Config, error) {
return nil, err
}

if conf.Polar.ForceForwardReCheckTxs, err = parser.GetBool(
flags.ForceForwardReCheckTxs); err != nil {
return nil, err
}

// Polar Miner settings
if conf.Polar.Miner.Etherbase, err =
parser.GetCommonAddress(flags.MinerEtherbase); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cosmos/config/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
RPCGasCap = "polaris.polar.rpc-gas-cap"
IsValidator = "polaris.polar.is-validator"
ValidatorJSONRPCEndpoint = "polaris.polar.validator-jsonrpc-endpoint"
ForceForwardReCheckTxs = "polaris.polar.force-forward-recheck-txs"

// Miner.
MinerEtherbase = "polaris.polar.miner.etherbase"
Expand Down
6 changes: 6 additions & 0 deletions cosmos/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ rpc-evm-timeout = "{{ .Polaris.Polar.RPCEVMTimeout }}"
# Transaction fee cap for RPC requests
rpc-tx-fee-cap = "{{ .Polaris.Polar.RPCTxFeeCap }}"
# JSON-RPC endpoint for forwarding ethereum transactions directly to validators.
validator-jsonrpc-endpoint = "{{ .Polaris.Polar.ValidatorJSONRPCEndpoint }}"
# Whether the node is a validator
is-validator = {{ .Polaris.Polar.IsValidator }}
# If we want to force forwarding on ReCheckTxs
force-forward-recheck-txs = {{ .Polaris.Polar.ForceForwardReCheckTxs }}
# Chain config
[polaris.polar.chain]
chain-id = "{{ .Polaris.Polar.Chain.ChainID }}"
Expand Down
1 change: 1 addition & 0 deletions cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func New(
&p.blockBuilderMu,
priceLimit,
p.cfg.Polar.IsValidator,
p.cfg.Polar.ForceForwardReCheckTxs,
p.cfg.Polar.ValidatorJSONRPCEndpoint,
)

Expand Down
5 changes: 3 additions & 2 deletions cosmos/runtime/txpool/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func (m *Mempool) AnteHandle(
telemetry.IncrCounter(float32(1), MetricKeyCometPoolTxs)
msgs := tx.GetMsgs()

// TODO: Record the time it takes to build a payload.

// We only want to eject transactions from comet on recheck.
if ctx.ExecMode() == sdk.ExecModeCheck || ctx.ExecMode() == sdk.ExecModeReCheck {
if wet, ok := utils.GetAs[*types.WrappedEthereumTransaction](msgs[0]); ok {
Expand All @@ -55,6 +53,9 @@ func (m *Mempool) AnteHandle(
); shouldEject {
telemetry.IncrCounter(float32(1), MetricKeyAnteEjectedTxs)
return ctx, errors.New("eject from comet mempool")
} else if ctx.ExecMode() == sdk.ExecModeReCheck && m.forceBroadcastOnRecheck {
// We optionally force a re-broadcast.
m.ForwardToValidator(ethTx)
}
}
}
Expand Down
56 changes: 31 additions & 25 deletions cosmos/runtime/txpool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ type Mempool struct {
blockBuilderMu *sync.RWMutex
priceLimit *big.Int

isValidator bool
validatorJSONRPC string
ethclient *ethclient.Client
isValidator bool
validatorJSONRPC string
forceBroadcastOnRecheck bool
ethclient *ethclient.Client
}

// New creates a new Mempool.
func New(
logger log.Logger,
chain core.ChainReader, txpool eth.TxPool, lifetime int64,
blockBuilderMu *sync.RWMutex, priceLimit *big.Int, isValidator bool,
blockBuilderMu *sync.RWMutex, priceLimit *big.Int, isValidator,
forceBroadcastOnRecheck bool,
validatorJSONRPC string,
) *Mempool {
var (
Expand Down Expand Up @@ -118,16 +120,17 @@ func New(
}

return &Mempool{
logger: logger,
TxPool: txpool,
chain: chain,
lifetime: lifetime,
crc: newCometRemoteCache(),
blockBuilderMu: blockBuilderMu,
priceLimit: priceLimit,
isValidator: isValidator,
validatorJSONRPC: validatorJSONRPC,
ethclient: ec,
logger: logger,
TxPool: txpool,
chain: chain,
lifetime: lifetime,
crc: newCometRemoteCache(),
blockBuilderMu: blockBuilderMu,
priceLimit: priceLimit,
isValidator: isValidator,
forceBroadcastOnRecheck: forceBroadcastOnRecheck,
validatorJSONRPC: validatorJSONRPC,
ethclient: ec,
}
}

Expand Down Expand Up @@ -170,17 +173,8 @@ func (m *Mempool) Insert(ctx context.Context, sdkTx sdk.Tx) error {
// Add the eth tx to the Geth txpool.
ethTx := wet.Unwrap()

// Optmistically send to the validator.
if m.ethclient != nil {
// Broadcast the transaction to the validator.
// Note: we don't care about the response here.
go func() {
sCtx.Logger().Info("broadcasting transaction to validator", "hash", ethTx.Hash().Hex())
if err := m.ethclient.SendTransaction(context.Background(), ethTx); err != nil {
sCtx.Logger().Error("failed to broadcast transaction to validator", "error", err)
}
}()
}
// Fowrad to a validator if we have one.
m.ForwardToValidator(ethTx)

// Insert the tx into the txpool as a remote.
m.blockBuilderMu.RLock()
Expand All @@ -204,6 +198,18 @@ func (m *Mempool) Insert(ctx context.Context, sdkTx sdk.Tx) error {
return nil
}

func (m *Mempool) ForwardToValidator(ethTx *ethtypes.Transaction) {
if m.ethclient != nil {
// Broadcast the transaction to the validator.
// Note: we don't care about the response here.
go func() {
if err := m.ethclient.SendTransaction(context.Background(), ethTx); err != nil {
m.logger.Error("failed to broadcast transaction to validator", "error", err)
}
}()
}
}

// CountTx returns the number of transactions currently in the mempool.
func (m *Mempool) CountTx() int {
runnable, blocked := m.TxPool.Stats()
Expand Down
10 changes: 9 additions & 1 deletion eth/polar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ type Config struct {
// send-transaction variants. The unit is ether.
RPCTxFeeCap float64

// ValidatorJSONRPCEndpoint is the JSON-RPC endpoint of a validator, you
// want to forward transactions to.
ValidatorJSONRPCEndpoint string
IsValidator bool

// IsValidator is a flag to indicate if the node is a validator.
IsValidator bool

// ForceForwardReCheckTxs is a flag to indicate if the node should forward
// transactions on recheck.
ForceForwardReCheckTxs bool
}

0 comments on commit 1f5b812

Please sign in to comment.