|
| 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