1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package dashboard
import (
"crypto/ed25519"
"fmt"
"sync"
"kesim.org/seal"
)
type inmemory struct {
sync.RWMutex
auctions map[string]*auction
}
func NewInMemoryDashboard() *inmemory {
return &inmemory{}
}
type auction struct {
sync.RWMutex
description *seal.SignedDescription
state AuctionState
round uint8 // which bit has been completed
commitments map[string]SignedCommitment
messages map[string][]SignedMessage // per-bidder and per-round messages, pre-allocated.
}
func (m *inmemory) NewAuction(description *seal.SignedDescription) (auctionId string, e error) {
if ok, e := description.Verify(); e != nil {
return "", e
} else if !ok {
return "", ErrSellerIncorrectSignature
}
auctionId, e = description.Hash()
if e != nil {
return "", e
}
auction := &auction{
description: description,
state: AuctionStateReady,
messages: make(map[string][]SignedMessage),
}
m.Lock()
defer m.Unlock()
if _, exists := m.auctions[auctionId]; exists {
return "", ErrSellerAuctionExists
}
m.auctions[auctionId] = auction
return auctionId, e
}
func (m *inmemory) Auctions(state AuctionState) map[string]*seal.SignedDescription {
if state == AuctionStateUnknown || state >= AuctionStateLAST {
return nil
}
m.RLock()
defer m.RUnlock()
r := make(map[string]*seal.SignedDescription)
for id, auction := range m.auctions {
if auction.state == state {
r[id] = auction.description
}
}
return r
}
func (m *inmemory) getAuction(auctionId string) (*auction, error) {
m.RLock()
defer m.RUnlock()
auction, exists := m.auctions[auctionId]
if !exists {
return nil, ErrAuctionUnknown
}
return auction, nil
}
func (m *inmemory) Join(auctionId string,
commitment SignedCommitment,
pubkey ed25519.PublicKey) error {
// Check signature first
if !commitment.Verify(pubkey) {
return ErrBidderIncorrectSignature
}
bidderId := Pub2String(pubkey)
auction, e := m.getAuction(auctionId)
if e != nil {
return e
}
// We allow overwriting
auction.Lock()
defer auction.Unlock()
if auction.state != AuctionStateReady {
return ErrAuctionNotReady
}
auction.commitments[bidderId] = commitment
auction.messages[bidderId] = make([]SignedMessage, auction.description.BitLength)
return nil
}
func (m *inmemory) Commitments(auctionId string) (bidders map[string]SignedCommitment, e error) {
auction, e := m.getAuction(auctionId)
if e != nil {
return nil, e
}
auction.RLock()
defer auction.RUnlock()
bidders = make(map[string]SignedCommitment)
for id, commitment := range auction.commitments {
bidders[id] = commitment
}
return bidders, nil
}
func (m *inmemory) Publish(auctionId string, round uint8, message SignedMessage, pubkey ed25519.PublicKey) error {
if len(message.Message) == 0 {
return ErrBidderEmptyMessage
}
// check signature first
if !message.Verify(pubkey) {
return ErrBidderIncorrectSignature
}
bidderId := Pub2String(pubkey)
auction, e := m.getAuction(auctionId)
if e != nil {
return e
}
auction.Lock()
defer auction.Unlock()
if auction.state == AuctionStateFinished {
return ErrAuctionFinished
} else if auction.state != AuctionStateRunning {
return ErrAuctionNotRunning
} else if auction.round != round {
return ErrBidderWrongRound
}
messages, ok := auction.messages[bidderId]
if !ok {
return ErrBidderUnknown
}
messages[auction.round] = message
completed := true
for _, messages := range auction.messages {
completed = completed && len(messages[auction.round].Message) > 0
}
if completed {
auction.round += 1
if auction.round == auction.description.BitLength {
auction.state = AuctionStateFinished
}
}
return nil
}
func (m *inmemory) Messages(auctionId string, round uint8) (messages map[string]SignedMessage, e error) {
return nil, fmt.Errorf("inmemory.Messages not implemented")
}
|