Skip to content

Commit 0700b1a

Browse files
authored
Merge pull request #234 from blinklabs-io/feat/bulk-chainsync-ntn
feat: bulk chainsync/blockfetch support in test program
2 parents 9f951e8 + 579d368 commit 0700b1a

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

cmd/gouroboros/chainsync.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"encoding/hex"
55
"flag"
66
"fmt"
7+
"os"
8+
79
ouroboros "github.com/blinklabs-io/gouroboros"
810
"github.com/blinklabs-io/gouroboros/ledger"
11+
"github.com/blinklabs-io/gouroboros/protocol/blockfetch"
912
"github.com/blinklabs-io/gouroboros/protocol/chainsync"
1013
"github.com/blinklabs-io/gouroboros/protocol/common"
11-
"os"
1214
)
1315

1416
type chainSyncState struct {
@@ -23,6 +25,7 @@ type chainSyncFlags struct {
2325
flagset *flag.FlagSet
2426
startEra string
2527
tip bool
28+
bulk bool
2629
}
2730

2831
func newChainSyncFlags() *chainSyncFlags {
@@ -31,6 +34,7 @@ func newChainSyncFlags() *chainSyncFlags {
3134
}
3235
f.flagset.StringVar(&f.startEra, "start-era", "genesis", "era which to start chain-sync at")
3336
f.flagset.BoolVar(&f.tip, "tip", false, "start chain-sync at current chain tip")
37+
f.flagset.BoolVar(&f.bulk, "bulk", false, "use bulk chain-sync mode with NtN")
3438
return f
3539
}
3640

@@ -55,7 +59,8 @@ var eraIntersect = map[string]map[string][]interface{}{
5559
},
5660
"mainnet": map[string][]interface{}{
5761
"genesis": []interface{}{},
58-
"byron": []interface{}{},
62+
// Chain genesis, but explicit
63+
"byron": []interface{}{0, "89d9b5a5b8ddc8d7e5a6795e9774d97faf1efea59b2caf7eaf9f8c5b32059df4"},
5964
// Last block of epoch 207 (Byron era)
6065
"shelley": []interface{}{4492799, "f8084c61b6a238acec985b59310b6ecec49c0ab8352249afd7268da5cff2a457"},
6166
// Last block of epoch 235 (Shelley era)
@@ -85,6 +90,12 @@ func buildChainSyncConfig() chainsync.Config {
8590
)
8691
}
8792

93+
func buildBlockFetchConfig() blockfetch.Config {
94+
return blockfetch.NewConfig(
95+
blockfetch.WithBlockFunc(blockFetchBlockHandler),
96+
)
97+
}
98+
8899
func testChainSync(f *globalFlags) {
89100
chainSyncFlags := newChainSyncFlags()
90101
err := chainSyncFlags.flagset.Parse(f.flagset.Args()[1:])
@@ -124,6 +135,7 @@ func testChainSync(f *globalFlags) {
124135
ouroboros.WithNodeToNode(f.ntnProto),
125136
ouroboros.WithKeepAlive(true),
126137
ouroboros.WithChainSyncConfig(buildChainSyncConfig()),
138+
ouroboros.WithBlockFetchConfig(buildBlockFetchConfig()),
127139
)
128140
if err != nil {
129141
fmt.Printf("ERROR: %s\n", err)
@@ -135,6 +147,7 @@ func testChainSync(f *globalFlags) {
135147
}
136148

137149
syncState.oConn = o
150+
138151
var point common.Point
139152
if chainSyncFlags.tip {
140153
tip, err := o.ChainSync().Client.GetCurrentTip()
@@ -152,9 +165,21 @@ func testChainSync(f *globalFlags) {
152165
} else {
153166
point = common.NewPointOrigin()
154167
}
155-
if err := o.ChainSync().Client.Sync([]common.Point{point}); err != nil {
156-
fmt.Printf("ERROR: failed to start chain-sync: %s\n", err)
157-
os.Exit(1)
168+
if !f.ntnProto || !chainSyncFlags.bulk {
169+
if err := o.ChainSync().Client.Sync([]common.Point{point}); err != nil {
170+
fmt.Printf("ERROR: failed to start chain-sync: %s\n", err)
171+
os.Exit(1)
172+
}
173+
} else {
174+
tip, err := o.ChainSync().Client.GetCurrentTip()
175+
if err != nil {
176+
fmt.Printf("ERROR: failed to get chain tip: %s\n", err)
177+
os.Exit(1)
178+
}
179+
if err := o.BlockFetch().Client.GetBlockRange(point, tip.Point); err != nil {
180+
fmt.Printf("ERROR: failed to request block range: %s\n", err)
181+
os.Exit(1)
182+
}
158183
}
159184
// Wait forever...the rest of the sync operations are async
160185
select {}
@@ -209,3 +234,19 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}, tip chai
209234
}
210235
return nil
211236
}
237+
238+
func blockFetchBlockHandler(blockData ledger.Block) error {
239+
switch block := blockData.(type) {
240+
case *ledger.ByronEpochBoundaryBlock:
241+
if syncState.byronEpochSlot > 0 {
242+
syncState.byronEpochBaseSlot += syncState.byronEpochSlot + 1
243+
}
244+
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", block.Header.ConsensusData.Epoch, block.Hash())
245+
case *ledger.ByronMainBlock:
246+
syncState.byronEpochSlot = uint64(block.Header.ConsensusData.SlotId.Slot)
247+
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", block.Header.ConsensusData.SlotId.Epoch, block.SlotNumber(), block.Hash())
248+
case ledger.Block:
249+
fmt.Printf("era = %s, slot = %d, block_no = %d, id = %s\n", block.Era().Name, block.SlotNumber(), block.BlockNumber(), block.Hash())
250+
}
251+
return nil
252+
}

protocol/blockfetch/blockfetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type BlockFetchOptionFunc func(*Config)
9292
func NewConfig(options ...BlockFetchOptionFunc) Config {
9393
c := Config{
9494
BatchStartTimeout: 5 * time.Second,
95-
BlockTimeout: 5 * time.Second,
95+
BlockTimeout: 60 * time.Second,
9696
}
9797
// Apply provided options functions
9898
for _, option := range options {

0 commit comments

Comments
 (0)