Skip to content

Commit

Permalink
[BCI-1435] Improve EVM Client Readability (#9662)
Browse files Browse the repository at this point in the history
* added type assertion to client

* moved chain and client interfaces to common

* updated mocks

* Added concrete interface of methods in evm client

* renamed commontypes to types for headbroadcaster

* removed redundent declaration of function in evm client

* refactored commontypes to types for txmgr

---------

Co-authored-by: Prashant Yadav <34992934+prashantkumar1982@users.noreply.github.com>
  • Loading branch information
yongkangc and prashantkumar1982 authored Jun 21, 2023
1 parent 9c4d3d5 commit 171034e
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 244 deletions.
18 changes: 9 additions & 9 deletions common/headtracker/head_broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
"sync"
"time"

commontypes "github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

const TrackableCallbackTimeout = 2 * time.Second

type callbackSet[H commontypes.Head[BLOCK_HASH], BLOCK_HASH commontypes.Hashable] map[int]commontypes.HeadTrackable[H, BLOCK_HASH]
type callbackSet[H types.Head[BLOCK_HASH], BLOCK_HASH types.Hashable] map[int]types.HeadTrackable[H, BLOCK_HASH]

func (set callbackSet[H, BLOCK_HASH]) values() []commontypes.HeadTrackable[H, BLOCK_HASH] {
var values []commontypes.HeadTrackable[H, BLOCK_HASH]
func (set callbackSet[H, BLOCK_HASH]) values() []types.HeadTrackable[H, BLOCK_HASH] {
var values []types.HeadTrackable[H, BLOCK_HASH]
for _, callback := range set {
values = append(values, callback)
}
return values
}

type HeadBroadcaster[H commontypes.Head[BLOCK_HASH], BLOCK_HASH commontypes.Hashable] struct {
type HeadBroadcaster[H types.Head[BLOCK_HASH], BLOCK_HASH types.Hashable] struct {
logger logger.Logger
callbacks callbackSet[H, BLOCK_HASH]
mailbox *utils.Mailbox[H]
Expand All @@ -38,8 +38,8 @@ type HeadBroadcaster[H commontypes.Head[BLOCK_HASH], BLOCK_HASH commontypes.Hash

// NewHeadBroadcaster creates a new HeadBroadcaster
func NewHeadBroadcaster[
H commontypes.Head[BLOCK_HASH],
BLOCK_HASH commontypes.Hashable,
H types.Head[BLOCK_HASH],
BLOCK_HASH types.Hashable,
](
lggr logger.Logger,
) *HeadBroadcaster[H, BLOCK_HASH] {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (hb *HeadBroadcaster[H, BLOCK_HASH]) BroadcastNewLongestChain(head H) {

// Subscribe subscribes to OnNewLongestChain and Connect until HeadBroadcaster is closed,
// or unsubscribe callback is called explicitly
func (hb *HeadBroadcaster[H, BLOCK_HASH]) Subscribe(callback commontypes.HeadTrackable[H, BLOCK_HASH]) (currentLongestChain H, unsubscribe func()) {
func (hb *HeadBroadcaster[H, BLOCK_HASH]) Subscribe(callback types.HeadTrackable[H, BLOCK_HASH]) (currentLongestChain H, unsubscribe func()) {
hb.mutex.Lock()
defer hb.mutex.Unlock()

Expand Down Expand Up @@ -147,7 +147,7 @@ func (hb *HeadBroadcaster[H, BLOCK_HASH]) executeCallbacks() {
defer cancel()

for _, callback := range callbacks {
go func(trackable commontypes.HeadTrackable[H, BLOCK_HASH]) {
go func(trackable types.HeadTrackable[H, BLOCK_HASH]) {
defer wg.Done()
start := time.Now()
cctx, cancel := context.WithTimeout(ctx, TrackableCallbackTimeout)
Expand Down
5 changes: 2 additions & 3 deletions common/headtracker/head_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"

htrktypes "github.com/smartcontractkit/chainlink/v2/common/headtracker/types"
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/utils"
Expand All @@ -30,7 +29,7 @@ var (
type HeadListener[
HTH htrktypes.Head[BLOCK_HASH, ID],
S types.Subscription,
ID txmgrtypes.ID,
ID types.ID,
BLOCK_HASH types.Hashable,
] struct {
config htrktypes.Config
Expand All @@ -46,7 +45,7 @@ type HeadListener[
func NewHeadListener[
HTH htrktypes.Head[BLOCK_HASH, ID],
S types.Subscription,
ID txmgrtypes.ID,
ID types.ID,
BLOCK_HASH types.Hashable,
CLIENT htrktypes.Client[HTH, S, ID, BLOCK_HASH],
](
Expand Down
29 changes: 14 additions & 15 deletions common/headtracker/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
"golang.org/x/exp/maps"

htrktypes "github.com/smartcontractkit/chainlink/v2/common/headtracker/types"
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
commontypes "github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/utils"
Expand All @@ -37,13 +36,13 @@ const HeadsBufferSize = 10

type HeadTracker[
HTH htrktypes.Head[BLOCK_HASH, ID],
S commontypes.Subscription,
ID txmgrtypes.ID,
BLOCK_HASH commontypes.Hashable,
S types.Subscription,
ID types.ID,
BLOCK_HASH types.Hashable,
] struct {
log logger.Logger
headBroadcaster commontypes.HeadBroadcaster[HTH, BLOCK_HASH]
headSaver commontypes.HeadSaver[HTH, BLOCK_HASH]
headBroadcaster types.HeadBroadcaster[HTH, BLOCK_HASH]
headSaver types.HeadSaver[HTH, BLOCK_HASH]
mailMon *utils.MailboxMonitor
client htrktypes.Client[HTH, S, ID, BLOCK_HASH]
chainID ID
Expand All @@ -52,7 +51,7 @@ type HeadTracker[

backfillMB *utils.Mailbox[HTH]
broadcastMB *utils.Mailbox[HTH]
headListener commontypes.HeadListener[HTH, BLOCK_HASH]
headListener types.HeadListener[HTH, BLOCK_HASH]
chStop utils.StopChan
wgDone sync.WaitGroup
utils.StartStopOnce
Expand All @@ -62,19 +61,19 @@ type HeadTracker[
// NewHeadTracker instantiates a new HeadTracker using HeadSaver to persist new block numbers.
func NewHeadTracker[
HTH htrktypes.Head[BLOCK_HASH, ID],
S commontypes.Subscription,
ID txmgrtypes.ID,
BLOCK_HASH commontypes.Hashable,
S types.Subscription,
ID types.ID,
BLOCK_HASH types.Hashable,
](
lggr logger.Logger,
client htrktypes.Client[HTH, S, ID, BLOCK_HASH],
config htrktypes.Config,
htConfig htrktypes.HeadTrackerConfig,
headBroadcaster commontypes.HeadBroadcaster[HTH, BLOCK_HASH],
headSaver commontypes.HeadSaver[HTH, BLOCK_HASH],
headBroadcaster types.HeadBroadcaster[HTH, BLOCK_HASH],
headSaver types.HeadSaver[HTH, BLOCK_HASH],
mailMon *utils.MailboxMonitor,
getNilHead func() HTH,
) commontypes.HeadTracker[HTH, BLOCK_HASH] {
) types.HeadTracker[HTH, BLOCK_HASH] {
chStop := make(chan struct{})
lggr = lggr.Named("HeadTracker")
return &HeadTracker[HTH, S, ID, BLOCK_HASH]{
Expand Down Expand Up @@ -305,7 +304,7 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) backfillLoop() {
}

// backfill fetches all missing heads up until the base height
func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) backfill(ctx context.Context, head commontypes.Head[BLOCK_HASH], baseHeight int64) (err error) {
func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) backfill(ctx context.Context, head types.Head[BLOCK_HASH], baseHeight int64) (err error) {
headBlockNumber := head.BlockNumber()
if headBlockNumber <= baseHeight {
return nil
Expand Down
5 changes: 2 additions & 3 deletions common/headtracker/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"math/big"

txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
commontypes "github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
)

type Client[H commontypes.Head[BLOCK_HASH], S commontypes.Subscription, ID txmgrtypes.ID, BLOCK_HASH commontypes.Hashable] interface {
type Client[H types.Head[BLOCK_HASH], S types.Subscription, ID types.ID, BLOCK_HASH types.Hashable] interface {
HeadByNumber(ctx context.Context, number *big.Int) (head H, err error)
// ConfiguredChainID returns the chain ID that the node is configured to connect to
ConfiguredChainID() (id ID)
Expand Down
3 changes: 1 addition & 2 deletions common/headtracker/types/head.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package types

import (
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
)

//go:generate mockery --quiet --name Head --output ./mocks/ --case=underscore
type Head[BLOCK_HASH types.Hashable, CHAIN_ID txmgrtypes.ID] interface {
type Head[BLOCK_HASH types.Hashable, CHAIN_ID types.ID] interface {
types.Head[BLOCK_HASH]
// ChainID returns the chain ID that the head is for
ChainID() CHAIN_ID
Expand Down
5 changes: 2 additions & 3 deletions common/headtracker/types/mocks/head.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions common/txmgr/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ type ProcessUnstartedTxs[ADDR types.Hashable] func(ctx context.Context, fromAddr

// TransmitCheckerFactory creates a transmit checker based on a spec.
type TransmitCheckerFactory[
CHAIN_ID txmgrtypes.ID,
CHAIN_ID types.ID,
ADDR types.Hashable,
TX_HASH, BLOCK_HASH types.Hashable,
SEQ txmgrtypes.Sequence,
SEQ types.Sequence,
FEE feetypes.Fee,
] interface {
// BuildChecker builds a new TransmitChecker based on the given spec.
Expand All @@ -69,10 +69,10 @@ type TransmitCheckerFactory[

// TransmitChecker determines whether a transaction should be submitted on-chain.
type TransmitChecker[
CHAIN_ID txmgrtypes.ID,
CHAIN_ID types.ID,
ADDR types.Hashable,
TX_HASH, BLOCK_HASH types.Hashable,
SEQ txmgrtypes.Sequence,
SEQ types.Sequence,
FEE feetypes.Fee,
] interface {

Expand All @@ -97,12 +97,12 @@ type TransmitChecker[
// - transition of eth_txes out of unstarted into either fatal_error or unconfirmed
// - existence of a saved eth_tx_attempt
type Broadcaster[
CHAIN_ID txmgrtypes.ID,
CHAIN_ID types.ID,
HEAD types.Head[BLOCK_HASH],
ADDR types.Hashable,
TX_HASH types.Hashable,
BLOCK_HASH types.Hashable,
SEQ txmgrtypes.Sequence,
SEQ types.Sequence,
FEE feetypes.Fee,
] struct {
logger logger.Logger
Expand Down Expand Up @@ -146,12 +146,12 @@ type Broadcaster[
}

func NewBroadcaster[
CHAIN_ID txmgrtypes.ID,
CHAIN_ID types.ID,
HEAD types.Head[BLOCK_HASH],
ADDR types.Hashable,
TX_HASH types.Hashable,
BLOCK_HASH types.Hashable,
SEQ txmgrtypes.Sequence,
SEQ types.Sequence,
FEE feetypes.Fee,
](
txStore txmgrtypes.TransactionStore[ADDR, CHAIN_ID, TX_HASH, BLOCK_HASH, SEQ, FEE],
Expand Down Expand Up @@ -767,7 +767,7 @@ func (eb *Broadcaster[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) incr
return eb.ks.IncrementNextSequence(address, eb.chainID, currentSequence, qopts...)
}

func observeTimeUntilBroadcast[CHAIN_ID txmgrtypes.ID](chainID CHAIN_ID, createdAt, broadcastAt time.Time) {
func observeTimeUntilBroadcast[CHAIN_ID types.ID](chainID CHAIN_ID, createdAt, broadcastAt time.Time) {
duration := float64(broadcastAt.Sub(createdAt))
promTimeUntilBroadcast.WithLabelValues(chainID.String()).Observe(duration)
}
Loading

0 comments on commit 171034e

Please sign in to comment.