forked from perlin-network/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.go
278 lines (208 loc) · 7.97 KB
/
collapse.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/log"
"github.com/perlin-network/wavelet/sys"
queue2 "github.com/phf/go-queue/queue"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
)
func processRewardWithdrawals(round uint64, snapshot *avl.Tree) {
rws := GetRewardWithdrawalRequests(snapshot, round-uint64(sys.RewardWithdrawalsRoundLimit))
for _, rw := range rws {
balance, _ := ReadAccountBalance(snapshot, rw.account)
WriteAccountBalance(snapshot, rw.account, balance+rw.amount)
snapshot.Delete(rw.Key())
}
}
// rewardValidators deducts a transaction fee from a transactions creator, and transfers the fee
// to a rewardee which is determined by a validator reward scheme given the selected ancestry
// of the transaction.
//
// If no rewardee is selected, then the transaction fee is simply burned. A reference to
// a transaction is expected when calling this function to prevent any additional requirements
// of looking up said transaction within the graph.
func rewardValidators(g *Graph, snapshot *avl.Tree, tx *Transaction, logging bool) error {
fee := sys.TransactionFeeAmount
creatorBalance, _ := ReadAccountBalance(snapshot, tx.Creator)
if creatorBalance < fee {
return errors.Errorf("stake: creator %x does not have enough PERLs to pay transaction fees (comprised of %d PERLs)", tx.Creator, fee)
}
WriteAccountBalance(snapshot, tx.Creator, creatorBalance-fee)
var candidates []*Transaction
var stakes []uint64
var totalStake uint64
visited := make(map[TransactionID]struct{})
queue := AcquireQueue()
defer ReleaseQueue(queue)
for _, parentID := range tx.ParentIDs {
if parent := g.FindTransaction(parentID); parent != nil {
queue.PushBack(parent)
}
visited[parentID] = struct{}{}
}
hasher, _ := blake2b.New256(nil)
var depthCounter uint64
var lastDepth = tx.Depth
for queue.Len() > 0 {
popped := queue.PopFront().(*Transaction)
if popped.Depth != lastDepth {
lastDepth = popped.Depth
depthCounter++
}
// If we exceed the max eligible depth we search for candidate
// validators to reward from, stop traversing.
if depthCounter >= sys.MaxDepthDiff {
break
}
// Filter for all ancestral transactions not from the same sender,
// and within the desired graph depth.
if popped.Sender != tx.Sender {
stake, _ := ReadAccountStake(snapshot, popped.Sender)
if stake > sys.MinimumStake {
candidates = append(candidates, popped)
stakes = append(stakes, stake)
totalStake += stake
// Record entropy source.
if _, err := hasher.Write(popped.ID[:]); err != nil {
return errors.Wrap(err, "stake: failed to hash transaction ID for entropy source")
}
}
}
for _, parentID := range popped.ParentIDs {
if _, seen := visited[parentID]; !seen {
if parent := g.FindTransaction(parentID); parent != nil {
queue.PushBack(parent)
}
visited[parentID] = struct{}{}
}
}
}
// If there are no eligible rewardee candidates, do not reward anyone.
if len(candidates) == 0 || len(stakes) == 0 || totalStake == 0 {
return nil
}
entropy := hasher.Sum(nil)
acc, threshold := float64(0), float64(binary.LittleEndian.Uint64(entropy)%uint64(0xffff))/float64(0xffff)
var rewardee *Transaction
// Model a weighted uniform distribution by a random variable X, and select
// whichever validator has a weight X ≥ X' as a reward recipient.
for i, tx := range candidates {
acc += float64(stakes[i]) / float64(totalStake)
if acc >= threshold {
rewardee = tx
break
}
}
// If there is no selected transaction that deserves a reward, give the
// reward to the last reward candidate.
if rewardee == nil {
rewardee = candidates[len(candidates)-1]
}
rewardeeBalance, _ := ReadAccountReward(snapshot, rewardee.Sender)
WriteAccountReward(snapshot, rewardee.Sender, rewardeeBalance+fee)
if logging {
logger := log.Stake("reward_validator")
logger.Info().
Hex("creator", tx.Creator[:]).
Hex("recipient", rewardee.Sender[:]).
Hex("creator_tx_id", tx.ID[:]).
Hex("rewardee_tx_id", rewardee.ID[:]).
Hex("entropy", entropy).
Float64("acc", acc).
Float64("threshold", threshold).Msg("Rewarded validator.")
}
return nil
}
func collapseTransactions(g *Graph, accounts *Accounts, round uint64, current *Round, start, end Transaction, logging bool) (*collapseResults, error) {
res := &collapseResults{snapshot: accounts.Snapshot()}
res.snapshot.SetViewID(round)
visited := map[TransactionID]struct{}{start.ID: {}}
queue := queue2.New()
queue.PushBack(&end)
order := queue2.New()
for queue.Len() > 0 {
popped := queue.PopFront().(*Transaction)
if popped.Depth <= start.Depth {
continue
}
order.PushBack(popped)
for _, parentID := range popped.ParentIDs {
if _, seen := visited[parentID]; seen {
continue
}
visited[parentID] = struct{}{}
parent := g.FindTransaction(parentID)
if parent == nil {
g.MarkTransactionAsMissing(parentID, popped.Depth)
return nil, errors.Errorf("missing ancestor %x to correctly collapse down ledger state from critical transaction %x", parentID, end.ID)
}
queue.PushBack(parent)
}
}
res.applied = make([]*Transaction, 0, order.Len())
res.rejected = make([]*Transaction, 0, order.Len())
res.rejectedErrors = make([]error, 0, order.Len())
ctx := NewCollapseContext()
// Apply transactions in reverse order from the end of the round
// all the way down to the beginning of the round.
for order.Len() > 0 {
popped := order.PopBack().(*Transaction)
// Update nonce.
nonce, exists := ReadAccountNonce(res.snapshot, popped.Creator)
if !exists {
WriteAccountsLen(res.snapshot, ReadAccountsLen(res.snapshot)+1)
}
WriteAccountNonce(res.snapshot, popped.Creator, nonce+1)
// FIXME(kenta): FOR TESTNET ONLY. FAUCET DOES NOT GET ANY PERLs DEDUCTED.
if hex.EncodeToString(popped.Creator[:]) != sys.FaucetAddress {
if err := rewardValidators(g, res.snapshot, popped, logging); err != nil {
res.rejected = append(res.rejected, popped)
res.rejectedErrors = append(res.rejectedErrors, err)
res.rejectedCount += popped.LogicalUnits()
continue
}
}
if err := ApplyTransaction(current, res.snapshot, popped, ctx); err != nil {
res.rejected = append(res.rejected, popped)
res.rejectedErrors = append(res.rejectedErrors, err)
res.rejectedCount += popped.LogicalUnits()
fmt.Println("error applying transaction", err)
continue
}
// Update statistics.
res.applied = append(res.applied, popped)
res.appliedCount += popped.LogicalUnits()
}
startDepth, endDepth := start.Depth+1, end.Depth
for _, tx := range g.GetTransactionsByDepth(&startDepth, &endDepth) {
res.ignoredCount += tx.LogicalUnits()
}
res.ignoredCount -= res.appliedCount + res.rejectedCount
if round >= uint64(sys.RewardWithdrawalsRoundLimit) {
processRewardWithdrawals(round, res.snapshot)
}
ctx.Flush(res.snapshot)
return res, nil
}