From 15283cd1bf926254fad09ae04b1e04e381966c06 Mon Sep 17 00:00:00 2001 From: Özgür Kesim Date: Tue, 9 Apr 2024 22:32:39 +0200 Subject: dashboard: Messages and Cancel implemented --- dashboard/dashboard.go | 4 +++- dashboard/inmemory.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/dashboard/dashboard.go b/dashboard/dashboard.go index 9b6dedd..6f384f9 100644 --- a/dashboard/dashboard.go +++ b/dashboard/dashboard.go @@ -46,6 +46,7 @@ const ( ErrAuctionFinished ErrAuctionCanceled ErrAuctionTimeout + ErrAuctionInvalidRound ErrBidderUnknown ErrBidderTimeout @@ -79,6 +80,8 @@ func (d Error) Error() string { return "action has timed out" case ErrAuctionCanceled: return "auction has been canceled" + case ErrAuctionInvalidRound: + return "invalid round" case ErrBidderUnknown: return "bidder is unknown" @@ -120,7 +123,6 @@ func (s *SignedCommitment) Verify(pubkey ed25519.PublicKey) bool { buf := &bytes.Buffer{} e := json.NewEncoder(buf).Encode(s.Commitment) if e != nil { - // TODO: log message? return false } diff --git a/dashboard/inmemory.go b/dashboard/inmemory.go index 82412cb..0548494 100644 --- a/dashboard/inmemory.go +++ b/dashboard/inmemory.go @@ -24,6 +24,10 @@ type auction struct { description *seal.SignedDescription state AuctionState round uint8 // which bit has been completed + canceled struct { + reason SignedMessage + by string // bidderId + } commitments map[string]SignedCommitment messages map[string][]SignedMessage // per-bidder and per-round messages, pre-allocated. @@ -183,5 +187,56 @@ func (m *inmemory) Publish(auctionId string, round uint8, message SignedMessage, } func (m *inmemory) Messages(auctionId string, round uint8) (messages map[string]SignedMessage, e error) { - return nil, fmt.Errorf("inmemory.Messages not implemented") + auction, e := m.getAuction(auctionId) + if e != nil { + return nil, e + } + + auction.RLock() + defer auction.RUnlock() + + if auction.round <= round { + return nil, ErrAuctionInvalidRound + } + + messages = make(map[string]SignedMessage) + for id, msgs := range auction.messages { + messages[id] = msgs[round] + } + + return messages, nil +} + +func (m *inmemory) Cancel(auctionId string, reason SignedMessage, publickey ed25519.PublicKey) error { + if !reason.Verify(publickey) { + return ErrBidderIncorrectSignature + } + + bidderId := Pub2String(publickey) + + auction, e := m.getAuction(auctionId) + if e != nil { + return e + } + + auction.Lock() + defer auction.Unlock() + + if auction.state != AuctionStateRunning { + return ErrAuctionNotRunning + } + + if _, known := auction.commitments[bidderId]; !known { + return ErrBidderUnknown + } + + auction.state = AuctionStateCanceled + auction.canceled.reason = reason + auction.canceled.by = bidderId + + return nil +} + +func (m *inmemory) Result(auctionId string) (price uint64, winner ed25519.PublicKey, e error) { + return 0, nil, fmt.Errorf("Result not implemented") } \ No newline at end of file -- cgit v1.2.3