|
| 1 | +// Package nodeapi dials the embedded Lantern daemon over standard |
| 2 | +// Lotus-compatible JSON-RPC and exposes the parts curio-core wires |
| 3 | +// into upstream curio/pdp. |
| 4 | +// |
| 5 | +// Architecture: curio-core's embedded Lantern (pkg/daemon.Daemon) |
| 6 | +// mounts a /rpc/v1 HTTP endpoint exactly like a standalone Lantern |
| 7 | +// would. We talk to it the same way an external SP operator would |
| 8 | +// talk to a remote Lantern: through lotus/api/client with a JWT auth |
| 9 | +// header. Self-issued at boot via daemon.AdminToken(). |
| 10 | +// |
| 11 | +// Benefits over hand-rolling TipSet construction or borrowing |
| 12 | +// internal handler state: |
| 13 | +// |
| 14 | +// - Single source of chain truth (no duplicate ChainHead synthesizer |
| 15 | +// that drifts from Lantern's). |
| 16 | +// - Every Filecoin.* and eth_* method Lantern adds in the future |
| 17 | +// becomes available to curio-core for free. |
| 18 | +// - Same wire format external operators consume; bugs surface |
| 19 | +// identically in both deployments. |
| 20 | +// - Standard lotus/api/client gives us a fully-typed FullNode handle. |
| 21 | +package nodeapi |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "net/http" |
| 27 | + |
| 28 | + lotusapi "github.com/filecoin-project/lotus/api" |
| 29 | + lotusclient "github.com/filecoin-project/lotus/api/client" |
| 30 | + "github.com/filecoin-project/go-jsonrpc" |
| 31 | + |
| 32 | + lanterndaemon "github.com/Reiers/lantern/pkg/daemon" |
| 33 | +) |
| 34 | + |
| 35 | +// Client is the curio-core handle to embedded Lantern's JSON-RPC. |
| 36 | +// Holds the underlying lotus FullNode RPC client + a Close function |
| 37 | +// that releases the HTTP transport. |
| 38 | +type Client struct { |
| 39 | + Full lotusapi.FullNode |
| 40 | + close jsonrpc.ClientCloser |
| 41 | +} |
| 42 | + |
| 43 | +// New dials the embedded Lantern Daemon over its in-process |
| 44 | +// /rpc/v1 endpoint and self-issues an admin-scope JWT. |
| 45 | +// |
| 46 | +// Callers must call Close when done to release the HTTP transport. |
| 47 | +// |
| 48 | +// Returns an error if the Daemon hasn't mounted the RPC server yet |
| 49 | +// (Started() == false, or RPCListen was empty). |
| 50 | +func New(ctx context.Context, d *lanterndaemon.Daemon) (*Client, error) { |
| 51 | + if d == nil { |
| 52 | + return nil, fmt.Errorf("nodeapi.New: daemon is nil") |
| 53 | + } |
| 54 | + if !d.Started() { |
| 55 | + return nil, fmt.Errorf("nodeapi.New: daemon not started") |
| 56 | + } |
| 57 | + addr := d.RPCAddr() |
| 58 | + if addr == "" { |
| 59 | + return nil, fmt.Errorf("nodeapi.New: daemon has no RPC address (RPCListen empty?)") |
| 60 | + } |
| 61 | + tok := d.AdminToken() |
| 62 | + if tok == "" { |
| 63 | + return nil, fmt.Errorf("nodeapi.New: daemon admin token unavailable") |
| 64 | + } |
| 65 | + |
| 66 | + hdr := http.Header{} |
| 67 | + hdr.Set("Authorization", "Bearer "+tok) |
| 68 | + |
| 69 | + url := "http://" + addr + "/rpc/v1" |
| 70 | + full, closer, err := lotusclient.NewFullNodeRPCV1(ctx, url, hdr) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("dial embedded lantern at %s: %w", url, err) |
| 73 | + } |
| 74 | + return &Client{Full: full, close: closer}, nil |
| 75 | +} |
| 76 | + |
| 77 | +// Close releases the JSON-RPC transport. Safe to call multiple times. |
| 78 | +func (c *Client) Close() { |
| 79 | + if c.close != nil { |
| 80 | + c.close() |
| 81 | + c.close = nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// FullNode returns the underlying lotus FullNode handle. Use this when |
| 86 | +// passing curio-core's nodeapi to subsystems that want the full Lotus |
| 87 | +// API surface (curio/pdp.PDPServiceNodeApi is one such consumer; it |
| 88 | +// only needs ChainHead but the upstream type expects the broader |
| 89 | +// interface). |
| 90 | +func (c *Client) FullNode() lotusapi.FullNode { return c.Full } |
0 commit comments