Skip to content

Commit

Permalink
replay fix (#16530)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtaco authored and mmaxim committed Mar 11, 2019
1 parent afcf1a1 commit 4c383e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
15 changes: 13 additions & 2 deletions go/chat/flip/dealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flip
import (
"context"
"encoding/base64"
"encoding/hex"
"io"
"math"
"math/big"
Expand Down Expand Up @@ -77,6 +78,7 @@ type Game struct {
stage Stage
stageForTimeout Stage
players map[UserDeviceKey]*GamePlayerState
commitments map[string]bool
gameUpdateCh chan GameStateUpdateMessage
nPlayers int
dealer *Dealer
Expand Down Expand Up @@ -334,16 +336,24 @@ func (g *Game) handleMessage(ctx context.Context, msg *GameMessageWrapped, now t
if g.players[key] != nil {
return DuplicateRegistrationError{g.md, msg.Sender}
}

com := msg.Msg.Body.Commitment()
comHex := hex.EncodeToString(com[:])
if g.commitments[comHex] {
return DuplicateCommitmentError{}
}
g.commitments[comHex] = true

g.players[key] = &GamePlayerState{
ud: msg.Sender,
commitment: msg.Msg.Body.Commitment(),
commitment: com,
commitmentTime: now,
}
g.gameUpdateCh <- GameStateUpdateMessage{
Metadata: g.GameMetadata(),
Commitment: &CommitmentUpdate{
User: msg.Sender,
Commitment: msg.Msg.Body.Commitment(),
Commitment: com,
},
}

Expand Down Expand Up @@ -633,6 +643,7 @@ func (d *Dealer) handleMessageStart(ctx context.Context, msg *GameMessageWrapped
stageForTimeout: Stage_ROUND1,
gameUpdateCh: d.gameUpdateCh,
players: make(map[UserDeviceKey]*GamePlayerState),
commitments: make(map[string]bool),
dealer: d,
me: me,
clock: d.dh.Clock,
Expand Down
6 changes: 6 additions & 0 deletions go/chat/flip/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,9 @@ func NewReplayError(s string) ReplayError {
func (r ReplayError) Error() string {
return fmt.Sprintf("")
}

type DuplicateCommitmentError struct{}

func (d DuplicateCommitmentError) Error() string {
return "Duplicated commitment, something is fishy"
}
1 change: 1 addition & 0 deletions go/chat/flip/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (g GameHistory) start(rh ReplayHelper) (game *Game, rest GameHistory, err e
params: start,
gameUpdateCh: make(chan GameStateUpdateMessage),
players: make(map[UserDeviceKey]*GamePlayerState),
commitments: make(map[string]bool),
stage: Stage_ROUND1,
clogf: rh.CLogf,
}
Expand Down

0 comments on commit 4c383e6

Please sign in to comment.