-
Notifications
You must be signed in to change notification settings - Fork 491
/
Copy pathselector.go
98 lines (83 loc) · 3.45 KB
/
selector.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
// Copyright (C) 2019-2025 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package agreement
import (
"fmt"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/committee"
"github.com/algorand/go-algorand/protocol"
)
// A Selector is the input used to define proposers and members of voting
// committees.
type selector struct {
_struct struct{} `codec:""` // not omitempty
Seed committee.Seed `codec:"seed"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
}
// ToBeHashed implements the crypto.Hashable interface.
func (sel selector) ToBeHashed() (protocol.HashID, []byte) {
return protocol.AgreementSelector, protocol.Encode(&sel)
}
// CommitteeSize returns the size of the committee, which is determined by
// Selector.Step.
func (sel selector) CommitteeSize(proto config.ConsensusParams) uint64 {
return sel.Step.committeeSize(proto)
}
// BalanceRound returns the round that should be considered by agreement when
// looking at online stake (and status and key material). It is exported so that
// AVM can provide opcodes that return the same data.
func BalanceRound(r basics.Round, cparams config.ConsensusParams) basics.Round {
return r.SubSaturate(BalanceLookback(cparams))
}
// BalanceLookback is how far back agreement looks when considering balances for
// voting stake.
func BalanceLookback(cparams config.ConsensusParams) basics.Round {
return basics.Round(2 * cparams.SeedRefreshInterval * cparams.SeedLookback)
}
func seedRound(r basics.Round, cparams config.ConsensusParams) basics.Round {
return r.SubSaturate(basics.Round(cparams.SeedLookback))
}
// a helper function for obtaining membership verification parameters.
func membership(l LedgerReader, addr basics.Address, r basics.Round, p period, s step) (m committee.Membership, err error) {
cparams, err := l.ConsensusParams(ParamsRound(r))
if err != nil {
return
}
balanceRound := BalanceRound(r, cparams)
seedRound := seedRound(r, cparams)
record, err := l.LookupAgreement(balanceRound, addr)
if err != nil {
err = fmt.Errorf("Service.initializeVote (r=%d): Failed to obtain balance record for address %v in round %d: %w", r, addr, balanceRound, err)
return
}
total, err := l.Circulation(balanceRound, r)
if err != nil {
err = fmt.Errorf("Service.initializeVote (r=%d): Failed to obtain total circulation in round %d: %v", r, balanceRound, err)
return
}
seed, err := l.Seed(seedRound)
if err != nil {
err = fmt.Errorf("Service.initializeVote (r=%d): Failed to obtain seed in round %d: %v", r, seedRound, err)
return
}
m.Record = committee.BalanceRecord{OnlineAccountData: record, Addr: addr}
m.Selector = selector{Seed: seed, Round: r, Period: p, Step: s}
m.TotalMoney = total
return m, nil
}