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

chore (pkg/scale) integrate scale into dot/network and dot/digest #1685

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
73c29bb
integrate scale into transaction.go
jimjbrettj Jul 7, 2021
ee794d5
integrate scale into light.go
jimjbrettj Jul 7, 2021
5500183
integrate scale into dot/digest and consensus_digest
jimjbrettj Jul 7, 2021
515f049
WIP/integrate scale into block announce in network
jimjbrettj Jul 7, 2021
5cf0caf
integrate scale into block announce
jimjbrettj Jul 8, 2021
20deecc
integrate scale into network
jimjbrettj Jul 8, 2021
14cf450
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 8, 2021
0af2398
clean up code
jimjbrettj Jul 8, 2021
14c532f
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 8, 2021
4b0b681
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 9, 2021
0133bda
clean up
jimjbrettj Jul 9, 2021
e0f8d08
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 9, 2021
b7223a7
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 12, 2021
dbae23b
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 12, 2021
f092850
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 14, 2021
f8cb24b
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 14, 2021
e1a0898
fix block announce method encoding
jimjbrettj Jul 14, 2021
ecafc47
fix handshake encoding
jimjbrettj Jul 14, 2021
9e9dd44
lint
jimjbrettj Jul 14, 2021
6b7c6a5
update consensus digest
jimjbrettj Jul 14, 2021
4ce2cc8
update light messages
jimjbrettj Jul 15, 2021
e036a26
fix digest encoding
jimjbrettj Jul 15, 2021
772bb8f
Merge branch 'development' into jimmy/integrateScaleNetwork
jimjbrettj Jul 15, 2021
a3cf779
undo digest changes
jimjbrettj Jul 15, 2021
14be893
Merge branch 'jimmy/integrateScaleNetwork' of github.com:ChainSafe/go…
jimjbrettj Jul 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions dot/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"math/big"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/pkg/scale"

log "github.com/ChainSafe/log15"
)
Expand Down Expand Up @@ -298,12 +298,11 @@ func (h *Handler) handleScheduledChange(d *types.ConsensusDigest, header *types.
return nil
}

sc := &types.GrandpaScheduledChange{}
dec, err := scale.Decode(d.Data[1:], sc)
var sc *types.GrandpaScheduledChange
err = scale.Unmarshal(d.Data[1:], &sc)
if err != nil {
return err
}
sc = dec.(*types.GrandpaScheduledChange)

logger.Debug("handling GrandpaScheduledChange", "data", sc)

Expand Down Expand Up @@ -339,12 +338,11 @@ func (h *Handler) handleForcedChange(d *types.ConsensusDigest, header *types.Hea
return errors.New("already have forced change scheduled")
}

fc := &types.GrandpaForcedChange{}
dec, err := scale.Decode(d.Data[1:], fc)
var fc *types.GrandpaForcedChange
err := scale.Unmarshal(d.Data[1:], &fc)
if err != nil {
return err
}
fc = dec.(*types.GrandpaForcedChange)

logger.Debug("handling GrandpaForcedChange", "data", fc)

Expand Down Expand Up @@ -373,12 +371,11 @@ func (h *Handler) handlePause(d *types.ConsensusDigest) error {
return err
}

p := &types.GrandpaPause{}
dec, err := scale.Decode(d.Data[1:], p)
var p *types.GrandpaPause
err = scale.Unmarshal(d.Data[1:], &p)
if err != nil {
return err
}
p = dec.(*types.GrandpaPause)

delay := big.NewInt(int64(p.Delay))

Expand All @@ -395,12 +392,11 @@ func (h *Handler) handleResume(d *types.ConsensusDigest) error {
return err
}

p := &types.GrandpaResume{}
dec, err := scale.Decode(d.Data[1:], p)
var p *types.GrandpaResume
err = scale.Unmarshal(d.Data[1:], &p)
if err != nil {
return err
}
p = dec.(*types.GrandpaResume)

delay := big.NewInt(int64(p.Delay))

Expand Down Expand Up @@ -432,12 +428,11 @@ func (h *Handler) handleBABEOnDisabled(d *types.ConsensusDigest, _ *types.Header
}

func (h *Handler) handleNextEpochData(d *types.ConsensusDigest, header *types.Header) error {
od := &types.NextEpochData{}
dec, err := scale.Decode(d.Data[1:], od)
var od *types.NextEpochData
err := scale.Unmarshal(d.Data[1:], &od)
if err != nil {
return err
}
od = dec.(*types.NextEpochData)

logger.Debug("handling BABENextEpochData", "data", od)

Expand All @@ -457,12 +452,11 @@ func (h *Handler) handleNextEpochData(d *types.ConsensusDigest, header *types.He
}

func (h *Handler) handleNextConfigData(d *types.ConsensusDigest, header *types.Header) error {
od := &types.NextConfigData{}
dec, err := scale.Decode(d.Data[1:], od)
var od *types.NextConfigData
err := scale.Unmarshal(d.Data[1:], &od)
if err != nil {
return err
}
od = dec.(*types.NextConfigData)

logger.Debug("handling BABENextConfigData", "data", od)

Expand Down
44 changes: 12 additions & 32 deletions dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
package network

import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/pkg/scale"

"github.com/libp2p/go-libp2p-core/peer"
)
Expand Down Expand Up @@ -66,7 +65,7 @@ func (bm *BlockAnnounceMessage) String() string {

// Encode a BlockAnnounce Msg Type containing the BlockAnnounceMessage using scale.Encode
func (bm *BlockAnnounceMessage) Encode() ([]byte, error) {
enc, err := scale.Encode(bm)
enc, err := scale.Marshal(*bm)
if err != nil {
return enc, err
}
Expand All @@ -75,31 +74,16 @@ func (bm *BlockAnnounceMessage) Encode() ([]byte, error) {

// Decode the message into a BlockAnnounceMessage
func (bm *BlockAnnounceMessage) Decode(in []byte) error {
r := &bytes.Buffer{}
_, _ = r.Write(in)
h, err := types.NewEmptyHeader().Decode(r)
err := scale.Unmarshal(in, bm)
if err != nil {
return err
}

bm.ParentHash = h.ParentHash
bm.Number = h.Number
bm.StateRoot = h.StateRoot
bm.ExtrinsicsRoot = h.ExtrinsicsRoot
bm.Digest = h.Digest
bestBlock, err := common.ReadByte(r)
if err != nil {
return err
}

bm.BestBlock = bestBlock == 1
return nil
}

// Hash returns the hash of the BlockAnnounceMessage
func (bm *BlockAnnounceMessage) Hash() common.Hash {
// scale encode each extrinsic
encMsg, _ := bm.Encode()
encMsg, _ := scale.Marshal(*bm)
hash, _ := common.Blake2bHash(encMsg)
return hash
}
Expand All @@ -110,22 +94,23 @@ func (bm *BlockAnnounceMessage) IsHandshake() bool {
}

func decodeBlockAnnounceHandshake(in []byte) (Handshake, error) {
hs, err := scale.Decode(in, new(BlockAnnounceHandshake))
var hs BlockAnnounceHandshake
err := scale.Unmarshal(in, &hs)
if err != nil {
return nil, err
}

return hs.(*BlockAnnounceHandshake), err
return &hs, err
}

func decodeBlockAnnounceMessage(in []byte) (NotificationsMessage, error) {
msg := new(BlockAnnounceMessage)
err := msg.Decode(in)
var msg BlockAnnounceMessage
err := scale.Unmarshal(in, &msg)
if err != nil {
return nil, err
}

return msg, nil
return &msg, nil
}

// BlockAnnounceHandshake is exchanged by nodes that are beginning the BlockAnnounce protocol
Expand All @@ -152,20 +137,15 @@ func (hs *BlockAnnounceHandshake) String() string {

// Encode encodes a BlockAnnounceHandshake message using SCALE
func (hs *BlockAnnounceHandshake) Encode() ([]byte, error) {
return scale.Encode(hs)
return scale.Marshal(*hs)
}

// Decode the message into a BlockAnnounceHandshake
func (hs *BlockAnnounceHandshake) Decode(in []byte) error {
msg, err := scale.Decode(in, hs)
err := scale.Unmarshal(in, hs)
if err != nil {
return err
}

hs.Roles = msg.(*BlockAnnounceHandshake).Roles
hs.BestBlockNumber = msg.(*BlockAnnounceHandshake).BestBlockNumber
hs.BestBlockHash = msg.(*BlockAnnounceHandshake).BestBlockHash
hs.GenesisHash = msg.(*BlockAnnounceHandshake).GenesisHash
return nil
}

Expand Down
15 changes: 9 additions & 6 deletions dot/network/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ func TestBlockAnnounce_Encode(t *testing.T) {
Number: big.NewInt(77),
StateRoot: common.Hash{2},
ExtrinsicsRoot: common.Hash{3},
Digest: types.Digest{},
Digest: types.Digest(nil),
}

enc, err := testBlockAnnounce.Encode()
require.NoError(t, err)

res := &BlockAnnounceMessage{
Number: big.NewInt(0),
Digest: types.Digest{},
}
enc2 := enc

res := new(BlockAnnounceMessage)
err = res.Decode(enc)
require.NoError(t, err)
require.Equal(t, testBlockAnnounce, res)

res2, err := decodeBlockAnnounceMessage(enc2)
require.NoError(t, err)
require.Equal(t, res2, res)
}

func TestDecodeBlockAnnounceHandshake(t *testing.T) {
Expand All @@ -72,7 +75,7 @@ func TestDecodeBlockAnnounceMessage(t *testing.T) {
Number: big.NewInt(77),
StateRoot: common.Hash{2},
ExtrinsicsRoot: common.Hash{3},
Digest: types.Digest{},
Digest: types.Digest(nil),
}

enc, err := testBlockAnnounce.Encode()
Expand Down
22 changes: 6 additions & 16 deletions dot/network/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/common/optional"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// Pair is a pair of arbitrary bytes.
Expand Down Expand Up @@ -40,22 +40,17 @@ func (l *LightRequest) SubProtocol() string {
}

// Encode encodes a LightRequest message using SCALE and appends the type byte to the start
// TODO Remove encode/decode receiver functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woudl this require updating the Message interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I believe so, I don't have to change it if I shouldn't. Was just thinking less receivers the better

func (l *LightRequest) Encode() ([]byte, error) {
return scale.Encode(l)
return scale.Marshal(*l)
}

// Decode the message into a LightRequest, it assumes the type byte has been removed
func (l *LightRequest) Decode(in []byte) error {
msg, err := scale.Decode(in, l)
err := scale.Unmarshal(in, l)
if err != nil {
return err
}

l.RmtCallRequest = msg.(*LightRequest).RmtCallRequest
l.RmtReadRequest = msg.(*LightRequest).RmtReadRequest
l.RmtHeaderRequest = msg.(*LightRequest).RmtHeaderRequest
l.RmtReadChildRequest = msg.(*LightRequest).RmtReadChildRequest
l.RmtChangesRequest = msg.(*LightRequest).RmtChangesRequest
return nil
}

Expand Down Expand Up @@ -92,20 +87,15 @@ func (l *LightResponse) SubProtocol() string {

// Encode encodes a LightResponse message using SCALE and appends the type byte to the start
func (l *LightResponse) Encode() ([]byte, error) {
return scale.Encode(l)
return scale.Marshal(*l)
}

// Decode the message into a LightResponse, it assumes the type byte has been removed
func (l *LightResponse) Decode(in []byte) error {
msg, err := scale.Decode(in, l)
err := scale.Unmarshal(in, l)
if err != nil {
return err
}

l.RmtCallResponse = msg.(*LightResponse).RmtCallResponse
l.RmtReadResponse = msg.(*LightResponse).RmtReadResponse
l.RmtHeaderResponse = msg.(*LightResponse).RmtHeaderResponse
l.RmtChangeResponse = msg.(*LightResponse).RmtChangeResponse
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions dot/network/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/common/optional"
"github.com/ChainSafe/gossamer/lib/common/variadic"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/pkg/scale"

"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -311,12 +311,13 @@ func protobufToBlockData(pbd *pb.BlockData) (*types.BlockData, error) {
}

if pbd.Header != nil {
header, err := scale.Decode(pbd.Header, types.NewEmptyHeader())
var header types.Header
err := scale.Unmarshal(pbd.Header, &header)
if err != nil {
return nil, err
}

bd.Header = header.(*types.Header).AsOptional()
bd.Header = header.AsOptional()
}

if pbd.Body != nil {
Expand Down
13 changes: 8 additions & 5 deletions dot/network/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ func TestEncodeBlockRequestMessage_NoOptionals(t *testing.T) {
func TestEncodeBlockResponseMessage_WithHeader(t *testing.T) {
hash := common.NewHash([]byte{0})
testHash := common.NewHash([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf})
d := types.Digest(nil)

header := &optional.CoreHeader{
ParentHash: testHash,
Number: big.NewInt(1),
StateRoot: testHash,
ExtrinsicsRoot: testHash,
Digest: &types.Digest{},
Digest: &d,
}

bd := &types.BlockData{
Expand Down Expand Up @@ -132,13 +133,14 @@ func TestEncodeBlockResponseMessage_WithHeader(t *testing.T) {
func TestEncodeBlockResponseMessage_WithBody(t *testing.T) {
hash := common.NewHash([]byte{0})
testHash := common.NewHash([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf})
d := types.Digest(nil)

header := &optional.CoreHeader{
ParentHash: testHash,
Number: big.NewInt(1),
StateRoot: testHash,
ExtrinsicsRoot: testHash,
Digest: &types.Digest{},
Digest: &d,
}

exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}}
Expand Down Expand Up @@ -170,13 +172,14 @@ func TestEncodeBlockResponseMessage_WithBody(t *testing.T) {
func TestEncodeBlockResponseMessage_WithAll(t *testing.T) {
hash := common.NewHash([]byte{0})
testHash := common.NewHash([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf})
d := types.Digest(nil)

header := &optional.CoreHeader{
ParentHash: testHash,
Number: big.NewInt(1),
StateRoot: testHash,
ExtrinsicsRoot: testHash,
Digest: &types.Digest{},
Digest: &d,
}

exts := [][]byte{{16, 1, 3, 5, 7}, {12, 9, 1, 2}, {12, 3, 4, 5}}
Expand Down Expand Up @@ -231,7 +234,7 @@ func TestEncodeBlockAnnounceMessage(t *testing.T) {
Number: big.NewInt(1),
StateRoot: stateRoot,
ExtrinsicsRoot: extrinsicsRoot,
Digest: types.Digest{},
Digest: types.Digest(nil),
}
encMsg, err := bhm.Encode()
require.Nil(t, err)
Expand Down Expand Up @@ -262,7 +265,7 @@ func TestDecode_BlockAnnounceMessage(t *testing.T) {
Number: big.NewInt(1),
StateRoot: stateRoot,
ExtrinsicsRoot: extrinsicsRoot,
Digest: types.Digest{},
Digest: types.Digest(nil),
}

require.Equal(t, expected, bhm)
Expand Down
Loading