fix style, change constant

pull/75/head
Minh Doan 6 years ago
parent ef2521e068
commit b77dd1a7cb
  1. 8
      node/node_handler.go
  2. 20
      proto/common.go
  3. 8
      proto/node/node.go

@ -243,7 +243,7 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) {
node.log.Error("Failed to deserialize transaction list", "error", err) node.log.Error("Failed to deserialize transaction list", "error", err)
} }
node.addPendingTransactions(*txList) node.addPendingTransactions(*txList)
case proto_node.REQUEST: case proto_node.Request:
reader := bytes.NewBuffer(msgPayload[1:]) reader := bytes.NewBuffer(msgPayload[1:])
var txIDs map[[32]byte]bool var txIDs map[[32]byte]bool
buf := make([]byte, 32) // 32 byte hash Id buf := make([]byte, 32) // 32 byte hash Id
@ -265,15 +265,15 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) {
} }
} }
// TODO: return the transaction list to requester // TODO: return the transaction list to requester
case proto_node.UNLOCK: case proto_node.Unlock:
txAndProofDecoder := gob.NewDecoder(bytes.NewReader(msgPayload[1:])) // skip the UNLOCK messge type txAndProofDecoder := gob.NewDecoder(bytes.NewReader(msgPayload[1:])) // skip the Unlock messge type
txAndProofs := new([]*blockchain.Transaction) txAndProofs := new([]*blockchain.Transaction)
err := txAndProofDecoder.Decode(&txAndProofs) err := txAndProofDecoder.Decode(&txAndProofs)
if err != nil { if err != nil {
node.log.Error("Failed deserializing transaction and proofs list", "node", node) node.log.Error("Failed deserializing transaction and proofs list", "node", node)
} }
node.log.Debug("RECEIVED UNLOCK MESSAGE", "num", len(*txAndProofs)) node.log.Debug("RECEIVED Unlock MESSAGE", "num", len(*txAndProofs))
node.addPendingTransactions(*txAndProofs) node.addPendingTransactions(*txAndProofs)
} }

@ -33,32 +33,32 @@ const (
// TODO: add more types // TODO: add more types
) )
// MESSAGE_CATEGORY_BYTES is the number of bytes message category takes // MessageCategoryBytes is the number of bytes message category takes
const MESSAGE_CATEGORY_BYTES = 1 const MessageCategoryBytes = 1
// MESSAGE_TYPE_BYTES is the number of bytes message type takes // MessageTypeBytes is the number of bytes message type takes
const MESSAGE_TYPE_BYTES = 1 const MessageTypeBytes = 1
// Get the message category from the p2p message content // Get the message category from the p2p message content
func GetMessageCategory(message []byte) (MessageCategory, error) { func GetMessageCategory(message []byte) (MessageCategory, error) {
if len(message) < MESSAGE_CATEGORY_BYTES { if len(message) < MessageCategoryBytes {
return 0, errors.New("Failed to get message category: no data available.") return 0, errors.New("Failed to get message category: no data available.")
} }
return MessageCategory(message[MESSAGE_CATEGORY_BYTES-1]), nil return MessageCategory(message[MessageCategoryBytes-1]), nil
} }
// Get the message type from the p2p message content // Get the message type from the p2p message content
func GetMessageType(message []byte) (byte, error) { func GetMessageType(message []byte) (byte, error) {
if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES { if len(message) < MessageCategoryBytes+MessageTypeBytes {
return 0, errors.New("Failed to get message type: no data available.") return 0, errors.New("Failed to get message type: no data available.")
} }
return byte(message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES-1]), nil return byte(message[MessageCategoryBytes+MessageTypeBytes-1]), nil
} }
// Get the node message payload from the p2p message content // Get the node message payload from the p2p message content
func GetMessagePayload(message []byte) ([]byte, error) { func GetMessagePayload(message []byte) ([]byte, error) {
if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES { if len(message) < MessageCategoryBytes+MessageTypeBytes {
return []byte{}, errors.New("Failed to get message payload: no data available.") return []byte{}, errors.New("Failed to get message payload: no data available.")
} }
return message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES:], nil return message[MessageCategoryBytes+MessageTypeBytes:], nil
} }

@ -42,8 +42,8 @@ type TransactionMessageType int
const ( const (
Send TransactionMessageType = iota Send TransactionMessageType = iota
REQUEST Request
UNLOCK Unlock
) )
// BlockMessageType represents the types of messages used for NODE/BLOCK // BlockMessageType represents the types of messages used for NODE/BLOCK
@ -101,7 +101,7 @@ func DeserializeBlockchainSyncMessage(d []byte) (*BlockchainSyncMessage, error)
func ConstructUnlockToCommitOrAbortMessage(txsAndProofs []*blockchain.Transaction) []byte { func ConstructUnlockToCommitOrAbortMessage(txsAndProofs []*blockchain.Transaction) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)}) byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(Transaction)) byteBuffer.WriteByte(byte(Transaction))
byteBuffer.WriteByte(byte(UNLOCK)) byteBuffer.WriteByte(byte(Unlock))
encoder := gob.NewEncoder(byteBuffer) encoder := gob.NewEncoder(byteBuffer)
encoder.Encode(txsAndProofs) encoder.Encode(txsAndProofs)
return byteBuffer.Bytes() return byteBuffer.Bytes()
@ -161,7 +161,7 @@ func GenerateBlockchainSyncMessage(payload []byte) *BlockchainSyncMessage {
func ConstructRequestTransactionsMessage(transactionIds [][]byte) []byte { func ConstructRequestTransactionsMessage(transactionIds [][]byte) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)}) byteBuffer := bytes.NewBuffer([]byte{byte(proto.NODE)})
byteBuffer.WriteByte(byte(Transaction)) byteBuffer.WriteByte(byte(Transaction))
byteBuffer.WriteByte(byte(REQUEST)) byteBuffer.WriteByte(byte(Request))
for _, txID := range transactionIds { for _, txID := range transactionIds {
byteBuffer.Write(txID) byteBuffer.Write(txID)
} }

Loading…
Cancel
Save