The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/shard/shard_state.go

380 lines
9.3 KiB

package shard
import (
"bytes"
"encoding/hex"
"encoding/json"
"math/big"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/bls/ffi/go/bls"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/numeric"
"golang.org/x/crypto/sha3"
)
5 years ago
var (
emptyBlsPubKey = BlsPublicKey{}
)
// PublicKeySizeInBytes ..
const (
PublicKeySizeInBytes = 48
[slash][consensus] Notice double sign & broadcast, factor out tech debt of consensus (#2152) * [slash] Remove dead interface, associated piping * [slash] Expand out structs * [consensus] Write to a chan when find a case of double-signing, remove dead code * [slash] Broadcast the noticing of a double signing * [rawdb] CRUD for slashing candidates * [slashing][node][proto] Broadcast the slash record after receive from consensus, handle received proto message, persist in off-chain db while pending * [slash][node][propose-block] Add verified slashes proposed into the header in block proposal * [slash][shard] Factor out external validator as method on shard state, add double-signature field * [slash][engine] Apply slash, name boolean expression for sorts, use stable sort * [slash] Abstract Ballot results so keep track of both pre and post double sign event * [slash] Fix type errors on test code * [slash] Read from correct rawdb * [slash] Add epoch based guards in CRUD of slashing * [slash] Write to correct cache for slashing candidates * [shard] Use explicit named type of BLS Signature, use convention * [slash] Fix mistake done in refactor, improper header used. Factor out fromSlice to set * [slash][node] Restore newblock to master, try again minimial change * [cx-receipts] Break up one-liner, use SliceStable, not Slice * [network] Finish refactor that makes network message headers once * [network] Simplify creation further of headers write * [slash] Adjust data structure of slash after offline discussion with RJ, Chao * [slash] Still did need signature of the double signature * [consensus] Prepare message does not have block header * [consensus] Soft reset three files to 968517d~1 * [consensus] Begin factor consensus network intended message out with prepare first * [consensus] Factor out Prepared message * [consensus] Factor out announce message creation * [consensus] Committed Message, branch on verify sender key for clearer log * [consensus] Committed Message Factor out * [consensus] Do jenkins MVP of signatures adjustment * [main][slash] Provide YAML config as webhook config for double sign event * [consensus] Adjust signatures, whitespace, lessen GC pressure * [consensus] Remove dead code * [consensus] Factor out commit overloaded message, give commit payload override in construct * [consensus] Fix travis tests * [consensus] Provide block bytes in SubmitVote(quorum.Commit) * [consensus] Factor out noisy sanity checks in BFT, move existing commit check earlier as was before * [quorum] Adjust signatures in quorum * [staking] Adjust after merge from master * [consensus] Finish refactor of consensus * [node] Fix import * [consensus] Fix travis * [consensus] Use origin/master copy of block, fix mistake of pointer to empty byte * [consensus] Less verbose bools * [consensus] Remove unused trailing mutation hook in message construct * [consensus] Address some TODOs on err, comment out double sign
5 years ago
BLSSignatureSizeInBytes = 96
)
// State is the collection of all committees
type State struct {
Epoch *big.Int `json:"epoch"`
Shards []Committee `json:"shards"`
}
// BlsPublicKey defines the bls public key
type BlsPublicKey [PublicKeySizeInBytes]byte
[slash][consensus] Notice double sign & broadcast, factor out tech debt of consensus (#2152) * [slash] Remove dead interface, associated piping * [slash] Expand out structs * [consensus] Write to a chan when find a case of double-signing, remove dead code * [slash] Broadcast the noticing of a double signing * [rawdb] CRUD for slashing candidates * [slashing][node][proto] Broadcast the slash record after receive from consensus, handle received proto message, persist in off-chain db while pending * [slash][node][propose-block] Add verified slashes proposed into the header in block proposal * [slash][shard] Factor out external validator as method on shard state, add double-signature field * [slash][engine] Apply slash, name boolean expression for sorts, use stable sort * [slash] Abstract Ballot results so keep track of both pre and post double sign event * [slash] Fix type errors on test code * [slash] Read from correct rawdb * [slash] Add epoch based guards in CRUD of slashing * [slash] Write to correct cache for slashing candidates * [shard] Use explicit named type of BLS Signature, use convention * [slash] Fix mistake done in refactor, improper header used. Factor out fromSlice to set * [slash][node] Restore newblock to master, try again minimial change * [cx-receipts] Break up one-liner, use SliceStable, not Slice * [network] Finish refactor that makes network message headers once * [network] Simplify creation further of headers write * [slash] Adjust data structure of slash after offline discussion with RJ, Chao * [slash] Still did need signature of the double signature * [consensus] Prepare message does not have block header * [consensus] Soft reset three files to 968517d~1 * [consensus] Begin factor consensus network intended message out with prepare first * [consensus] Factor out Prepared message * [consensus] Factor out announce message creation * [consensus] Committed Message, branch on verify sender key for clearer log * [consensus] Committed Message Factor out * [consensus] Do jenkins MVP of signatures adjustment * [main][slash] Provide YAML config as webhook config for double sign event * [consensus] Adjust signatures, whitespace, lessen GC pressure * [consensus] Remove dead code * [consensus] Factor out commit overloaded message, give commit payload override in construct * [consensus] Fix travis tests * [consensus] Provide block bytes in SubmitVote(quorum.Commit) * [consensus] Factor out noisy sanity checks in BFT, move existing commit check earlier as was before * [quorum] Adjust signatures in quorum * [staking] Adjust after merge from master * [consensus] Finish refactor of consensus * [node] Fix import * [consensus] Fix travis * [consensus] Use origin/master copy of block, fix mistake of pointer to empty byte * [consensus] Less verbose bools * [consensus] Remove unused trailing mutation hook in message construct * [consensus] Address some TODOs on err, comment out double sign
5 years ago
// BLSSignature defines the bls signature
type BLSSignature [BLSSignatureSizeInBytes]byte
// Slot represents node id (BLS address)
type Slot struct {
EcdsaAddress common.Address `json:"ecdsa-address"`
BlsPublicKey BlsPublicKey `json:"bls-pubkey"`
// nil means our node, 0 means not active, > 0 means staked node
EffectiveStake *numeric.Dec `json:"effective-stake" rlp:"nil"`
}
// SlotList is a list of Slot.
type SlotList []Slot
// Committee contains the active nodes in one shard
type Committee struct {
ShardID uint32 `json:"shard-id"`
Slots SlotList `json:"subcommittee"`
}
/* Legacy
These are the pre-staking used data-structures, needed to maintain
compatibilty for RLP decode/encode
*/
// StateLegacy ..
type StateLegacy []CommitteeLegacy
// SlotLegacy represents node id (BLS address)
type SlotLegacy struct {
EcdsaAddress common.Address `json:"ecdsa-address"`
BlsPublicKey BlsPublicKey `json:"bls-pubkey"`
}
// SlotListLegacy is a list of SlotList.
type SlotListLegacy []SlotLegacy
// CommitteeLegacy contains the active nodes in one shard
type CommitteeLegacy struct {
ShardID uint32 `json:"shard-id"`
Slots SlotListLegacy `json:"subcommittee"`
}
// DecodeWrapper ..
func DecodeWrapper(shardState []byte) (*State, error) {
oldSS := StateLegacy{}
newSS := State{}
var (
err1 error
err2 error
)
err1 = rlp.DecodeBytes(shardState, &newSS)
if err1 == nil {
return &newSS, nil
}
err2 = rlp.DecodeBytes(shardState, &oldSS)
if err2 == nil {
newSS := State{}
newSS.Shards = make([]Committee, len(oldSS))
for i := range oldSS {
newSS.Shards[i] = Committee{ShardID: oldSS[i].ShardID, Slots: SlotList{}}
for _, slot := range oldSS[i].Slots {
newSS.Shards[i].Slots = append(newSS.Shards[i].Slots, Slot{
slot.EcdsaAddress, slot.BlsPublicKey, nil,
})
}
}
newSS.Epoch = nil // Make sure for legacy state, the epoch is nil
return &newSS, nil
}
return nil, err2
}
// EncodeWrapper ..
func EncodeWrapper(shardState State, isStaking bool) ([]byte, error) {
var (
data []byte
err error
)
if isStaking {
data, err = rlp.EncodeToBytes(shardState)
} else {
shardStateLegacy := make(StateLegacy, len(shardState.Shards))
for i := range shardState.Shards {
shardStateLegacy[i] = CommitteeLegacy{
ShardID: shardState.Shards[i].ShardID, Slots: SlotListLegacy{},
}
for _, slot := range shardState.Shards[i].Slots {
shardStateLegacy[i].Slots = append(shardStateLegacy[i].Slots, SlotLegacy{
slot.EcdsaAddress, slot.BlsPublicKey,
})
}
}
data, err = rlp.EncodeToBytes(shardStateLegacy)
}
return data, err
}
[slash][consensus] Notice double sign & broadcast, factor out tech debt of consensus (#2152) * [slash] Remove dead interface, associated piping * [slash] Expand out structs * [consensus] Write to a chan when find a case of double-signing, remove dead code * [slash] Broadcast the noticing of a double signing * [rawdb] CRUD for slashing candidates * [slashing][node][proto] Broadcast the slash record after receive from consensus, handle received proto message, persist in off-chain db while pending * [slash][node][propose-block] Add verified slashes proposed into the header in block proposal * [slash][shard] Factor out external validator as method on shard state, add double-signature field * [slash][engine] Apply slash, name boolean expression for sorts, use stable sort * [slash] Abstract Ballot results so keep track of both pre and post double sign event * [slash] Fix type errors on test code * [slash] Read from correct rawdb * [slash] Add epoch based guards in CRUD of slashing * [slash] Write to correct cache for slashing candidates * [shard] Use explicit named type of BLS Signature, use convention * [slash] Fix mistake done in refactor, improper header used. Factor out fromSlice to set * [slash][node] Restore newblock to master, try again minimial change * [cx-receipts] Break up one-liner, use SliceStable, not Slice * [network] Finish refactor that makes network message headers once * [network] Simplify creation further of headers write * [slash] Adjust data structure of slash after offline discussion with RJ, Chao * [slash] Still did need signature of the double signature * [consensus] Prepare message does not have block header * [consensus] Soft reset three files to 968517d~1 * [consensus] Begin factor consensus network intended message out with prepare first * [consensus] Factor out Prepared message * [consensus] Factor out announce message creation * [consensus] Committed Message, branch on verify sender key for clearer log * [consensus] Committed Message Factor out * [consensus] Do jenkins MVP of signatures adjustment * [main][slash] Provide YAML config as webhook config for double sign event * [consensus] Adjust signatures, whitespace, lessen GC pressure * [consensus] Remove dead code * [consensus] Factor out commit overloaded message, give commit payload override in construct * [consensus] Fix travis tests * [consensus] Provide block bytes in SubmitVote(quorum.Commit) * [consensus] Factor out noisy sanity checks in BFT, move existing commit check earlier as was before * [quorum] Adjust signatures in quorum * [staking] Adjust after merge from master * [consensus] Finish refactor of consensus * [node] Fix import * [consensus] Fix travis * [consensus] Use origin/master copy of block, fix mistake of pointer to empty byte * [consensus] Less verbose bools * [consensus] Remove unused trailing mutation hook in message construct * [consensus] Address some TODOs on err, comment out double sign
5 years ago
// ExternalValidators returns only the staking era,
// external validators aka non-harmony nodes
func (ss *State) ExternalValidators() []common.Address {
processed := make(map[common.Address]struct{})
for i := range ss.Shards {
shard := ss.Shards[i]
for j := range shard.Slots {
slot := shard.Slots[j]
if slot.EffectiveStake != nil { // For external validator
_, ok := processed[slot.EcdsaAddress]
if !ok {
processed[slot.EcdsaAddress] = struct{}{}
}
}
}
}
slice, i := make([]common.Address, len(processed)), 0
for key := range processed {
slice[i] = key
i++
}
return slice
}
// String ..
func (ss *State) String() string {
s, _ := json.Marshal(ss)
return string(s)
}
// MarshalJSON ..
func (ss *State) MarshalJSON() ([]byte, error) {
type t struct {
Slot
EcdsaAddress string `json:"ecdsa-address"`
}
type v struct {
Committee
Count int `json:"member-count"`
NodeList []t `json:"subcommittee"`
}
dump := make([]v, len(ss.Shards))
for i := range ss.Shards {
c := len(ss.Shards[i].Slots)
dump[i].ShardID = ss.Shards[i].ShardID
dump[i].NodeList = make([]t, c)
dump[i].Count = c
for j := range ss.Shards[i].Slots {
n := ss.Shards[i].Slots[j]
dump[i].NodeList[j].BlsPublicKey = n.BlsPublicKey
dump[i].NodeList[j].EffectiveStake = n.EffectiveStake
dump[i].NodeList[j].EcdsaAddress = common2.MustAddressToBech32(n.EcdsaAddress)
}
}
return json.Marshal(dump)
}
// FindCommitteeByID returns the committee configuration for the given shard,
// or nil if the given shard is not found.
func (ss *State) FindCommitteeByID(shardID uint32) *Committee {
if ss == nil {
return nil
}
for committee := range ss.Shards {
if ss.Shards[committee].ShardID == shardID {
return &ss.Shards[committee]
}
}
return nil
}
// DeepCopy returns a deep copy of the receiver.
func (ss *State) DeepCopy() *State {
var r State
if ss.Epoch != nil {
r.Epoch = big.NewInt(0).Set(ss.Epoch)
}
for _, c := range ss.Shards {
r.Shards = append(r.Shards, c.DeepCopy())
}
return &r
}
// Big ..
func (pk BlsPublicKey) Big() *big.Int {
return new(big.Int).SetBytes(pk[:])
}
5 years ago
// IsEmpty returns whether the bls public key is empty 0 bytes
func (pk BlsPublicKey) IsEmpty() bool {
return bytes.Compare(pk[:], emptyBlsPubKey[:]) == 0
}
// Hex returns the hex string of bls public key
func (pk BlsPublicKey) Hex() string {
return hex.EncodeToString(pk[:])
}
// MarshalJSON ..
func (pk BlsPublicKey) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
buf.WriteString(`"`)
buf.WriteString(pk.Hex())
buf.WriteString(`"`)
return buf.Bytes(), nil
}
// FromLibBLSPublicKeyUnsafe could give back nil, use only in cases when
// have invariant that return value won't be nil
func FromLibBLSPublicKeyUnsafe(key *bls.PublicKey) *BlsPublicKey {
result := &BlsPublicKey{}
if err := result.FromLibBLSPublicKey(key); err != nil {
return nil
}
return result
}
// FromLibBLSPublicKey replaces the key contents with the given key,
func (pk *BlsPublicKey) FromLibBLSPublicKey(key *bls.PublicKey) error {
bytes := key.Serialize()
if len(bytes) != len(pk) {
return ctxerror.New("BLS public key size mismatch",
"expected", len(pk),
"actual", len(bytes))
}
copy(pk[:], bytes)
return nil
}
// ToLibBLSPublicKey copies the key contents into the given key.
func (pk *BlsPublicKey) ToLibBLSPublicKey(key *bls.PublicKey) error {
return key.Deserialize(pk[:])
}
// CompareBlsPublicKey compares two BlsPublicKey, lexicographically.
func CompareBlsPublicKey(k1, k2 BlsPublicKey) int {
return bytes.Compare(k1[:], k2[:])
6 years ago
}
// CompareNodeID compares two node IDs.
func CompareNodeID(id1, id2 *Slot) int {
if c := bytes.Compare(id1.EcdsaAddress[:], id2.EcdsaAddress[:]); c != 0 {
return c
}
if c := CompareBlsPublicKey(id1.BlsPublicKey, id2.BlsPublicKey); c != 0 {
return c
}
return 0
}
// DeepCopy returns a deep copy of the receiver.
func (l SlotList) DeepCopy() SlotList {
return append(l[:0:0], l...)
}
// CompareNodeIDList compares two node ID lists.
func CompareNodeIDList(l1, l2 SlotList) int {
commonLen := len(l1)
if commonLen > len(l2) {
commonLen = len(l2)
}
for idx := 0; idx < commonLen; idx++ {
if c := CompareNodeID(&l1[idx], &l2[idx]); c != 0 {
return c
}
}
switch {
case len(l1) < len(l2):
return -1
case len(l1) > len(l2):
return +1
}
return 0
}
// DeepCopy returns a deep copy of the receiver.
func (c Committee) DeepCopy() Committee {
r := Committee{}
r.ShardID = c.ShardID
r.Slots = c.Slots.DeepCopy()
return r
}
// CompareCommittee compares two committees and their leader/node list.
func CompareCommittee(c1, c2 *Committee) int {
switch {
case c1.ShardID < c2.ShardID:
return -1
case c1.ShardID > c2.ShardID:
return +1
}
if c := CompareNodeIDList(c1.Slots, c2.Slots); c != 0 {
return c
}
return 0
}
// GetHashFromNodeList will sort the list, then use Keccak256 to hash the list
// NOTE: do not modify the underlining content for hash
func GetHashFromNodeList(nodeList []Slot) []byte {
// in general, nodeList should not be empty
if nodeList == nil || len(nodeList) == 0 {
return []byte{}
}
d := sha3.NewLegacyKeccak256()
for _, nodeID := range nodeList {
d.Write(nodeID.Serialize())
}
return d.Sum(nil)
}
// Hash is the root hash of State
func (ss *State) Hash() (h common.Hash) {
6 years ago
// TODO ek – this sorting really doesn't belong here; it should instead
// be made an explicit invariant to be maintained and, if needed, checked.
copy := ss.DeepCopy()
sort.Slice(copy.Shards, func(i, j int) bool {
return copy.Shards[i].ShardID < copy.Shards[j].ShardID
})
d := sha3.NewLegacyKeccak256()
for i := range copy.Shards {
hash := GetHashFromNodeList(copy.Shards[i].Slots)
d.Write(hash)
}
d.Sum(h[:0])
return h
}
// Serialize serialize Slot into bytes
func (n Slot) Serialize() []byte {
return append(n.EcdsaAddress[:], n.BlsPublicKey[:]...)
}
func (n Slot) String() string {
total := "nil"
if n.EffectiveStake != nil {
total = n.EffectiveStake.String()
}
return "ECDSA: " + common2.MustAddressToBech32(n.EcdsaAddress) + ", BLS: " + hex.EncodeToString(n.BlsPublicKey[:]) + ", EffectiveStake: " + total
}