Skip to content

Commit c321f28

Browse files
authored
refactor(event): move block, rollback, and transaction from input/chainsync (#525)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 6ee84ce commit c321f28

File tree

14 files changed

+86
-91
lines changed

14 files changed

+86
-91
lines changed

input/chainsync/block.go renamed to event/block.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,25 +12,25 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package chainsync
15+
package event
1616

1717
import (
1818
"github.com/blinklabs-io/gouroboros/ledger"
1919
)
2020

2121
type BlockContext struct {
22+
Era string `json:"era"`
2223
BlockNumber uint64 `json:"blockNumber"`
2324
SlotNumber uint64 `json:"slotNumber"`
2425
NetworkMagic uint32 `json:"networkMagic"`
25-
Era string `json:"era"`
2626
}
2727

2828
type BlockEvent struct {
2929
Block ledger.Block `json:"-"`
30-
BlockBodySize uint64 `json:"blockBodySize"`
3130
IssuerVkey string `json:"issuerVkey"`
3231
BlockHash string `json:"blockHash"`
3332
BlockCbor byteSliceJsonHex `json:"blockCbor,omitempty"`
33+
BlockBodySize uint64 `json:"blockBodySize"`
3434
TransactionCount uint64 `json:"transactionCount"`
3535
}
3636

event/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
)
2020

2121
type Event struct {
22-
Type string `json:"type"`
2322
Timestamp time.Time `json:"timestamp"`
2423
Context any `json:"context,omitempty"`
2524
Payload any `json:"payload"`
25+
Type string `json:"type"`
2626
}
2727

2828
func New(

input/chainsync/json.go renamed to event/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package chainsync
15+
package event
1616

1717
import (
1818
"encoding/hex"

input/chainsync/rollback.go renamed to event/rollback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package chainsync
15+
package event
1616

1717
import (
1818
"encoding/hex"

input/chainsync/tx.go renamed to event/tx.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package chainsync
15+
package event
1616

1717
import (
1818
"github.com/blinklabs-io/gouroboros/cbor"
@@ -21,27 +21,27 @@ import (
2121
)
2222

2323
type TransactionContext struct {
24+
TransactionHash string `json:"transactionHash"`
2425
BlockNumber uint64 `json:"blockNumber"`
2526
SlotNumber uint64 `json:"slotNumber"`
26-
TransactionHash string `json:"transactionHash"`
2727
TransactionIdx uint32 `json:"transactionIdx"`
2828
NetworkMagic uint32 `json:"networkMagic"`
2929
}
3030

3131
type TransactionEvent struct {
3232
Transaction ledger.Transaction `json:"-"`
33+
Witnesses lcommon.TransactionWitnessSet `json:"witnesses,omitempty"`
34+
Withdrawals map[string]uint64 `json:"withdrawals,omitempty"`
35+
Metadata *cbor.LazyValue `json:"metadata,omitempty"`
3336
BlockHash string `json:"blockHash"`
34-
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
35-
Inputs []ledger.TransactionInput `json:"inputs"`
36-
Outputs []ledger.TransactionOutput `json:"outputs"`
37-
Certificates []ledger.Certificate `json:"certificates,omitempty"`
3837
ReferenceInputs []ledger.TransactionInput `json:"referenceInputs,omitempty"`
39-
Metadata *cbor.LazyValue `json:"metadata,omitempty"`
38+
Certificates []ledger.Certificate `json:"certificates,omitempty"`
39+
Outputs []ledger.TransactionOutput `json:"outputs"`
40+
ResolvedInputs []ledger.TransactionOutput `json:"resolvedInputs,omitempty"`
41+
Inputs []ledger.TransactionInput `json:"inputs"`
42+
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
4043
Fee uint64 `json:"fee"`
4144
TTL uint64 `json:"ttl,omitempty"`
42-
ResolvedInputs []ledger.TransactionOutput `json:"resolvedInputs,omitempty"`
43-
Withdrawals map[string]uint64 `json:"withdrawals,omitempty"`
44-
Witnesses lcommon.TransactionWitnessSet `json:"witnesses,omitempty"`
4545
}
4646

4747
func NewTransactionContext(

filter/chainsync/chainsync.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ import (
1919
"strings"
2020

2121
"github.com/blinklabs-io/adder/event"
22-
"github.com/blinklabs-io/adder/input/chainsync"
2322
"github.com/blinklabs-io/adder/plugin"
2423
"github.com/blinklabs-io/gouroboros/ledger"
2524
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -61,14 +60,14 @@ func (c *ChainSync) Start() error {
6160
return
6261
}
6362
switch v := evt.Payload.(type) {
64-
case chainsync.BlockEvent:
63+
case event.BlockEvent:
6564
// Check pool filter
6665
if len(c.filterPoolIds) > 0 {
6766
filterMatched := false
6867
for _, filterPoolId := range c.filterPoolIds {
6968
isPoolBech32 := strings.HasPrefix(filterPoolId, "pool")
7069
foundMatch := false
71-
be := evt.Payload.(chainsync.BlockEvent)
70+
be := evt.Payload.(event.BlockEvent)
7271
if be.IssuerVkey == filterPoolId {
7372
foundMatch = true
7473
} else if isPoolBech32 {
@@ -100,7 +99,7 @@ func (c *ChainSync) Start() error {
10099
continue
101100
}
102101
}
103-
case chainsync.TransactionEvent:
102+
case event.TransactionEvent:
104103
// Check address filter
105104
if len(c.filterAddresses) > 0 {
106105
filterMatched := false

filter/chainsync/chainsync_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"time"
2121

2222
"github.com/blinklabs-io/adder/event"
23-
"github.com/blinklabs-io/adder/input/chainsync"
2423
"github.com/blinklabs-io/gouroboros/cbor"
2524
"github.com/blinklabs-io/gouroboros/ledger"
2625
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -303,7 +302,7 @@ func TestFilterByAddress(t *testing.T) {
303302
datum: nil,
304303
}
305304

306-
txEvent := chainsync.TransactionEvent{
305+
txEvent := event.TransactionEvent{
307306
Outputs: []ledger.TransactionOutput{output},
308307
ResolvedInputs: []ledger.TransactionOutput{output},
309308
}
@@ -365,7 +364,7 @@ func TestFilterByPolicyId(t *testing.T) {
365364
datum: nil,
366365
}
367366
evt := event.Event{
368-
Payload: chainsync.TransactionEvent{
367+
Payload: event.TransactionEvent{
369368
Outputs: []ledger.TransactionOutput{output},
370369
ResolvedInputs: []ledger.TransactionOutput{output},
371370
},
@@ -418,7 +417,7 @@ func TestFilterByAssetFingerprint(t *testing.T) {
418417
datum: nil,
419418
}
420419
evt := event.Event{
421-
Payload: chainsync.TransactionEvent{
420+
Payload: event.TransactionEvent{
422421
Outputs: []ledger.TransactionOutput{output},
423422
ResolvedInputs: []ledger.TransactionOutput{output},
424423
},
@@ -452,7 +451,7 @@ func TestFilterByPoolId(t *testing.T) {
452451

453452
// Mock block event
454453
evt := event.Event{
455-
Payload: chainsync.BlockEvent{
454+
Payload: event.BlockEvent{
456455
IssuerVkey: "pool1", // Match the filterPoolIds
457456
},
458457
}

filter/chainsync/plugin_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/blinklabs-io/adder/event"
8-
"github.com/blinklabs-io/adder/input/chainsync"
98
"github.com/blinklabs-io/adder/plugin"
109
"github.com/stretchr/testify/assert"
1110
)
@@ -73,7 +72,7 @@ func TestPluginEventProcessing(t *testing.T) {
7372
testEvent := event.Event{
7473
Type: "transaction",
7574
Timestamp: time.Now(),
76-
Payload: chainsync.TransactionEvent{},
75+
Payload: event.TransactionEvent{},
7776
}
7877

7978
// Send the event to the input channel

input/chainsync/block_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chainsync
33
import (
44
"testing"
55

6+
"github.com/blinklabs-io/adder/event"
67
"github.com/blinklabs-io/gouroboros/ledger/common"
78
"github.com/stretchr/testify/assert"
89
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
@@ -197,7 +198,7 @@ func TestNewBlockContext(t *testing.T) {
197198

198199
for _, tc := range testCases {
199200
t.Run(tc.name, func(t *testing.T) {
200-
blockContext := NewBlockContext(tc.block, tc.networkMagic)
201+
blockContext := event.NewBlockContext(tc.block, tc.networkMagic)
201202
assert.Equal(
202203
t,
203204
tc.expectedEra,
@@ -279,7 +280,7 @@ func TestNewBlockContextEdgeCases(t *testing.T) {
279280

280281
for _, tc := range testCases {
281282
t.Run(tc.name, func(t *testing.T) {
282-
blockContext := NewBlockContext(tc.block, tc.networkMagic)
283+
blockContext := event.NewBlockContext(tc.block, tc.networkMagic)
283284
assert.Equal(
284285
t,
285286
tc.expectedEra,

input/chainsync/chainsync.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,40 @@ const (
5151
)
5252

5353
type ChainSync struct {
54-
oConn *ouroboros.Connection
5554
logger plugin.Logger
56-
network string
57-
networkMagic uint32
58-
address string
59-
socketPath string
60-
ntcTcp bool
61-
intersectTip bool
62-
intersectPoints []ocommon.Point
63-
includeCbor bool
64-
autoReconnect bool
65-
autoReconnectDelay time.Duration
6655
statusUpdateFunc StatusUpdateFunc
67-
status *ChainSyncStatus
68-
errorChan chan error
56+
blockfetchDoneChan chan struct{}
57+
kupoClient *kugo.Client
58+
oConn *ouroboros.Connection
6959
eventChan chan event.Event
70-
cursorCache []ocommon.Point
71-
dialAddress string
60+
errorChan chan error
61+
status *ChainSyncStatus
7262
dialFamily string
7363
kupoUrl string
74-
kupoClient *kugo.Client
75-
delayConfirmations uint
76-
delayBuffer [][]event.Event
64+
network string
65+
socketPath string
66+
dialAddress string
67+
address string
68+
intersectPoints []ocommon.Point
7769
pendingBlockPoints []ocommon.Point
78-
blockfetchDoneChan chan struct{}
70+
delayBuffer [][]event.Event
71+
cursorCache []ocommon.Point
7972
lastTip ochainsync.Tip
73+
delayConfirmations uint
74+
autoReconnectDelay time.Duration
75+
networkMagic uint32
76+
includeCbor bool
77+
ntcTcp bool
78+
intersectTip bool
79+
autoReconnect bool
8080
}
8181

8282
type ChainSyncStatus struct {
83+
BlockHash string
84+
TipBlockHash string
8385
SlotNumber uint64
8486
BlockNumber uint64
85-
BlockHash string
8687
TipSlotNumber uint64
87-
TipBlockHash string
8888
TipReached bool
8989
}
9090

@@ -297,15 +297,15 @@ func (c *ChainSync) handleRollBackward(
297297
"chainsync.rollback",
298298
time.Now(),
299299
nil,
300-
NewRollbackEvent(point),
300+
event.NewRollbackEvent(point),
301301
)
302302
// Remove rolled-back events from buffer
303303
if len(c.delayBuffer) > 0 {
304304
// We iterate backwards to avoid the issues with deleting from a list while iterating over it
305305
for i := len(c.delayBuffer) - 1; i >= 0; i-- {
306306
for _, evt := range c.delayBuffer[i] {
307307
// Look for block event
308-
if blockEvtCtx, ok := evt.Context.(BlockContext); ok {
308+
if blockEvtCtx, ok := evt.Context.(event.BlockContext); ok {
309309
// Delete event batch if slot is after rollback point
310310
if blockEvtCtx.SlotNumber > point.Slot {
311311
c.delayBuffer = slices.Delete(c.delayBuffer, i, i+1)
@@ -367,8 +367,8 @@ func (c *ChainSync) handleRollForward(
367367
blockEvt := event.New(
368368
"chainsync.block",
369369
time.Now(),
370-
NewBlockHeaderContext(block.Header()),
371-
NewBlockEvent(block, c.includeCbor),
370+
event.NewBlockHeaderContext(block.Header()),
371+
event.NewBlockEvent(block, c.includeCbor),
372372
)
373373
tmpEvents = append(tmpEvents, blockEvt)
374374
for t, transaction := range block.Transactions() {
@@ -382,13 +382,13 @@ func (c *ChainSync) handleRollForward(
382382
txEvt := event.New(
383383
"chainsync.transaction",
384384
time.Now(),
385-
NewTransactionContext(
385+
event.NewTransactionContext(
386386
block,
387387
transaction,
388388
uint32(t),
389389
c.networkMagic,
390390
),
391-
NewTransactionEvent(
391+
event.NewTransactionEvent(
392392
block,
393393
transaction,
394394
c.includeCbor,
@@ -416,7 +416,7 @@ func (c *ChainSync) handleRollForward(
416416
if uint(len(c.delayBuffer)) > c.delayConfirmations {
417417
for _, evt := range c.delayBuffer[0] {
418418
// Look for block event
419-
if blockEvt, ok := evt.Payload.(BlockEvent); ok {
419+
if blockEvt, ok := evt.Payload.(event.BlockEvent); ok {
420420
// Populate current point for update status based on most recently sent events
421421
updateTip = ochainsync.Tip{
422422
Point: ocommon.Point{
@@ -449,8 +449,8 @@ func (c *ChainSync) handleBlockFetchBlock(
449449
blockEvt := event.New(
450450
"chainsync.block",
451451
time.Now(),
452-
NewBlockContext(block, c.networkMagic),
453-
NewBlockEvent(block, c.includeCbor),
452+
event.NewBlockContext(block, c.networkMagic),
453+
event.NewBlockEvent(block, c.includeCbor),
454454
)
455455
c.eventChan <- blockEvt
456456
for t, transaction := range block.Transactions() {
@@ -464,13 +464,13 @@ func (c *ChainSync) handleBlockFetchBlock(
464464
txEvt := event.New(
465465
"chainsync.transaction",
466466
time.Now(),
467-
NewTransactionContext(
467+
event.NewTransactionContext(
468468
block,
469469
transaction,
470470
uint32(t),
471471
c.networkMagic,
472472
),
473-
NewTransactionEvent(
473+
event.NewTransactionEvent(
474474
block,
475475
transaction,
476476
c.includeCbor,

0 commit comments

Comments
 (0)