Skip to content

Commit 05a9903

Browse files
authored
Merge pull request #213 from cloudstruct/feat/test-program-fetch-block
feat: test program to fetch single block
2 parents b28884a + fc3cd31 commit 05a9903

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.dylib
77
/go-ouroboros-network
88
/tx-monitor
9+
/block-fetch
910

1011
# Test binary, built with `go test -c`
1112
*.test

cmd/block-fetch/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"os"
7+
8+
"github.com/cloudstruct/go-ouroboros-network"
9+
"github.com/cloudstruct/go-ouroboros-network/cmd/common"
10+
"github.com/cloudstruct/go-ouroboros-network/ledger"
11+
ocommon "github.com/cloudstruct/go-ouroboros-network/protocol/common"
12+
)
13+
14+
type blockFetchFlags struct {
15+
*common.GlobalFlags
16+
slot uint64
17+
hash string
18+
}
19+
20+
func main() {
21+
// Parse commandline
22+
f := blockFetchFlags{
23+
GlobalFlags: common.NewGlobalFlags(),
24+
}
25+
f.Flagset.Uint64Var(&f.slot, "slot", 0, "slot for single block to fetch")
26+
f.Flagset.StringVar(&f.hash, "hash", "", "hash for single block to fetch")
27+
f.Parse()
28+
// Create connection
29+
conn := common.CreateClientConnection(f.GlobalFlags)
30+
errorChan := make(chan error)
31+
go func() {
32+
for {
33+
err := <-errorChan
34+
fmt.Printf("ERROR(async): %s\n", err)
35+
os.Exit(1)
36+
}
37+
}()
38+
o, err := ouroboros.New(
39+
ouroboros.WithConnection(conn),
40+
ouroboros.WithNetworkMagic(uint32(f.NetworkMagic)),
41+
ouroboros.WithErrorChan(errorChan),
42+
ouroboros.WithNodeToNode(f.NtnProto),
43+
ouroboros.WithKeepAlive(true),
44+
)
45+
if err != nil {
46+
fmt.Printf("ERROR: %s\n", err)
47+
os.Exit(1)
48+
}
49+
o.BlockFetch().Client.Start()
50+
51+
blockHash, err := hex.DecodeString(f.hash)
52+
if err != nil {
53+
fmt.Printf("ERROR: failed to decode block hash: %s\n", err)
54+
os.Exit(1)
55+
}
56+
block, err := o.BlockFetch().Client.GetBlock(ocommon.NewPoint(f.slot, blockHash))
57+
if err != nil {
58+
fmt.Printf("ERROR: failed to fetch block: %s\n", err)
59+
os.Exit(1)
60+
}
61+
62+
// Display block info
63+
switch v := block.(type) {
64+
case *ledger.ByronEpochBoundaryBlock:
65+
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", v.Header.ConsensusData.Epoch, v.Hash())
66+
case *ledger.ByronMainBlock:
67+
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", v.Header.ConsensusData.SlotId.Epoch, v.SlotNumber(), v.Hash())
68+
case ledger.Block:
69+
fmt.Printf("era = %s, slot = %d, block_no = %d, id = %s\n", v.Era().Name, v.SlotNumber(), v.BlockNumber(), v.Hash())
70+
}
71+
}

0 commit comments

Comments
 (0)