diff options
Diffstat (limited to 'auction.go')
-rw-r--r-- | auction.go | 130 |
1 files changed, 129 insertions, 1 deletions
@@ -6,7 +6,13 @@ import ( "crypto/sha512" "encoding/base32" "encoding/json" + "errors" + "fmt" + "log/slog" "time" + + "kesim.org/seal/nizk/commit" + "kesim.org/seal/nizk/stage1" ) type Type int @@ -55,4 +61,126 @@ func (d *Description) Hash() (hash string, e error) { } h := sha512.Sum512(buf.Bytes()) return base32.StdEncoding.EncodeToString(h[:]), nil -}
\ No newline at end of file +} + +func (d *Description) validate() error { + if d == nil { + return fmt.Errorf("description is nil") + } + if d.BitLength > MAXBITLENGTH { + return fmt.Errorf("invalid BitLength in description: %d", d.BitLength) + } + return nil +} + +type Auction interface { + Start() error + Cancel() error + Message(msg []byte) error +} + +type Result struct { + Price uint32 + Winner string + Error error +} + +type auction struct { + description *Description + + // non nil if run by an bidder, via Join() + bidder Bidder + // non nil if run by an observer, via Observe() + observer Observer + + // The commitments we received from the bidders. + bidders map[string][]*commit.Commitment + + // sorted list of the bidders. + bidder_ids []string + + // Stage 1 data per round + stage1 []*stage1.Statement + + log *slog.Logger +} + +func (a *auction) Start() error { + return errors.New("Start not implemented") +} + +func (a *auction) Cancel() error { + return errors.New("Cancel not implemented") +} + +// Message is called by the Bidder or Visitor +// whenever a message came in for the auction via the dashboard +// or other means of communication. +func (a *auction) Message(msg []byte) error { + return fmt.Errorf("Auction.Received not implemented") +} + +const MAXBITLENGTH = 64 + +// Bidder is the interface that the Auction engine uses to interact +type Bidder interface { + Broadcast([]byte) error + Result(Result) + Start() +} + +func Join( + bidder Bidder, + description *Description, + options ...Option) (Auction, error) { + + if bidder == nil { + return nil, fmt.Errorf("missing bidder") + } + if e := description.validate(); e != nil { + return nil, e + } + + a := &auction{ + description: description, + bidder: bidder, + log: slog.Default(), + } + + for _, opt := range options { + opt(a) + } + + return a, nil +} + +// Observer +type Observer interface { + Result(Result) + Start() +} + +func Observe( + observer Observer, + description *Description, + options ...Option) (Auction, error) { + + if observer == nil { + return nil, fmt.Errorf("missing observer") + } + if e := description.validate(); e != nil { + return nil, e + } + + a := &auction{ + description: description, + observer: observer, + log: slog.Default(), + } + + for _, opt := range options { + opt(a) + } + + return a, nil +} |