Minor Waku version subpackage message.go tidy (#1964)

Moved sendBundle() from v*/message_response.go to v*/peer.go
Moved TestSendBundle() from v*/message_response_test.go to v*/peer_test.go
Renamed message.go to v*/message_response.go
Renamed message_test.go to v*/message_response_test.go
This commit is contained in:
Samuel Hawksby-Robinson 2020-05-07 15:45:25 +01:00 committed by GitHub
parent 4d00656c41
commit a21f8a39bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 94 deletions

View File

@ -1,11 +1,7 @@
package v0
import (
"bytes"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/waku/common"
)
@ -37,19 +33,3 @@ func NewMessagesResponse(batch gethcommon.Hash, errors []common.EnvelopeError) V
},
}
}
func sendBundle(rw p2p.MsgWriter, bundle []*common.Envelope) (rst gethcommon.Hash, err error) {
data, err := rlp.EncodeToBytes(bundle)
if err != nil {
return
}
err = rw.WriteMsg(p2p.Msg{
Code: messagesCode,
Size: uint32(len(data)),
Payload: bytes.NewBuffer(data),
})
if err != nil {
return
}
return crypto.Keccak256Hash(data), nil
}

View File

@ -26,7 +26,6 @@ import (
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/waku/common"
)
@ -41,29 +40,3 @@ func TestEncodeDecodeVersionedResponse(t *testing.T) {
require.NoError(t, err)
require.Equal(t, response.Response.Hash, v1resp.Hash)
}
func TestSendBundle(t *testing.T) {
rw1, rw2 := p2p.MsgPipe()
defer func() { handleError(t, rw1.Close()) }()
defer func() { handleError(t, rw2.Close()) }()
envelopes := []*common.Envelope{{
Expiry: 0,
TTL: 30,
Topic: common.TopicType{1},
Data: []byte{1, 1, 1},
}}
errc := make(chan error)
go func() {
_, err := sendBundle(rw1, envelopes)
errc <- err
}()
require.NoError(t, p2p.ExpectMsg(rw2, messagesCode, envelopes))
require.NoError(t, <-errc)
}
func handleError(t *testing.T, err error) {
if err != nil {
t.Logf("deferred function error: '%s'", err)
}
}

View File

@ -555,6 +555,22 @@ func (p *Peer) broadcast() error {
return nil
}
func sendBundle(rw p2p.MsgWriter, bundle []*common.Envelope) (rst gethcommon.Hash, err error) {
data, err := rlp.EncodeToBytes(bundle)
if err != nil {
return
}
err = rw.WriteMsg(p2p.Msg{
Code: messagesCode,
Size: uint32(len(data)),
Payload: bytes.NewBuffer(data),
})
if err != nil {
return
}
return crypto.Keccak256Hash(data), nil
}
func (p *Peer) setBloomFilter(bloom []byte) {
p.bloomMu.Lock()
defer p.bloomMu.Unlock()

View File

@ -23,7 +23,10 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/waku/common"
)
@ -98,6 +101,26 @@ func TestPeerBasic(t *testing.T) {
}
}
func TestSendBundle(t *testing.T) {
rw1, rw2 := p2p.MsgPipe()
defer func() { handleError(t, rw1.Close()) }()
defer func() { handleError(t, rw2.Close()) }()
envelopes := []*common.Envelope{{
Expiry: 0,
TTL: 30,
Topic: common.TopicType{1},
Data: []byte{1, 1, 1},
}}
errc := make(chan error)
go func() {
_, err := sendBundle(rw1, envelopes)
errc <- err
}()
require.NoError(t, p2p.ExpectMsg(rw2, messagesCode, envelopes))
require.NoError(t, <-errc)
}
func generateMessageParams() (*common.MessageParams, error) {
// set all the parameters except p.Dst and p.Padding
@ -123,3 +146,9 @@ func generateMessageParams() (*common.MessageParams, error) {
return &p, nil
}
func handleError(t *testing.T, err error) {
if err != nil {
t.Logf("deferred function error: '%s'", err)
}
}

View File

@ -1,11 +1,7 @@
package v1
import (
"bytes"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/waku/common"
)
@ -37,19 +33,3 @@ func NewMessagesResponse(batch gethcommon.Hash, errors []common.EnvelopeError) V
},
}
}
func sendBundle(rw p2p.MsgWriter, bundle []*common.Envelope) (rst gethcommon.Hash, err error) {
data, err := rlp.EncodeToBytes(bundle)
if err != nil {
return
}
err = rw.WriteMsg(p2p.Msg{
Code: messagesCode,
Size: uint32(len(data)),
Payload: bytes.NewBuffer(data),
})
if err != nil {
return
}
return crypto.Keccak256Hash(data), nil
}

View File

@ -26,7 +26,6 @@ import (
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/waku/common"
)
@ -41,29 +40,3 @@ func TestEncodeDecodeVersionedResponse(t *testing.T) {
require.NoError(t, err)
require.Equal(t, response.Response.Hash, v1resp.Hash)
}
func TestSendBundle(t *testing.T) {
rw1, rw2 := p2p.MsgPipe()
defer func() { handleError(t, rw1.Close()) }()
defer func() { handleError(t, rw2.Close()) }()
envelopes := []*common.Envelope{{
Expiry: 0,
TTL: 30,
Topic: common.TopicType{1},
Data: []byte{1, 1, 1},
}}
errc := make(chan error)
go func() {
_, err := sendBundle(rw1, envelopes)
errc <- err
}()
require.NoError(t, p2p.ExpectMsg(rw2, messagesCode, envelopes))
require.NoError(t, <-errc)
}
func handleError(t *testing.T, err error) {
if err != nil {
t.Logf("deferred function error: '%s'", err)
}
}

View File

@ -542,6 +542,22 @@ func (p *Peer) broadcast() error {
return nil
}
func sendBundle(rw p2p.MsgWriter, bundle []*common.Envelope) (rst gethcommon.Hash, err error) {
data, err := rlp.EncodeToBytes(bundle)
if err != nil {
return
}
err = rw.WriteMsg(p2p.Msg{
Code: messagesCode,
Size: uint32(len(data)),
Payload: bytes.NewBuffer(data),
})
if err != nil {
return
}
return crypto.Keccak256Hash(data), nil
}
func (p *Peer) setBloomFilter(bloom []byte) {
p.bloomMu.Lock()
defer p.bloomMu.Unlock()

View File

@ -23,7 +23,10 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/waku/common"
)
@ -98,6 +101,26 @@ func TestPeerBasic(t *testing.T) {
}
}
func TestSendBundle(t *testing.T) {
rw1, rw2 := p2p.MsgPipe()
defer func() { handleError(t, rw1.Close()) }()
defer func() { handleError(t, rw2.Close()) }()
envelopes := []*common.Envelope{{
Expiry: 0,
TTL: 30,
Topic: common.TopicType{1},
Data: []byte{1, 1, 1},
}}
errc := make(chan error)
go func() {
_, err := sendBundle(rw1, envelopes)
errc <- err
}()
require.NoError(t, p2p.ExpectMsg(rw2, messagesCode, envelopes))
require.NoError(t, <-errc)
}
func generateMessageParams() (*common.MessageParams, error) {
// set all the parameters except p.Dst and p.Padding
@ -123,3 +146,9 @@ func generateMessageParams() (*common.MessageParams, error) {
return &p, nil
}
func handleError(t *testing.T, err error) {
if err != nil {
t.Logf("deferred function error: '%s'", err)
}
}