From d26e7e41d2094bff671c39d0a7f24d2a31bba178 Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 21 Jun 2018 11:32:14 +0800 Subject: [PATCH] use new public key sort function in crypto (#382) Signed-off-by: laizy --- common/config/config.go | 6 ++-- core/program/program.go | 14 ++++---- core/program/program_test.go | 7 +--- core/store/ledgerstore/ledger_store.go | 2 +- core/vote/validator.go | 50 -------------------------- 5 files changed, 10 insertions(+), 69 deletions(-) diff --git a/common/config/config.go b/common/config/config.go index e63c9ce2..4917048f 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -21,14 +21,12 @@ package config import ( "encoding/hex" "fmt" - "io" - "sort" - "github.com/ontio/ontology-crypto/keypair" "github.com/ontio/ontology/common" "github.com/ontio/ontology/common/constants" "github.com/ontio/ontology/common/serialization" "github.com/ontio/ontology/errors" + "io" ) const ( @@ -530,7 +528,6 @@ func (this *OntologyConfig) GetBookkeepers() ([]keypair.PublicKey, error) { return nil, fmt.Errorf("Does not support %s consensus", this.Genesis.ConsensusType) } - sort.Strings(bookKeepers) pubKeys := make([]keypair.PublicKey, 0, len(bookKeepers)) for _, key := range bookKeepers { pubKey, err := hex.DecodeString(key) @@ -540,6 +537,7 @@ func (this *OntologyConfig) GetBookkeepers() ([]keypair.PublicKey, error) { } pubKeys = append(pubKeys, k) } + keypair.SortPublicKeys(pubKeys) return pubKeys, nil } diff --git a/core/program/program.go b/core/program/program.go index aebafd59..c489dd25 100644 --- a/core/program/program.go +++ b/core/program/program.go @@ -22,15 +22,13 @@ import ( "bytes" "errors" "fmt" - "math" - "math/big" - "sort" - "github.com/ontio/ontology-crypto/keypair" "github.com/ontio/ontology/common" "github.com/ontio/ontology/common/constants" "github.com/ontio/ontology/common/serialization" "github.com/ontio/ontology/vm/neovm" + "math" + "math/big" ) type ProgramBuilder struct { @@ -95,13 +93,13 @@ func ProgramFromMultiPubKey(pubkeys []keypair.PublicKey, m int) ([]byte, error) return nil, errors.New("wrong multi-sig param") } - list := keypair.NewPublicList(pubkeys) - sort.Sort(list) + pubkeys = keypair.SortPublicKeys(pubkeys) builder := ProgramBuilder{} builder.PushNum(uint16(m)) - for _, pubkey := range list { - builder.PushBytes(pubkey) + for _, pubkey := range pubkeys { + key := keypair.SerializePublicKey(pubkey) + builder.PushBytes(key) } builder.PushNum(uint16(len(pubkeys))) diff --git a/core/program/program_test.go b/core/program/program_test.go index 3ea30deb..ea6207b4 100644 --- a/core/program/program_test.go +++ b/core/program/program_test.go @@ -21,7 +21,6 @@ package program import ( "github.com/ontio/ontology-crypto/keypair" "github.com/stretchr/testify/assert" - "sort" "testing" ) @@ -47,12 +46,8 @@ func TestGetProgramInfo(t *testing.T) { _, key, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) pubkeys = append(pubkeys, key) } - list := keypair.NewPublicList(pubkeys) - sort.Sort(list) - for i := 0; i < N; i++ { - pubkeys[i], _ = keypair.DeserializePublicKey(list[i]) - } + pubkeys = keypair.SortPublicKeys(pubkeys) progInfo := ProgramInfo{PubKeys: pubkeys, M: uint16(M)} prog, err := ProgramFromMultiPubKey(progInfo.PubKeys, int(progInfo.M)) assert.Nil(t, err) diff --git a/core/store/ledgerstore/ledger_store.go b/core/store/ledgerstore/ledger_store.go index 394384a4..8c9a9d1a 100644 --- a/core/store/ledgerstore/ledger_store.go +++ b/core/store/ledgerstore/ledger_store.go @@ -121,7 +121,7 @@ func (this *LedgerStoreImp) InitLedgerStoreWithGenesisBlock(genesisBlock *types. if err != nil { return fmt.Errorf("eventStore.ClearAll error %s", err) } - sort.Sort(keypair.NewPublicList(defaultBookkeeper)) + defaultBookkeeper = keypair.SortPublicKeys(defaultBookkeeper) bookkeeperState := &states.BookkeeperState{ CurrBookkeeper: defaultBookkeeper, NextBookkeeper: defaultBookkeeper, diff --git a/core/vote/validator.go b/core/vote/validator.go index aa3d42e0..68aa8094 100644 --- a/core/vote/validator.go +++ b/core/vote/validator.go @@ -19,12 +19,8 @@ package vote import ( - "sort" - "github.com/ontio/ontology-crypto/keypair" - "github.com/ontio/ontology/common" "github.com/ontio/ontology/core/genesis" - "github.com/ontio/ontology/core/states" "github.com/ontio/ontology/core/types" ) @@ -32,49 +28,3 @@ func GetValidators(txs []*types.Transaction) ([]keypair.PublicKey, error) { // TODO implement vote return genesis.GenesisBookkeepers, nil } - -func weightedAverage(votes []*states.VoteState) int64 { - var sumWeight, sumValue int64 - for _, v := range votes { - sumWeight += v.Count.GetData() - sumValue += v.Count.GetData() * int64(len(v.PublicKeys)) - } - if sumValue == 0 { - return 0 - } - return sumValue / sumWeight -} - -type Pair struct { - Key string - Value int64 -} - -// A slice of Pairs that implements sort.Interface to sort by Value. -type PairList []Pair - -func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p PairList) Len() int { return len(p) } -func (p PairList) Less(i, j int) bool { - if p[j].Value < p[i].Value { - return true - } else if p[j].Value > p[i].Value { - return false - } else { - return p[j].Key < p[i].Key - } -} - -// A function to turn a map into a PairList, then sort and return it. -func sortMapByValue(m map[string]common.Fixed64) []string { - p := make(PairList, 0, len(m)) - for k, v := range m { - p = append(p, Pair{k, v.GetData()}) - } - sort.Sort(p) - keys := make([]string, len(m)) - for i, k := range p { - keys[i] = k.Key - } - return keys -}