@@ -2,25 +2,24 @@ package utils
22
33import (
44 "crypto/sha256"
5- "github.com/Fantom-foundation/go-lachesis/inter/pos"
65 "testing"
76
8- "github.com/ethereum/go-ethereum/common"
7+ "github.com/Fantom-foundation/lachesis-base/common/littleendian"
8+ "github.com/Fantom-foundation/lachesis-base/hash"
9+ "github.com/Fantom-foundation/lachesis-base/inter/pos"
910 "github.com/stretchr/testify/assert"
10-
11- "github.com/Fantom-foundation/go-lachesis/common/littleendian"
1211)
1312
14- func getTestWeightsIncreasing (num int ) []pos.Stake {
15- weights := make ([]pos.Stake , num )
13+ func getTestWeightsIncreasing (num int ) []pos.Weight {
14+ weights := make ([]pos.Weight , num )
1615 for i := 0 ; i < num ; i ++ {
17- weights [i ] = pos .Stake (i + 1 ) * 1000
16+ weights [i ] = pos .Weight (i + 1 ) * 1000
1817 }
1918 return weights
2019}
2120
22- func getTestWeightsEqual (num int ) []pos.Stake {
23- weights := make ([]pos.Stake , num )
21+ func getTestWeightsEqual (num int ) []pos.Weight {
22+ weights := make ([]pos.Weight , num )
2423 for i := 0 ; i < num ; i ++ {
2524 weights [i ] = 1000
2625 }
@@ -33,7 +32,7 @@ func Test_Permutation_distribution(t *testing.T) {
3332
3433 weightHits := make (map [int ]int ) // weight -> number of occurrences
3534 for roundSeed := 0 ; roundSeed < 3000 ; roundSeed ++ {
36- seed := hashOf (common .Hash {}, uint32 (roundSeed ))
35+ seed := hashOf (hash .Hash {}, uint32 (roundSeed ))
3736 perm := WeightedPermutation (len (weightsArr )/ 10 , weightsArr , seed )
3837 for _ , p := range perm {
3938 weight := weightsArr [p ]
@@ -59,10 +58,10 @@ func Test_Permutation_distribution(t *testing.T) {
5958}
6059
6160// test that WeightedPermutation provides a correct permaition
62- func testCorrectPermutation (t * testing.T , weightsArr []pos.Stake ) {
61+ func testCorrectPermutation (t * testing.T , weightsArr []pos.Weight ) {
6362 assertar := assert .New (t )
6463
65- perm := WeightedPermutation (len (weightsArr ), weightsArr , common .Hash {})
64+ perm := WeightedPermutation (len (weightsArr ), weightsArr , hash .Hash {})
6665 assertar .Equal (len (weightsArr ), len (perm ))
6766
6867 met := make (map [int ]bool )
@@ -80,21 +79,21 @@ func Test_Permutation_correctness(t *testing.T) {
8079 testCorrectPermutation (t , getTestWeightsEqual (1000 ))
8180}
8281
83- func hashOf (a common .Hash , b uint32 ) common .Hash {
82+ func hashOf (a hash .Hash , b uint32 ) hash .Hash {
8483 hasher := sha256 .New ()
8584 hasher .Write (a .Bytes ())
86- hasher .Write (littleendian .Int32ToBytes (uint32 (b )))
87- return common . BytesToHash (hasher .Sum (nil ))
85+ hasher .Write (littleendian .Uint32ToBytes (uint32 (b )))
86+ return hash . FromBytes (hasher .Sum (nil ))
8887}
8988
9089func Test_Permutation_determinism (t * testing.T ) {
9190 weightsArr := getTestWeightsIncreasing (5 )
9291
9392 assertar := assert .New (t )
9493
95- assertar .Equal ([]int {3 , 2 , 4 , 1 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (common .Hash {}, 0 )))
96- assertar .Equal ([]int {0 , 4 , 2 , 1 , 3 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (common .Hash {}, 1 )))
97- assertar .Equal ([]int {3 , 4 , 2 , 1 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (common .Hash {}, 2 )))
98- assertar .Equal ([]int {4 , 2 , 1 , 3 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (common .Hash {}, 3 )))
99- assertar .Equal ([]int {1 , 4 }, WeightedPermutation (len (weightsArr )/ 2 , weightsArr , hashOf (common .Hash {}, 4 )))
94+ assertar .Equal ([]int {3 , 2 , 4 , 1 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (hash .Hash {}, 0 )))
95+ assertar .Equal ([]int {0 , 4 , 2 , 1 , 3 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (hash .Hash {}, 1 )))
96+ assertar .Equal ([]int {3 , 4 , 2 , 1 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (hash .Hash {}, 2 )))
97+ assertar .Equal ([]int {4 , 2 , 1 , 3 , 0 }, WeightedPermutation (len (weightsArr ), weightsArr , hashOf (hash .Hash {}, 3 )))
98+ assertar .Equal ([]int {1 , 4 }, WeightedPermutation (len (weightsArr )/ 2 , weightsArr , hashOf (hash .Hash {}, 4 )))
10099}
0 commit comments