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

Introduce new keywords #793

Merged
merged 3 commits into from
Sep 8, 2021
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
3 changes: 2 additions & 1 deletion api/flip_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ func (api *FlipApi) WordPairs(addr common.Address, vrfHash hexutil.Bytes) []Flip
var hash [32]byte
copy(hash[:], vrfHash[:])

wordPairs := ceremony.GeneratePairsFromVrfHash(hash, common.WordDictionarySize, identity.GetTotalWordPairsCount())
firstIndex, wordsDictionarySize := api.ceremony.GetWordDictionaryRange()
wordPairs := ceremony.GeneratePairsFromVrfHash(hash, firstIndex, wordsDictionarySize, identity.GetTotalWordPairsCount())

usedPairs := mapset.NewSet()
for _, v := range identity.Flips {
Expand Down
3 changes: 3 additions & 0 deletions common/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const (
WordDictionarySize = 3300
WordPairsPerFlip = 3

WordDictionary2Size = 640
WordDictionary2FirstIndex = 3300

MaxFlipSize = 1024 * 600
MaxProfileSize = 1024 * 1024
MaxCustomDataSize = 1024 * 1024
Expand Down
2 changes: 2 additions & 0 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type ConsensusConf struct {
IncreaseGodInvitesLimit bool
FixDelegation bool
FixSmallReportCommittee bool
NewKeyWordsEpoch uint16
}

type ConsensusVerson uint16
Expand Down Expand Up @@ -176,6 +177,7 @@ func ApplyConsensusVersion(ver ConsensusVerson, cfg *ConsensusConf) {
cfg.IncreaseGodInvitesLimit = true
cfg.FixDelegation = true
cfg.FixSmallReportCommittee = true
cfg.NewKeyWordsEpoch = 75
cfg.Version = ConsensusV6
cfg.MigrationTimeout = 0
cfg.GenerateGenesisAfterUpgrade = true
Expand Down
14 changes: 12 additions & 2 deletions core/ceremony/ceremony.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,17 @@ func (vc *ValidationCeremony) FlipKeyWordPairs() []int {
return vc.flipWordsInfo.pairs
}

func (vc *ValidationCeremony) GetWordDictionaryRange() (firstIndex, size int) {
if vc.config.Consensus.NewKeyWordsEpoch > 0 && vc.epoch >= vc.config.Consensus.NewKeyWordsEpoch {
return common.WordDictionary2FirstIndex, common.WordDictionary2Size
}
return 0, common.WordDictionarySize
}

func (vc *ValidationCeremony) generateFlipKeyWordPairs(seed []byte) {
identity := vc.appState.State.GetIdentity(vc.secStore.GetAddress())
vc.flipWordsInfo.pairs, vc.flipWordsInfo.proof = vc.GeneratePairs(seed, common.WordDictionarySize,
firstIndex, wordsDictionarySize := vc.GetWordDictionaryRange()
vc.flipWordsInfo.pairs, vc.flipWordsInfo.proof = vc.GeneratePairs(seed, firstIndex, wordsDictionarySize,
identity.GetTotalWordPairsCount())
}

Expand Down Expand Up @@ -1448,7 +1456,9 @@ func (vc *ValidationCeremony) GetFlipWords(cid []byte) (word1, word2 int, err er
}
}

return GetWords(rnd, common.WordDictionarySize, identity.GetTotalWordPairsCount(), pairId)
firstIndex, wordsDictionarySize := vc.GetWordDictionaryRange()

return GetWords(rnd, firstIndex, wordsDictionarySize, identity.GetTotalWordPairsCount(), pairId)
}

func (vc *ValidationCeremony) getCandidateIndex(addr common.Address) int {
Expand Down
18 changes: 9 additions & 9 deletions core/ceremony/word.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ import (
"math/rand"
)

func GeneratePairsFromVrfHash(hash [32]byte, dictionarySize, pairCount int) (nums []int) {
func GeneratePairsFromVrfHash(hash [32]byte, firstIndex, dictionarySize, pairCount int) (nums []int) {
rnd := rand.New(rand.NewSource(int64(getWordsRnd(hash))))
pairs := mapset.NewSet()
for i := 0; i < pairCount; i++ {
var num1, num2 int
num1, num2 = nextPair(rnd, dictionarySize, pairCount, pairs)
num1, num2 = nextPair(rnd, firstIndex, dictionarySize, pairCount, pairs)
nums = append(nums, num1, num2)
}
return nums
}

func (vc *ValidationCeremony) GeneratePairs(seed []byte, dictionarySize, pairCount int) (nums []int, proof []byte) {
func (vc *ValidationCeremony) GeneratePairs(seed []byte, firstIndex, dictionarySize, pairCount int) (nums []int, proof []byte) {
hash, p := vc.secStore.VrfEvaluate(seed)
nums = GeneratePairsFromVrfHash(hash, dictionarySize, pairCount)
nums = GeneratePairsFromVrfHash(hash, firstIndex, dictionarySize, pairCount)
return nums, p
}

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

func GetWords(authorRnd uint64, dictionarySize, pairCount, pairIndex int) (word1, word2 int, err error) {
func GetWords(authorRnd uint64, firstIndex, dictionarySize, pairCount, pairIndex int) (word1, word2 int, err error) {
rnd := rand.New(rand.NewSource(int64(authorRnd)))
pairs := mapset.NewSet()
for i := 0; i < pairCount; i++ {
var val1, val2 int
val1, val2 = nextPair(rnd, dictionarySize, pairCount, pairs)
val1, val2 = nextPair(rnd, firstIndex, dictionarySize, pairCount, pairs)
if i == pairIndex {
return val1, val2, nil
}
Expand All @@ -46,11 +46,11 @@ func maxUniquePairs(dictionarySize int) int {
return (dictionarySize - 1) * dictionarySize / 2
}

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

pairs.Add(pairToString(num1, num2))
Expand Down
13 changes: 7 additions & 6 deletions core/ceremony/word_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ func Test_GeneratePairs(t *testing.T) {
{1, 1, false},
{3, 4, false},
} {
nums, proof := vc.GeneratePairs([]byte("data"), tc.dictionarySize, tc.pairCount)
nums, proof := vc.GeneratePairs([]byte("data"), 5, tc.dictionarySize, tc.pairCount)

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

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

if !tc.checkUniqueness {
Expand Down Expand Up @@ -71,20 +72,20 @@ func Test_GetWords(t *testing.T) {
seed := []byte("data1")
dictionarySize := 3300
pairCount := 9
nums, proof := vc.GeneratePairs(seed, dictionarySize, pairCount)
nums, proof := vc.GeneratePairs(seed, 5, dictionarySize, pairCount)

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

w1, w2, _ := GetWords(rnd, dictionarySize, pairCount, 1)
w1, w2, _ := GetWords(rnd, 5, dictionarySize, pairCount, 1)
require.Equal(nums[2], w1)
require.Equal(nums[3], w2)

w1, w2, _ = GetWords(rnd, dictionarySize, pairCount, 8)
w1, w2, _ = GetWords(rnd, 5, dictionarySize, pairCount, 8)
require.Equal(nums[16], w1)
require.Equal(nums[17], w2)

w1, w2, err := GetWords(rnd, dictionarySize, pairCount, 15)
w1, w2, err := GetWords(rnd, 5, dictionarySize, pairCount, 15)
require.Error(err, "out of bounds pair index should throw error")
}

Expand Down
2 changes: 1 addition & 1 deletion keywords/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Keyword struct {
var list []Keyword

func init() {
list = make([]Keyword, 0, 3350)
list = make([]Keyword, 0, 3990)
data, _ := Asset("keywords.json")
if err := json.Unmarshal(data, &list); err != nil {
log.Warn("cannot parse keywords.json", "err", err)
Expand Down
Loading