Skip to content

Commit 5265134

Browse files
mbidenaiosidenaio
authored andcommitted
Introduce new keywords (#793)
1 parent 08175f7 commit 5265134

File tree

9 files changed

+2507
-59
lines changed

9 files changed

+2507
-59
lines changed

api/flip_api.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ func (api *FlipApi) WordPairs(addr common.Address, vrfHash hexutil.Bytes) []Flip
495495
var hash [32]byte
496496
copy(hash[:], vrfHash[:])
497497

498-
wordPairs := ceremony.GeneratePairsFromVrfHash(hash, common.WordDictionarySize, identity.GetTotalWordPairsCount())
498+
firstIndex, wordsDictionarySize := api.ceremony.GetWordDictionaryRange()
499+
wordPairs := ceremony.GeneratePairsFromVrfHash(hash, firstIndex, wordsDictionarySize, identity.GetTotalWordPairsCount())
499500

500501
usedPairs := mapset.NewSet()
501502
for _, v := range identity.Flips {

common/network.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const (
1616
WordDictionarySize = 3300
1717
WordPairsPerFlip = 3
1818

19+
WordDictionary2Size = 640
20+
WordDictionary2FirstIndex = 3300
21+
1922
MaxFlipSize = 1024 * 600
2023
MaxProfileSize = 1024 * 1024
2124
MaxCustomDataSize = 1024 * 1024

config/consensus.go

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type ConsensusConf struct {
6262
IncreaseGodInvitesLimit bool
6363
FixDelegation bool
6464
FixSmallReportCommittee bool
65+
NewKeyWordsEpoch uint16
6566
}
6667

6768
type ConsensusVerson uint16
@@ -176,6 +177,7 @@ func ApplyConsensusVersion(ver ConsensusVerson, cfg *ConsensusConf) {
176177
cfg.IncreaseGodInvitesLimit = true
177178
cfg.FixDelegation = true
178179
cfg.FixSmallReportCommittee = true
180+
cfg.NewKeyWordsEpoch = 75
179181
cfg.Version = ConsensusV6
180182
cfg.MigrationTimeout = 0
181183
cfg.GenerateGenesisAfterUpgrade = true

core/ceremony/ceremony.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1426,9 +1426,17 @@ func (vc *ValidationCeremony) FlipKeyWordPairs() []int {
14261426
return vc.flipWordsInfo.pairs
14271427
}
14281428

1429+
func (vc *ValidationCeremony) GetWordDictionaryRange() (firstIndex, size int) {
1430+
if vc.config.Consensus.NewKeyWordsEpoch > 0 && vc.epoch >= vc.config.Consensus.NewKeyWordsEpoch {
1431+
return common.WordDictionary2FirstIndex, common.WordDictionary2Size
1432+
}
1433+
return 0, common.WordDictionarySize
1434+
}
1435+
14291436
func (vc *ValidationCeremony) generateFlipKeyWordPairs(seed []byte) {
14301437
identity := vc.appState.State.GetIdentity(vc.secStore.GetAddress())
1431-
vc.flipWordsInfo.pairs, vc.flipWordsInfo.proof = vc.GeneratePairs(seed, common.WordDictionarySize,
1438+
firstIndex, wordsDictionarySize := vc.GetWordDictionaryRange()
1439+
vc.flipWordsInfo.pairs, vc.flipWordsInfo.proof = vc.GeneratePairs(seed, firstIndex, wordsDictionarySize,
14321440
identity.GetTotalWordPairsCount())
14331441
}
14341442

@@ -1466,7 +1474,9 @@ func (vc *ValidationCeremony) GetFlipWords(cid []byte) (word1, word2 int, err er
14661474
}
14671475
}
14681476

1469-
return GetWords(rnd, common.WordDictionarySize, identity.GetTotalWordPairsCount(), pairId)
1477+
firstIndex, wordsDictionarySize := vc.GetWordDictionaryRange()
1478+
1479+
return GetWords(rnd, firstIndex, wordsDictionarySize, identity.GetTotalWordPairsCount(), pairId)
14701480
}
14711481

14721482
func (vc *ValidationCeremony) getCandidateIndex(addr common.Address) int {

core/ceremony/word.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ import (
88
"math/rand"
99
)
1010

11-
func GeneratePairsFromVrfHash(hash [32]byte, dictionarySize, pairCount int) (nums []int) {
11+
func GeneratePairsFromVrfHash(hash [32]byte, firstIndex, dictionarySize, pairCount int) (nums []int) {
1212
rnd := rand.New(rand.NewSource(int64(getWordsRnd(hash))))
1313
pairs := mapset.NewSet()
1414
for i := 0; i < pairCount; i++ {
1515
var num1, num2 int
16-
num1, num2 = nextPair(rnd, dictionarySize, pairCount, pairs)
16+
num1, num2 = nextPair(rnd, firstIndex, dictionarySize, pairCount, pairs)
1717
nums = append(nums, num1, num2)
1818
}
1919
return nums
2020
}
2121

22-
func (vc *ValidationCeremony) GeneratePairs(seed []byte, dictionarySize, pairCount int) (nums []int, proof []byte) {
22+
func (vc *ValidationCeremony) GeneratePairs(seed []byte, firstIndex, dictionarySize, pairCount int) (nums []int, proof []byte) {
2323
hash, p := vc.secStore.VrfEvaluate(seed)
24-
nums = GeneratePairsFromVrfHash(hash, dictionarySize, pairCount)
24+
nums = GeneratePairsFromVrfHash(hash, firstIndex, dictionarySize, pairCount)
2525
return nums, p
2626
}
2727

2828
func getWordsRnd(hash [32]byte) uint64 {
2929
return binary.LittleEndian.Uint64(hash[:])
3030
}
3131

32-
func GetWords(authorRnd uint64, dictionarySize, pairCount, pairIndex int) (word1, word2 int, err error) {
32+
func GetWords(authorRnd uint64, firstIndex, dictionarySize, pairCount, pairIndex int) (word1, word2 int, err error) {
3333
rnd := rand.New(rand.NewSource(int64(authorRnd)))
3434
pairs := mapset.NewSet()
3535
for i := 0; i < pairCount; i++ {
3636
var val1, val2 int
37-
val1, val2 = nextPair(rnd, dictionarySize, pairCount, pairs)
37+
val1, val2 = nextPair(rnd, firstIndex, dictionarySize, pairCount, pairs)
3838
if i == pairIndex {
3939
return val1, val2, nil
4040
}
@@ -46,11 +46,11 @@ func maxUniquePairs(dictionarySize int) int {
4646
return (dictionarySize - 1) * dictionarySize / 2
4747
}
4848

49-
func nextPair(rnd *rand.Rand, dictionarySize, pairCount int, pairs mapset.Set) (num1, num2 int) {
50-
num1, num2 = rnd.Intn(dictionarySize), rnd.Intn(dictionarySize)
49+
func nextPair(rnd *rand.Rand, firstIndex, dictionarySize, pairCount int, pairs mapset.Set) (num1, num2 int) {
50+
num1, num2 = rnd.Intn(dictionarySize)+firstIndex, rnd.Intn(dictionarySize)+firstIndex
5151
maxPairs := maxUniquePairs(dictionarySize)
5252
for pairCount <= maxPairs && !checkPair(num1, num2, pairs) {
53-
num1, num2 = rnd.Intn(dictionarySize), rnd.Intn(dictionarySize)
53+
num1, num2 = rnd.Intn(dictionarySize)+firstIndex, rnd.Intn(dictionarySize)+firstIndex
5454
}
5555

5656
pairs.Add(pairToString(num1, num2))

core/ceremony/word_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ func Test_GeneratePairs(t *testing.T) {
3131
{1, 1, false},
3232
{3, 4, false},
3333
} {
34-
nums, proof := vc.GeneratePairs([]byte("data"), tc.dictionarySize, tc.pairCount)
34+
nums, proof := vc.GeneratePairs([]byte("data"), 5, tc.dictionarySize, tc.pairCount)
3535

3636
require.Equal(t, tc.pairCount*2, len(nums))
3737
require.NotNil(t, proof)
3838

3939
for i := 0; i < len(nums); i++ {
40-
require.True(t, nums[i] < tc.dictionarySize)
40+
require.True(t, nums[i] >= 5)
41+
require.True(t, nums[i] < tc.dictionarySize+5)
4142
}
4243

4344
if !tc.checkUniqueness {
@@ -71,20 +72,20 @@ func Test_GetWords(t *testing.T) {
7172
seed := []byte("data1")
7273
dictionarySize := 3300
7374
pairCount := 9
74-
nums, proof := vc.GeneratePairs(seed, dictionarySize, pairCount)
75+
nums, proof := vc.GeneratePairs(seed, 5, dictionarySize, pairCount)
7576

7677
h, _ := vrf.HashFromProof(proof)
7778
rnd := getWordsRnd(h)
7879

79-
w1, w2, _ := GetWords(rnd, dictionarySize, pairCount, 1)
80+
w1, w2, _ := GetWords(rnd, 5, dictionarySize, pairCount, 1)
8081
require.Equal(nums[2], w1)
8182
require.Equal(nums[3], w2)
8283

83-
w1, w2, _ = GetWords(rnd, dictionarySize, pairCount, 8)
84+
w1, w2, _ = GetWords(rnd, 5, dictionarySize, pairCount, 8)
8485
require.Equal(nums[16], w1)
8586
require.Equal(nums[17], w2)
8687

87-
w1, w2, err := GetWords(rnd, dictionarySize, pairCount, 15)
88+
w1, w2, err := GetWords(rnd, 5, dictionarySize, pairCount, 15)
8889
require.Error(err, "out of bounds pair index should throw error")
8990
}
9091

keywords/keywords.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Keyword struct {
1414
var list []Keyword
1515

1616
func init() {
17-
list = make([]Keyword, 0, 3350)
17+
list = make([]Keyword, 0, 3990)
1818
data, _ := Asset("keywords.json")
1919
if err := json.Unmarshal(data, &list); err != nil {
2020
log.Warn("cannot parse keywords.json", "err", err)

0 commit comments

Comments
 (0)