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:
38
transaction/interchain_test.go
Normal file
38
transaction/interchain_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user