Skip to content

Commit 1acef23

Browse files
committed
refactor(signer): carry payment continuation in signed client state
Keep receipt replay enforcement at the orchestrator, remove signer SQLite storage, and use the extracted sender implementation. Include updated cross-component compatibility tests.
1 parent 3956d7f commit 1acef23

9 files changed

Lines changed: 510 additions & 252 deletions

File tree

‎orchestrator/payment_test.go‎

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"runtime"
1717
"strings"
18+
"sync/atomic"
1819
"testing"
1920
"time"
2021

@@ -32,10 +33,13 @@ type paymentTestChain struct{}
3233
func (paymentTestChain) Snapshot(context.Context) (pm.ChainSnapshot, error) {
3334
return pm.ChainSnapshot{Block: big.NewInt(50), Round: big.NewInt(5), RoundHash: ethcommon.HexToHash("0x1234")}, nil
3435
}
35-
func (paymentTestChain) ValidateSender(context.Context, ethcommon.Address, *big.Int) error {
36-
return nil
36+
func (c paymentTestChain) SenderInfo(ctx context.Context, _, _ ethcommon.Address) (eth.SenderInfo, error) {
37+
snapshot, err := c.Snapshot(ctx)
38+
return eth.SenderInfo{Snapshot: snapshot, Deposit: big.NewInt(1000000000), Reserve: big.NewInt(1000000000), WithdrawRound: new(big.Int)}, err
39+
}
40+
func (paymentTestChain) IsActiveAt(context.Context, ethcommon.Address, pm.ChainSnapshot) (bool, error) {
41+
return true, nil
3742
}
38-
func (paymentTestChain) IsActive(context.Context, ethcommon.Address) (bool, error) { return true, nil }
3943

4044
func paymentTestKey(t *testing.T) *eth.Key {
4145
t.Helper()
@@ -57,7 +61,7 @@ func TestPaidFixedSessionWithRemoteSigner(t *testing.T) {
5761
require.NoError(t, err)
5862
defer store.Close()
5963
max := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
60-
engine, err := pm.NewEngine(store, paymentTestChain{}, recipient.Address(), big.NewInt(10), new(big.Int).Sub(max, big.NewInt(1)))
64+
engine, err := pm.NewEngine(store, paymentTestChain{}, recipient.Address(), big.NewInt(11), new(big.Int).Sub(max, big.NewInt(1)))
6165
require.NoError(t, err)
6266
registry := NewRegistry("bootstrap", "http://127.0.0.1:8935", time.Second, 30*time.Second)
6367
registry.SetWeiPerUSD(big.NewRat(10, 1))
@@ -69,7 +73,7 @@ func TestPaidFixedSessionWithRemoteSigner(t *testing.T) {
6973
require.NoError(t, err)
7074
server := NewServer(registry, policy, policy, slog.New(slog.NewTextHandler(io.Discard, nil)))
7175
server.SetPayment(engine)
72-
signerService, err := signer.OpenService(payer, filepath.Join(t.TempDir(), "signer.sqlite"), "")
76+
signerService, err := signer.NewService(payer, "")
7377
require.NoError(t, err)
7478
defer signerService.Close()
7579
signerService.SetPaymentChain(paymentTestChain{})
@@ -93,6 +97,10 @@ func TestPaidFixedSessionWithRemoteSigner(t *testing.T) {
9397
SegCreds string `json:"segCreds"`
9498
}
9599
require.NoError(t, json.Unmarshal(signed.Body.Bytes(), &payment))
100+
// A quote already issued to the payer remains valid when a heartbeat
101+
// changes the advertised price before the reservation is paid.
102+
_, _, err = registry.Heartbeat(heartbeatRequest{RunnerID: "runner1", RunnerURL: "http://127.0.0.1:9000", App: "demo", Mode: "persistent", Capacity: 1, PriceInfo: priceInfo{Price: "2", Currency: "usd", Unit: "fixed"}}, resp.HeartbeatSecret)
103+
require.NoError(t, err)
96104
paid := httptest.NewRequest(http.MethodPost, reserveURL, nil)
97105
paid.Header.Set("Livepeer-Payment", payment.Payment)
98106
paid.Header.Set("Livepeer-Segment", payment.SegCreds)
@@ -102,9 +110,12 @@ func TestPaidFixedSessionWithRemoteSigner(t *testing.T) {
102110
var reservation map[string]string
103111
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &reservation))
104112
require.Equal(t, challenge.ManifestID, reservation["session_id"])
113+
pinned, _, err := registry.PriceForSession("runner1", challenge.ManifestID)
114+
require.NoError(t, err)
115+
require.Equal(t, "10", pinned.Price.String())
105116
balance, err := engine.Balance(challenge.ManifestID)
106117
require.NoError(t, err)
107-
require.Zero(t, balance.Sign())
118+
require.True(t, balance.Sign() >= 0 && balance.Cmp(big.NewRat(1, 1)) < 0)
108119
count, err := store.WinningTicketCount(payer.Address(), 0)
109120
require.NoError(t, err)
110121
require.Equal(t, 1, count)
@@ -140,7 +151,7 @@ func TestPinnedPythonPaidSingleShot(t *testing.T) {
140151
require.NoError(t, err)
141152
defer store.Close()
142153
max := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
143-
engine, err := pm.NewEngine(store, paymentTestChain{}, recipient.Address(), big.NewInt(10), new(big.Int).Sub(max, big.NewInt(1)))
154+
engine, err := pm.NewEngine(store, paymentTestChain{}, recipient.Address(), big.NewInt(11), new(big.Int).Sub(max, big.NewInt(1)))
144155
require.NoError(t, err)
145156
registry := NewRegistry("", "http://127.0.0.1:0", time.Second, 30*time.Second)
146157
registry.SetWeiPerUSD(big.NewRat(10, 1))
@@ -154,7 +165,7 @@ func TestPinnedPythonPaidSingleShot(t *testing.T) {
154165
defer orch.Close()
155166
registry.service = "http://" + orch.Listener.Addr().String()
156167
orch.Start()
157-
signerService, err := signer.OpenService(payer, filepath.Join(t.TempDir(), "signer.sqlite"), "")
168+
signerService, err := signer.NewService(payer, "")
158169
require.NoError(t, err)
159170
defer signerService.Close()
160171
signerService.SetPaymentChain(paymentTestChain{})
@@ -185,7 +196,7 @@ func TestPinnedPythonSignerDiscovery(t *testing.T) {
185196
defer orch.Close()
186197
registry.service = "http://" + orch.Listener.Addr().String()
187198
orch.Start()
188-
signerService, err := signer.OpenService(paymentTestKey(t), filepath.Join(t.TempDir(), "signer.sqlite"), "")
199+
signerService, err := signer.NewService(paymentTestKey(t), "")
189200
require.NoError(t, err)
190201
defer signerService.Close()
191202
signerService.SetPaymentChain(paymentTestChain{})
@@ -222,6 +233,13 @@ func pinnedPythonSDK(t *testing.T) (python, export, root string) {
222233
revision, err := exec.Command("git", "-C", sdkDir, "rev-parse", "HEAD").Output()
223234
require.NoError(t, err)
224235
require.Equal(t, "44df06157fcdb864e37d971e8caba86b2a7dc92e", strings.TrimSpace(string(revision)))
236+
t.Attr("python_sdk_revision", strings.TrimSpace(string(revision)))
237+
t.Setenv("PYTHONDONTWRITEBYTECODE", "1")
238+
if os.Getenv("PYTHON_RUNNER_USE_WORKING_TREE") == "1" {
239+
t.Attr("python_sdk_tree", "working-copy")
240+
return python, sdkDir, root
241+
}
242+
t.Attr("python_sdk_tree", "committed")
225243
export = t.TempDir()
226244
archive := exec.Command("git", "-C", sdkDir, "archive", "HEAD")
227245
tar := exec.Command("tar", "-xf", "-", "-C", export)
@@ -234,11 +252,70 @@ func pinnedPythonSDK(t *testing.T) (python, export, root string) {
234252
return
235253
}
236254

237-
func (c paymentTestChain) SenderInfo(ctx context.Context, _, _ ethcommon.Address) (eth.SenderInfo, error) {
255+
type advancingPaymentChain struct{ block atomic.Int64 }
256+
257+
func (c *advancingPaymentChain) Snapshot(context.Context) (pm.ChainSnapshot, error) {
258+
return pm.ChainSnapshot{Block: big.NewInt(c.block.Load()), Round: big.NewInt(5), RoundHash: ethcommon.HexToHash("0x1234")}, nil
259+
}
260+
func (c *advancingPaymentChain) SenderInfo(ctx context.Context, _, _ ethcommon.Address) (eth.SenderInfo, error) {
238261
snapshot, err := c.Snapshot(ctx)
239262
return eth.SenderInfo{Snapshot: snapshot, Deposit: big.NewInt(1000000000), Reserve: big.NewInt(1000000000), WithdrawRound: new(big.Int)}, err
240263
}
241-
242-
func (paymentTestChain) IsActiveAt(context.Context, ethcommon.Address, pm.ChainSnapshot) (bool, error) {
264+
func (c *advancingPaymentChain) IsActiveAt(context.Context, ethcommon.Address, pm.ChainSnapshot) (bool, error) {
243265
return true, nil
244266
}
267+
268+
func TestPinnedPythonSustainedPaymentRefresh(t *testing.T) {
269+
if testing.Short() {
270+
t.Skip("Python SDK integration")
271+
}
272+
python, export, root := pinnedPythonSDK(t)
273+
chain := new(advancingPaymentChain)
274+
chain.block.Store(50)
275+
payer, recipient := paymentTestKey(t), paymentTestKey(t)
276+
store, err := pm.OpenSQLite(filepath.Join(t.TempDir(), "payments.sqlite"))
277+
require.NoError(t, err)
278+
defer store.Close()
279+
max := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
280+
engine, err := pm.NewEngine(store, chain, recipient.Address(), big.NewInt(20), new(big.Int).Add(new(big.Int).Quo(max, big.NewInt(2)), big.NewInt(1)))
281+
require.NoError(t, err)
282+
registry := NewRegistry("", "http://127.0.0.1:0", time.Second, time.Minute)
283+
registry.SetWeiPerUSD(big.NewRat(360000, 1))
284+
require.NoError(t, registry.AddStatic(StaticRunner{ID: "live", RunnerURL: "https://runner.example", App: "refresh-test", Mode: "persistent", PriceInfo: priceInfo{Price: "1", Currency: "usd", Unit: "hour"}}))
285+
policy, err := destination.New("runner", nil)
286+
require.NoError(t, err)
287+
app := NewServer(registry, policy, policy, slog.New(slog.NewTextHandler(io.Discard, nil)))
288+
defer app.Close()
289+
app.SetPayment(engine)
290+
var refreshes atomic.Int32
291+
orch := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
292+
if r.URL.Path == "/test/advance" {
293+
chain.block.Add(40)
294+
w.WriteHeader(200)
295+
return
296+
}
297+
if r.URL.Path == "/refresh-payment" {
298+
refreshes.Add(1)
299+
}
300+
app.ServeHTTP(w, r)
301+
}))
302+
registry.service = "http://" + orch.Listener.Addr().String()
303+
orch.Start()
304+
defer orch.Close()
305+
service, err := signer.NewService(payer, "")
306+
require.NoError(t, err)
307+
defer service.Close()
308+
service.SetPaymentChain(chain)
309+
signed := httptest.NewServer(service)
310+
defer signed.Close()
311+
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
312+
defer cancel()
313+
cmd := exec.CommandContext(ctx, python, filepath.Join(root, "cmd", "livepeer", "testdata", "python_payment_refresh.py"), orch.URL, signed.URL)
314+
cmd.Env = append(os.Environ(), "PYTHONPATH="+filepath.Join(export, "src"), "PYTHONDONTWRITEBYTECODE=1")
315+
output, err := cmd.CombinedOutput()
316+
require.NoError(t, err, string(output))
317+
require.Contains(t, string(output), "signed state preserved")
318+
require.Equal(t, int32(3), refreshes.Load())
319+
_, sessions := registry.Counts()
320+
require.Zero(t, sessions)
321+
}

‎signer/app.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ func Serve(p Params) error {
101101
if err != nil {
102102
return err
103103
}
104-
store, err := openStateStore(p.StateDB)
104+
service, err := NewService(key, p.AuthToken)
105105
if err != nil {
106106
return err
107107
}
108-
defer store.close()
109-
service := NewService(key, store, p.AuthToken)
108+
defer service.Close()
110109
if p.RPCURL != "" {
111110
rpc, err := eth.OpenRPC(p.RPCURL, p.RPCGrants, p.RPCCAFile)
112111
if err != nil {

‎signer/auth_test.go‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package signer
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
"sync/atomic"
10+
"testing"
11+
"time"
12+
13+
"github.com/livepeer/node/signercompat"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestAuthWebhookCachePriceAndIdentity(t *testing.T) {
18+
s, info := testService(t)
19+
var calls atomic.Int32
20+
var reject atomic.Bool
21+
webhook := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
calls.Add(1)
23+
require.Equal(t, "configured", r.Header.Get("X-Webhook-Secret"))
24+
var body struct {
25+
Headers http.Header
26+
State paymentState
27+
}
28+
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
29+
require.Equal(t, "manifest-1", body.State.ManifestID)
30+
if calls.Load() == 1 {
31+
require.Equal(t, uint64(0), body.State.SequenceNumber)
32+
require.Equal(t, uint32(1), body.State.SenderNonce)
33+
}
34+
require.NotEmpty(t, body.State.PMSessionID)
35+
require.NotEmpty(t, body.State.Balance)
36+
require.WithinDuration(t, time.Now(), body.State.LastUpdate, time.Second)
37+
if calls.Load() == 1 {
38+
require.Empty(t, body.State.AuthID)
39+
}
40+
status := 200
41+
if reject.Load() {
42+
status = 403
43+
}
44+
_ = json.NewEncoder(w).Encode(map[string]any{"status": status, "reason": "denied", "expiry": time.Now().Add(time.Minute).Unix(), "auth_id": "alice", "maxPrice": map[string]any{"price": "10", "currency": " WEI ", "unit": " FIXED "}})
45+
}))
46+
defer webhook.Close()
47+
require.NoError(t, s.SetAuthWebhook(webhook.URL, []string{strings.TrimPrefix(webhook.URL, "http://")}, "", map[string]string{"X-Webhook-Secret": "configured"}))
48+
req := map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"}
49+
w := postPayment(t, s, req)
50+
require.Equal(t, 200, w.Code, w.Body.String())
51+
var first paymentResponse
52+
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &first))
53+
var state paymentState
54+
require.NoError(t, json.Unmarshal(first.State.State, &state))
55+
require.Equal(t, "alice", state.AuthID)
56+
require.Equal(t, "10", state.AuthMaxPrice)
57+
req["state"] = first.State
58+
w = postPayment(t, s, req)
59+
require.Equal(t, 200, w.Code, w.Body.String())
60+
require.Equal(t, int32(1), calls.Load())
61+
// Identity checks apply to signed state on any replica.
62+
body, err := json.Marshal(req)
63+
require.NoError(t, err)
64+
r := httptest.NewRequest("POST", "/generate-live-payment", strings.NewReader(string(body)))
65+
r.Header.Set("Signer-Auth-Id", "bob")
66+
w = httptest.NewRecorder()
67+
s.ServeHTTP(w, r)
68+
require.Equal(t, 403, w.Code)
69+
// Changing configured credentials invalidates prior webhook authorization.
70+
var second paymentResponse
71+
require.NoError(t, json.Unmarshal(postPayment(t, s, req).Body.Bytes(), &second))
72+
req["state"] = second.State
73+
reject.Store(true)
74+
require.NoError(t, s.SetAuthWebhook(webhook.URL, []string{strings.TrimPrefix(webhook.URL, "http://")}, "", map[string]string{"X-Webhook-Secret": "configured", "X-Policy": "new"}))
75+
w = postPayment(t, s, req)
76+
require.Equal(t, 403, w.Code, w.Body.String())
77+
require.Equal(t, int32(2), calls.Load())
78+
require.NotContains(t, w.Body.String(), "payment")
79+
require.NotContains(t, w.Body.String(), "state")
80+
}
81+
82+
func TestAuthWebhookPriceRejectsBeforeResponse(t *testing.T) {
83+
s, info := testService(t)
84+
webhook := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
85+
_, _ = w.Write([]byte(`{"status":200,"maxPrice":{"price":9,"currency":"wei","unit":"fixed"}}`))
86+
}))
87+
defer webhook.Close()
88+
require.NoError(t, s.SetAuthWebhook(webhook.URL, []string{strings.TrimPrefix(webhook.URL, "http://")}, "", nil))
89+
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"})
90+
require.Equal(t, 481, w.Code, w.Body.String())
91+
require.NotContains(t, w.Body.String(), "segCreds")
92+
require.NotContains(t, w.Body.String(), "payment")
93+
require.NotContains(t, w.Body.String(), "state")
94+
}
95+
96+
func TestExpiredParamsRequestRefresh(t *testing.T) {
97+
for _, expired := range []string{"auth", "params"} {
98+
t.Run(expired, func(t *testing.T) {
99+
s, info := testService(t)
100+
if expired == "auth" {
101+
info.Auth.Expiration = time.Now().Add(-time.Second).Unix()
102+
} else {
103+
info.TicketParams.ExpirationBlock = []byte{51}
104+
}
105+
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"})
106+
require.Equal(t, 480, w.Code, w.Body.String())
107+
require.Equal(t, info.Transcoder, w.Header().Get("Livepeer-Orchestrator-URL"))
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)