Skip to content

Commit

Permalink
fixup: Switch to alias enum and move log handler to common
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Dec 10, 2024
1 parent 357372d commit c1d41d0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
21 changes: 1 addition & 20 deletions tests/fixture/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
Expand Down Expand Up @@ -76,25 +75,7 @@ func NewWallet(tc tests.TestContext, keychain *secp256k1fx.Keychain, nodeURI tmp
require.NoError(tc, err)
wallet := primary.NewWalletWithOptions(
baseWallet,
common.WithPostIssuanceHandler(
func(walletID rune, txID ids.ID, duration time.Duration) {
tc.Log().Info("issued transaction",
zap.String("walletID", string(walletID)),
zap.Stringer("txID", txID),
zap.Duration("duration", duration),
)
},
),
common.WithPostConfirmationHandler(
func(walletID rune, txID ids.ID, totalDuration time.Duration, issuanceToConfirmationDuration time.Duration) {
tc.Log().Info("confirmed transaction",
zap.String("walletID", string(walletID)),
zap.Stringer("txID", txID),
zap.Duration("totalDuration", totalDuration),
zap.Duration("issuanceToConfirmationDuration", issuanceToConfirmationDuration),
)
},
),
common.WithLoggedIssuranceAndConfirmation(tc.Log()),
// Use a low poll frequency to ensure more accurate determination of confirmation duration
common.WithPollFrequency(10*time.Millisecond),
)
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/txstest/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *client) IssueTx(
ops := common.NewOptions(options)
if f := ops.PostIssuanceHandler(); f != nil {
txID := tx.ID()
f('P', txID, time.Duration(0))
f(common.PChainAlias, txID, time.Duration(0))
}
ctx := ops.Context()
return c.backend.AcceptTx(ctx, tx)
Expand Down
6 changes: 2 additions & 4 deletions wallet/chain/c/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

var _ Wallet = (*wallet)(nil)

const WalletID = 'C'

type Wallet interface {
// Builder returns the builder that will be used to create the transactions.
Builder() Builder
Expand Down Expand Up @@ -159,7 +157,7 @@ func (w *wallet) IssueAtomicTx(
}

if f := ops.PostIssuanceHandler(); f != nil {
f(WalletID, txID, issuanceDuration)
f(common.PChainAlias, txID, issuanceDuration)
}

if ops.AssumeDecided() {
Expand All @@ -173,7 +171,7 @@ func (w *wallet) IssueAtomicTx(
issuanceToConfirmationDuration := totalDuration - issuanceDuration

if f := ops.PostConfirmationHandler(); f != nil {
f(WalletID, txID, totalDuration, issuanceToConfirmationDuration)
f(common.CChainAlias, txID, totalDuration, issuanceToConfirmationDuration)
}

return w.Backend.AcceptAtomicTx(ctx, tx)
Expand Down
6 changes: 3 additions & 3 deletions wallet/chain/p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var _ wallet.Client = (*Client)(nil)

const WalletID = 'P'
const chainAlias = "P"

func NewClient(
c platformvm.Client,
Expand Down Expand Up @@ -45,7 +45,7 @@ func (c *Client) IssueTx(
}

if f := ops.PostIssuanceHandler(); f != nil {
f(WalletID, txID, issuanceDuration)
f(chainAlias, txID, issuanceDuration)
}

if ops.AssumeDecided() {
Expand All @@ -59,7 +59,7 @@ func (c *Client) IssueTx(
issuanceToConfirmationDuration := totalDuration - issuanceDuration

if f := ops.PostConfirmationHandler(); f != nil {
f(WalletID, txID, totalDuration, issuanceToConfirmationDuration)
f(chainAlias, txID, totalDuration, issuanceToConfirmationDuration)
}

return c.backend.AcceptTx(ctx, tx)
Expand Down
6 changes: 2 additions & 4 deletions wallet/chain/x/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

var _ Wallet = (*wallet)(nil)

const WalletID = 'X'

type Wallet interface {
// Builder returns the builder that will be used to create the transactions.
Builder() builder.Builder
Expand Down Expand Up @@ -305,7 +303,7 @@ func (w *wallet) IssueTx(
}

if f := ops.PostIssuanceHandler(); f != nil {
f(WalletID, txID, issuanceDuration)
f(common.XChainAlias, txID, issuanceDuration)
}

if ops.AssumeDecided() {
Expand All @@ -319,7 +317,7 @@ func (w *wallet) IssueTx(
issuanceToConfirmationDuration := totalDuration - issuanceDuration

if f := ops.PostConfirmationHandler(); f != nil {
f(WalletID, txID, totalDuration, issuanceToConfirmationDuration)
f(common.XChainAlias, txID, totalDuration, issuanceToConfirmationDuration)
}

return w.backend.AcceptTx(ctx, tx)
Expand Down
45 changes: 40 additions & 5 deletions wallet/subnet/primary/common/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ import (
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

"go.uber.org/zap"

ethcommon "github.com/ethereum/go-ethereum/common"
)

const defaultPollFrequency = 100 * time.Millisecond
type PrimaryChainAlias string

const (
PChainAlias PrimaryChainAlias = "P"

Check failure on line 24 in wallet/subnet/primary/common/options.go

View workflow job for this annotation

GitHub Actions / Lint

SA9004: only the first constant in this group has an explicit type (staticcheck)
XChainAlias = "X"
CChainAlias = "C"

defaultPollFrequency = 100 * time.Millisecond
)

// Signature of the function that will be called after a transaction has been issued.
type TxIssuanceHandler func(
// Identifies the primary chain ('P', 'X' or 'C')
chainChar rune,
// Identifies the primary chain ("P", "X" or "C")
chainAlias PrimaryChainAlias,
// ID of the confirmed transaction
txID ids.ID,
// The time from initiation to issuance
Expand All @@ -29,8 +40,8 @@ type TxIssuanceHandler func(

// Signature of the function that will be called after a transaction has been confirmed.
type TxConfirmationHandler func(
// Identifies the primary chain ('P', 'X' or 'C')
chainChar rune,
// Identifies the primary chain ("P", "X" or "C")
chainAlias PrimaryChainAlias,
// ID of the confirmed transaction
txID ids.ID,
// The time from initiation to confirmation (includes duration of issuance)
Expand Down Expand Up @@ -234,3 +245,27 @@ func WithPostConfirmationHandler(f TxConfirmationHandler) Option {
o.postConfirmationHandler = f
}
}

func WithLoggedIssuranceAndConfirmation(log logging.Logger) Option {
return func(o *Options) {
WithPostIssuanceHandler(
func(chainAlias PrimaryChainAlias, txID ids.ID, duration time.Duration) {
log.Info("issued transaction",
zap.String("chainAlias", string(chainAlias)),
zap.Stringer("txID", txID),
zap.Duration("duration", duration),
)
},
)(o)
WithPostConfirmationHandler(
func(chainAlias PrimaryChainAlias, txID ids.ID, totalDuration time.Duration, issuanceToConfirmationDuration time.Duration) {
log.Info("confirmed transaction",
zap.String("chainAlias", string(chainAlias)),
zap.Stringer("txID", txID),
zap.Duration("totalDuration", totalDuration),
zap.Duration("issuanceToConfirmationDuration", issuanceToConfirmationDuration),
)
},
)(o)
}
}

0 comments on commit c1d41d0

Please sign in to comment.