@@ -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{}
3233func (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
4044func 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+ }
0 commit comments