Skip to content

Commit

Permalink
Make fee destination less predictable and avoid using go-specific ran…
Browse files Browse the repository at this point in the history
…domness.
  • Loading branch information
kape1395 committed Sep 20, 2023
1 parent 03abfe3 commit c927b78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/chain/cons/bp/aggregated_batch_proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
// Here we store just an aggregated info.
type AggregatedBatchProposals struct {
shouldBeSkipped bool
batchProposalSet batchProposalSet
decidedIndexProposals map[gpa.NodeID][]int
decidedBaseAliasOutput *isc.AliasOutputWithID
decidedRequestRefs []*isc.RequestRef
aggregatedTime time.Time
validatorFeeTarget isc.AgentID
}

func AggregateBatchProposals(inputs map[gpa.NodeID][]byte, nodeIDs []gpa.NodeID, f int, log *logger.Logger) *AggregatedBatchProposals {
Expand Down Expand Up @@ -51,11 +51,11 @@ func AggregateBatchProposals(inputs map[gpa.NodeID][]byte, nodeIDs []gpa.NodeID,
aggregatedTime := bps.aggregatedTime(f)
decidedBaseAliasOutput := bps.decidedBaseAliasOutput(f)
abp := &AggregatedBatchProposals{
batchProposalSet: bps,
decidedIndexProposals: bps.decidedDSSIndexProposals(),
decidedBaseAliasOutput: decidedBaseAliasOutput,
decidedRequestRefs: bps.decidedRequestRefs(f, decidedBaseAliasOutput),
aggregatedTime: aggregatedTime,
validatorFeeTarget: bps.selectedFeeDestination(aggregatedTime),
}
if abp.decidedBaseAliasOutput == nil || len(abp.decidedRequestRefs) == 0 || abp.aggregatedTime.IsZero() {
log.Debugf(
Expand Down Expand Up @@ -92,11 +92,11 @@ func (abp *AggregatedBatchProposals) AggregatedTime() time.Time {
return abp.aggregatedTime
}

func (abp *AggregatedBatchProposals) ValidatorFeeTarget() isc.AgentID {
func (abp *AggregatedBatchProposals) ValidatorFeeTarget(randomness hashing.HashValue) isc.AgentID {
if abp.shouldBeSkipped {
panic("trying to use aggregated proposal marked to be skipped")
}
return abp.validatorFeeTarget
return abp.batchProposalSet.selectedFeeDestination(abp.aggregatedTime, randomness)
}

func (abp *AggregatedBatchProposals) DecidedRequestRefs() []*isc.RequestRef {
Expand Down
19 changes: 13 additions & 6 deletions packages/chain/cons/bp/batch_proposal_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bp

import (
"bytes"
"encoding/binary"
"sort"
"time"

Expand All @@ -13,7 +14,6 @@ import (
"github.com/iotaledger/wasp/packages/gpa"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/util"
)

type batchProposalSet map[gpa.NodeID]*BatchProposal
Expand Down Expand Up @@ -113,19 +113,26 @@ func (bps batchProposalSet) aggregatedTime(f int) time.Time {
return ts[proposalCount-f-1] // Max(|acsProposals|-F Lowest) ~= 66 percentile.
}

func (bps batchProposalSet) selectedProposal(aggregatedTime time.Time) gpa.NodeID {
func (bps batchProposalSet) selectedProposal(aggregatedTime time.Time, randomness hashing.HashValue) gpa.NodeID {
peers := make([]gpa.NodeID, 0, len(bps))
for nid := range bps {
peers = append(peers, nid)
}
slices.SortFunc(peers, func(a gpa.NodeID, b gpa.NodeID) int {
return bytes.Compare(a[:], b[:])
})
rnd := util.NewPseudoRand(aggregatedTime.UnixNano())
return peers[rnd.Intn(len(bps))]
uint64Bytes := make([]byte, 8)
binary.BigEndian.PutUint64(uint64Bytes, uint64(aggregatedTime.UnixNano()))
hashed := hashing.HashDataBlake2b(
uint64Bytes,
randomness[:],
)
randomUint := binary.BigEndian.Uint64(hashed[:])
randomPos := int(randomUint % uint64(len(bps)))
return peers[randomPos]
}

func (bps batchProposalSet) selectedFeeDestination(aggregatedTime time.Time) isc.AgentID {
bp := bps[bps.selectedProposal(aggregatedTime)]
func (bps batchProposalSet) selectedFeeDestination(aggregatedTime time.Time, randomness hashing.HashValue) isc.AgentID {
bp := bps[bps.selectedProposal(aggregatedTime, randomness)]
return bp.validatorFeeDestination
}
2 changes: 1 addition & 1 deletion packages/chain/cons/cons.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func (c *consImpl) uponVMInputsReceived(aggregatedProposals *bp.AggregatedBatchP
Requests: aggregatedProposals.OrderedRequests(requests, *randomness),
TimeAssumption: aggregatedProposals.AggregatedTime(),
Entropy: *randomness,
ValidatorFeeTarget: aggregatedProposals.ValidatorFeeTarget(),
ValidatorFeeTarget: aggregatedProposals.ValidatorFeeTarget(*randomness),
EstimateGasMode: false,
EnableGasBurnLogging: false,
Log: c.log.Named("VM"),
Expand Down

0 comments on commit c927b78

Please sign in to comment.