Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Aug 31, 2023
1 parent eb47dff commit a555726
Show file tree
Hide file tree
Showing 16 changed files with 861 additions and 1,095 deletions.
1,058 changes: 332 additions & 726 deletions vms/platformvm/camino_vm_test.go

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions vms/platformvm/dac/camino_change_base_fee_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ func (p *BaseFeeProposal) CreateProposalState(allowedVoters []ids.ShortID) Propo
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: make([]SimpleVoteOption[uint64], len(p.Options)),
},
Start: p.Start,
End: p.End,
AllowedVoters: allowedVoters,
EarlySuccessThreshold: uint32(len(allowedVoters) / 2),
FinishThreshold: uint32(len(allowedVoters)),
Start: p.Start,
End: p.End,
AllowedVoters: allowedVoters,
TotalAllowedVoters: uint32(len(allowedVoters)),
}
for i := range p.Options {
stateProposal.Options[i].Value = p.Options[i]
Expand All @@ -77,11 +76,10 @@ func (p *BaseFeeProposal) Visit(visitor VerifierVisitor) error {
type BaseFeeProposalState struct {
SimpleVoteOptions[uint64] `serialize:"true"`

Start uint64 `serialize:"true"`
End uint64 `serialize:"true"`
AllowedVoters []ids.ShortID `serialize:"true"`
EarlySuccessThreshold uint32 `serialize:"true"`
FinishThreshold uint32 `serialize:"true"`
Start uint64 `serialize:"true"`
End uint64 `serialize:"true"`
AllowedVoters []ids.ShortID `serialize:"true"`
TotalAllowedVoters uint32 `serialize:"true"`
}

func (p *BaseFeeProposalState) StartTime() time.Time {
Expand All @@ -99,16 +97,14 @@ func (p *BaseFeeProposalState) IsActiveAt(time time.Time) bool {

func (p *BaseFeeProposalState) CanBeFinished() bool {
mostVotedWeight, _, unambiguous := p.GetMostVoted()
voted := uint32(0)
for i := range p.Options {
voted += p.Options[i].Weight
}
return voted == p.FinishThreshold || (unambiguous && mostVotedWeight > p.EarlySuccessThreshold)
voted := p.Voted()
return voted == p.TotalAllowedVoters || unambiguous && mostVotedWeight > p.TotalAllowedVoters/2
}

func (p *BaseFeeProposalState) IsSuccessful() bool {
_, _, unambiguous := p.GetMostVoted()
return unambiguous
mostVotedWeight, _, unambiguous := p.GetMostVoted()
voted := p.Voted()
return unambiguous && voted > p.TotalAllowedVoters/2 && mostVotedWeight > voted/2
}

func (p *BaseFeeProposalState) Outcome() any {
Expand Down Expand Up @@ -149,7 +145,7 @@ func (p *BaseFeeProposalState) AddVote(voterAddress ids.ShortID, voteIntf Vote)
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: make([]SimpleVoteOption[uint64], len(p.Options)),
},
EarlySuccessThreshold: p.EarlySuccessThreshold,
TotalAllowedVoters: p.TotalAllowedVoters,
}
// we can't use the same slice, cause we need to change its elements
copy(updatedProposal.AllowedVoters, p.AllowedVoters[:voterAddrPos])
Expand Down
97 changes: 54 additions & 43 deletions vms/platformvm/dac/camino_change_base_fee_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func TestBaseFeeProposalCreateProposalState(t *testing.T) {
{Value: 7},
},
},
EarlySuccessThreshold: 2,
FinishThreshold: 4,
TotalAllowedVoters: 4,
},
expectedProposal: &BaseFeeProposal{
Start: 100,
Expand All @@ -62,8 +61,7 @@ func TestBaseFeeProposalCreateProposalState(t *testing.T) {
{Value: 7},
},
},
EarlySuccessThreshold: 2,
FinishThreshold: 5,
TotalAllowedVoters: 5,
},
expectedProposal: &BaseFeeProposal{
Start: 100,
Expand Down Expand Up @@ -96,9 +94,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
}{
"Wrong vote type": {
proposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -113,9 +112,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
voterAddr: voterAddr1,
vote: &DummyVote{}, // not *SimpleVote
expectedOriginalProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -131,9 +131,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
"Wrong vote option index": {
proposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -148,9 +149,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
voterAddr: ids.ShortID{3},
vote: &SimpleVote{OptionIndex: 3},
expectedOriginalProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand Down Expand Up @@ -183,9 +185,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
"OK: adding vote to not voted option": {
proposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -200,9 +203,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
voterAddr: voterAddr1,
vote: &SimpleVote{OptionIndex: 1},
expectedUpdatedProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -212,9 +216,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
},
expectedOriginalProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -229,9 +234,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
"OK: adding vote to already voted option": {
proposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -246,9 +252,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
voterAddr: voterAddr1,
vote: &SimpleVote{OptionIndex: 2},
expectedUpdatedProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -258,9 +265,10 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
},
expectedOriginalProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{
{Value: 10, Weight: 2}, // 0
Expand All @@ -275,27 +283,30 @@ func TestBaseFeeProposalStateAddVote(t *testing.T) {
},
"OK: voter addr in the middle of allowedVoters array": {
proposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr2, voterAddr3},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr2, voterAddr3},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{{Value: 1}},
},
},
voterAddr: voterAddr2,
vote: &SimpleVote{OptionIndex: 0},
expectedUpdatedProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr3},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr3},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{{Value: 1, Weight: 1}},
},
},
expectedOriginalProposal: &BaseFeeProposalState{
Start: 100,
End: 101,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr2, voterAddr3},
Start: 100,
End: 101,
TotalAllowedVoters: 555,
AllowedVoters: []ids.ShortID{voterAddr1, voterAddr2, voterAddr3},
SimpleVoteOptions: SimpleVoteOptions[uint64]{
Options: []SimpleVoteOption[uint64]{{Value: 1}},
},
Expand Down
8 changes: 8 additions & 0 deletions vms/platformvm/dac/camino_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (p SimpleVoteOptions[T]) GetMostVoted() (
return p.mostVotedWeight, p.mostVotedOptionIndex, p.unambiguous
}

func (p SimpleVoteOptions[T]) Voted() uint32 {
voted := uint32(0)
for i := range p.Options {
voted += p.Options[i].Weight
}
return voted
}

var _ Vote = (*DummyVote)(nil)

type DummyVote struct {
Expand Down
30 changes: 13 additions & 17 deletions vms/platformvm/state/camino.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ type caminoDiff struct {
modifiedShortLinks map[ids.ID]*ids.ShortID
modifiedClaimables map[ids.ID]*Claimable
modifiedProposals map[ids.ID]*proposalDiff
addedProposalIDsToFinish []ids.ID
removedProposalIDsToFinish map[ids.ID]struct{}
modifiedProposalIDsToFinish map[ids.ID]bool
modifiedNotDistributedValidatorReward *uint64
modifiedBaseFee *uint64
}
Expand Down Expand Up @@ -231,19 +230,18 @@ type caminoState struct {
proposalsDB database.Database
proposalIDsByEndtimeDB database.Database
proposalIDsToFinishDB database.Database
proposalIDsToFinishList linkeddb.LinkedDB // it is important to have them in order they were added
}

func newCaminoDiff() *caminoDiff {
return &caminoDiff{
modifiedAddressStates: make(map[ids.ShortID]txs.AddressState),
modifiedDepositOffers: make(map[ids.ID]*deposit.Offer),
modifiedDeposits: make(map[ids.ID]*depositDiff),
modifiedMultisigAliases: make(map[ids.ShortID]*multisig.AliasWithNonce),
modifiedShortLinks: make(map[ids.ID]*ids.ShortID),
modifiedClaimables: make(map[ids.ID]*Claimable),
modifiedProposals: make(map[ids.ID]*proposalDiff),
removedProposalIDsToFinish: make(map[ids.ID]struct{}),
modifiedAddressStates: make(map[ids.ShortID]txs.AddressState),
modifiedDepositOffers: make(map[ids.ID]*deposit.Offer),
modifiedDeposits: make(map[ids.ID]*depositDiff),
modifiedMultisigAliases: make(map[ids.ShortID]*multisig.AliasWithNonce),
modifiedShortLinks: make(map[ids.ID]*ids.ShortID),
modifiedClaimables: make(map[ids.ID]*Claimable),
modifiedProposals: make(map[ids.ID]*proposalDiff),
modifiedProposalIDsToFinish: make(map[ids.ID]bool),
}
}

Expand Down Expand Up @@ -303,7 +301,6 @@ func newCaminoState(baseDB, validatorsDB database.Database, metricsReg prometheu
}

deferredValidatorsDB := prefixdb.New(deferredPrefix, validatorsDB)
proposalIDsToFinishDB := prefixdb.New(proposalIDsToFinishPrefix, baseDB)

return &caminoState{
// Address State
Expand Down Expand Up @@ -336,11 +333,10 @@ func newCaminoState(baseDB, validatorsDB database.Database, metricsReg prometheu
deferredValidatorsDB: deferredValidatorsDB,
deferredValidatorList: linkeddb.NewDefault(deferredValidatorsDB),

proposalsCache: proposalsCache,
proposalsDB: prefixdb.New(proposalsPrefix, baseDB),
proposalIDsByEndtimeDB: prefixdb.New(proposalIDsByEndtimePrefix, baseDB),
proposalIDsToFinishDB: proposalIDsToFinishDB,
proposalIDsToFinishList: linkeddb.NewDefault(proposalIDsToFinishDB),
proposalsCache: proposalsCache,
proposalsDB: prefixdb.New(proposalsPrefix, baseDB),
proposalIDsByEndtimeDB: prefixdb.New(proposalIDsByEndtimePrefix, baseDB),
proposalIDsToFinishDB: prefixdb.New(proposalIDsToFinishPrefix, baseDB),

caminoDB: prefixdb.New(caminoPrefix, baseDB),
caminoDiff: newCaminoDiff(),
Expand Down
Loading

0 comments on commit a555726

Please sign in to comment.