Skip to content

Commit

Permalink
feat: test program to fetch single block
Browse files Browse the repository at this point in the history
Fixes #212
  • Loading branch information
agaffney committed Mar 10, 2023
1 parent b28884a commit c456699
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.dylib
/go-ouroboros-network
/tx-monitor
/block-fetch

# Test binary, built with `go test -c`
*.test
Expand Down
79 changes: 79 additions & 0 deletions cmd/block-fetch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"encoding/hex"
"fmt"
"os"

"github.com/cloudstruct/go-ouroboros-network"
"github.com/cloudstruct/go-ouroboros-network/cmd/common"
"github.com/cloudstruct/go-ouroboros-network/ledger"
ocommon "github.com/cloudstruct/go-ouroboros-network/protocol/common"
)

type blockFetchFlags struct {
*common.GlobalFlags
startSlot uint64
startHash string
endSlot uint64
endHash string
}

func main() {
// Parse commandline
f := blockFetchFlags{
GlobalFlags: common.NewGlobalFlags(),
}
f.Flagset.Uint64Var(&f.startSlot, "slot", 0, "slot for single block to fetch")
f.Flagset.StringVar(&f.startHash, "hash", "", "hash for single block to fetch")
/*
f.Flagset.Uint64Var(&f.startSlot, "start-slot", 0, "start slot, for use when specifying a range, overrides -slot")
f.Flagset.StringVar(&f.startHash, "start-hash", "", "start hash, for use when specifying a range, overrides -hash")
f.Flagset.Uint64Var(&f.endSlot, "end-slot", 0, "end slot, for use when specifying a range")
f.Flagset.StringVar(&f.endHash, "end-hash", "", "end hash, for use when specifying a range")
*/
f.Parse()
// Create connection
conn := common.CreateClientConnection(f.GlobalFlags)
errorChan := make(chan error)
go func() {
for {
err := <-errorChan
fmt.Printf("ERROR(async): %s\n", err)
os.Exit(1)
}
}()
o, err := ouroboros.New(
ouroboros.WithConnection(conn),
ouroboros.WithNetworkMagic(uint32(f.NetworkMagic)),
ouroboros.WithErrorChan(errorChan),
ouroboros.WithNodeToNode(f.NtnProto),
ouroboros.WithKeepAlive(true),
)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.BlockFetch().Client.Start()

blockHash, err := hex.DecodeString(f.startHash)
if err != nil {
fmt.Printf("ERROR: failed to decode block hash: %s\n", err)
os.Exit(1)
}
block, err := o.BlockFetch().Client.GetBlock(ocommon.NewPoint(f.startSlot, blockHash))
if err != nil {
fmt.Printf("ERROR: failed to fetch block: %s\n", err)
os.Exit(1)
}

// Display block info
switch v := block.(type) {
case *ledger.ByronEpochBoundaryBlock:
fmt.Printf("era = Byron (EBB), epoch = %d, id = %s\n", v.Header.ConsensusData.Epoch, v.Hash())
case *ledger.ByronMainBlock:
fmt.Printf("era = Byron, epoch = %d, slot = %d, id = %s\n", v.Header.ConsensusData.SlotId.Epoch, v.SlotNumber(), v.Hash())
case ledger.Block:
fmt.Printf("era = %s, slot = %d, block_no = %d, id = %s\n", v.Era().Name, v.SlotNumber(), v.BlockNumber(), v.Hash())
}
}

0 comments on commit c456699

Please sign in to comment.