From 0232b6f99266f162e4be872163f74b14892e908f Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sat, 14 Sep 2024 17:19:37 -0400 Subject: [PATCH] fix(examples): pass full inputs to functions (#45) Signed-off-by: Chris Gianelloni --- examples/query/main.go | 21 ++++++++++---- examples/sync/main.go | 65 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/examples/query/main.go b/examples/query/main.go index 1494f77..d7258c3 100644 --- a/examples/query/main.go +++ b/examples/query/main.go @@ -31,7 +31,7 @@ func main() { case "readParams": readParams(ctx, client) case "readUtxos": - readUtxos(ctx, client, "71a7498f086d378ec5e558581286629b678be1dd65d5d4e2a5d634ba6fdf8299") + readUtxos(ctx, client, "71a7498f086d378ec5e558581286629b678be1dd65d5d4e2a5d634ba6fdf8299", 0) case "searchUtxos": searchUtxos(ctx, client, "60c0359ebb7d0688d79064bd118c99c8b87b5853e3af59245bb97e84d2") default: @@ -49,15 +49,23 @@ func readParams(ctx context.Context, client *utxorpc.UtxorpcClient) { utxorpc.HandleError(err) } fmt.Printf("Response: %+v\n", resp) + + if resp.Msg.LedgerTip != nil { + fmt.Printf("Ledger Tip: Slot: %d, Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash) + } + if resp.Msg.Values != nil { + fmt.Printf("Cardano: %+v\n", resp.Msg.Values) + } } -func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr string) { +func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr string, txIndex uint32) { txHash, err := hex.DecodeString(txHashStr) if err != nil { log.Fatalf("failed to decode hex string: %v", err) } txoRef := &query.TxoRef{ - Hash: txHash, + Hash: txHash, + Index: txIndex, } req := connect.NewRequest(&query.ReadUtxosRequest{ @@ -70,6 +78,8 @@ func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr str utxorpc.HandleError(err) } + fmt.Printf("Response: %+v\n", resp) + if resp.Msg.LedgerTip != nil { fmt.Printf("Ledger Tip:\n Slot: %d\n Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash) } @@ -101,9 +111,6 @@ func searchUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, rawAddress Address: &cardano.AddressPattern{ ExactAddress: exactAddress, }, - Asset: &cardano.AssetPattern{ - // Populate the fields as necessary - }, }, }, }, @@ -117,6 +124,8 @@ func searchUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, rawAddress utxorpc.HandleError(err) } + fmt.Printf("Response: %+v\n", resp) + if resp.Msg.LedgerTip != nil { fmt.Printf("Ledger Tip:\n Slot: %d\n Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash) } diff --git a/examples/sync/main.go b/examples/sync/main.go index e6d7489..8b60a15 100644 --- a/examples/sync/main.go +++ b/examples/sync/main.go @@ -22,22 +22,69 @@ func main() { }), ) - // fetchBlock(ctx, client) - followTip(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil) - // followTip(ctx, client, "") + // Set mode to "fetchBlock" or "followTip" to select the desired example. + var mode string = "followTip" + + switch mode { + case "fetchBlock": + fetchBlock(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil) + case "followTip": + followTip(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil) + default: + fmt.Println("Unknown mode:", mode) + } } -func fetchBlock(ctx context.Context, client *utxorpc.UtxorpcClient) { - req := connect.NewRequest(&sync.FetchBlockRequest{}) - client.AddHeadersToRequest(req) +func fetchBlock(ctx context.Context, client *utxorpc.UtxorpcClient, blockHash string, blockIndex int64, fieldMaskPaths []string) { + var req *connect.Request[sync.FetchBlockRequest] + var intersect []*sync.BlockRef + var fieldMask *fieldmaskpb.FieldMask + + // Construct the BlockRef based on the provided parameters + blockRef := &sync.BlockRef{} + if blockHash != "" { + hash, err := hex.DecodeString(blockHash) + if err != nil { + log.Fatalf("failed to decode hex string: %v", err) + } + blockRef.Hash = hash + } + // We assume blockIndex can be 0 or any positive number + if blockIndex > -1 { + blockRef.Index = uint64(blockIndex) + } + + // Only add blockRef to intersect if at least one of blockHash or blockIndex is provided + if blockHash != "" || blockIndex > -1 { + intersect = []*sync.BlockRef{blockRef} + } + // Construct the FieldMask if paths are provided + if len(fieldMaskPaths) > 0 { + fieldMask = &fieldmaskpb.FieldMask{ + Paths: fieldMaskPaths, + } + } + + // Create the FetchBlockRequest + req = connect.NewRequest(&sync.FetchBlockRequest{ + Ref: intersect, + FieldMask: fieldMask, + }) + + // Print BlockRef details if intersect is provided + if len(intersect) > 0 { + fmt.Printf("Blockref: %d, %x\n", req.Msg.Ref[0].Index, req.Msg.Ref[0].Hash) + } + + client.AddHeadersToRequest(req) fmt.Println("connecting to utxorpc host:", client.URL()) - chainSync, err := client.Sync.FetchBlock(ctx, req) + resp, err := client.Sync.FetchBlock(ctx, req) if err != nil { utxorpc.HandleError(err) } - fmt.Println("connected to utxorpc...") - for i, blockRef := range chainSync.Msg.Block { + fmt.Printf("Response: %+v\n", resp) + for i, blockRef := range resp.Msg.Block { fmt.Printf("Block[%d]:\n", i) fmt.Printf("Index: %d\n", blockRef.GetCardano().GetHeader().GetSlot()) fmt.Printf("Hash: %x\n", blockRef.GetCardano().GetHeader().GetHash())