GetInterchain: perChain + chains options (default first anchor per chain)

Transaction.GetInterchain and Block.GetInterchain take variadic options
(client.WithPerChain / client.WithChains, re-exported as sdk.WithPerChain /
sdk.WithChains) that map to the new prime-node ?perChain=&chains= query params.
Backward compatible: existing no-option calls get the default (one anchor per
chain). client.InterchainQuery builds the suffix; unit-tested.
This commit is contained in:
2026-06-05 10:54:45 -04:00
parent 7d8e23768f
commit 17057ad1f2
7 changed files with 162 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
package transaction
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"git.dragonchain.com/dragonchain/prime-sdk-go/client"
)
// TestGetInterchainQuery confirms GetInterchain builds the request URI (path +
// the perChain/chains query) end-to-end through the client.
func TestGetInterchainQuery(t *testing.T) {
var gotURI string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotURI = r.URL.RequestURI()
_, _ = io.WriteString(w, `{"blockId":"42","validatorBlocks":[],"interchainTransactions":[]}`)
}))
defer srv.Close()
tc := NewTransactionClient(client.NewClient("pid", "kid", "key", srv.URL))
if _, err := tc.GetInterchain(context.Background(), "tx1", client.WithPerChain(2), client.WithChains("1", "0")); err != nil {
t.Fatalf("GetInterchain with opts: %v", err)
}
if want := "/api/v1/transaction/tx1/interchain?perChain=2&chains=1,0"; gotURI != want {
t.Errorf("with opts: URI = %q, want %q", gotURI, want)
}
if _, err := tc.GetInterchain(context.Background(), "tx1"); err != nil {
t.Fatalf("GetInterchain default: %v", err)
}
if want := "/api/v1/transaction/tx1/interchain"; gotURI != want {
t.Errorf("default (no opts): URI = %q, want %q", gotURI, want)
}
}

View File

@@ -56,9 +56,14 @@ func (tc *TransactionClient) Get(ctx context.Context, transactionID string) (*mo
// validated its prime block and the public-chain interchain anchors those
// validator blocks were bundled into. If the transaction is still pending (not
// yet in a block) the trace's slices are empty.
func (tc *TransactionClient) GetInterchain(ctx context.Context, transactionID string) (*models.InterchainTrace, error) {
//
// By default it returns the first anchor per public chain (anchor proofs are
// chained, so the earliest per chain is the meaningful one). Use
// client.WithPerChain / client.WithChains to return more anchors per chain or
// restrict to specific chains.
func (tc *TransactionClient) GetInterchain(ctx context.Context, transactionID string, opts ...client.InterchainOption) (*models.InterchainTrace, error) {
var resp models.InterchainTrace
path := fmt.Sprintf("/api/v1/transaction/%s/interchain", transactionID)
path := fmt.Sprintf("/api/v1/transaction/%s/interchain%s", transactionID, client.InterchainQuery(opts...))
err := tc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err