Skip to content

Commit afa0f9b

Browse files
authored
chains/txmgr/types: simpler KeyStore (#29)
1 parent 586bd76 commit afa0f9b

13 files changed

Lines changed: 426 additions & 505 deletions

File tree

.github/workflows/golangci_lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
go-version-file: ${{ matrix.module }}/go.mod
3535

3636
- name: Run golangci-lint for ${{ matrix.module }}
37-
uses: golangci/golangci-lint-action@v3
37+
uses: golangci/golangci-lint-action@4696ba8babb6127d732c3c6dde519db15edab9ea # v6.5.1
3838
with:
3939
args: --config=${{ github.workspace }}/.golangci.yml
4040
only-new-issues: true

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ linters-settings:
3131
default-signifies-exhaustive: true
3232
goimports:
3333
local-prefixes: github.com/smartcontractkit/chainlink
34-
golint:
35-
min-confidence: 1.0
3634
gosec:
3735
excludes:
3836
- G101

chains/heads/tracker.go

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,37 @@ type headPair[HTH any] struct {
6565
prevHead HTH
6666
}
6767

68-
type tracker[
69-
HTH Head[BLOCK_HASH, ID],
70-
S chains.Subscription,
71-
ID chains.ID,
72-
BLOCK_HASH chains.Hashable,
73-
] struct {
68+
type tracker[HTH Head[BHASH, ID], S chains.Subscription, ID chains.ID, BHASH chains.Hashable] struct {
7469
services.Service
7570
eng *services.Engine
7671

7772
log logger.SugaredLogger
78-
headBroadcaster Broadcaster[HTH, BLOCK_HASH]
79-
headSaver Saver[HTH, BLOCK_HASH]
73+
headBroadcaster Broadcaster[HTH, BHASH]
74+
headSaver Saver[HTH, BHASH]
8075
mailMon *mailbox.Monitor
81-
client Client[HTH, S, ID, BLOCK_HASH]
76+
client Client[HTH, S, ID, BHASH]
8277
chainID chains.ID
8378
config ChainConfig
8479
htConfig TrackerConfig
8580

8681
backfillMB *mailbox.Mailbox[headPair[HTH]]
8782
broadcastMB *mailbox.Mailbox[HTH]
88-
headListener Listener[HTH, BLOCK_HASH]
83+
headListener Listener[HTH, BHASH]
8984
getNilHead func() HTH
9085
}
9186

9287
// NewTracker instantiates a new Tracker using Saver to persist new block numbers.
93-
func NewTracker[
94-
HTH Head[BLOCK_HASH, ID],
95-
S chains.Subscription,
96-
ID chains.ID,
97-
BLOCK_HASH chains.Hashable,
98-
](
88+
func NewTracker[HTH Head[BHASH, ID], S chains.Subscription, ID chains.ID, BHASH chains.Hashable](
9989
lggr logger.Logger,
100-
client Client[HTH, S, ID, BLOCK_HASH],
90+
client Client[HTH, S, ID, BHASH],
10191
config ChainConfig,
10292
htConfig TrackerConfig,
103-
headBroadcaster Broadcaster[HTH, BLOCK_HASH],
104-
headSaver Saver[HTH, BLOCK_HASH],
93+
headBroadcaster Broadcaster[HTH, BHASH],
94+
headSaver Saver[HTH, BHASH],
10595
mailMon *mailbox.Monitor,
10696
getNilHead func() HTH,
107-
) Tracker[HTH, BLOCK_HASH] {
108-
ht := &tracker[HTH, S, ID, BLOCK_HASH]{
97+
) Tracker[HTH, BHASH] {
98+
ht := &tracker[HTH, S, ID, BHASH]{
10999
headBroadcaster: headBroadcaster,
110100
client: client,
111101
chainID: client.ConfiguredChainID(),
@@ -120,7 +110,7 @@ func NewTracker[
120110
ht.Service, ht.eng = services.Config{
121111
Name: "HeadTracker",
122112
NewSubServices: func(lggr logger.Logger) []services.Service {
123-
ht.headListener = NewListener[HTH, S, ID, BLOCK_HASH](lggr, client, config,
113+
ht.headListener = NewListener[HTH, S, ID, BHASH](lggr, client, config,
124114
// NOTE: Always try to start the head tracker off with whatever the
125115
// latest head is, without waiting for the subscription to send us one.
126116
//
@@ -144,7 +134,7 @@ func NewTracker[
144134
}
145135

146136
// Start starts Tracker service.
147-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) start(context.Context) error {
137+
func (t *tracker[HTH, S, ID, BHASH]) start(context.Context) error {
148138
t.eng.Go(t.backfillLoop)
149139
t.eng.Go(t.broadcastLoop)
150140

@@ -153,7 +143,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) start(context.Context) error {
153143
return nil
154144
}
155145

156-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) handleInitialHead(ctx context.Context) error {
146+
func (t *tracker[HTH, S, ID, BHASH]) handleInitialHead(ctx context.Context) error {
157147
initialHead, err := t.client.HeadByNumber(ctx, nil)
158148
if err != nil {
159149
return fmt.Errorf("failed to fetch initial head: %w", err)
@@ -196,12 +186,12 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) handleInitialHead(ctx context.Context)
196186
return nil
197187
}
198188

199-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) close() error {
189+
func (t *tracker[HTH, S, ID, BHASH]) close() error {
200190
return t.broadcastMB.Close()
201191
}
202192

203193
// verifyFinalizedBlockHashes returns finality violated error if a block hash mismatch is found in provided chains
204-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) verifyFinalizedBlockHashes(finalizedHeadWithChain chains.Head[BLOCK_HASH], prevHeadWithChain chains.Head[BLOCK_HASH]) error {
194+
func (t *tracker[HTH, S, ID, BHASH]) verifyFinalizedBlockHashes(finalizedHeadWithChain chains.Head[BHASH], prevHeadWithChain chains.Head[BHASH]) error {
205195
if finalizedHeadWithChain == nil || prevHeadWithChain == nil {
206196
return nil
207197
}
@@ -232,11 +222,11 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) verifyFinalizedBlockHashes(finalizedHe
232222
return nil
233223
}
234224

235-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) instantFinality() bool {
225+
func (t *tracker[HTH, S, ID, BHASH]) instantFinality() bool {
236226
return !t.config.FinalityTagEnabled() && t.config.FinalityDepth() == 0 && t.config.FinalizedBlockOffset() == 0
237227
}
238228

239-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) Backfill(ctx context.Context, headWithChain HTH, prevHeadWithChain HTH) (err error) {
229+
func (t *tracker[HTH, S, ID, BHASH]) Backfill(ctx context.Context, headWithChain HTH, prevHeadWithChain HTH) (err error) {
240230
latestFinalized, err := t.calculateLatestFinalized(ctx, headWithChain, t.htConfig.FinalityTagBypass())
241231
if err != nil {
242232
return fmt.Errorf("failed to calculate finalized block: %w", err)
@@ -273,11 +263,11 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) Backfill(ctx context.Context, headWith
273263
return t.backfill(ctx, headWithChain, latestFinalized)
274264
}
275265

276-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) LatestChain() HTH {
266+
func (t *tracker[HTH, S, ID, BHASH]) LatestChain() HTH {
277267
return t.headSaver.LatestChain()
278268
}
279269

280-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) handleNewHead(ctx context.Context, head HTH) error {
270+
func (t *tracker[HTH, S, ID, BHASH]) handleNewHead(ctx context.Context, head HTH) error {
281271
prevHead := t.headSaver.LatestChain()
282272

283273
t.log.Debugw(fmt.Sprintf("Received new head %v", head.BlockNumber()),
@@ -288,7 +278,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) handleNewHead(ctx context.Context, hea
288278
"blockDifficulty", head.BlockDifficulty(),
289279
)
290280

291-
var prevLatestFinalized chains.Head[BLOCK_HASH]
281+
var prevLatestFinalized chains.Head[BHASH]
292282
if prevHead.IsValid() {
293283
prevLatestFinalized = prevHead.LatestFinalizedHead()
294284
}
@@ -345,7 +335,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) handleNewHead(ctx context.Context, hea
345335
return nil
346336
}
347337

348-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) broadcastLoop(ctx context.Context) {
338+
func (t *tracker[HTH, S, ID, BHASH]) broadcastLoop(ctx context.Context) {
349339
samplingInterval := t.htConfig.SamplingInterval()
350340
if samplingInterval > 0 {
351341
t.log.Debugf("Head sampling is enabled - sampling interval is set to: %v", samplingInterval)
@@ -382,7 +372,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) broadcastLoop(ctx context.Context) {
382372
}
383373
}
384374

385-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) backfillLoop(ctx context.Context) {
375+
func (t *tracker[HTH, S, ID, BHASH]) backfillLoop(ctx context.Context) {
386376
for {
387377
select {
388378
case <-ctx.Done():
@@ -409,7 +399,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) backfillLoop(ctx context.Context) {
409399
// LatestAndFinalizedBlock - returns latest and latest finalized blocks.
410400
// NOTE: Returns latest finalized block as is, ignoring the FinalityTagBypass feature flag.
411401
// TODO: BCI-3321 use cached values instead of making RPC requests
412-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) LatestAndFinalizedBlock(ctx context.Context) (latest, finalized HTH, err error) {
402+
func (t *tracker[HTH, S, ID, BHASH]) LatestAndFinalizedBlock(ctx context.Context) (latest, finalized HTH, err error) {
413403
latest, err = t.client.HeadByNumber(ctx, nil)
414404
if err != nil {
415405
err = fmt.Errorf("failed to get latest block: %w", err)
@@ -434,7 +424,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) LatestAndFinalizedBlock(ctx context.Co
434424
return
435425
}
436426

437-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) getHeadAtHeight(ctx context.Context, chainHeadHash BLOCK_HASH, blockHeight int64) (HTH, error) {
427+
func (t *tracker[HTH, S, ID, BHASH]) getHeadAtHeight(ctx context.Context, chainHeadHash BHASH, blockHeight int64) (HTH, error) {
438428
chainHead := t.headSaver.Chain(chainHeadHash)
439429
if chainHead.IsValid() {
440430
// check if provided chain contains a block of specified height
@@ -455,7 +445,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) getHeadAtHeight(ctx context.Context, c
455445
// calculateLatestFinalized - returns latest finalized block. It's expected that currentHeadNumber - is the head of
456446
// canonical chain. There is no guaranties that returned block belongs to the canonical chain. Additional verification
457447
// must be performed before usage.
458-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) calculateLatestFinalized(ctx context.Context, currentHead HTH, finalityTagBypass bool) (HTH, error) {
448+
func (t *tracker[HTH, S, ID, BHASH]) calculateLatestFinalized(ctx context.Context, currentHead HTH, finalityTagBypass bool) (HTH, error) {
459449
if t.config.FinalityTagEnabled() && !finalityTagBypass {
460450
latestFinalized, err := t.client.LatestFinalizedBlock(ctx)
461451
if err != nil {
@@ -485,7 +475,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) calculateLatestFinalized(ctx context.C
485475
}
486476

487477
// backfill fetches all missing heads up until the latestFinalizedHead
488-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) backfill(ctx context.Context, head, latestFinalizedHead HTH) (err error) {
478+
func (t *tracker[HTH, S, ID, BHASH]) backfill(ctx context.Context, head, latestFinalizedHead HTH) (err error) {
489479
headBlockNumber := head.BlockNumber()
490480
mark := time.Now()
491481
fetched := 0
@@ -527,7 +517,7 @@ func (t *tracker[HTH, S, ID, BLOCK_HASH]) backfill(ctx context.Context, head, la
527517
t.log.Criticalw("Finalized block missing from conical chain",
528518
"finalized_block_number", latestFinalizedHead.BlockNumber(), "finalized_hash", latestFinalizedHead.BlockHash(),
529519
"canonical_chain_block_number", head.BlockNumber(), "canonical_chain_hash", head.BlockHash())
530-
return FinalizedMissingError[BLOCK_HASH]{latestFinalizedHead.BlockHash(), head.BlockHash()}
520+
return FinalizedMissingError[BHASH]{latestFinalizedHead.BlockHash(), head.BlockHash()}
531521
}
532522

533523
l = l.With("latest_finalized_block_hash", latestFinalizedHead.BlockHash(),
@@ -552,7 +542,7 @@ func (e FinalizedMissingError[BLOCK_HASH]) Error() string {
552542
return fmt.Sprintf("finalized block %s missing from canonical chain %s", e.Finalized, e.Canonical)
553543
}
554544

555-
func (t *tracker[HTH, S, ID, BLOCK_HASH]) fetchAndSaveHead(ctx context.Context, n int64, hash BLOCK_HASH) (HTH, error) {
545+
func (t *tracker[HTH, S, ID, BHASH]) fetchAndSaveHead(ctx context.Context, n int64, hash BHASH) (HTH, error) {
556546
t.log.Debugw("Fetching head", "blockHeight", n, "blockHash", hash)
557547
head, err := t.client.HeadByHash(ctx, hash)
558548
if err != nil {

0 commit comments

Comments
 (0)