-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: test program to fetch single block
Fixes #212
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
*.dylib | ||
/go-ouroboros-network | ||
/tx-monitor | ||
/block-fetch | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |