Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change seed from int64 to uint64 #2438

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
38 changes: 21 additions & 17 deletions snow/consensus/snowball/consensus_performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import (
func TestDualAlphaOptimization(t *testing.T) {
require := require.New(t)

numColors := 10
numNodes := 100
params := Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 15,
BetaRogue: 20,
}
seed := int64(0)
var (
numColors = 10
numNodes = 100
params = Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 15,
BetaRogue: 20,
}
seed uint64 = 0
)

singleAlphaNetwork := Network{}
singleAlphaNetwork.Initialize(params, numColors)
Expand Down Expand Up @@ -54,10 +56,12 @@ func TestDualAlphaOptimization(t *testing.T) {
func TestTreeConvergenceOptimization(t *testing.T) {
require := require.New(t)

numColors := 10
numNodes := 100
params := DefaultParameters
seed := int64(0)
var (
numColors = 10
numNodes = 100
params = DefaultParameters
seed uint64 = 0
)

treeNetwork := Network{}
treeNetwork.Initialize(params, numColors)
Expand All @@ -79,13 +83,13 @@ func TestTreeConvergenceOptimization(t *testing.T) {
runNetworksInLockstep(require, seed, &treeNetwork, &flatNetwork)
}

func runNetworksInLockstep(require *require.Assertions, seed int64, fast *Network, slow *Network) {
func runNetworksInLockstep(require *require.Assertions, seed uint64, fast *Network, slow *Network) {
numRounds := 0
for !fast.Finalized() && !fast.Disagreement() && !slow.Finalized() && !slow.Disagreement() {
sampler.Seed(int64(numRounds) + seed)
sampler.Seed(uint64(numRounds) + seed)
fast.Round()

sampler.Seed(int64(numRounds) + seed)
sampler.Seed(uint64(numRounds) + seed)
slow.Round()
numRounds++
}
Expand Down
14 changes: 8 additions & 6 deletions snow/consensus/snowball/consensus_reversibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
func TestSnowballGovernance(t *testing.T) {
require := require.New(t)

numColors := 2
numNodes := 100
numByzantine := 10
numRed := 55
params := DefaultParameters
seed := int64(0)
var (
numColors = 2
numNodes = 100
numByzantine = 10
numRed = 55
params = DefaultParameters
seed uint64 = 0
)

nBitwise := Network{}
nBitwise.Initialize(params, numColors)
Expand Down
22 changes: 12 additions & 10 deletions snow/consensus/snowball/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,16 +796,18 @@ func TestSnowballDoubleAdd(t *testing.T) {
func TestSnowballConsistent(t *testing.T) {
require := require.New(t)

numColors := 50
numNodes := 100
params := Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 20,
BetaRogue: 30,
}
seed := int64(0)
var (
numColors = 50
numNodes = 100
params = Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 20,
BetaRogue: 30,
}
seed uint64 = 0
)

sampler.Seed(seed)

Expand Down
30 changes: 16 additions & 14 deletions snow/consensus/snowman/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1585,20 +1585,22 @@ func ErrorOnTransitiveRejectionTest(t *testing.T, factory Factory) {
func RandomizedConsistencyTest(t *testing.T, factory Factory) {
require := require.New(t)

numColors := 50
numNodes := 100
params := snowball.Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 20,
BetaRogue: 30,
ConcurrentRepolls: 1,
OptimalProcessing: 1,
MaxOutstandingItems: 1,
MaxItemProcessingTime: 1,
}
seed := int64(0)
var (
numColors = 50
numNodes = 100
params = snowball.Parameters{
K: 20,
AlphaPreference: 15,
AlphaConfidence: 15,
BetaVirtuous: 20,
BetaRogue: 30,
ConcurrentRepolls: 1,
OptimalProcessing: 1,
MaxOutstandingItems: 1,
MaxItemProcessingTime: 1,
}
seed uint64 = 0
)

sampler.Seed(seed)

Expand Down
6 changes: 3 additions & 3 deletions utils/sampler/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newRNG() *rng {
return &rng{rng: source}
}

func Seed(seed int64) {
func Seed(seed uint64) {
globalRNG.Seed(seed)
}

Expand All @@ -37,9 +37,9 @@ type rng struct {

// Seed uses the provided seed value to initialize the generator to a
// deterministic state.
func (r *rng) Seed(seed int64) {
func (r *rng) Seed(seed uint64) {
r.lock.Lock()
r.rng.Seed(uint64(seed))
r.rng.Seed(seed)
Comment on lines +40 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really shouldn't have been casting this before... There wasn't a good reason to take in an int64.

r.lock.Unlock()
}

Expand Down
2 changes: 1 addition & 1 deletion utils/sampler/uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Uniform interface {
// negative the implementation may panic.
Sample(length int) ([]uint64, error)

Seed(int64)
Seed(uint64)
ClearSeed()

Reset()
Expand Down
2 changes: 1 addition & 1 deletion utils/sampler/uniform_replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *uniformReplacer) Sample(count int) ([]uint64, error) {
return results, nil
}

func (s *uniformReplacer) Seed(seed int64) {
func (s *uniformReplacer) Seed(seed uint64) {
s.rng = s.seededRNG
s.rng.Seed(seed)
}
Expand Down
2 changes: 1 addition & 1 deletion utils/sampler/uniform_resample.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *uniformResample) Sample(count int) ([]uint64, error) {
return results, nil
}

func (s *uniformResample) Seed(seed int64) {
func (s *uniformResample) Seed(seed uint64) {
s.rng = s.seededRNG
s.rng.Seed(seed)
}
Expand Down
2 changes: 1 addition & 1 deletion utils/sampler/weighted_without_replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type WeightedWithoutReplacement interface {
Initialize(weights []uint64) error
Sample(count int) ([]int, error)

Seed(int64)
Seed(uint64)
ClearSeed()
}

Expand Down
2 changes: 1 addition & 1 deletion utils/sampler/weighted_without_replacement_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *weightedWithoutReplacementGeneric) Sample(count int) ([]int, error) {
return indices, nil
}

func (s *weightedWithoutReplacementGeneric) Seed(seed int64) {
func (s *weightedWithoutReplacementGeneric) Seed(seed uint64) {
s.u.Seed(seed)
}

Expand Down
2 changes: 1 addition & 1 deletion vms/proposervm/proposer/windower.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (w *windower) Proposers(ctx context.Context, chainHeight, pChainHeight uint
}

seed := chainHeight ^ w.chainSource
w.sampler.Seed(int64(seed))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

w.sampler.Seed(seed)

indices, err := w.sampler.Sample(numToSample)
if err != nil {
Expand Down
Loading