Skip to content

chore: replace polling with subscription-based acceptance #4059

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

Merged
merged 5 commits into from
Jul 17, 2025
Merged
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
2 changes: 1 addition & 1 deletion tests/load2/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func main() {
chainID,
metricsNamespace,
registry,
load2.ZeroTransferTest{PollFrequency: pollFrequency},
load2.ZeroTransferTest{},
)
require.NoError(err)

Expand Down
9 changes: 3 additions & 6 deletions tests/load2/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package load2
import (
"context"
"math/big"
"time"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/types"
Expand All @@ -19,11 +18,9 @@ import (

var _ Test = (*ZeroTransferTest)(nil)

type ZeroTransferTest struct {
PollFrequency time.Duration
}
type ZeroTransferTest struct{}

func (z ZeroTransferTest) Run(
func (ZeroTransferTest) Run(
tc tests.TestContext,
ctx context.Context,
wallet *Wallet,
Expand All @@ -48,5 +45,5 @@ func (z ZeroTransferTest) Run(
})
require.NoError(err)

require.NoError(wallet.SendTx(ctx, tx, z.PollFrequency))
require.NoError(wallet.SendTx(ctx, tx))
}
75 changes: 51 additions & 24 deletions tests/load2/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ package load2
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"time"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/ethclient"

ethereum "github.com/ava-labs/libevm"
)

type Wallet struct {
Expand Down Expand Up @@ -47,8 +45,22 @@ func newWallet(
func (w *Wallet) SendTx(
ctx context.Context,
tx *types.Transaction,
pingFrequency time.Duration,
) error {
// start listening for blocks
headers := make(chan *types.Header)
sub, err := w.client.SubscribeNewHead(ctx, headers)
if err != nil {
return err
}

defer func() {
sub.Unsubscribe()

// wait for err chan to close before safely closing headers
<-sub.Err()
close(headers)
}()

startTime := time.Now()
if err := w.client.SendTransaction(ctx, tx); err != nil {
return err
Expand All @@ -57,7 +69,13 @@ func (w *Wallet) SendTx(
issuanceDuration := time.Since(startTime)
w.metrics.issue(issuanceDuration)

if err := awaitTx(ctx, w.client, tx.Hash(), pingFrequency); err != nil {
err = w.awaitTx(
ctx,
headers,
sub.Err(),
tx.Hash(),
)
if err != nil {
return err
}

Expand All @@ -70,32 +88,41 @@ func (w *Wallet) SendTx(
return nil
}

func awaitTx(
func (w Wallet) awaitTx(
ctx context.Context,
client *ethclient.Client,
headers chan *types.Header,
errs <-chan error,
txHash common.Hash,
pingFrequency time.Duration,
) error {
ticker := time.NewTicker(pingFrequency)
defer ticker.Stop()

account := crypto.PubkeyToAddress(w.privKey.PublicKey)
for {
receipt, err := client.TransactionReceipt(ctx, txHash)
if err == nil {
if receipt.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("failed tx: %d", receipt.Status)
}
return nil
}

if !errors.Is(err, ethereum.NotFound) {
return err
}

select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
case err := <-errs:
return err
case h := <-headers:
latestNonce, err := w.client.NonceAt(
ctx,
account,
h.Number,
)
if err != nil {
return err
}

if latestNonce == w.nonce+1 {
receipt, err := w.client.TransactionReceipt(ctx, txHash)
if err != nil {
return err
}

if receipt.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("failed tx: %d", receipt.Status)
}

return nil
}
}
}
}