Skip to content

Commit

Permalink
use new public key sort function in crypto (DNAProject#382)
Browse files Browse the repository at this point in the history
Signed-off-by: laizy <[email protected]>
  • Loading branch information
laizy authored Jun 21, 2018
1 parent 21255df commit d26e7e4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 69 deletions.
6 changes: 2 additions & 4 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -540,6 +537,7 @@ func (this *OntologyConfig) GetBookkeepers() ([]keypair.PublicKey, error) {
}
pubKeys = append(pubKeys, k)
}
keypair.SortPublicKeys(pubKeys)
return pubKeys, nil
}

Expand Down
14 changes: 6 additions & 8 deletions core/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)))
Expand Down
7 changes: 1 addition & 6 deletions core/program/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package program
import (
"github.com/ontio/ontology-crypto/keypair"
"github.com/stretchr/testify/assert"
"sort"
"testing"
)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/store/ledgerstore/ledger_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 0 additions & 50 deletions core/vote/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,12 @@
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"
)

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
}

0 comments on commit d26e7e4

Please sign in to comment.