|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/harmony-one/bls/ffi/go/bls"
|
|
|
|
"github.com/harmony-one/harmony/api/proto"
|
|
|
|
proto_discovery "github.com/harmony-one/harmony/api/proto/discovery"
|
|
|
|
proto_node "github.com/harmony-one/harmony/api/proto/node"
|
|
|
|
"github.com/harmony-one/harmony/block"
|
|
|
|
"github.com/harmony-one/harmony/core/types"
|
|
|
|
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
|
|
|
|
"github.com/harmony-one/harmony/internal/ctxerror"
|
|
|
|
"github.com/harmony-one/harmony/internal/utils"
|
|
|
|
"github.com/harmony-one/harmony/msgq"
|
|
|
|
"github.com/harmony-one/harmony/p2p"
|
|
|
|
"github.com/harmony-one/harmony/p2p/host"
|
|
|
|
"github.com/harmony-one/harmony/shard"
|
|
|
|
"github.com/harmony-one/harmony/staking/slash"
|
|
|
|
staking "github.com/harmony-one/harmony/staking/types"
|
|
|
|
libp2p_peer "github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const p2pMsgPrefixSize = 5
|
|
|
|
|
|
|
|
// receiveGroupMessage use libp2p pubsub mechanism to receive broadcast messages
|
|
|
|
func (node *Node) receiveGroupMessage(
|
|
|
|
receiver p2p.GroupReceiver, rxQueue msgq.MessageAdder,
|
|
|
|
) {
|
|
|
|
ctx := context.Background()
|
|
|
|
// TODO ek – infinite loop; add shutdown/cleanup logic
|
|
|
|
for {
|
|
|
|
msg, sender, err := receiver.Receive(ctx)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Warn().Err(err).
|
|
|
|
Msg("cannot receive from group")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sender == node.host.GetID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//utils.Logger().Info("[PUBSUB]", "received group msg", len(msg), "sender", sender)
|
|
|
|
// skip the first 5 bytes, 1 byte is p2p type, 4 bytes are message size
|
|
|
|
if len(msg) < p2pMsgPrefixSize {
|
|
|
|
utils.Logger().Warn().Err(err).Int("msg size", len(msg)).
|
|
|
|
Msg("invalid p2p message size")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := rxQueue.AddMessage(msg[p2pMsgPrefixSize:], sender); err != nil {
|
|
|
|
utils.Logger().Warn().Err(err).
|
|
|
|
Str("sender", sender.Pretty()).
|
|
|
|
Msg("cannot enqueue incoming message for processing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
// some messages have uninteresting fields in header, slash, receipt and crosslink are
|
|
|
|
// such messages. This function assumes that input bytes are a slice which already
|
|
|
|
// past those not relevant header bytes.
|
|
|
|
func (node *Node) processSkippedMsgTypeByteValue(cat proto_node.BlockMessageType, content []byte) {
|
|
|
|
switch cat {
|
|
|
|
case proto_node.SlashCandidate:
|
|
|
|
node.processSlashCandidateMessage(content)
|
|
|
|
case proto_node.Receipt:
|
|
|
|
utils.Logger().Debug().Msg("NET: received message: Node/Receipt")
|
|
|
|
node.ProcessReceiptMessage(content)
|
|
|
|
case proto_node.CrossLink:
|
|
|
|
// only beacon chain will accept the header from other shards
|
|
|
|
utils.Logger().Debug().
|
|
|
|
Uint32("shardID", node.NodeConfig.ShardID).
|
|
|
|
Msg("NET: received message: Node/CrossLink")
|
|
|
|
if node.NodeConfig.ShardID != shard.BeaconChainShardID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node.ProcessCrossLinkMessage(content)
|
|
|
|
default:
|
|
|
|
utils.Logger().Error().
|
|
|
|
Int("message-iota-value", int(cat)).
|
|
|
|
Msg("Invariant usage of processSkippedMsgTypeByteValue violated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMessage parses the message and dispatch the actions.
|
|
|
|
func (node *Node) HandleMessage(content []byte, sender libp2p_peer.ID) {
|
|
|
|
msgCategory, err := proto.GetMessageCategory(content)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("HandleMessage get message category failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msgType, err := proto.GetMessageType(content)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("HandleMessage get message type failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msgPayload, err := proto.GetMessagePayload(content)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("HandleMessage get message payload failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msgCategory {
|
|
|
|
case proto.Consensus:
|
|
|
|
msgPayload, _ := proto.GetConsensusMessagePayload(content)
|
|
|
|
if node.NodeConfig.Role() == nodeconfig.ExplorerNode {
|
|
|
|
node.ExplorerMessageHandler(msgPayload)
|
|
|
|
} else {
|
|
|
|
node.ConsensusMessageHandler(msgPayload)
|
|
|
|
}
|
|
|
|
case proto.DRand:
|
|
|
|
msgPayload, _ := proto.GetDRandMessagePayload(content)
|
|
|
|
if node.DRand != nil {
|
|
|
|
if node.DRand.IsLeader {
|
|
|
|
node.DRand.ProcessMessageLeader(msgPayload)
|
|
|
|
} else {
|
|
|
|
node.DRand.ProcessMessageValidator(msgPayload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case proto.Node:
|
|
|
|
actionType := proto_node.MessageType(msgType)
|
|
|
|
switch actionType {
|
|
|
|
case proto_node.Transaction:
|
|
|
|
utils.Logger().Debug().Msg("NET: received message: Node/Transaction")
|
|
|
|
node.transactionMessageHandler(msgPayload)
|
|
|
|
case proto_node.Staking:
|
|
|
|
utils.Logger().Debug().Msg("NET: received message: Node/Staking")
|
|
|
|
node.stakingMessageHandler(msgPayload)
|
|
|
|
case proto_node.Block:
|
|
|
|
utils.Logger().Debug().Msg("NET: received message: Node/Block")
|
|
|
|
if len(msgPayload) < 1 {
|
|
|
|
utils.Logger().Debug().Msgf("Invalid block message size")
|
|
|
|
return
|
|
|
|
}
|
[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
|
|
|
|
|
|
|
switch blockMsgType := proto_node.BlockMessageType(msgPayload[0]); blockMsgType {
|
|
|
|
case proto_node.Sync:
|
|
|
|
utils.Logger().Debug().Msg("NET: received message: Node/Sync")
|
|
|
|
var blocks []*types.Block
|
|
|
|
err := rlp.DecodeBytes(msgPayload[1:], &blocks)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("block sync")
|
|
|
|
} else {
|
|
|
|
// for non-beaconchain node, subscribe to beacon block broadcast
|
[double-sign] Provide proof of double sign in slash record sent to beaconchain (#2253)
* [double-sign] Commit changes in consensus needed for double-sign
* [double-sign] Leader captures when valdator double signs, broadcasts to beaconchain
* [slash] Add quick iteration tool for testing double-signing
* [slash] Add webhook example
* [slash] Add http server for hook to trigger double sign behavior
* [double-sign] Use bin/trigger-double-sign to cause a double-sign
* [double-sign] Full feedback loop working
* [slash] Thread through the slash records in the block proposal step
* [slash] Compute the slashing rate
* [double-sign] Generalize yaml malicious for many keys
* [double-sign][slash] Modify data structures, verify via webhook handler
* [slash][double-sign] Find one address of bls public key signer, seemingly settle on data structures
* [slash] Apply to state slashing for double signing
* [slash][double-sign] Checkpoint for working code that slashes on beaconchain
* [slash] Keep track of the total slash and total reporters reward
* [slash] Dump account state before and after the slash
* [slash] Satisfy Travis
* [slash][state] Apply slash to the snapshot at beginning of epoch, now need to capture also the new delegates
* [slash] Capture the unique new delegations since snapshot as well
* [slash] Filter undelegation by epoch of double sign
* [slash] Add TODO of correctness needed in slash needs on off-chain data
* [rpc] Fix closure issue on shardID
* [slash] Add delegator to double-sign testing script
* [slash] Expand crt-validator.sh with commenting printfs and make delegation
* [slash] Finish track payment of leftover slash debt after undelegation runs out
* [slash] Now be explicit about error wrt delegatorSlashApply
* [slash] Capture specific sanity check on slash paidoff
* [slash] Track slash from undelegation piecemeal
* [slash][delegation] Named slice types, .String()
* [slash] Do no RLP encode twice, once is enough
* [slash] Remove special case of validators own delegation
* [slash] Refactor approach to slash state application
* [slash] Begin expanding out Verify
* [slash] Slash on snapshot delegations, not current
* [slash] Fix Epoch Cmp
* [slash] Third iteration on slash logic
* [slash] Use full slash amount
* [slash] More log, whitespace
* [slash] Remove Println, add log
* [slash] Remove debug Println
* [slash] Add record in unit test
* [slash] Build Validator snapshot, current. Fill out slash record
* [slash] Need to get RLP dump of a header to use in test
* [slash] Factor out double sign test constants
* [slash] Factor out common for validator, stub out slash application, finish out deserialization setup
* [slash] Factor out data structure creation because of var lexical scoping
* [slash] Seem to have pipeline of unit test e2e executing
* [slash] Add expected snitch, slash amounts
* [slash] Checkpoint
* [slash] Unit test correctly checks case of validator own stake which could drop below 1 ONE in slashing
* [config] add double-sign testnet config (#1)
Signed-off-by: Leo Chen <leo@harmony.one>
* [slash] Commit for as is code & data of current dump.json
* [slash] Order of state operation not correct in test, hence bad results, thank you dlv
* [slash] Add snapshot state dump
* [slash] Pay off slash of validator own delegation correctly
* [slash] Pay off slash debt with special case for min-self
* [slash] Pass first scenario conclusively
* [slash] 2% slash passes unit test for own delegation and external
* [slash] Parameterize unit test to easily test .02 vs .80 slash
* [slash] Handle own delegation correctly at 80% slash
* [slash] Have 80% slash working with external delegator
* [slash] Remove debug code from slash
* [slash] Adjust Apply signature, test again for 2% slash
* [slash] Factor out scenario in testing so can test 2% and 80% at same time
* [slash] Correct balance deduction on plan delegation
* [slash] Mock out ChainReader for TestVerify
* [slash] Small surface area interface, now feedback loop for verify
* [slash] Remove development json
* [slash] trigger-double-sign consumes yaml
* [slash] Remove dead code
* [slash][test] Factor ValidatorWrapper into scenario
* [slash][test] Add example from local-testing dump - caution might be off
* [slash] Factor out mutation of slashDebt
* [slash][test] Factor out tests so can easily load test-case from bytes
* [slash] Fix payment mistake in validator own delegation wrt min-self-delgation respected
* [slash] Satisfy Travis
* [slash] Begin cleanup of PR
* [slash] Apply slash from header to Finalize via state processor
* [slash] Productionize code, Println => logs; adjust slash picked in newblock
* [slash] Need pointer for rlp.Decode
* [slash] ValidatorInformation use full wrapper
* Fix median stake
* [staking] Adjust MarshalJSON for Validator, Wrapper
* Refactor offchain data commit; Make block onchain/offchain commit atomic (#2279)
* Refactor offchain data; Add epoch to ValidatorSnapshot
* Make block onchain/offchain data commit atomically
* [slash][committee] Set .Active to false on double sign, do not consider banned or inactive for committee assignment
* [effective] VC eligible.go
* [consensus] Redundant field in printf
* [docker] import-ks for a dev account
* [slash] Create BLS key for dockerfile and crt-validator.sh
* [slash][docker] Easy deployment of double-sign testing
* [docker] Have slash work as single docker command
* [rpc] Fix median-stake RPC
* [slash] Update webhook with default docker BLS key
* [docker][slash] Fresh yaml copy for docker build, remove dev code in main.go
* [slash] Remove helper binary, commented out code, change to local config
* [params] Factor out test genesis value
* Add shard checking to Tx-Pool & correct blacklist (#2301)
* [core] Fix blacklist & add shardID check
* [staking + node + cmd] Fix blacklist & add shardID check
* [slash] Adjust to PR comments part 1
* [docker] Use different throw away funded account
* [docker] Create easier testing for delegation with private keys
* [docker] Update yaml
* [slash] Remove special case for slashing validator own delegation wrt min-self-delegate
* [docker] Install nano as well
* [slash] Early error if banned
* [quorum] Expose earning account in decider marshal json
* Revert "Refactor offchain data commit; Make block onchain/offchain commit atomic (#2279)"
This reverts commit 9ffbf682c075b49188923c65a0bbf39ac188be00.
* [slash] Add non-sanity check way to update validator
* [reward] Increase percision on percentage in schedule
* [slash] Adjust logs
* [committee] Check eligibility of validator before doing sanity check
* [slash] Update docker
* [slash] Move create validator script to test
* [slash] More log
* [param] Make things faster
* [slash][off-chain] Clear out slashes from pending in writeblockwithstate
* [cross-link] Log is not error, just info
* [blockchain] Not necessary to guard DeletePendingSlashingCandidates
* [slash][consensus] Use plain []byte for signature b/c bls.Sign has private impl fields, rlp does not encode that
* [slash][test] Use faucet as sender, assume user imported
* [slash] Test setup
* [slash] reserve error for real error in logs
* [slash][availability] Apply availability correct, bump signing count each block
* [slash][staking] Consider banned field in sanity check, pay snitch only half of what was actually slashed
* [slash] Pay as much as can
* [slash] use right nowAmt
* [slash] Take away from rewards as well
* [slash] iterate faster
* [slash] Remove dev based timing
* [slash] Add more log, sanity check incoming slash records, only count external for slash rate
* [availability][state] Adjust signature of ValidatorWrapper wrt state, filter out for staked validators, correct availaibility measure on running counters
* [availability] More log
* [slash] Simply pre slash erra slashing
* [slash] Remove development code
* [slash] Use height from recvMsg, todo on epoch
* [staking] Not necessary to touch LastEpochInCommittee in staking_verifier
* [slash] Undo ds in endpoint pattern config
* [slash] Add TODO and log when delegation becomes 0 b/c slash debt payment
* [slash] Abstract staked validators from shard.State into type, set slash rate based BLSKey count
Co-authored-by: Leo Chen <leo@harmony.one>
Co-authored-by: flicker-harmony <52401354+flicker-harmony@users.noreply.github.com>
Co-authored-by: Rongjian Lan <rongjian@harmony.one>
Co-authored-by: Daniel Van Der Maden <daniel@harmony.one>
5 years ago
|
|
|
if node.Blockchain().ShardID() != shard.BeaconChainShardID &&
|
|
|
|
node.NodeConfig.Role() != nodeconfig.ExplorerNode {
|
|
|
|
for _, block := range blocks {
|
|
|
|
if block.ShardID() == 0 {
|
|
|
|
utils.Logger().Info().
|
|
|
|
Uint64("block", blocks[0].NumberU64()).
|
|
|
|
Msgf("Beacon block being handled by block channel: %d", block.NumberU64())
|
|
|
|
node.BeaconBlockChannel <- block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if node.Client != nil && node.Client.UpdateBlocks != nil && blocks != nil {
|
|
|
|
utils.Logger().Info().Msg("Block being handled by client")
|
|
|
|
node.Client.UpdateBlocks(blocks)
|
|
|
|
}
|
|
|
|
}
|
[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
|
|
|
case
|
|
|
|
proto_node.SlashCandidate,
|
|
|
|
proto_node.Receipt,
|
|
|
|
proto_node.CrossLink:
|
|
|
|
// skip first byte which is blockMsgType
|
|
|
|
node.processSkippedMsgTypeByteValue(blockMsgType, msgPayload[1:])
|
|
|
|
}
|
|
|
|
case proto_node.PING:
|
|
|
|
node.pingMessageHandler(msgPayload, sender)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
utils.Logger().Error().
|
|
|
|
Str("Unknown MsgCateogry", string(msgCategory))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) transactionMessageHandler(msgPayload []byte) {
|
|
|
|
if len(msgPayload) < 1 {
|
|
|
|
utils.Logger().Debug().Msgf("Invalid transaction message size")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txMessageType := proto_node.TransactionMessageType(msgPayload[0])
|
|
|
|
|
|
|
|
switch txMessageType {
|
|
|
|
case proto_node.Send:
|
|
|
|
txs := types.Transactions{}
|
|
|
|
err := rlp.Decode(bytes.NewReader(msgPayload[1:]), &txs) // skip the Send messge type
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to deserialize transaction list")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node.addPendingTransactions(txs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) stakingMessageHandler(msgPayload []byte) {
|
|
|
|
if len(msgPayload) < 1 {
|
|
|
|
utils.Logger().Debug().Msgf("Invalid staking transaction message size")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txMessageType := proto_node.TransactionMessageType(msgPayload[0])
|
|
|
|
|
|
|
|
switch txMessageType {
|
|
|
|
case proto_node.Send:
|
|
|
|
txs := staking.StakingTransactions{}
|
|
|
|
err := rlp.Decode(bytes.NewReader(msgPayload[1:]), &txs) // skip the Send messge type
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to deserialize staking transaction list")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node.addPendingStakingTransactions(txs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastNewBlock is called by consensus leader to sync new blocks with other clients/nodes.
|
|
|
|
// NOTE: For now, just send to the client (basically not broadcasting)
|
|
|
|
// TODO (lc): broadcast the new blocks to new nodes doing state sync
|
|
|
|
func (node *Node) BroadcastNewBlock(newBlock *types.Block) {
|
|
|
|
groups := []nodeconfig.GroupID{node.NodeConfig.GetClientGroupID()}
|
|
|
|
utils.Logger().Info().Msgf("broadcasting new block %d, group %s", newBlock.NumberU64(), groups[0])
|
|
|
|
msg := host.ConstructP2pMessage(byte(0), proto_node.ConstructBlocksSyncMessage([]*types.Block{newBlock}))
|
|
|
|
if err := node.host.SendMessageToGroups(groups, msg); err != nil {
|
|
|
|
utils.Logger().Warn().Err(err).Msg("cannot broadcast new block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastSlash ..
|
|
|
|
func (node *Node) BroadcastSlash(witness *slash.Record) {
|
[double-sign] Provide proof of double sign in slash record sent to beaconchain (#2253)
* [double-sign] Commit changes in consensus needed for double-sign
* [double-sign] Leader captures when valdator double signs, broadcasts to beaconchain
* [slash] Add quick iteration tool for testing double-signing
* [slash] Add webhook example
* [slash] Add http server for hook to trigger double sign behavior
* [double-sign] Use bin/trigger-double-sign to cause a double-sign
* [double-sign] Full feedback loop working
* [slash] Thread through the slash records in the block proposal step
* [slash] Compute the slashing rate
* [double-sign] Generalize yaml malicious for many keys
* [double-sign][slash] Modify data structures, verify via webhook handler
* [slash][double-sign] Find one address of bls public key signer, seemingly settle on data structures
* [slash] Apply to state slashing for double signing
* [slash][double-sign] Checkpoint for working code that slashes on beaconchain
* [slash] Keep track of the total slash and total reporters reward
* [slash] Dump account state before and after the slash
* [slash] Satisfy Travis
* [slash][state] Apply slash to the snapshot at beginning of epoch, now need to capture also the new delegates
* [slash] Capture the unique new delegations since snapshot as well
* [slash] Filter undelegation by epoch of double sign
* [slash] Add TODO of correctness needed in slash needs on off-chain data
* [rpc] Fix closure issue on shardID
* [slash] Add delegator to double-sign testing script
* [slash] Expand crt-validator.sh with commenting printfs and make delegation
* [slash] Finish track payment of leftover slash debt after undelegation runs out
* [slash] Now be explicit about error wrt delegatorSlashApply
* [slash] Capture specific sanity check on slash paidoff
* [slash] Track slash from undelegation piecemeal
* [slash][delegation] Named slice types, .String()
* [slash] Do no RLP encode twice, once is enough
* [slash] Remove special case of validators own delegation
* [slash] Refactor approach to slash state application
* [slash] Begin expanding out Verify
* [slash] Slash on snapshot delegations, not current
* [slash] Fix Epoch Cmp
* [slash] Third iteration on slash logic
* [slash] Use full slash amount
* [slash] More log, whitespace
* [slash] Remove Println, add log
* [slash] Remove debug Println
* [slash] Add record in unit test
* [slash] Build Validator snapshot, current. Fill out slash record
* [slash] Need to get RLP dump of a header to use in test
* [slash] Factor out double sign test constants
* [slash] Factor out common for validator, stub out slash application, finish out deserialization setup
* [slash] Factor out data structure creation because of var lexical scoping
* [slash] Seem to have pipeline of unit test e2e executing
* [slash] Add expected snitch, slash amounts
* [slash] Checkpoint
* [slash] Unit test correctly checks case of validator own stake which could drop below 1 ONE in slashing
* [config] add double-sign testnet config (#1)
Signed-off-by: Leo Chen <leo@harmony.one>
* [slash] Commit for as is code & data of current dump.json
* [slash] Order of state operation not correct in test, hence bad results, thank you dlv
* [slash] Add snapshot state dump
* [slash] Pay off slash of validator own delegation correctly
* [slash] Pay off slash debt with special case for min-self
* [slash] Pass first scenario conclusively
* [slash] 2% slash passes unit test for own delegation and external
* [slash] Parameterize unit test to easily test .02 vs .80 slash
* [slash] Handle own delegation correctly at 80% slash
* [slash] Have 80% slash working with external delegator
* [slash] Remove debug code from slash
* [slash] Adjust Apply signature, test again for 2% slash
* [slash] Factor out scenario in testing so can test 2% and 80% at same time
* [slash] Correct balance deduction on plan delegation
* [slash] Mock out ChainReader for TestVerify
* [slash] Small surface area interface, now feedback loop for verify
* [slash] Remove development json
* [slash] trigger-double-sign consumes yaml
* [slash] Remove dead code
* [slash][test] Factor ValidatorWrapper into scenario
* [slash][test] Add example from local-testing dump - caution might be off
* [slash] Factor out mutation of slashDebt
* [slash][test] Factor out tests so can easily load test-case from bytes
* [slash] Fix payment mistake in validator own delegation wrt min-self-delgation respected
* [slash] Satisfy Travis
* [slash] Begin cleanup of PR
* [slash] Apply slash from header to Finalize via state processor
* [slash] Productionize code, Println => logs; adjust slash picked in newblock
* [slash] Need pointer for rlp.Decode
* [slash] ValidatorInformation use full wrapper
* Fix median stake
* [staking] Adjust MarshalJSON for Validator, Wrapper
* Refactor offchain data commit; Make block onchain/offchain commit atomic (#2279)
* Refactor offchain data; Add epoch to ValidatorSnapshot
* Make block onchain/offchain data commit atomically
* [slash][committee] Set .Active to false on double sign, do not consider banned or inactive for committee assignment
* [effective] VC eligible.go
* [consensus] Redundant field in printf
* [docker] import-ks for a dev account
* [slash] Create BLS key for dockerfile and crt-validator.sh
* [slash][docker] Easy deployment of double-sign testing
* [docker] Have slash work as single docker command
* [rpc] Fix median-stake RPC
* [slash] Update webhook with default docker BLS key
* [docker][slash] Fresh yaml copy for docker build, remove dev code in main.go
* [slash] Remove helper binary, commented out code, change to local config
* [params] Factor out test genesis value
* Add shard checking to Tx-Pool & correct blacklist (#2301)
* [core] Fix blacklist & add shardID check
* [staking + node + cmd] Fix blacklist & add shardID check
* [slash] Adjust to PR comments part 1
* [docker] Use different throw away funded account
* [docker] Create easier testing for delegation with private keys
* [docker] Update yaml
* [slash] Remove special case for slashing validator own delegation wrt min-self-delegate
* [docker] Install nano as well
* [slash] Early error if banned
* [quorum] Expose earning account in decider marshal json
* Revert "Refactor offchain data commit; Make block onchain/offchain commit atomic (#2279)"
This reverts commit 9ffbf682c075b49188923c65a0bbf39ac188be00.
* [slash] Add non-sanity check way to update validator
* [reward] Increase percision on percentage in schedule
* [slash] Adjust logs
* [committee] Check eligibility of validator before doing sanity check
* [slash] Update docker
* [slash] Move create validator script to test
* [slash] More log
* [param] Make things faster
* [slash][off-chain] Clear out slashes from pending in writeblockwithstate
* [cross-link] Log is not error, just info
* [blockchain] Not necessary to guard DeletePendingSlashingCandidates
* [slash][consensus] Use plain []byte for signature b/c bls.Sign has private impl fields, rlp does not encode that
* [slash][test] Use faucet as sender, assume user imported
* [slash] Test setup
* [slash] reserve error for real error in logs
* [slash][availability] Apply availability correct, bump signing count each block
* [slash][staking] Consider banned field in sanity check, pay snitch only half of what was actually slashed
* [slash] Pay as much as can
* [slash] use right nowAmt
* [slash] Take away from rewards as well
* [slash] iterate faster
* [slash] Remove dev based timing
* [slash] Add more log, sanity check incoming slash records, only count external for slash rate
* [availability][state] Adjust signature of ValidatorWrapper wrt state, filter out for staked validators, correct availaibility measure on running counters
* [availability] More log
* [slash] Simply pre slash erra slashing
* [slash] Remove development code
* [slash] Use height from recvMsg, todo on epoch
* [staking] Not necessary to touch LastEpochInCommittee in staking_verifier
* [slash] Undo ds in endpoint pattern config
* [slash] Add TODO and log when delegation becomes 0 b/c slash debt payment
* [slash] Abstract staked validators from shard.State into type, set slash rate based BLSKey count
Co-authored-by: Leo Chen <leo@harmony.one>
Co-authored-by: flicker-harmony <52401354+flicker-harmony@users.noreply.github.com>
Co-authored-by: Rongjian Lan <rongjian@harmony.one>
Co-authored-by: Daniel Van Der Maden <daniel@harmony.one>
5 years ago
|
|
|
if err := node.host.SendMessageToGroups(
|
|
|
|
[]nodeconfig.GroupID{nodeconfig.NewGroupIDByShardID(shard.BeaconChainShardID)},
|
|
|
|
host.ConstructP2pMessage(
|
|
|
|
byte(0),
|
|
|
|
proto_node.ConstructSlashMessage(slash.Records{*witness})),
|
|
|
|
); err != nil {
|
|
|
|
utils.Logger().Err(err).
|
|
|
|
RawJSON("record", []byte(witness.String())).
|
|
|
|
Msg("could not send slash record to beaconchain")
|
[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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastCrossLink is called by consensus leader to send the new header as cross link to beacon chain.
|
|
|
|
func (node *Node) BroadcastCrossLink(newBlock *types.Block) {
|
|
|
|
// no point to broadcast the crosslink if we aren't even in the right epoch yet
|
|
|
|
if !node.Blockchain().Config().IsCrossLink(
|
|
|
|
node.Blockchain().CurrentHeader().Epoch(),
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Logger().Info().Msgf(
|
|
|
|
"Construct and Broadcasting new crosslink to beacon chain groupID %s",
|
|
|
|
nodeconfig.NewGroupIDByShardID(shard.BeaconChainShardID),
|
|
|
|
)
|
|
|
|
headers := []*block.Header{}
|
|
|
|
lastLink, err := node.Beaconchain().ReadShardLastCrossLink(newBlock.ShardID())
|
|
|
|
var latestBlockNum uint64
|
|
|
|
|
|
|
|
// TODO chao: record the missing crosslink in local database instead of using latest crosslink
|
|
|
|
// if cannot find latest crosslink, broadcast latest 3 block headers
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Debug().Err(err).Msg("[BroadcastCrossLink] ReadShardLastCrossLink Failed")
|
|
|
|
header := node.Blockchain().GetHeaderByNumber(newBlock.NumberU64() - 2)
|
|
|
|
if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) {
|
|
|
|
headers = append(headers, header)
|
|
|
|
}
|
|
|
|
header = node.Blockchain().GetHeaderByNumber(newBlock.NumberU64() - 1)
|
|
|
|
if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) {
|
|
|
|
headers = append(headers, header)
|
|
|
|
}
|
|
|
|
headers = append(headers, newBlock.Header())
|
|
|
|
} else {
|
|
|
|
latestBlockNum = lastLink.BlockNum()
|
|
|
|
for blockNum := latestBlockNum + 1; blockNum <= newBlock.NumberU64(); blockNum++ {
|
|
|
|
header := node.Blockchain().GetHeaderByNumber(blockNum)
|
|
|
|
if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) {
|
|
|
|
headers = append(headers, header)
|
|
|
|
if len(headers) == crossLinkBatchSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Logger().Info().Msgf("[BroadcastCrossLink] Broadcasting Block Headers, latestBlockNum %d, currentBlockNum %d, Number of Headers %d", latestBlockNum, newBlock.NumberU64(), len(headers))
|
|
|
|
for _, header := range headers {
|
|
|
|
utils.Logger().Debug().Msgf(
|
|
|
|
"[BroadcastCrossLink] Broadcasting %d",
|
|
|
|
header.Number().Uint64(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
node.host.SendMessageToGroups(
|
|
|
|
[]nodeconfig.GroupID{nodeconfig.NewGroupIDByShardID(shard.BeaconChainShardID)},
|
|
|
|
host.ConstructP2pMessage(
|
|
|
|
byte(0),
|
|
|
|
proto_node.ConstructCrossLinkMessage(node.Consensus.ChainReader, headers)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyNewBlock is called by consensus participants to verify the block (account model) they are
|
|
|
|
// running consensus on
|
|
|
|
func (node *Node) VerifyNewBlock(newBlock *types.Block) error {
|
|
|
|
if err := node.Blockchain().Validator().ValidateHeader(newBlock, true); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Str("blockHash", newBlock.Hash().Hex()).
|
|
|
|
Err(err).
|
|
|
|
Msg("cannot ValidateHeader for the new block")
|
|
|
|
return ctxerror.New(
|
|
|
|
"cannot ValidateHeader for the new block",
|
|
|
|
"blockHash",
|
|
|
|
newBlock.Hash(),
|
|
|
|
).WithCause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newBlock.ShardID() != node.Blockchain().ShardID() {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Uint32("my shard ID", node.Blockchain().ShardID()).
|
|
|
|
Uint32("new block's shard ID", newBlock.ShardID()).
|
|
|
|
Msg("wrong shard ID")
|
|
|
|
return ctxerror.New("wrong shard ID",
|
|
|
|
"my shard ID", node.Blockchain().ShardID(),
|
|
|
|
"new block's shard ID", newBlock.ShardID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := node.Blockchain().Engine().VerifyShardState(
|
|
|
|
node.Blockchain(), node.Beaconchain(), newBlock.Header(),
|
|
|
|
); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Str("blockHash", newBlock.Hash().Hex()).
|
|
|
|
Err(err).
|
|
|
|
Msg("cannot VerifyShardState for the new block")
|
|
|
|
return ctxerror.New(
|
|
|
|
"cannot VerifyShardState for the new block", "blockHash",
|
|
|
|
newBlock.Hash(),
|
|
|
|
).WithCause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := node.Blockchain().ValidateNewBlock(newBlock); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Str("blockHash", newBlock.Hash().Hex()).
|
|
|
|
Int("numTx", len(newBlock.Transactions())).
|
|
|
|
Int("numStakingTx", len(newBlock.StakingTransactions())).
|
|
|
|
Err(err).
|
|
|
|
Msg("[VerifyNewBlock] Cannot Verify New Block!!!")
|
|
|
|
return ctxerror.New("[VerifyNewBlock] Cannot Verify New Block!!!",
|
|
|
|
"blockHash", newBlock.Hash(),
|
|
|
|
"numTx", len(newBlock.Transactions()),
|
|
|
|
).WithCause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify cross links
|
|
|
|
// TODO: move into ValidateNewBlock
|
|
|
|
if node.NodeConfig.ShardID == shard.BeaconChainShardID {
|
|
|
|
err := node.VerifyBlockCrossLinks(newBlock)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Debug().Err(err).Msg("ops2 VerifyBlockCrossLinks Failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move into ValidateNewBlock
|
|
|
|
if err := node.verifyIncomingReceipts(newBlock); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Str("blockHash", newBlock.Hash().Hex()).
|
|
|
|
Int("numIncomingReceipts", len(newBlock.IncomingReceipts())).
|
|
|
|
Err(err).
|
|
|
|
Msg("[VerifyNewBlock] Cannot ValidateNewBlock")
|
|
|
|
return ctxerror.New("[VerifyNewBlock] Cannot ValidateNewBlock", "blockHash", newBlock.Hash(),
|
|
|
|
"numIncomingReceipts", len(newBlock.IncomingReceipts())).WithCause(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigMaxUint64 is maximum possible uint64 value, that is, (1**64)-1.
|
|
|
|
var BigMaxUint64 = new(big.Int).SetBytes([]byte{
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
})
|
|
|
|
|
|
|
|
// PostConsensusProcessing is called by consensus participants, after consensus is done, to:
|
|
|
|
// 1. add the new block to blockchain
|
|
|
|
// 2. [leader] send new block to the client
|
|
|
|
// 3. [leader] send cross shard tx receipts to destination shard
|
|
|
|
func (node *Node) PostConsensusProcessing(
|
|
|
|
newBlock *types.Block, commitSigAndBitmap []byte,
|
|
|
|
) {
|
|
|
|
if _, err := node.Blockchain().InsertChain([]*types.Block{newBlock}, true); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Uint64("blockNum", newBlock.NumberU64()).
|
|
|
|
Str("parentHash", newBlock.Header().ParentHash().Hex()).
|
|
|
|
Str("hash", newBlock.Header().Hash().Hex()).
|
|
|
|
Msg("Error Adding new block to blockchain")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
utils.Logger().Info().
|
|
|
|
Uint64("blockNum", newBlock.NumberU64()).
|
|
|
|
Str("hash", newBlock.Header().Hash().Hex()).
|
|
|
|
Msg("Added New Block to Blockchain!!!")
|
|
|
|
|
|
|
|
// Update last consensus time for metrics
|
|
|
|
// TODO: randomly selected a few validators to broadcast messages instead of only leader broadcast
|
|
|
|
// TODO: refactor the asynchronous calls to separate go routine.
|
|
|
|
node.lastConsensusTime = time.Now().Unix()
|
|
|
|
if node.Consensus.PubKey.IsEqual(node.Consensus.LeaderPubKey) {
|
|
|
|
if node.NodeConfig.ShardID == shard.BeaconChainShardID {
|
|
|
|
node.BroadcastNewBlock(newBlock)
|
|
|
|
}
|
|
|
|
if node.NodeConfig.ShardID != shard.BeaconChainShardID &&
|
|
|
|
node.Blockchain().Config().IsCrossLink(newBlock.Epoch()) {
|
|
|
|
node.BroadcastCrossLink(newBlock)
|
|
|
|
}
|
|
|
|
node.BroadcastCXReceipts(newBlock, commitSigAndBitmap)
|
|
|
|
} else {
|
|
|
|
utils.Logger().Info().
|
|
|
|
Uint64("blockNum", newBlock.NumberU64()).
|
|
|
|
Uint64("epochNum", newBlock.Epoch().Uint64()).
|
|
|
|
Uint64("ViewId", newBlock.Header().ViewID().Uint64()).
|
|
|
|
Str("blockHash", newBlock.Hash().String()).
|
|
|
|
Int("numTxns", len(newBlock.Transactions())).
|
|
|
|
Int("numStakingTxns", len(newBlock.StakingTransactions())).
|
|
|
|
Msg("BINGO !!! Reached Consensus")
|
|
|
|
// 1% of the validator also need to do broadcasting
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
rnd := rand.Intn(100)
|
|
|
|
if rnd < 1 {
|
|
|
|
// Beacon validators also broadcast new blocks to make sure beacon sync is strong.
|
|
|
|
if node.NodeConfig.ShardID == shard.BeaconChainShardID {
|
|
|
|
node.BroadcastNewBlock(newBlock)
|
|
|
|
}
|
|
|
|
node.BroadcastCXReceipts(newBlock, commitSigAndBitmap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast client requested missing cross shard receipts if there is any
|
|
|
|
node.BroadcastMissingCXReceipts()
|
|
|
|
|
|
|
|
// Update consensus keys at last so the change of leader status doesn't mess up normal flow
|
|
|
|
if len(newBlock.Header().ShardState()) > 0 {
|
|
|
|
node.Consensus.UpdateConsensusInformation()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO chao: uncomment this after beacon syncing is stable
|
|
|
|
// node.Blockchain().UpdateCXReceiptsCheckpointsByBlock(newBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) pingMessageHandler(msgPayload []byte, sender libp2p_peer.ID) int {
|
|
|
|
ping, err := proto_discovery.GetPingMessage(msgPayload)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("Can't get Ping Message")
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
peer := new(p2p.Peer)
|
|
|
|
peer.IP = ping.Node.IP
|
|
|
|
peer.Port = ping.Node.Port
|
|
|
|
peer.PeerID = ping.Node.PeerID
|
|
|
|
peer.ConsensusPubKey = nil
|
|
|
|
|
|
|
|
if ping.Node.PubKey != nil {
|
|
|
|
peer.ConsensusPubKey = &bls.PublicKey{}
|
|
|
|
if err := peer.ConsensusPubKey.Deserialize(ping.Node.PubKey[:]); err != nil {
|
|
|
|
utils.Logger().Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("UnmarshalBinary Failed")
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Logger().Debug().
|
|
|
|
Str("Version", ping.NodeVer).
|
|
|
|
Str("BlsKey", peer.ConsensusPubKey.SerializeToHexStr()).
|
|
|
|
Str("IP", peer.IP).
|
|
|
|
Str("Port", peer.Port).
|
|
|
|
Interface("PeerID", peer.PeerID).
|
|
|
|
Msg("[PING] PeerInfo")
|
|
|
|
|
|
|
|
senderStr := string(sender)
|
|
|
|
if senderStr != "" {
|
|
|
|
_, ok := node.duplicatedPing.LoadOrStore(senderStr, true)
|
|
|
|
if ok {
|
|
|
|
// duplicated ping message return
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to incoming peer list
|
|
|
|
//node.host.AddIncomingPeer(*peer)
|
|
|
|
node.host.ConnectHostPeer(*peer)
|
|
|
|
|
|
|
|
if ping.Node.Role != proto_node.ClientRole {
|
|
|
|
node.AddPeers([]*p2p.Peer{peer})
|
|
|
|
utils.Logger().Info().
|
|
|
|
Str("Peer", peer.String()).
|
|
|
|
Int("# Peers", node.numPeers).
|
|
|
|
Msg("Add Peer to Node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// bootstrapConsensus is the a goroutine to check number of peers and start the consensus
|
|
|
|
func (node *Node) bootstrapConsensus() {
|
|
|
|
tick := time.NewTicker(5 * time.Second)
|
|
|
|
defer tick.Stop()
|
|
|
|
lastPeerNum := node.numPeers
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
numPeersNow := node.numPeers
|
|
|
|
// no peers, wait for another tick
|
|
|
|
if numPeersNow == 0 {
|
|
|
|
utils.Logger().Info().
|
|
|
|
Int("numPeersNow", numPeersNow).
|
|
|
|
Msg("No peers, continue")
|
|
|
|
continue
|
|
|
|
} else if numPeersNow > lastPeerNum {
|
|
|
|
utils.Logger().Info().
|
|
|
|
Int("previousNumPeers", lastPeerNum).
|
|
|
|
Int("numPeersNow", numPeersNow).
|
|
|
|
Int("targetNumPeers", node.Consensus.MinPeers).
|
|
|
|
Msg("New peers increased")
|
|
|
|
lastPeerNum = numPeersNow
|
|
|
|
}
|
|
|
|
|
|
|
|
if numPeersNow >= node.Consensus.MinPeers {
|
|
|
|
utils.Logger().Info().Msg("[bootstrap] StartConsensus")
|
|
|
|
node.startConsensus <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsensusMessageHandler passes received message in node_handler to consensus
|
|
|
|
func (node *Node) ConsensusMessageHandler(msgPayload []byte) {
|
|
|
|
node.Consensus.MsgChan <- msgPayload
|
|
|
|
}
|