Skip to content

Commit

Permalink
fix(coordinator&prover): jwt token expired bug (scroll-tech#736)
Browse files Browse the repository at this point in the history
Co-authored-by: colinlyguo <[email protected]>
  • Loading branch information
georgehao and colinlyguo authored Aug 6, 2023
1 parent 11fac03 commit 816a3b4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion common/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func (c *Cmd) Write(data []byte) (int, error) {
out := string(data)
if verbose || c.openLog {
fmt.Printf("%s:\n\t%v", c.name, out)
} else if strings.Contains(strings.ToLower(out), "error") || strings.Contains(strings.ToLower(out), "warning") || strings.Contains(strings.ToLower(out), "info") {
} else if strings.Contains(strings.ToLower(out), "error") ||
strings.Contains(strings.ToLower(out), "warning") ||
strings.Contains(strings.ToLower(out), "info") {
fmt.Printf("%s:\n\t%v", c.name, out)
}
go c.checkFuncs.IterCb(func(_ string, value interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.1.11"
var tag = "v4.1.12"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
2 changes: 2 additions & 0 deletions coordinator/cmd/app/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (c *CoordinatorApp) MockConfig(store bool) error {
}
cfg.DB.DSN = base.DBImg.Endpoint()
cfg.L2.ChainID = 111
cfg.Auth.ChallengeExpireDurationSec = 1
cfg.Auth.LoginExpireDurationSec = 1
c.Config = cfg

if !store {
Expand Down
4 changes: 2 additions & 2 deletions coordinator/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"auth": {
"secret": "prover secret key",
"challenge_expire_duration_sec": 3600,
"login_expire_duration_sec": 10
"challenge_expire_duration_sec": 10,
"login_expire_duration_sec": 3600
}
}
2 changes: 1 addition & 1 deletion coordinator/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type L2 struct {
type Auth struct {
Secret string `json:"secret"`
ChallengeExpireDurationSec int `json:"challenge_expire_duration_sec"`
LoginExpireDurationSec int `json:"token_expire_duration_sec"`
LoginExpireDurationSec int `json:"login_expire_duration_sec"`
}

// Config load configuration items.
Expand Down
2 changes: 2 additions & 0 deletions coordinator/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func setupCoordinator(t *testing.T, proversPerSession uint8, coordinatorURL stri
assert.NoError(t, runErr)
}
}()
time.Sleep(time.Second * 2)

return proofCollector, srv
}

Expand Down
10 changes: 7 additions & 3 deletions prover/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ func (c *CoordinatorClient) Login(ctx context.Context) error {
Signature: authMsg.Signature,
}

// store JWT token for login requests
c.client.SetAuthToken(challengeResult.Data.Token)

var loginResult LoginResponse
loginResp, err := c.client.R().
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", fmt.Sprintf("Bearer %s", challengeResult.Data.Token)).
SetBody(loginReq).
SetResult(&loginResult).
Post("/coordinator/v1/login")
Expand Down Expand Up @@ -136,10 +138,11 @@ func (c *CoordinatorClient) GetTask(ctx context.Context, req *GetTaskRequest) (*
}

if result.ErrCode == types.ErrJWTTokenExpired {
log.Debug("JWT expired, attempting to re-login")
log.Info("JWT expired, attempting to re-login")
if err := c.Login(ctx); err != nil {
return nil, fmt.Errorf("JWT expired, re-login failed: %v", err)
}
log.Info("re-login success")
return c.GetTask(ctx, req)
}
if result.ErrCode != types.Success {
Expand Down Expand Up @@ -168,10 +171,11 @@ func (c *CoordinatorClient) SubmitProof(ctx context.Context, req *SubmitProofReq
}

if result.ErrCode == types.ErrJWTTokenExpired {
log.Debug("JWT expired, attempting to re-login")
log.Info("JWT expired, attempting to re-login")
if err := c.Login(ctx); err != nil {
return fmt.Errorf("JWT expired, re-login failed: %v", err)
}
log.Info("re-login success")
return c.SubmitProof(ctx, req)
}
if result.ErrCode != types.Success {
Expand Down
20 changes: 20 additions & 0 deletions tests/integration-test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,23 @@ func TestCoordinatorProverInteraction(t *testing.T) {
batchProverApp.WaitExit()
coordinatorApp.WaitExit()
}

func TestProverReLogin(t *testing.T) {
// Start postgres docker containers.
base.RunL2Geth(t)
base.RunDBImage(t)

assert.NoError(t, migrate.ResetDB(base.DBClient(t)))

// Run coordinator app.
coordinatorApp.RunApp(t) // login timeout: 1 sec

// Run prover app.
chunkProverApp.RunAppWithExpectedResult(t, "re-login success") // chunk prover login.
batchProverApp.RunAppWithExpectedResult(t, "re-login success") // batch prover login.

// Free apps.
chunkProverApp.WaitExit()
batchProverApp.WaitExit()
coordinatorApp.WaitExit()
}

0 comments on commit 816a3b4

Please sign in to comment.