Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p, add Block Announce Network Message #296

Merged
merged 8 commits into from
Sep 18, 2019
28 changes: 27 additions & 1 deletion p2p/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io"

scale "github.com/ChainSafe/gossamer/codec"
common "github.com/ChainSafe/gossamer/common"
"github.com/ChainSafe/gossamer/common"
)

const (
Expand Down Expand Up @@ -172,3 +172,29 @@ func (bm *BlockRequestMessage) Encode() ([]byte, error) {
func (bm *BlockRequestMessage) Decode(msg []byte) 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{BlockAnnounceMsg}, enc...), nil
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
}

//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
}
79 changes: 79 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 @@ -257,3 +258,81 @@ func TestEncodeBlockRequestMessage_NoOptionals(t *testing.T) {
t.Fatalf("Fail: got %x expected %x", encMsg, 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()
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}