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

feat(miner): Add NewPayloadTimeout to the miner. #1244

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cosmos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/grpc v1.58.1
google.golang.org/protobuf v1.31.0
pkg.berachain.dev/polaris/contracts v0.0.0-20231010191645-a5ed99983be4
pkg.berachain.dev/polaris/eth v0.0.0-20231010191645-a5ed99983be4
pkg.berachain.dev/polaris/lib v0.0.0-20231010191645-a5ed99983be4
pkg.berachain.dev/polaris/contracts v0.0.0-20231011003341-63d98bc34da2
pkg.berachain.dev/polaris/eth v0.0.0-20231022210635-216cf48c4787
pkg.berachain.dev/polaris/lib v0.0.0-20231011003341-63d98bc34da2
)

require (
Expand Down
12 changes: 6 additions & 6 deletions cosmos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1459,12 +1459,12 @@ nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
pkg.berachain.dev/polaris/contracts v0.0.0-20231010191645-a5ed99983be4 h1:q9gSJk2kQQbz/3cPcXGCW+JdQvxAm+7ukcDqzZiUcmw=
pkg.berachain.dev/polaris/contracts v0.0.0-20231010191645-a5ed99983be4/go.mod h1:LwuVx7lVFkIa/EXGNz2536i+YTK/mEU4A1vQJFZ8y4Q=
pkg.berachain.dev/polaris/eth v0.0.0-20231010191645-a5ed99983be4 h1:gOqKP7ZCzIU1NZrwdISnxswAatut/6Xi7JjdSpHbZk8=
pkg.berachain.dev/polaris/eth v0.0.0-20231010191645-a5ed99983be4/go.mod h1:HuT/i4PZCz3ItXAvDWHA31z4OZ10we6RH6xz+Fq0Nlw=
pkg.berachain.dev/polaris/lib v0.0.0-20231010191645-a5ed99983be4 h1:3zGPs5JJpBtysLMNWa5wKEhwC+j3p5AAK0gZ1fWuEc8=
pkg.berachain.dev/polaris/lib v0.0.0-20231010191645-a5ed99983be4/go.mod h1:OcMl9wH8872Q4++ikKXIm1FC4FbuGUWHxrjb6LCzvIY=
pkg.berachain.dev/polaris/contracts v0.0.0-20231011003341-63d98bc34da2 h1:Vd82baPNQh+6wUaj+qARezJ9dOHHdupJQncgj1UO1FQ=
pkg.berachain.dev/polaris/contracts v0.0.0-20231011003341-63d98bc34da2/go.mod h1:LwuVx7lVFkIa/EXGNz2536i+YTK/mEU4A1vQJFZ8y4Q=
pkg.berachain.dev/polaris/eth v0.0.0-20231022210635-216cf48c4787 h1:HH1KTrf4WpN0XieP26GLzKMy5YLJxj8CGVWX7olBWaY=
pkg.berachain.dev/polaris/eth v0.0.0-20231022210635-216cf48c4787/go.mod h1:Wo7D3qNsCaBcibLwCmSY2O7Rzc94sslbk+qBMA8KSl8=
pkg.berachain.dev/polaris/lib v0.0.0-20231011003341-63d98bc34da2 h1:XV+f50FxTAbJcQCIXPV6CzDiHsRj5YX+qlPjk+Hg1IA=
pkg.berachain.dev/polaris/lib v0.0.0-20231011003341-63d98bc34da2/go.mod h1:OcMl9wH8872Q4++ikKXIm1FC4FbuGUWHxrjb6LCzvIY=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
4 changes: 2 additions & 2 deletions cosmos/lib/ante/eject.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func (e *EjectOnRecheckTxDecorator) AnteHandle(
if wet, ok := utils.GetAs[*types.WrappedEthereumTransaction](msgs[0]); ok {
hash := wet.Unwrap().Hash()
e.seen[hash]++
if e.seen[hash] > 25 { //nolint:gomnd // temp fix.
if e.seen[hash] > 15 { //nolint:gomnd // temp fix.
delete(e.seen, hash) // prevent leak
return ctx, errors.New("recheck tx")
return ctx, errors.New("erroring to eject transaction from the comet bft mempool")
}
}

Expand Down
55 changes: 43 additions & 12 deletions cosmos/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package miner

import (
"context"
"time"

abci "github.com/cometbft/cometbft/abci/types"

Expand Down Expand Up @@ -50,12 +51,14 @@ type Miner struct {
eth.Miner
serializer EnvelopeSerializer
currentPayload *miner.Payload
payloadTimeout time.Duration
}

// New produces a cosmos miner from a geth miner.
func New(gm eth.Miner) *Miner {
func New(gm eth.Miner, payloadTimeout time.Duration) *Miner {
return &Miner{
Miner: gm,
Miner: gm,
payloadTimeout: payloadTimeout,
}
}

Expand All @@ -78,12 +81,12 @@ func (m *Miner) PrepareProposal(

// buildBlock builds and submits a payload, it also waits for the txs
// to resolve from the underying worker.
func (m *Miner) buildBlock(ctx sdk.Context) ([]byte, error) {
func (m *Miner) buildBlock(ctx context.Context) ([]byte, error) {
defer m.clearPayload()
if err := m.submitPayloadForBuilding(ctx); err != nil {
return nil, err
}
return m.resolveEnvelope(), nil
return m.resolveEnvelope(ctx, m.payloadTimeout)
}

// submitPayloadForBuilding submits a payload for building.
Expand All @@ -100,7 +103,6 @@ func (m *Miner) submitPayloadForBuilding(ctx context.Context) error {
return err
}
m.currentPayload = payload
sCtx.Logger().Info("submitted payload for building")
return nil
}

Expand All @@ -116,16 +118,45 @@ func (m *Miner) constructPayloadArgs(ctx sdk.Context) *miner.BuildPayloadArgs {
}

// resolveEnvelope resolves the payload.
func (m *Miner) resolveEnvelope() []byte {
func (m *Miner) resolveEnvelope(ctx context.Context, timeout time.Duration) ([]byte, error) {
sCtx := sdk.UnwrapSDKContext(ctx).Logger()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

resultChan := make(chan []byte, 1)
errChan := make(chan error, 1)

go m.resolvePayload(resultChan, errChan)

select {
case <-ctx.Done():
// If we timed out, return an empty payload.
// TODO: penalize validators for not being able to deliver the payload?
sCtx.Error("failed to resolve envelope, proposing empty payload", "err", ctx.Err())
return m.resolveEmptyPayload()
case result := <-resultChan:
sdk.UnwrapSDKContext(ctx).Logger().Info("successfully resolved envelope")
return result, <-errChan
}
}
Comment on lines +121 to +141
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolveEnvelope function now accepts a context.Context and a timeout argument. It creates a new context with the provided timeout and uses it to resolve the payload in a separate goroutine. If the context times out, it logs an error and returns an empty payload. This is a good practice as it allows for better control over timeouts and cancellations. However, the error handling could be improved. Currently, if the context times out, the function logs an error and returns an empty payload. It might be better to return the error to the caller so that they can decide how to handle it.

-		sCtx.Error("failed to resolve envelope, proposing empty payload", "err", ctx.Err())
-		return m.resolveEmptyPayload()
+		return nil, fmt.Errorf("failed to resolve envelope: %w", ctx.Err())
Committable suggestion (Beta)
Suggested change
func (m *Miner) resolveEnvelope(ctx context.Context, timeout time.Duration) ([]byte, error) {
sCtx := sdk.UnwrapSDKContext(ctx).Logger()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
resultChan := make(chan []byte, 1)
errChan := make(chan error, 1)
go m.resolvePayload(resultChan, errChan)
select {
case <-ctx.Done():
// If we timed out, return an empty payload.
// TODO: penalize validators for not being able to deliver the payload?
sCtx.Error("failed to resolve envelope, proposing empty payload", "err", ctx.Err())
return m.resolveEmptyPayload()
case result := <-resultChan:
sdk.UnwrapSDKContext(ctx).Logger().Info("successfully resolved envelope")
return result, <-errChan
}
}
return nil, fmt.Errorf("failed to resolve envelope: %w", ctx.Err())


// resolvePayload is a helper function to resolve the payload in a separate goroutine.
func (m *Miner) resolvePayload(resultChan chan []byte, errChan chan error) {
if m.currentPayload == nil {
return nil
resultChan <- nil
errChan <- nil
return
}
envelope := m.currentPayload.ResolveFull()
bz, err := m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
if err != nil {
panic(err)
}
return bz
result, err := m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
resultChan <- result
errChan <- err
}

// resolveEmptyPayload is a helper function to resolve the empty payload in a separate goroutine.
func (m *Miner) resolveEmptyPayload() ([]byte, error) {
envelope := m.currentPayload.ResolveEmpty()
return m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
}

// clearPayload clears the payload.
Expand Down
6 changes: 3 additions & 3 deletions cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ func New(
panic(err)
}

p.WrappedMiner = miner.New(p.Miner(), cfg.Polar.Miner.NewPayloadTimeout)
p.WrappedTxPool = txpool.New(p.TxPool())

return p
}

// Build is a function that sets up the Polaris struct.
// It takes a BaseApp and an EVMKeeper as arguments.
// It returns an error if the setup fails.
func (p *Polaris) Build(app CosmosApp, ek EVMKeeper) error {
p.WrappedTxPool = txpool.New(p.TxPool())
app.SetMempool(p.WrappedTxPool)

p.WrappedMiner = miner.New(p.Miner())
app.SetPrepareProposal(p.WrappedMiner.PrepareProposal)

if err := ek.Setup(p.Blockchain()); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions e2e/testapp/polard/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func initCometBFTConfig() *cmtcfg.Config {
consensus.TimeoutPrecommit = time.Second * 2
consensus.TimeoutCommit = time.Second * 2

// Increase Default Mempool size to 50000 (aggressive lol).
cfg.Mempool.Size = 50000
return cfg
}

Expand Down
Loading