Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Jun 30, 2023
1 parent 85bfe88 commit 1a7b4b1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
28 changes: 22 additions & 6 deletions vms/platformvm/dao/camino_change_base_fee_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,39 @@ func (p *ChangeBaseFeeProposal) Visit(visitor Visitor) error {
return visitor.ChangeBaseFeeProposal(p)
}

func (p *ChangeBaseFeeProposal) canBeExecuted(proposalWithVotes *ProposalWithVotes) bool {
_, weight, err := p.Result(proposalWithVotes.Votes)
return err == nil && weight > proposalWithVotes.SuccessThreshold()
}

// Votes must be valid for this proposal, could panic otherwise.
func (p *ChangeBaseFeeProposal) Result(votes []VoteWithAddr) (any, error) { // TODO@ optimize
func (p *ChangeBaseFeeProposal) Result(votes []VoteWithAddr) (uint64, uint32, error) { // TODO@ optimize
if len(votes) == 0 {
return nil, errNoVotes
return 0, 0, errNoVotes
}

optionWeights := make([]int, len(p.FeeOptions))
uncertain := false
optionWeights := make([]uint32, len(p.FeeOptions))
mostVotedOptionIndex := uint32(0)

for i := range votes {
vote, ok := votes[i].Vote.(*SimpleVote)
if !ok {
return nil, errWrongVoteType
return 0, 0, errWrongVoteType
}
optionWeights[vote.OptionIndex]++
if optionWeights[vote.OptionIndex] > optionWeights[mostVotedOptionIndex] {
if vote.OptionIndex != mostVotedOptionIndex &&
optionWeights[vote.OptionIndex] == optionWeights[mostVotedOptionIndex] {
uncertain = true
} else if optionWeights[vote.OptionIndex] > optionWeights[mostVotedOptionIndex] {
mostVotedOptionIndex = vote.OptionIndex
uncertain = false
}
}
return p.FeeOptions[mostVotedOptionIndex], nil

if uncertain {
return 0, 0, errUncertainResult
}

return p.FeeOptions[mostVotedOptionIndex], optionWeights[mostVotedOptionIndex], nil
}
12 changes: 9 additions & 3 deletions vms/platformvm/dao/camino_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MaxProposalSize = 2048 // TODO@
var (
errEndNotAfterStart = errors.New("proposal end-time is not after start-time")
errNoVotes = errors.New("no votes")
errUncertainResult = errors.New("uncertain result")
)

type Visitor interface {
Expand All @@ -29,8 +30,9 @@ type Proposal interface {
StartTime() time.Time
EndTime() time.Time
IsActiveAt(time time.Time) bool
Visit(visitor Visitor) error
Result([]VoteWithAddr) (any, error)
Visit(Visitor) error

canBeExecuted(*ProposalWithVotes) bool
}

type ProposalWithVotes struct {
Expand All @@ -39,6 +41,10 @@ type ProposalWithVotes struct {
Votes []VoteWithAddr `serialize:"true"` // TODO@ optimize
}

func (p *ProposalWithVotes) SuccessThreshold() uint32 {
return uint32(len(p.AllowedVoters) / 2)
}

func (p *ProposalWithVotes) CanBeVotedBy(voterAddr ids.ShortID) bool { // TODO@ optimize
for _, allowedVoterAddress := range p.AllowedVoters {
if allowedVoterAddress == voterAddr {
Expand All @@ -58,7 +64,7 @@ func (p *ProposalWithVotes) WasVotedBy(voterAddr ids.ShortID) bool { // TODO@ op
}

func (p *ProposalWithVotes) CanBeExecuted() bool {
return false // TODO@
return p.Proposal.canBeExecuted(p)
}

// Will return modified proposal with added vote, original proposal will not be modified!
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/txs/executor/camino_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func (e *proposalVerifier) ChangeBaseFeeProposal(*dao.ChangeBaseFeeProposal) err
}

func (e *proposalExecutor) ChangeBaseFeeProposal(proposal *dao.ChangeBaseFeeProposal) error {
result, err := proposal.Result(e.proposalWithVotes.Votes)
newBaseFee, _, err := proposal.Result(e.proposalWithVotes.Votes)
if err != nil {
return err
}
e.state.SetBaseFee(result.(uint64))
e.state.SetBaseFee(newBaseFee)
return nil
}

0 comments on commit 1a7b4b1

Please sign in to comment.