-
Notifications
You must be signed in to change notification settings - Fork 0
feat(runtime): implement node pricing and economic settlement #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package hostcall | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/binary" | ||
|
|
||
| "github.com/simonovic86/igor/internal/eventlog" | ||
| "github.com/tetratelabs/wazero" | ||
| ) | ||
|
|
||
| // PricingState provides pricing hostcalls with access to node price configuration. | ||
| type PricingState interface { | ||
| GetNodePrice() int64 // price per second in microcents | ||
| } | ||
|
|
||
| // registerPricing adds node_price to the host module builder. | ||
| // node_price is an observation hostcall recorded in the event log (CM-4). | ||
| func (r *Registry) registerPricing(builder wazero.HostModuleBuilder, ps PricingState) { | ||
| builder.NewFunctionBuilder(). | ||
| WithFunc(func(_ context.Context) int64 { | ||
| price := ps.GetNodePrice() | ||
| payload := binary.LittleEndian.AppendUint64(nil, uint64(price)) | ||
| r.eventLog.Record(eventlog.NodePrice, payload) | ||
| return price | ||
| }). | ||
| Export("node_price") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Package pricing implements the /igor/price/1.0.0 protocol for inter-node | ||
| // price discovery. Nodes respond to price queries with their current execution | ||
| // pricing, enabling agents to make cost-aware migration decisions. | ||
| package pricing | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "time" | ||
|
|
||
| "github.com/libp2p/go-libp2p/core/host" | ||
| "github.com/libp2p/go-libp2p/core/network" | ||
| "github.com/libp2p/go-libp2p/core/peer" | ||
| "github.com/libp2p/go-libp2p/core/protocol" | ||
| "github.com/multiformats/go-multiaddr" | ||
| ) | ||
|
|
||
| // PriceProtocol is the Igor price query protocol identifier. | ||
| const PriceProtocol protocol.ID = "/igor/price/1.0.0" | ||
|
|
||
| // PriceRequest is sent by the querying node. | ||
| type PriceRequest struct { | ||
| // AgentID is optional: future use for agent-specific pricing. | ||
| AgentID string `json:"agent_id,omitempty"` | ||
| } | ||
|
|
||
| // PriceResponse is returned by the responding node. | ||
| type PriceResponse struct { | ||
| PricePerSecond int64 `json:"price_per_second"` // microcents/sec | ||
| NodeID string `json:"node_id"` // peer ID of responding node | ||
| } | ||
|
|
||
| // Service handles price advertisement and queries over libp2p streams. | ||
| type Service struct { | ||
| host host.Host | ||
| pricePerSecond int64 | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| // NewService creates and registers the pricing service on the given host. | ||
| func NewService(h host.Host, pricePerSecond int64, logger *slog.Logger) *Service { | ||
| svc := &Service{ | ||
| host: h, | ||
| pricePerSecond: pricePerSecond, | ||
| logger: logger, | ||
| } | ||
| h.SetStreamHandler(PriceProtocol, svc.handlePriceQuery) | ||
| logger.Info("Pricing service initialized", | ||
| "price_per_second", pricePerSecond, | ||
| ) | ||
| return svc | ||
| } | ||
|
|
||
| // handlePriceQuery responds to incoming price queries. | ||
| func (s *Service) handlePriceQuery(stream network.Stream) { | ||
| defer stream.Close() | ||
|
|
||
| if err := stream.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil { | ||
| s.logger.Error("Failed to set price query read deadline", "error", err) | ||
| return | ||
| } | ||
|
|
||
| remotePeer := stream.Conn().RemotePeer() | ||
|
|
||
| var req PriceRequest | ||
| if err := json.NewDecoder(stream).Decode(&req); err != nil { | ||
| s.logger.Error("Failed to decode price request", | ||
| "from_peer", remotePeer.String(), | ||
| "error", err, | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| resp := PriceResponse{ | ||
| PricePerSecond: s.pricePerSecond, | ||
| NodeID: s.host.ID().String(), | ||
| } | ||
|
|
||
| if err := json.NewEncoder(stream).Encode(resp); err != nil { | ||
| s.logger.Error("Failed to encode price response", | ||
| "to_peer", remotePeer.String(), | ||
| "error", err, | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| s.logger.Info("Served price query", | ||
| "from_peer", remotePeer.String(), | ||
| "price", s.pricePerSecond, | ||
| ) | ||
| } | ||
|
|
||
| // QueryPeerPrice queries a remote peer's execution price. | ||
| func (s *Service) QueryPeerPrice(ctx context.Context, peerAddr string) (*PriceResponse, error) { | ||
| maddr, err := multiaddr.NewMultiaddr(peerAddr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid peer address: %w", err) | ||
| } | ||
|
|
||
| addrInfo, err := peer.AddrInfoFromP2pAddr(maddr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to extract peer info: %w", err) | ||
| } | ||
|
|
||
| if err := s.host.Connect(ctx, *addrInfo); err != nil { | ||
| return nil, fmt.Errorf("failed to connect to peer: %w", err) | ||
| } | ||
|
|
||
| stream, err := s.host.NewStream(ctx, addrInfo.ID, PriceProtocol) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open price stream: %w", err) | ||
| } | ||
| defer stream.Close() | ||
|
|
||
| req := PriceRequest{} | ||
| if err := json.NewEncoder(stream).Encode(req); err != nil { | ||
| return nil, fmt.Errorf("failed to send price request: %w", err) | ||
| } | ||
|
|
||
| var resp PriceResponse | ||
| if err := json.NewDecoder(stream).Decode(&resp); err != nil { | ||
| return nil, fmt.Errorf("failed to read price response: %w", err) | ||
| } | ||
|
|
||
| s.logger.Info("Received peer price", | ||
| "peer_id", addrInfo.ID.String(), | ||
| "price_per_second", resp.PricePerSecond, | ||
| ) | ||
|
|
||
| return &resp, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QueryPeerPricestream reads to the request contextAfter
NewStreamsucceeds, the code doesjson.NewDecoder(stream).Decode(&resp)without setting a read deadline, so a peer that accepts the stream but never sends a response can block this call indefinitely even if the caller passed a timed context. This makes price discovery hang in partially-failing or adversarial networks instead of respecting the caller’s timeout budget.Useful? React with 👍 / 👎.