Skip to content

Commit

Permalink
p2p, add Block Announce Network Message (#296)
Browse files Browse the repository at this point in the history
- Added BlockHeaderMessage type
- Created Encode function for BlockHeaderMessage
- Created Decode function for BlockHeaderMessage
- Created String function for BlockHeaderMessage
- Created TestEncodeBlockAnnounceMessage test
- Created TestDecodeBlockAnnounceMessage test
  • Loading branch information
edwardmack authored and ryanchristo committed Jun 24, 2020
1 parent 0886b37 commit 710b1f9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
27 changes: 27 additions & 0 deletions p2p/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,32 @@ func (bm *BlockRequestMessage) Decode(r io.Reader) error {
return nil
}

type BlockHeaderMessage common.BlockHeader

// string formats a BlockHeaderMessage as a string
func (bhm *BlockHeaderMessage) String() string {
return fmt.Sprintf("BlockHeaderMessage ParentHash=0x%x Number=%d StateRoot=0x%x ExtrinsicsRoot=0x%x Digest=0x%x",
bhm.ParentHash,
bhm.Number,
bhm.StateRoot,
bhm.ExtrinsicsRoot,
bhm.Digest)
}

func (bhm *BlockHeaderMessage) Encode() ([]byte, error) {
enc, err := scale.Encode(bhm)
if err != nil {
return enc, err
}
return append([]byte{BlockAnnounceMsgType}, enc...), nil
}

//Decodes the message into a BlockHeaderMessage, it assumes the type byte has been removed
func (bhm *BlockHeaderMessage) Decode(msg []byte) error {
_, err := scale.Decode(msg, bhm)
return err
}

type BlockResponseMessage struct {
Id uint64
Data []byte // TODO: change this to BlockData type
Expand Down Expand Up @@ -333,4 +359,5 @@ func readHash(r io.Reader) (common.Hash, error) {
h := [32]byte{}
copy(h[:], buf)
return common.Hash(h), nil

}
84 changes: 84 additions & 0 deletions p2p/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package p2p

import (
"bytes"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -340,6 +341,7 @@ func TestEncodeBlockRequestMessage_NoOptionals(t *testing.T) {

func TestDecodeBlockRequestMessage_NoOptionals(t *testing.T) {
encMsg, err := common.HexToBytes("0x0107000000000000000100dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b000100")

if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -374,6 +376,7 @@ func TestDecodeBlockRequestMessage_NoOptionals(t *testing.T) {

func TestEncodeBlockResponseMessage(t *testing.T) {
expected, err := common.HexToBytes("0x02070000000000000000")

if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -417,3 +420,84 @@ func TestDecodeBlockResponseMessage(t *testing.T) {
t.Fatalf("Fail: got %v expected %v", m, expected)
}
}

func TestEncodeBlockAnnounceMessage(t *testing.T) {
// this value is a concatenation of:
// message type: byte(3)
// ParentHash: Hash: 0x4545454545454545454545454545454545454545454545454545454545454545
// Number: *big.Int // block number: 1
// StateRoot: Hash: 0xb3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe0
// ExtrinsicsRoot: Hash: 0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314
// Digest: []byte

// mtparenthash bnstateroot extrinsicsroot di
expected, err := common.HexToBytes("0x03454545454545454545454545454545454545454545454545454545454545454504b3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe003170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400")
if err != nil {
t.Fatal(err)
}

parentHash, err := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545")
if err != nil {
t.Fatal(err)
}
stateRoot, err := common.HexToHash("0xb3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe0")
if err != nil {
t.Fatal(err)
}
extrinsicsRoot, err := common.HexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")
if err != nil {
t.Fatal(err)
}

bhm := &BlockHeaderMessage{
ParentHash: parentHash,
Number: big.NewInt(1),
StateRoot: stateRoot,
ExtrinsicsRoot: extrinsicsRoot,
Digest: []byte{},
}
encMsg, err := bhm.Encode()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(encMsg, expected) {
t.Fatalf("Fail: got %x expected %x", encMsg, expected)
}
}

func TestDecodeBlockAnnounceMessage(t *testing.T) {
announceMessage, err := common.HexToBytes("0x454545454545454545454545454545454545454545454545454545454545454504b3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe003170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400")
if err != nil {
t.Fatal(err)
}

bhm := new(BlockHeaderMessage)
err = bhm.Decode(announceMessage)
if err != nil {
t.Fatal(err)
}

parentHash, err := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545")
if err != nil {
t.Fatal(err)
}
stateRoot, err := common.HexToHash("0xb3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe0")
if err != nil {
t.Fatal(err)
}
extrinsicsRoot, err := common.HexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")
if err != nil {
t.Fatal(err)
}
expected := &BlockHeaderMessage{
ParentHash: parentHash,
Number: big.NewInt(1),
StateRoot: stateRoot,
ExtrinsicsRoot: extrinsicsRoot,
Digest: []byte{},
}

if !reflect.DeepEqual(bhm, expected) {
t.Fatalf("Fail: got %v expected %v", bhm, expected)
}
}

0 comments on commit 710b1f9

Please sign in to comment.