Skip to content

Commit 2fbfd31

Browse files
committed
feat: make block/tx event CBOR optional
Fixes #31
1 parent a4177d5 commit 2fbfd31

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

input/chainsync/block.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ type BlockEvent struct {
2222
BlockNumber uint64 `json:"blockNumber"`
2323
BlockHash string `json:"blockHash"`
2424
SlotNumber uint64 `json:"slotNumber"`
25-
BlockCbor byteSliceJsonHex `json:"blockCbor"`
25+
BlockCbor byteSliceJsonHex `json:"blockCbor,omitempty"`
2626
}
2727

28-
func NewBlockEvent(block ledger.Block) BlockEvent {
28+
func NewBlockEvent(block ledger.Block, includeCbor bool) BlockEvent {
2929
evt := BlockEvent{
3030
BlockNumber: block.BlockNumber(),
3131
BlockHash: block.Hash(),
3232
SlotNumber: block.SlotNumber(),
33-
BlockCbor: block.Cbor(),
33+
}
34+
if includeCbor {
35+
evt.BlockCbor = block.Cbor()
3436
}
3537
return evt
3638
}

input/chainsync/chainsync.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/blinklabs-io/snek/event"
2323

24-
"github.com/blinklabs-io/gouroboros"
24+
ouroboros "github.com/blinklabs-io/gouroboros"
2525
"github.com/blinklabs-io/gouroboros/ledger"
2626
ochainsync "github.com/blinklabs-io/gouroboros/protocol/chainsync"
2727
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
@@ -36,6 +36,7 @@ type ChainSync struct {
3636
ntcTcp bool
3737
intersectTip bool
3838
intersectPoints []ocommon.Point
39+
includeCbor bool
3940
errorChan chan error
4041
eventChan chan event.Event
4142
byronEpochBaseSlot uint64
@@ -175,7 +176,7 @@ func (c *ChainSync) handleRollBackward(point ocommon.Point, tip ochainsync.Tip)
175176
func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip ochainsync.Tip) error {
176177
switch v := blockData.(type) {
177178
case ledger.Block:
178-
evt := event.New("block", time.Now(), NewBlockEvent(v))
179+
evt := event.New("block", time.Now(), NewBlockEvent(v, c.includeCbor))
179180
c.eventChan <- evt
180181
case ledger.BlockHeader:
181182
var blockSlot uint64
@@ -201,10 +202,10 @@ func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip
201202
if err != nil {
202203
return err
203204
}
204-
blockEvt := event.New("block", time.Now(), NewBlockEvent(block))
205+
blockEvt := event.New("block", time.Now(), NewBlockEvent(block, c.includeCbor))
205206
c.eventChan <- blockEvt
206207
for _, transaction := range block.Transactions() {
207-
txEvt := event.New("transaction", time.Now(), NewTransactionEvent(block, transaction))
208+
txEvt := event.New("transaction", time.Now(), NewTransactionEvent(block, transaction, c.includeCbor))
208209
c.eventChan <- txEvt
209210
}
210211
}

input/chainsync/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ func WithIntersectTip(intersectTip bool) ChainSyncOptionFunc {
6868
c.intersectTip = intersectTip
6969
}
7070
}
71+
72+
// WithIncludeCbor specifies whether to include the original CBOR for a block or transaction with the event
73+
func WithIncludeCbor(includeCbor bool) ChainSyncOptionFunc {
74+
return func(c *ChainSync) {
75+
c.includeCbor = includeCbor
76+
}
77+
}

input/chainsync/plugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var cmdlineOptions struct {
2626
ntcTcp bool
2727
intersectTip bool
2828
intersectPoint string
29+
includeCbor bool
2930
}
3031

3132
func init() {
@@ -81,6 +82,13 @@ func init() {
8182
Dest: &(cmdlineOptions.intersectTip),
8283
},
8384
// TODO: intersect-point
85+
{
86+
Name: "include-cbor",
87+
Type: plugin.PluginOptionTypeBool,
88+
Description: "include original CBOR for block/transaction in events",
89+
DefaultValue: false,
90+
Dest: &(cmdlineOptions.includeCbor),
91+
},
8492
},
8593
},
8694
)
@@ -95,6 +103,7 @@ func NewFromCmdlineOptions() plugin.Plugin {
95103
WithNtcTcp(cmdlineOptions.ntcTcp),
96104
WithIntersectTip(cmdlineOptions.intersectTip),
97105
// TODO: WithIntersectPoints
106+
WithIncludeCbor(cmdlineOptions.includeCbor),
98107
)
99108
return p
100109
}

input/chainsync/tx.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ type TransactionEvent struct {
2323
BlockHash string `json:"blockHash"`
2424
SlotNumber uint64 `json:"slotNumber"`
2525
TransactionHash string `json:"transactionHash"`
26-
TransactionCbor byteSliceJsonHex `json:"transactionCbor"`
26+
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
2727
}
2828

29-
func NewTransactionEvent(block ledger.Block, txBody ledger.TransactionBody) TransactionEvent {
29+
func NewTransactionEvent(block ledger.Block, txBody ledger.TransactionBody, includeCbor bool) TransactionEvent {
3030
evt := TransactionEvent{
3131
BlockNumber: block.BlockNumber(),
3232
BlockHash: block.Hash(),
3333
SlotNumber: block.SlotNumber(),
3434
TransactionHash: txBody.Hash(),
35-
TransactionCbor: txBody.Cbor(),
35+
}
36+
if includeCbor {
37+
evt.TransactionCbor = txBody.Cbor()
3638
}
3739
return evt
3840
}

0 commit comments

Comments
 (0)