Skip to content

Commit

Permalink
fix: to stream and receive stream response (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <verbotenj@blinklabs.io>
  • Loading branch information
verbotenj authored Jul 4, 2024
1 parent 2d9de6a commit 3970db2
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions examples/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
)

fetchBlock(ctx, client)
followTip(ctx, client, "230eeba5de6b0198f64a3e801f92fa1ebf0f3a42a74dbd1922187249ad3038e7")
followTip(ctx, client, "5d316b4722f832bb7cee7914cbcc7b977a03fea6884da5f2157a6ae420b91280")
followTip(ctx, client, "")
}

Expand Down Expand Up @@ -64,10 +64,54 @@ func followTip(ctx context.Context, client *utxorpc.UtxorpcClient, blockHash str
}
client.AddHeadersToRequest(req)
fmt.Println("connecting to utxorpc host:", client.URL())
resp, err := client.ChainSync.FollowTip(ctx, req)
stream, err := client.ChainSync.FollowTip(ctx, req)
if err != nil {
utxorpc.HandleError(err)
return
}
fmt.Println("connected to utxorpc...")
fmt.Printf("Response: %+v\n", resp)
fmt.Println("Connected to utxorpc host, following tip...")

for stream.Receive() {
resp := stream.Msg()
action := resp.GetAction()
switch a := action.(type) {
case *sync.FollowTipResponse_Apply:
fmt.Println("Action: Apply")
printAnyChainBlock(a.Apply)
case *sync.FollowTipResponse_Undo:
fmt.Println("Action: Undo")
printAnyChainBlock(a.Undo)
case *sync.FollowTipResponse_Reset_:
fmt.Println("Action: Reset")
printBlockRef(a.Reset_)
default:
fmt.Println("Unknown action type")
}
}

if err := stream.Err(); err != nil {
fmt.Println("Stream ended with error:", err)
} else {
fmt.Println("Stream ended normally.")
}
}

func printAnyChainBlock(block *sync.AnyChainBlock) {
if block == nil {
return
}
if cardanoBlock := block.GetCardano(); cardanoBlock != nil {
hash := hex.EncodeToString(cardanoBlock.Header.Hash)
slot := cardanoBlock.Header.Slot
fmt.Printf("Block Slot: %d, Block Hash: %s\n", slot, hash)
}
}

func printBlockRef(blockRef *sync.BlockRef) {
if blockRef == nil {
return
}
hash := hex.EncodeToString(blockRef.Hash)
slot := blockRef.Index
fmt.Printf("Block Slot: %d, Block Hash: %s\n", slot, hash)
}

0 comments on commit 3970db2

Please sign in to comment.