From 47bab858947054fc35fa26393a6173032b90db0f Mon Sep 17 00:00:00 2001 From: chao Date: Tue, 6 Aug 2019 17:33:02 -0700 Subject: [PATCH] add message type and message handler placeholder for cross shard transactions --- api/proto/node/node.go | 4 +++- node/node_cross_shard.go | 31 +++++++++++++++++++++++++++++++ node/node_handler.go | 17 ++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 node/node_cross_shard.go diff --git a/api/proto/node/node.go b/api/proto/node/node.go index 3ee002b6c..756842f24 100644 --- a/api/proto/node/node.go +++ b/api/proto/node/node.go @@ -27,7 +27,6 @@ const ( PING // node send ip/pki to register with leader PONG // node broadcast pubK ShardState - // TODO: add more types ) // BlockchainSyncMessage is a struct for blockchain sync message. @@ -96,6 +95,9 @@ type BlockMessageType int // Block sync message subtype const ( Sync BlockMessageType = iota + + Header // used for crosslink from beacon chain to shard chain + Receipt // cross-shard transaction receipts ) // SerializeBlockchainSyncMessage serializes BlockchainSyncMessage. diff --git a/node/node_cross_shard.go b/node/node_cross_shard.go new file mode 100644 index 000000000..364af0ebd --- /dev/null +++ b/node/node_cross_shard.go @@ -0,0 +1,31 @@ +package node + +import ( + "github.com/ethereum/go-ethereum/rlp" + + "github.com/harmony-one/harmony/core/types" + "github.com/harmony-one/harmony/internal/utils" +) + +// ProcessHeaderMessage verify and process Node/Header message into crosslink when it's valid +func (node *Node) ProcessHeaderMessage(msgPayload []byte) { + var headers []*types.Header + err := rlp.DecodeBytes(msgPayload, &headers) + if err != nil { + utils.Logger().Error(). + Err(err). + Msg("Crosslink Headers Broadcast Unable to Decode") + return + } + // TODO: add actual logic +} + +// ProcessReceiptMessage store the receipts and merkle proof in local data store +func (node *Node) ProcessReceiptMessage(msgPayload []byte) { + // TODO: add logic +} + +// ProcessCrossShardTx verify and process cross shard transaction on destination shard +func (node *Node) ProcessCrossShardTx(blocks []*types.Block) { + // TODO: add logic +} diff --git a/node/node_handler.go b/node/node_handler.go index d2113f7aa..c47546d34 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -171,11 +171,13 @@ func (node *Node) messageHandler(content []byte, sender libp2p_peer.ID) { } else { // for non-beaconchain node, subscribe to beacon block broadcast role := node.NodeConfig.Role() - if proto_node.BlockMessageType(msgPayload[0]) == proto_node.Sync && role == nodeconfig.Validator { + if role == nodeconfig.Validator { utils.Logger().Info(). Uint64("block", blocks[0].NumberU64()). Msg("Block being handled by block channel") + go node.ProcessCrossShardTx(blocks) + for _, block := range blocks { node.BeaconBlockChannel <- block } @@ -185,6 +187,19 @@ func (node *Node) messageHandler(content []byte, sender libp2p_peer.ID) { node.Client.UpdateBlocks(blocks) } } + + case proto_node.Header: + // only beacon chain will accept the header from other shards + if node.Consensus.ShardID != 0 { + return + } + utils.Logger().Debug().Msg("NET: received message: Node/Header") + node.ProcessHeaderMessage(msgPayload[1:]) // skip first byte which is blockMsgType + + case proto_node.Receipt: + utils.Logger().Debug().Msg("NET: received message: Node/Receipt") + node.ProcessReceiptMessage(msgPayload[1:]) // skip first byte which is blockMsgType + } case proto_node.PING: node.pingMessageHandler(msgPayload, sender)