From c7c8267747032c87b1cd761a6f6e609181d321cd Mon Sep 17 00:00:00 2001 From: edwardmack Date: Mon, 16 Sep 2019 16:24:15 -0400 Subject: [PATCH 1/6] created BlockHeaderMessage and test for testing encoding --- p2p/message.go | 26 +++++++++++++++++++++++--- p2p/message_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/p2p/message.go b/p2p/message.go index 0d782d5c95..611a13894b 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -20,10 +20,9 @@ import ( "encoding/binary" "errors" "fmt" - "io" - scale "github.com/ChainSafe/gossamer/codec" - common "github.com/ChainSafe/gossamer/common" + "github.com/ChainSafe/gossamer/common" + "io" ) const ( @@ -172,3 +171,24 @@ 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=%x Number=%d StateRoot=%x ExtrinsicsRoot=%x Digest=%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 +} \ No newline at end of file diff --git a/p2p/message_test.go b/p2p/message_test.go index 3eedd8644f..cc3dc54f5c 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -18,6 +18,7 @@ package p2p import ( "bytes" + "math/big" "reflect" "testing" @@ -257,3 +258,44 @@ 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() + if !bytes.Equal(encMsg, expected) { + t.Fatalf("Fail: got %x expected %x", encMsg, expected) + } +} \ No newline at end of file From 2a71cba3f6dd354fee7a55188105e510e254b6f5 Mon Sep 17 00:00:00 2001 From: edwardmack Date: Tue, 17 Sep 2019 10:46:45 -0400 Subject: [PATCH 2/6] added BlockHeaderMessage.Decode() function and tests --- p2p/message.go | 12 +++++++++--- p2p/message_test.go | 47 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/p2p/message.go b/p2p/message.go index 611a13894b..4dea7046f1 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -20,9 +20,10 @@ import ( "encoding/binary" "errors" "fmt" + "io" + scale "github.com/ChainSafe/gossamer/codec" "github.com/ChainSafe/gossamer/common" - "io" ) const ( @@ -172,7 +173,6 @@ func (bm *BlockRequestMessage) Decode(msg []byte) error { return nil } - type BlockHeaderMessage common.BlockHeader // string formats a BlockHeaderMessage as a string @@ -191,4 +191,10 @@ func (bhm *BlockHeaderMessage) Encode() ([]byte, error) { return enc, err } return append([]byte{BlockAnnounceMsg}, enc...), nil -} \ No newline at end of file +} + +//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 +} diff --git a/p2p/message_test.go b/p2p/message_test.go index cc3dc54f5c..3d27a7cabc 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -288,14 +288,51 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) { } bhm := &BlockHeaderMessage{ - ParentHash: parentHash, - Number: big.NewInt(1), - StateRoot: stateRoot, + ParentHash: parentHash, + Number: big.NewInt(1), + StateRoot: stateRoot, ExtrinsicsRoot: extrinsicsRoot, - Digest: []byte{}, + Digest: []byte{}, } encMsg, err := bhm.Encode() if !bytes.Equal(encMsg, expected) { t.Fatalf("Fail: got %x expected %x", encMsg, expected) } -} \ No newline at end of file +} + +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) + } +} From 32fdd993cf3313b5cb96d95b36074d764d872964 Mon Sep 17 00:00:00 2001 From: edwardmack Date: Tue, 17 Sep 2019 10:54:06 -0400 Subject: [PATCH 3/6] cleanup string formatting --- p2p/message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/message.go b/p2p/message.go index 4dea7046f1..b9b1226f55 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -177,7 +177,7 @@ type BlockHeaderMessage common.BlockHeader // string formats a BlockHeaderMessage as a string func (bhm *BlockHeaderMessage) String() string { - return fmt.Sprintf("BlockHeaderMessage ParentHash=%x Number=%d StateRoot=%x ExtrinsicsRoot=%x Digest=%x", + return fmt.Sprintf("BlockHeaderMessage ParentHash=0x%x Number=%d StateRoot=0x%x ExtrinsicsRoot=0x%x Digest=0x%x", bhm.ParentHash, bhm.Number, bhm.StateRoot, From ff268ad23361dfba3637a02db3d48baa1923b07a Mon Sep 17 00:00:00 2001 From: edwardmack Date: Tue, 17 Sep 2019 11:06:13 -0400 Subject: [PATCH 4/6] fixed ineffectual assignment --- p2p/message_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/p2p/message_test.go b/p2p/message_test.go index 3d27a7cabc..7f471728f5 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -295,6 +295,9 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) { 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) } From df262affd5c69282e2238add094af9b62daf2b8a Mon Sep 17 00:00:00 2001 From: edwardmack Date: Wed, 18 Sep 2019 14:32:38 -0400 Subject: [PATCH 5/6] merge refactor --- p2p/message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/message.go b/p2p/message.go index 23f24f9e54..699c7ecdd4 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -274,7 +274,7 @@ func (bhm *BlockHeaderMessage) Encode() ([]byte, error) { if err != nil { return enc, err } - return append([]byte{BlockAnnounceMsg}, enc...), nil + return append([]byte{BlockAnnounceMsgType}, enc...), nil } //Decodes the message into a BlockHeaderMessage, it assumes the type byte has been removed From 652d3d2f971b948f6c117ed471bde7fb94ccffb9 Mon Sep 17 00:00:00 2001 From: edwardmack Date: Wed, 18 Sep 2019 14:36:56 -0400 Subject: [PATCH 6/6] did goimported --- p2p/message.go | 1 - p2p/message_test.go | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/p2p/message.go b/p2p/message.go index 699c7ecdd4..6479b3ba34 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -256,7 +256,6 @@ func (bm *BlockRequestMessage) Decode(r io.Reader) error { return nil } - type BlockHeaderMessage common.BlockHeader // string formats a BlockHeaderMessage as a string diff --git a/p2p/message_test.go b/p2p/message_test.go index 29f8d50b89..3e0538398e 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -344,9 +344,9 @@ func TestDecodeBlockRequestMessage_NoOptionals(t *testing.T) { if err != nil { t.Fatal(err) - } + } - buf := &bytes.Buffer{} + buf := &bytes.Buffer{} buf.Write(encMsg) m, err := DecodeMessage(buf) @@ -373,14 +373,14 @@ func TestDecodeBlockRequestMessage_NoOptionals(t *testing.T) { t.Fatalf("Fail: got %v expected %v", bm, expected) } } - + func TestEncodeBlockResponseMessage(t *testing.T) { expected, err := common.HexToBytes("0x02070000000000000000") if err != nil { t.Fatal(err) } - + bm := &BlockResponseMessage{ Id: 7, Data: []byte{0}, @@ -395,13 +395,13 @@ func TestEncodeBlockResponseMessage(t *testing.T) { t.Fatalf("Fail: got %x expected %x", encMsg, expected) } } - + func TestDecodeBlockResponseMessage(t *testing.T) { encMsg, err := common.HexToBytes("0x070000000000000000") if err != nil { t.Fatal(err) } - + buf := &bytes.Buffer{} buf.Write(encMsg) @@ -435,7 +435,7 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) { if err != nil { t.Fatal(err) } - + parentHash, err := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545") if err != nil { t.Fatal(err) @@ -445,7 +445,7 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) { t.Fatal(err) } extrinsicsRoot, err := common.HexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314") - if err != nil { + if err != nil { t.Fatal(err) } @@ -460,7 +460,7 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) { if err != nil { t.Fatal(err) } - if !bytes.Equal(encMsg, expected) { + if !bytes.Equal(encMsg, expected) { t.Fatalf("Fail: got %x expected %x", encMsg, expected) } } @@ -499,5 +499,5 @@ func TestDecodeBlockAnnounceMessage(t *testing.T) { if !reflect.DeepEqual(bhm, expected) { t.Fatalf("Fail: got %v expected %v", bhm, expected) - } + } }