Add proof-measure client (public securedBy / measured-immutability service)

proof-measure is a separate, public, unauthenticated Dragonchain service. Adds:
- client.UnauthenticatedClient: HMAC-free transport mirroring Client's
  marshal/decode/error handling.
- proofmeasure.ProofMeasureClient: GetSecurity / Report / Health, default base
  URL https://proof-measure.dragonchain.com; standalone + a DragonchainSDK.ProofMeasure handle.
- models for SecurityResult/RawMeasure/ReportRequest/TransactionReport/etc
  (decimals as strings, timestamps int64).
- httptest unit tests.
This commit is contained in:
2026-06-04 13:40:21 -04:00
parent bc2b622873
commit 7d8e23768f
6 changed files with 441 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
// Package proofmeasure is a client for the Dragonchain proof-measure service —
// the measured-immutability / "securedBy" metric for L1L5 verification chains.
//
// proof-measure is a separate, public, UNauthenticated service (no API keys),
// so this client takes only a base URL. It exposes a network's accumulated
// security as both a raw measure (cumulative hashes / stake-seconds) and a USD
// valuation, plus a per-transaction "securedBy" report over interchain anchors.
package proofmeasure
import (
"context"
"fmt"
"net/http"
"git.dragonchain.com/dragonchain/prime-sdk-go/client"
"git.dragonchain.com/dragonchain/prime-sdk-go/models"
)
// DefaultBaseURL is the public production proof-measure endpoint.
const DefaultBaseURL = "https://proof-measure.dragonchain.com"
// ProofMeasureClient calls the proof-measure HTTP API.
type ProofMeasureClient struct {
client *client.UnauthenticatedClient
}
// NewProofMeasureClient builds a client for the proof-measure service. An empty
// baseURL defaults to the public production endpoint (DefaultBaseURL).
func NewProofMeasureClient(baseURL string) *ProofMeasureClient {
return NewProofMeasureClientWithHTTPClient(baseURL, nil)
}
// NewProofMeasureClientWithHTTPClient is like NewProofMeasureClient but routes
// requests through the caller-supplied *http.Client. A nil hc uses the default.
func NewProofMeasureClientWithHTTPClient(baseURL string, hc *http.Client) *ProofMeasureClient {
if baseURL == "" {
baseURL = DefaultBaseURL
}
return &ProofMeasureClient{
client: client.NewUnauthenticatedClientWithHTTPClient(baseURL, hc),
}
}
// GetSecurity returns the security a public network (network = "BTC" or "ETH")
// has accumulated since the given unix timestamp, as both a raw measure and a
// USD valuation. A non-positive since omits the parameter, letting the service
// apply its default window.
func (pc *ProofMeasureClient) GetSecurity(ctx context.Context, network string, since int64) (*models.SecurityResult, error) {
path := fmt.Sprintf("/api/v1/security/%s", network)
if since > 0 {
path = fmt.Sprintf("%s?since=%d", path, since)
}
var resp models.SecurityResult
if err := pc.client.Get(ctx, path, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// Report computes the per-transaction "securedBy" report for the supplied
// interchain anchors: each anchor's raw + USD security since it was placed, plus
// combined totals.
func (pc *ProofMeasureClient) Report(ctx context.Context, req *models.ReportRequest) (*models.TransactionReport, error) {
var resp models.TransactionReport
if err := pc.client.Post(ctx, "/api/v1/report", models.ContentTypeJSON, req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// Health reports service liveness and DB reachability.
func (pc *ProofMeasureClient) Health(ctx context.Context) (*models.HealthResponse, error) {
var resp models.HealthResponse
if err := pc.client.Get(ctx, "/api/v1/health", &resp); err != nil {
return nil, err
}
return &resp, nil
}

View File

@@ -0,0 +1,135 @@
package proofmeasure
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"git.dragonchain.com/dragonchain/prime-sdk-go/models"
)
func TestGetSecurity(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.RequestURI()
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{
"network":"BTC","consensus":"pow",
"raw":{"value":"71.63","unit":"Yottahashes","base":"7.16e25"},
"valueUsd":"13928170.26","valueUsdFormatted":"$13,928,170.26",
"label":"$13,928,170.26 energy consumed","normalizedScore":"0.9234",
"since":1780504894,"asOf":1780591253}`)
}))
defer srv.Close()
pc := NewProofMeasureClient(srv.URL)
res, err := pc.GetSecurity(context.Background(), "BTC", 1780504894)
if err != nil {
t.Fatalf("GetSecurity: %v", err)
}
if gotPath != "/api/v1/security/BTC?since=1780504894" {
t.Errorf("path = %q, want /api/v1/security/BTC?since=1780504894", gotPath)
}
if res.Network != "BTC" || res.Consensus != "pow" {
t.Errorf("network/consensus = %q/%q", res.Network, res.Consensus)
}
if res.Raw.Unit != "Yottahashes" || res.ValueUSD != "13928170.26" {
t.Errorf("raw.unit=%q valueUsd=%q", res.Raw.Unit, res.ValueUSD)
}
if res.SinceUnix != 1780504894 || res.AsOfUnix != 1780591253 {
t.Errorf("since/asOf = %d/%d", res.SinceUnix, res.AsOfUnix)
}
}
func TestGetSecurity_NoSinceOmitsParam(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.RequestURI()
_, _ = io.WriteString(w, `{"network":"ETH","consensus":"pos"}`)
}))
defer srv.Close()
if _, err := NewProofMeasureClient(srv.URL).GetSecurity(context.Background(), "ETH", 0); err != nil {
t.Fatalf("GetSecurity: %v", err)
}
if gotPath != "/api/v1/security/ETH" {
t.Errorf("path = %q, want /api/v1/security/ETH (no since)", gotPath)
}
}
func TestReport(t *testing.T) {
var gotMethod, gotPath string
var gotReq models.ReportRequest
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&gotReq)
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{
"transactionId":"tx-1","primeId":"p-1","blockId":"42",
"anchors":[{"network":"BTC","anchorTimestamp":1712345678,"anchorTxHash":"0xabc",
"security":{"network":"BTC","consensus":"pow","valueUsd":"100.00"}}],
"totalValueUsd":"100.00","totalValueUsdFormatted":"$100.00",
"hashPower":{"value":"40.06","units":"Yottahashes"},"totalNormalizedScore":"0.9"}`)
}))
defer srv.Close()
pc := NewProofMeasureClient(srv.URL)
rep, err := pc.Report(context.Background(), &models.ReportRequest{
TransactionID: "tx-1", PrimeID: "p-1", BlockID: "42",
Anchors: []models.ReportAnchorInput{{Network: "BTC", TxHash: "0xabc", Timestamp: 1712345678}},
})
if err != nil {
t.Fatalf("Report: %v", err)
}
if gotMethod != http.MethodPost || gotPath != "/api/v1/report" {
t.Errorf("method/path = %s %s", gotMethod, gotPath)
}
if len(gotReq.Anchors) != 1 || gotReq.Anchors[0].Network != "BTC" {
t.Errorf("request anchors not sent: %+v", gotReq.Anchors)
}
if rep.TotalValueUSD != "100.00" || rep.HashPower == nil || rep.HashPower.Units != "Yottahashes" {
t.Errorf("report decode wrong: %+v", rep)
}
if len(rep.Anchors) != 1 || rep.Anchors[0].Security.Network != "BTC" {
t.Errorf("report anchors decode wrong: %+v", rep.Anchors)
}
}
func TestHealth(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/health" {
t.Errorf("health path = %q", r.URL.Path)
}
_, _ = io.WriteString(w, `{"status":"ok"}`)
}))
defer srv.Close()
h, err := NewProofMeasureClient(srv.URL).Health(context.Background())
if err != nil {
t.Fatalf("Health: %v", err)
}
if h.Status != "ok" {
t.Errorf("status = %q, want ok", h.Status)
}
}
func TestDefaultBaseURL(t *testing.T) {
if NewProofMeasureClient("") == nil {
t.Fatal("nil client for empty base URL")
}
}
func TestErrorStatus(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, `bad request`)
}))
defer srv.Close()
if _, err := NewProofMeasureClient(srv.URL).Health(context.Background()); err == nil {
t.Fatal("expected an error on 400 status, got nil")
}
}