Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/ava-labs/coreth v0.11.0-rc.4 => /Users/tewodrosmitiku/Desktop/walruseth
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/ava-labs/avalanche-network-runner-sdk v0.2.0 h1:YNvM0oFlb7A825kGe0XwwZuvIXTKF1BsuvxJdRLhIaI=
github.com/ava-labs/avalanche-network-runner-sdk v0.2.0/go.mod h1:bEBRVZnGeRiNdDJAFUj+gA/TPzNDbpY/WzgDAHHwJb8=
github.com/ava-labs/coreth v0.11.0-rc.4 h1:oYZMWZcXYa4dH2hQBIAH/DD0rL2cB3btPGdabpCH5Ug=
github.com/ava-labs/coreth v0.11.0-rc.4/go.mod h1:IhfO9oA8KicFyYZA3nIqjV/TS6xzAqT5ml2QKfNGtGA=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
3 changes: 3 additions & 0 deletions snow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/vms/proposervm/proposer"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
)
Expand Down Expand Up @@ -55,6 +56,8 @@ type Context struct {
ValidatorState validators.State // interface for P-Chain validators
StakingLeafSigner crypto.Signer // block signer
StakingCertLeaf *x509.Certificate // block certificate

ProposerRetriever proposer.Retriever
}

// Expose gatherer interface for unit testing.
Expand Down
4 changes: 4 additions & 0 deletions vms/proposervm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (p *postForkCommonComponents) Verify(parentTimestamp time.Time, parentPChai

childHeight := child.Height()
proposerID := child.Proposer()
p.vm.Retriever.SetChainHeight(childHeight)
p.vm.Retriever.SetPChainHeight(parentPChainHeight)
minDelay, err := p.vm.Windower.Delay(childHeight, parentPChainHeight, proposerID)
if err != nil {
return err
Expand Down Expand Up @@ -182,6 +184,8 @@ func (p *postForkCommonComponents) buildChild(
if delay < proposer.MaxDelay {
parentHeight := p.innerBlk.Height()
proposerID := p.vm.ctx.NodeID
p.vm.Retriever.SetChainHeight(parentHeight+1)
p.vm.Retriever.SetPChainHeight(parentPChainHeight)
minDelay, err := p.vm.Windower.Delay(parentHeight+1, parentPChainHeight, proposerID)
if err != nil {
return nil, err
Expand Down
112 changes: 112 additions & 0 deletions vms/proposervm/proposer/retriever.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package proposer

import (
"sort"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

const (
NumProposers = 2
)

type Retriever interface {
GetCurrentProposers() (ids.NodeIDSet, error)
SetChainHeight(uint64)
SetPChainHeight(uint64)
}

type retriever struct {
state validators.State
subnetID ids.ID
chainSource uint64
sampler sampler.WeightedWithoutReplacement

// Updated by the VM
pChainHeight uint64
chainHeight uint64
}

func NewRetriever(state validators.State, subnetID ids.ID, chainID ids.ID) Retriever {
w := wrappers.Packer{Bytes: chainID[:]}
return &retriever{
state: state,
subnetID: subnetID,
chainSource: w.UnpackLong(),
sampler: sampler.NewDeterministicWeightedWithoutReplacement(),
pChainHeight: 0,
chainHeight: 0,
}
}

func (r *retriever) SetPChainHeight(pChainHeight uint64){
r.pChainHeight = pChainHeight
}

func (r *retriever) SetChainHeight(chainHeight uint64){
r.chainHeight = chainHeight
}

func (r *retriever) GetCurrentProposers() (ids.NodeIDSet, error) {
proposers := ids.NewNodeIDSet(NumProposers)

// get the validator set by the p-chain height
validatorsMap, err := r.state.GetValidatorSet(r.pChainHeight, r.subnetID)
if err != nil {
return proposers, err
}

// convert the map of validators to a slice
validators := make(validatorsSlice, 0, len(validatorsMap))
weight := uint64(0)
for k, v := range validatorsMap {
validators = append(validators, validatorData{
id: k,
weight: v,
})
newWeight, err := math.Add64(weight, v)
if err != nil {
return proposers, err
}
weight = newWeight
}

// canonically sort validators
// Note: validators are sorted by ID, sorting by weight would not create a
// canonically sorted list
sort.Sort(validators)

// convert the slice of validators to a slice of weights
validatorWeights := make([]uint64, len(validators))
for i, v := range validators {
validatorWeights[i] = v.weight
}

if err := r.sampler.Initialize(validatorWeights); err != nil {
return proposers, err
}

numToSample := NumProposers
if weight < uint64(numToSample) {
numToSample = int(weight)
}

seed := r.chainHeight ^ r.chainSource
r.sampler.Seed(int64(seed))

indices, err := r.sampler.Sample(numToSample)
if err != nil {
return proposers, err
}

for _, index := range indices {
proposerNodeID := validators[index].id
proposers.Add(proposerNodeID)
}

return proposers, nil
}
33 changes: 33 additions & 0 deletions vms/proposervm/proposer/retriever_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package proposer


import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
)

func TestRetrieverNoValidators(t *testing.T) {
require := require.New(t)

subnetID := ids.GenerateTestID()
chainID := ids.GenerateTestID()
nodeID := ids.GenerateTestNodeID()
vdrState := &validators.TestState{
T: t,
GetValidatorSetF: func(height uint64, subnetID ids.ID) (map[ids.NodeID]uint64, error) {
return nil, nil
},
}

w := New(vdrState, subnetID, chainID)

delay, err := w.Delay(1, 0, nodeID)
require.NoError(err)
require.EqualValues(0, delay)
}
9 changes: 9 additions & 0 deletions vms/proposervm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type VM struct {
hIndexer indexer.HeightIndexer
resetHeightIndexOngoing utils.AtomicBool

proposer.Retriever
proposer.Windower
tree.Tree
scheduler.Scheduler
Expand Down Expand Up @@ -160,6 +161,12 @@ func (vm *VM) Initialize(
prefixDB := prefixdb.New(dbPrefix, rawDB)
vm.db = versiondb.New(prefixDB)
vm.State = state.New(vm.db)

// Initialize the proposer retriever for VM and context
proposerRetriever := proposer.NewRetriever(ctx.ValidatorState, ctx.SubnetID, ctx.ChainID)
vm.Retriever = proposerRetriever
vm.ctx.ProposerRetriever = proposerRetriever

vm.Windower = proposer.New(ctx.ValidatorState, ctx.SubnetID, ctx.ChainID)
vm.Tree = tree.New()
innerBlkCache, err := metercacher.New(
Expand Down Expand Up @@ -283,6 +290,8 @@ func (vm *VM) SetPreference(preferred ids.ID) error {
}

// reset scheduler
vm.Retriever.SetChainHeight(blk.Height() + 1)
vm.Retriever.SetPChainHeight(pChainHeight)
minDelay, err := vm.Windower.Delay(blk.Height()+1, pChainHeight, vm.ctx.NodeID)
if err != nil {
vm.ctx.Log.Debug("failed to fetch the expected delay",
Expand Down