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/proto/node/node.go

195 lines
5.4 KiB

package node
import (
"bytes"
"encoding/gob"
"log"
"github.com/harmony-one/harmony/blockchain"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/proto"
)
// NodeMessageType is to indicate the specific type of message under NODE category
type NodeMessageType byte
const (
PROTOCOL_VERSION = 1
)
const (
Transaction NodeMessageType = iota
BLOCK
CLIENT
CONTROL
BlockchainSync
PING // node send ip/pki to register with leader
PONG // node broadcast pubK
// TODO: add more types
)
// BlockchainSyncMessage is a struct for blockchain sync message.
type BlockchainSyncMessage struct {
BlockHeight int
BlockHashes [][32]byte
}
// BlockchainSyncMessageType represents BlockchainSyncMessageType type.
type BlockchainSyncMessageType int
const (
Done BlockchainSyncMessageType = iota
GetLastBlockHashes
GetBlock
)
// TransactionMessageType representa the types of messages used for NODE/Transaction
type TransactionMessageType int
const (
Send TransactionMessageType = iota
Request
Unlock
)
// BlockMessageType represents the types of messages used for NODE/BLOCK
type BlockMessageType int
const (
Sync BlockMessageType = iota
)
// The types of messages used for NODE/BLOCK
type ClientMessageType int
const (
LookupUtxo ClientMessageType = iota
)
// The types of messages used for NODE/CONTROL
type ControlMessageType int
6 years ago
// ControlMessageType
const (
STOP ControlMessageType = iota
)
6 years ago
// FetchUtxoMessage is the wrapper struct FetchUtxoMessage sent from client wallet.
type FetchUtxoMessage struct {
Addresses [][20]byte
Sender p2p.Peer
}
6 years ago
// SerializeBlockchainSyncMessage serializes BlockchainSyncMessage.
func SerializeBlockchainSyncMessage(blockchainSyncMessage *BlockchainSyncMessage) []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(blockchainSyncMessage)
if err != nil {
log.Panic(err)
}
return result.Bytes()
}
6 years ago
// DeserializeBlockchainSyncMessage deserializes BlockchainSyncMessage.
func DeserializeBlockchainSyncMessage(d []byte) (*BlockchainSyncMessage, error) {
var blockchainSyncMessage BlockchainSyncMessage
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&blockchainSyncMessage)
if err != nil {
log.Panic(err)
}
return &blockchainSyncMessage, err
}
6 years ago
// ConstructUnlockToCommitOrAbortMessage constructs the unlock to commit or abort message that will be sent to leaders.
// This is for client.
func ConstructUnlockToCommitOrAbortMessage(txsAndProofs []*blockchain.Transaction) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(Transaction))
byteBuffer.WriteByte(byte(Unlock))
encoder := gob.NewEncoder(byteBuffer)
encoder.Encode(txsAndProofs)
return byteBuffer.Bytes()
}
6 years ago
// ConstructFetchUtxoMessage constructs the fetch utxo message that will be sent to Harmony network.
// this is for client.
func ConstructFetchUtxoMessage(sender p2p.Peer, addresses [][20]byte) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(CLIENT))
byteBuffer.WriteByte(byte(LookupUtxo))
encoder := gob.NewEncoder(byteBuffer)
encoder.Encode(FetchUtxoMessage{Addresses: addresses, Sender: sender})
return byteBuffer.Bytes()
}
6 years ago
// ConstructTransactionListMessage constructs serialized transactions
func ConstructTransactionListMessage(transactions []*blockchain.Transaction) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(Transaction))
byteBuffer.WriteByte(byte(Send))
encoder := gob.NewEncoder(byteBuffer)
// Copy over the tx data
txs := make([]blockchain.Transaction, len(transactions))
for i := range txs {
txs[i] = *transactions[i]
}
err := encoder.Encode(txs)
if err != nil {
return []byte{} // TODO(RJ): better handle of the error
}
return byteBuffer.Bytes()
}
6 years ago
// ConstructBlockchainSyncMessage constructs Blockchain Sync Message.
func ConstructBlockchainSyncMessage(msgType BlockchainSyncMessageType, blockHash [32]byte) []byte {
6 years ago
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(BlockchainSync))
byteBuffer.WriteByte(byte(msgType))
if msgType != GetLastBlockHashes {
byteBuffer.Write(blockHash[:])
}
6 years ago
return byteBuffer.Bytes()
}
6 years ago
// GenerateBlockchainSyncMessage generates blockchain sync message.
func GenerateBlockchainSyncMessage(payload []byte) *BlockchainSyncMessage {
dec := gob.NewDecoder(bytes.NewBuffer(payload))
var res BlockchainSyncMessage
dec.Decode(&res)
return &res
}
6 years ago
// ConstructRequestTransactionsMessage constructs serialized transactions
func ConstructRequestTransactionsMessage(transactionIds [][]byte) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(Transaction))
byteBuffer.WriteByte(byte(Request))
for _, txID := range transactionIds {
byteBuffer.Write(txID)
}
return byteBuffer.Bytes()
}
6 years ago
// ConstructStopMessage constructs STOP message for node to stop
func ConstructStopMessage() []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(CONTROL))
byteBuffer.WriteByte(byte(STOP))
return byteBuffer.Bytes()
}
6 years ago
// ConstructBlocksSyncMessage constructs blocks sync message to send blocks to other nodes
func ConstructBlocksSyncMessage(blocks []blockchain.Block) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(BLOCK))
byteBuffer.WriteByte(byte(Sync))
encoder := gob.NewEncoder(byteBuffer)
encoder.Encode(blocks)
return byteBuffer.Bytes()
}