Sitelet https://github.com/Fantom-foundation/Sonic/commit/fd3c6552e72c48271b2900c6550ef4f9894fa79b
Skip to content

Commit fd3c655

Browse files
committed
update utils types
1 parent 8d9dfe1 commit fd3c655

4 files changed

Lines changed: 52 additions & 38 deletions

File tree

‎utils/migration/kvdb_id_store.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package migration
22

33
import (
4+
"github.com/Fantom-foundation/lachesis-base/kvdb"
45
"github.com/ethereum/go-ethereum/log"
5-
6-
"github.com/Fantom-foundation/go-lachesis/kvdb"
76
)
87

98
// KvdbIDStore stores id
109
type KvdbIDStore struct {
11-
table kvdb.KeyValueStore
10+
table kvdb.Store
1211
key []byte
1312
}
1413

1514
// NewKvdbIDStore constructor
16-
func NewKvdbIDStore(table kvdb.KeyValueStore) *KvdbIDStore {
15+
func NewKvdbIDStore(table kvdb.Store) *KvdbIDStore {
1716
return &KvdbIDStore{
1817
table: table,
1918
key: []byte("id"),

‎utils/nameof.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Fantom-foundation/lachesis-base/hash"
7+
"github.com/Fantom-foundation/lachesis-base/inter/idx"
8+
)
9+
10+
// NameOf returns human readable string representation.
11+
func NameOf(p idx.ValidatorID) string {
12+
if name := hash.GetNodeName(p); len(name) > 0 {
13+
return name
14+
}
15+
16+
return fmt.Sprintf("%d", p)
17+
}

‎utils/weighted_shuffle.go‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ package utils
22

33
import (
44
"crypto/sha256"
5-
"github.com/Fantom-foundation/go-lachesis/inter/pos"
65

7-
"github.com/ethereum/go-ethereum/common"
8-
9-
"github.com/Fantom-foundation/go-lachesis/common/littleendian"
6+
"github.com/Fantom-foundation/lachesis-base/common/littleendian"
7+
"github.com/Fantom-foundation/lachesis-base/hash"
8+
"github.com/Fantom-foundation/lachesis-base/inter/pos"
109
)
1110

1211
type weightedShuffleNode struct {
13-
thisWeight pos.Stake
14-
leftWeight pos.Stake
15-
rightWeight pos.Stake
12+
thisWeight pos.Weight
13+
leftWeight pos.Weight
14+
rightWeight pos.Weight
1615
}
1716

1817
type weightedShuffleTree struct {
19-
seed common.Hash
18+
seed hash.Hash
2019
seedIndex int
2120

22-
weights []pos.Stake
21+
weights []pos.Weight
2322
nodes []weightedShuffleNode
2423
}
2524

@@ -31,7 +30,7 @@ func (t *weightedShuffleTree) rightIndex(i int) int {
3130
return i*2 + 2
3231
}
3332

34-
func (t *weightedShuffleTree) build(i int) pos.Stake {
33+
func (t *weightedShuffleTree) build(i int) pos.Weight {
3534
if i >= len(t.weights) {
3635
return 0
3736
}
@@ -55,11 +54,11 @@ func (t *weightedShuffleTree) rand64() uint64 {
5554
if t.seedIndex == 32 {
5655
hasher := sha256.New() // use sha2 instead of sha3 for speed
5756
hasher.Write(t.seed.Bytes())
58-
t.seed = common.BytesToHash(hasher.Sum(nil))
57+
t.seed = hash.BytesToHash(hasher.Sum(nil))
5958
t.seedIndex = 0
6059
}
6160
// use not used parts of old seed, instead of calculating new one
62-
res := littleendian.BytesToInt64(t.seed[t.seedIndex : t.seedIndex+8])
61+
res := littleendian.BytesToUint64(t.seed[t.seedIndex : t.seedIndex+8])
6362
t.seedIndex += 8
6463
return res
6564
}
@@ -68,7 +67,7 @@ func (t *weightedShuffleTree) retrieve(i int) int {
6867
node := t.nodes[i]
6968
total := node.rightWeight + node.leftWeight + node.thisWeight
7069

71-
r := pos.Stake(t.rand64()) % total
70+
r := pos.Weight(t.rand64()) % total
7271

7372
if r < node.thisWeight {
7473
t.nodes[i].thisWeight = 0
@@ -87,7 +86,7 @@ func (t *weightedShuffleTree) retrieve(i int) int {
8786
// WeightedPermutation builds weighted random permutation
8887
// Returns first {size} entries of {weights} permutation.
8988
// Call with {size} == len(weights) to get the whole permutation.
90-
func WeightedPermutation(size int, weights []pos.Stake, seed common.Hash) []int {
89+
func WeightedPermutation(size int, weights []pos.Weight, seed hash.Hash) []int {
9190
if len(weights) < size {
9291
panic("the permutation size must be less or equal to weights size")
9392
}

‎utils/weighted_shuffle_test.go‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ package utils
22

33
import (
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

9089
func 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

Comments
 (0)