Skip to content

Commit

Permalink
Pass a correct session end height to NewSession (#1545)
Browse files Browse the repository at this point in the history
With session rollover (#1536), we accept relays for older sessions. For
exaple, a node at block 101 accepts a relay for the session height 97.

There is a bug in the relay validation function `relay.Validate`. In the
example above, the function sees the following values:

```
ctx.BlockHeight() = 101
sessionBlockHeight = r.Proof.SessionBlockHeight = 97
sessionCtx.BlockHeight() = 97
```

and if the corresponding session is not cached, it passes `sessionCtx`
and `ctx` to `NewSession`. This may return a wrong session because the
second argument is supposed to be the end of the session, but in this
case it's not.

The proposed fix is if `ctx` is beyond the relay session, we get a new
context of the session end and pass it to `NewSession`.
  • Loading branch information
msmania authored Apr 20, 2023
1 parent e642a9d commit 8d4836b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion x/pocketcore/types/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,23 @@ func (r *Relay) Validate(ctx sdk.Ctx, posKeeper PosKeeper, appsKeeper AppsKeeper
if err != nil {
return sdk.ZeroInt(), sdk.ErrInternal(err.Error())
}

// With session rollover, the height of `ctx` may already be in the next
// session of the relay's session. In such a case, we need to pass the
// correct context of the session end instead of `ctx`.
sessionEndHeight :=
sessionBlockHeight + posKeeper.BlocksPerSession(sessionCtx) - 1
var sesssionEndCtx sdk.Ctx
if ctx.BlockHeight() > sessionEndHeight {
if sesssionEndCtx, err = ctx.PrevCtx(sessionEndHeight); err != nil {
return sdk.ZeroInt(), sdk.ErrInternal(er.Error())
}
} else {
sesssionEndCtx = ctx
}

var er sdk.Error
session, er = NewSession(sessionCtx, ctx, posKeeper, header, hex.EncodeToString(bh), int(sessionNodeCount))
session, er = NewSession(sessionCtx, sesssionEndCtx, posKeeper, header, hex.EncodeToString(bh), int(sessionNodeCount))
if er != nil {
return sdk.ZeroInt(), er
}
Expand Down

0 comments on commit 8d4836b

Please sign in to comment.