diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-01-04 14:43:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-04 14:43:20 +0800 |
commit | beaef47dd5dd4b342cef3c8b26508ce3c334be06 (patch) | |
tree | 673bdb4b03d98db44bf0535995df0a1793290dc1 | |
parent | 47cb69d390aedf53f44cae35629b3568a166e3fb (diff) | |
download | dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar.gz dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar.bz2 dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar.lz dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar.xz dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.tar.zst dexon-consensus-beaef47dd5dd4b342cef3c8b26508ce3c334be06.zip |
core: Change target of each nodeset (#397)
-rw-r--r-- | core/types/nodeset.go | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/core/types/nodeset.go b/core/types/nodeset.go index 89dfef3..da615e1 100644 --- a/core/types/nodeset.go +++ b/core/types/nodeset.go @@ -32,7 +32,9 @@ type NodeSet struct { } // SubSetTarget is the sub set target for GetSubSet(). -type SubSetTarget *big.Int +type SubSetTarget struct { + data [][]byte +} type subSetTargetType byte @@ -71,7 +73,7 @@ func NewNodeSet() *NodeSet { } // NewNotarySetTarget is the target for getting Notary Set. -func NewNotarySetTarget(crs common.Hash, chainID uint32) SubSetTarget { +func NewNotarySetTarget(crs common.Hash, chainID uint32) *SubSetTarget { binaryChainID := make([]byte, 4) binary.LittleEndian.PutUint32(binaryChainID, chainID) @@ -79,7 +81,7 @@ func NewNotarySetTarget(crs common.Hash, chainID uint32) SubSetTarget { } // NewDKGSetTarget is the target for getting DKG Set. -func NewDKGSetTarget(crs common.Hash) SubSetTarget { +func NewDKGSetTarget(crs common.Hash) *SubSetTarget { return newTarget(targetDKGSet, crs[:]) } @@ -99,7 +101,7 @@ func (ns *NodeSet) Clone() *NodeSet { // GetSubSet returns the subset of given target. func (ns *NodeSet) GetSubSet( - size int, target SubSetTarget) map[NodeID]struct{} { + size int, target *SubSetTarget) map[NodeID]struct{} { if size == 0 { return make(map[NodeID]struct{}) } @@ -129,18 +131,20 @@ func (ns *NodeSet) GetSubSet( return nIDs } -func newTarget(targetType subSetTargetType, data ...[]byte) SubSetTarget { +func newTarget(targetType subSetTargetType, data ...[]byte) *SubSetTarget { data = append(data, []byte{byte(targetType)}) - h := crypto.Keccak256Hash(data...) - num := big.NewInt(0) - num.SetBytes(h[:]) - return SubSetTarget(num) + return &SubSetTarget{ + data: data, + } } -func newNodeRank(ID NodeID, target SubSetTarget) *nodeRank { - num := big.NewInt(0) - num.SetBytes(ID.Hash[:]) - num.Abs(num.Sub((*big.Int)(target), num)) +func newNodeRank(ID NodeID, target *SubSetTarget) *nodeRank { + data := make([][]byte, 1, len(target.data)+1) + data[0] = make([]byte, len(ID.Hash)) + copy(data[0], ID.Hash[:]) + data = append(data, target.data...) + h := crypto.Keccak256Hash(data...) + num := new(big.Int).SetBytes(h[:]) return &nodeRank{ ID: ID, rank: num, |