1
1
package activation
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "errors"
5
7
"io"
6
8
"net/http"
7
9
"net/http/httptest"
@@ -375,8 +377,9 @@ func TestPoetClient_RecertifiesOnAuthFailure(t *testing.T) {
375
377
mCertifier .EXPECT ().
376
378
Certificate (gomock .Any (), sig .NodeID (), certifierAddress , certifierPubKey ).
377
379
Return (& certifier.PoetCert {Data : []byte ("first" )}, nil ),
380
+ mCertifier .EXPECT ().DeleteCertificate (sig .NodeID (), certifierPubKey ),
378
381
mCertifier .EXPECT ().
379
- Recertify (gomock .Any (), sig .NodeID (), certifierAddress , certifierPubKey ).
382
+ Certificate (gomock .Any (), sig .NodeID (), certifierAddress , certifierPubKey ).
380
383
Return (& certifier.PoetCert {Data : []byte ("second" )}, nil ),
381
384
)
382
385
@@ -389,6 +392,80 @@ func TestPoetClient_RecertifiesOnAuthFailure(t *testing.T) {
389
392
require .Equal (t , 2 , submitCount )
390
393
}
391
394
395
+ func TestPoetClient_FallbacksToPowWhenCannotRecertify (t * testing.T ) {
396
+ t .Parallel ()
397
+
398
+ sig , err := signing .NewEdSigner ()
399
+ require .NoError (t , err )
400
+
401
+ certifierAddress := & url.URL {Scheme : "http" , Host : "certifier" }
402
+ certifierPubKey := []byte ("certifier-pubkey" )
403
+
404
+ mux := http .NewServeMux ()
405
+ infoResp , err := protojson .Marshal (& rpcapi.InfoResponse {
406
+ ServicePubkey : []byte ("pubkey" ),
407
+ Certifier : & rpcapi.InfoResponse_Cerifier {
408
+ Url : certifierAddress .String (),
409
+ Pubkey : certifierPubKey ,
410
+ },
411
+ })
412
+ require .NoError (t , err )
413
+ mux .HandleFunc ("GET /v1/info" , func (w http.ResponseWriter , r * http.Request ) { w .Write (infoResp ) })
414
+
415
+ powChallenge := []byte ("challenge" )
416
+ powResp , err := protojson .Marshal (& rpcapi.PowParamsResponse {PowParams : & rpcapi.PowParams {Challenge : powChallenge }})
417
+ require .NoError (t , err )
418
+ mux .HandleFunc ("GET /v1/pow_params" , func (w http.ResponseWriter , r * http.Request ) { w .Write (powResp ) })
419
+
420
+ submitResp , err := protojson .Marshal (& rpcapi.SubmitResponse {})
421
+ require .NoError (t , err )
422
+ submitCount := 0
423
+ mux .HandleFunc ("POST /v1/submit" , func (w http.ResponseWriter , r * http.Request ) {
424
+ req := rpcapi.SubmitRequest {}
425
+ body , _ := io .ReadAll (r .Body )
426
+ protojson .Unmarshal (body , & req )
427
+
428
+ switch {
429
+ case submitCount == 0 :
430
+ w .WriteHeader (http .StatusUnauthorized )
431
+ case submitCount == 1 && req .Certificate == nil && bytes .Equal (req .PowParams .Challenge , powChallenge ):
432
+ w .Write (submitResp )
433
+ default :
434
+ w .WriteHeader (http .StatusUnauthorized )
435
+ }
436
+ submitCount ++
437
+ })
438
+
439
+ ts := httptest .NewServer (mux )
440
+ defer ts .Close ()
441
+
442
+ server := types.PoetServer {
443
+ Address : ts .URL ,
444
+ Pubkey : types .NewBase64Enc ([]byte ("pubkey" )),
445
+ }
446
+ cfg := PoetConfig {CertifierInfoCacheTTL : time .Hour }
447
+
448
+ ctrl := gomock .NewController (t )
449
+ mCertifier := NewMockcertifierService (ctrl )
450
+ gomock .InOrder (
451
+ mCertifier .EXPECT ().
452
+ Certificate (gomock .Any (), sig .NodeID (), certifierAddress , certifierPubKey ).
453
+ Return (& certifier.PoetCert {Data : []byte ("first" )}, nil ),
454
+ mCertifier .EXPECT ().DeleteCertificate (sig .NodeID (), certifierPubKey ),
455
+ mCertifier .EXPECT ().
456
+ Certificate (gomock .Any (), sig .NodeID (), certifierAddress , certifierPubKey ).
457
+ Return (nil , errors .New ("cannot recertify" )),
458
+ )
459
+
460
+ client , err := NewHTTPPoetClient (server , cfg , withCustomHttpClient (ts .Client ()))
461
+ require .NoError (t , err )
462
+ poet := NewPoetServiceWithClient (nil , client , cfg , zaptest .NewLogger (t ), WithCertifier (mCertifier ))
463
+
464
+ _ , err = poet .Submit (context .Background (), time.Time {}, nil , nil , types .RandomEdSignature (), sig .NodeID ())
465
+ require .NoError (t , err )
466
+ require .Equal (t , 2 , submitCount )
467
+ }
468
+
392
469
func TestPoetService_CachesCertifierInfo (t * testing.T ) {
393
470
t .Parallel ()
394
471
type test struct {
0 commit comments