Skip to content

Commit

Permalink
Merge pull request #318 from boschresearch/311-2-add-protobuf-adapter
Browse files Browse the repository at this point in the history
Implement protobuf adapter for wire serialization
  • Loading branch information
matthiasgeihs authored Feb 15, 2022
2 parents 80d6c73 + b22e8de commit d698d08
Show file tree
Hide file tree
Showing 31 changed files with 4,427 additions and 31 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Check vanity import
run: .scripts/check-vanity-imports.sh $GITHUB_WORKSPACE
- name: Lint

- name: Lint golang files
uses: golangci/golangci-lint-action@v2
with:
version: v1.43

- name: Lint proto files
uses: plexsystems/[email protected]
with:
configDirectory: .

tests:
name: Tests
runs-on: ubuntu-latest
Expand Down
21 changes: 21 additions & 0 deletions .protolint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
lint:
ignores:
# Sometimes, when variable names are long line length exceeds 80 char.
- id: MAX_LINE_LENGTH
files:
- wire/protobuf/wire.proto

# Some fields with repeated type intentionally have non plural names.
# Eg: balance, index_map etc.,
- id: REPEATED_FIELD_NAMES_PLURALIZED
files:
- wire/protobuf/wire.proto

rules:

# Enable a few additional linters, that are not part of official style
# guide for protonbuf.
add:
- MESSAGES_HAVE_COMMENT
- FILE_HAS_COMMENT
- SYNTAX_CONSISTENT
2 changes: 2 additions & 0 deletions client/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type (

// PeerRejectedError indicates the channel proposal or channel update was
// rejected by the peer.
//
// Reason should be a UTF-8 encodable string.
PeerRejectedError struct {
ItemType string // ItemType indicates the type of item rejected (channel proposal or channel update).
Reason string // Reason sent by the peer for the rejection.
Expand Down
2 changes: 2 additions & 0 deletions client/proposalmsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ func (acc *SubChannelProposalAccMsg) Decode(r io.Reader) error {
//
// The message is one of two possible responses in the
// Multi-Party Channel Proposal Protocol (MPCPP).
//
// Reason should be a UTF-8 encodable string.
type ChannelProposalRejMsg struct {
ProposalID ProposalID // The channel proposal to reject.
Reason string // The rejection reason.
Expand Down
1 change: 1 addition & 0 deletions client/proposalmsgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"perun.network/go-perun/channel/test"
"perun.network/go-perun/client"
clienttest "perun.network/go-perun/client/test"
_ "perun.network/go-perun/wire/perunio/serializer" // wire serialzer init
peruniotest "perun.network/go-perun/wire/perunio/test"
pkgtest "polycry.pt/poly-go/test"
)
Expand Down
16 changes: 10 additions & 6 deletions client/test/proposalmsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func channelProposalRejSerializationTest(t *testing.T, serializerTest func(t *te
for i := 0; i < 16; i++ {
m := &client.ChannelProposalRejMsg{
ProposalID: newRandomProposalID(rng),
Reason: newRandomString(rng, minLen, maxLenDiff),
Reason: newRandomASCIIString(rng, minLen, maxLenDiff),
}
serializerTest(t, m)
}
Expand All @@ -110,10 +110,14 @@ func newRandomProposalID(rng *rand.Rand) (id client.ProposalID) {
return
}

// newRandomstring returns a random ascii string of length between minLen and
// newRandomASCIIString returns a random ascii string of length between minLen and
// minLen+maxLenDiff.
func newRandomString(rng *rand.Rand, minLen, maxLenDiff int) string {
r := make([]byte, minLen+rng.Intn(maxLenDiff))
rng.Read(r)
return string(r)
func newRandomASCIIString(rng *rand.Rand, minLen, maxLenDiff int) string {
str := make([]byte, minLen+rng.Intn(maxLenDiff))
const firstPrintableASCII = 32
const lastPrintableASCII = 126
for i := range str {
str[i] = byte(firstPrintableASCII + rng.Intn(lastPrintableASCII-firstPrintableASCII))
}
return string(str)
}
2 changes: 1 addition & 1 deletion client/test/updatemsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func channelUpdateRejSerializationTest(t *testing.T, serializerTest func(t *test
m := &client.ChannelUpdateRejMsg{
ChannelID: test.NewRandomChannelID(rng),
Version: uint64(rng.Int63()),
Reason: newRandomString(rng, minLen, maxLenDiff),
Reason: newRandomASCIIString(rng, minLen, maxLenDiff),
}
serializerTest(t, m)
}
Expand Down
2 changes: 2 additions & 0 deletions client/updatemsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ type (
// ChannelUpdateRejMsg is the wire message sent as a negative reply to a
// ChannelUpdate. It references the channel ID and version and states a
// reason for the rejection.
//
// Reason should be a UTF-8 encodable string.
ChannelUpdateRejMsg struct {
// ChannelID is the channel ID.
ChannelID channel.ID
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
go.uber.org/goleak v1.1.11
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/protobuf v1.23.0
polycry.pt/poly-go v0.0.0-20211115212618-87069dfa360f
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -621,6 +622,7 @@ google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 1 addition & 1 deletion wire/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ import (
)

func TestAuthResponseMsg(t *testing.T) {
wiretest.AuthMsgsTest(t, peruniotest.MsgSerializerTest)
wiretest.AuthMsgsSerializationTest(t, peruniotest.MsgSerializerTest)
}
21 changes: 14 additions & 7 deletions wire/controlmsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,36 @@ func init() {

// Since ping and pong messages are essentially the same, this is a common
// implementation for both.
type pingPongMsg struct {

// PingPongMsg is the common message type used by ping and pong msgs, since
// both the messages are essentially the same.
type PingPongMsg struct {
Created time.Time
}

func (m pingPongMsg) Encode(writer io.Writer) error {
// Encode implements msg.Encode.
func (m PingPongMsg) Encode(writer io.Writer) error {
return perunio.Encode(writer, m.Created)
}

func (m *pingPongMsg) Decode(reader io.Reader) error {
// Decode implements msg.Decode.
func (m *PingPongMsg) Decode(reader io.Reader) error {
return perunio.Decode(reader, &m.Created)
}

func newPingPongMsg() pingPongMsg {
func newPingPongMsg() PingPongMsg {
// do not use `time.Now()` directly because it contains monotonic clock
// data specific to the current process which breaks, e.g.,
// `reflect.DeepEqual`, cf. "Marshal/Unmarshal functions are asymmetrical"
// https://github.com/golang/go/issues/19502
return pingPongMsg{Created: time.Unix(0, time.Now().UnixNano())}
return PingPongMsg{Created: time.Unix(0, time.Now().UnixNano())}
}

// PingMsg is a ping request.
// It contains the time at which it was sent, so that the recipient can also
// measure the time it took to transmit the ping request.
type PingMsg struct {
pingPongMsg
PingPongMsg
}

// Type returns Ping.
Expand All @@ -71,7 +76,7 @@ func NewPingMsg() *PingMsg {
// long the ping request took to be transmitted, and how quickly the response
// was sent.
type PongMsg struct {
pingPongMsg
PingPongMsg
}

// Type returns Pong.
Expand All @@ -85,6 +90,8 @@ func NewPongMsg() *PongMsg {
}

// ShutdownMsg is sent when orderly shutting down a connection.
//
// Reason should be a UTF-8 encodable string.
type ShutdownMsg struct {
Reason string
}
Expand Down
3 changes: 2 additions & 1 deletion wire/controlmsgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package wire_test
import (
"testing"

_ "perun.network/go-perun/wire/perunio/serializer" // wire serialzer init
peruniotest "perun.network/go-perun/wire/perunio/test"
wiretest "perun.network/go-perun/wire/test"
)

func TestControlMsgs(t *testing.T) {
wiretest.ControlMsgsTest(t, peruniotest.MsgSerializerTest)
wiretest.ControlMsgsSerializationTest(t, peruniotest.MsgSerializerTest)
}
1 change: 1 addition & 0 deletions wire/net/exchange_addr_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

wallettest "perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire"
_ "perun.network/go-perun/wire/protobuf" // wire serialzer init
wiretest "perun.network/go-perun/wire/test"
ctxtest "polycry.pt/poly-go/context/test"
"polycry.pt/poly-go/test"
Expand Down
2 changes: 0 additions & 2 deletions wire/net/ioconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"github.com/pkg/errors"

"perun.network/go-perun/wire"
// nolint: blank-imports // allow blank import package that is not main or test.
_ "perun.network/go-perun/wire/perunio/serializer"
"polycry.pt/poly-go/sync/atomic"
)

Expand Down
1 change: 1 addition & 0 deletions wire/net/simple/dialer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
simwallet "perun.network/go-perun/backend/sim/wallet"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wire"
_ "perun.network/go-perun/wire/protobuf" // wire serialzer init
ctxtest "polycry.pt/poly-go/context/test"
"polycry.pt/poly-go/test"
)
Expand Down
1 change: 1 addition & 0 deletions wire/net/test/connhub_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
_ "perun.network/go-perun/backend/sim" // backend init
wallettest "perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire"
_ "perun.network/go-perun/wire/protobuf" // wire serialzer init
wiretest "perun.network/go-perun/wire/test"
ctxtest "polycry.pt/poly-go/context/test"
"polycry.pt/poly-go/sync"
Expand Down
57 changes: 57 additions & 0 deletions wire/protobuf/controlmsgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package protobuf

import (
"time"

"perun.network/go-perun/wire"
)

func fromPingMsg(msg *wire.PingMsg) *Envelope_PingMsg {
protoMsg := &PingMsg{}
protoMsg.Created = msg.Created.UnixNano()
return &Envelope_PingMsg{protoMsg}
}

func fromPongMsg(msg *wire.PongMsg) *Envelope_PongMsg {
protoMsg := &PongMsg{}
protoMsg.Created = msg.Created.UnixNano()
return &Envelope_PongMsg{protoMsg}
}

func fromShutdownMsg(msg *wire.ShutdownMsg) *Envelope_ShutdownMsg {
protoMsg := &ShutdownMsg{}
protoMsg.Reason = msg.Reason
return &Envelope_ShutdownMsg{protoMsg}
}

func toPingMsg(protoMsg *Envelope_PingMsg) (msg *wire.PingMsg) {
msg = &wire.PingMsg{}
msg.Created = time.Unix(0, protoMsg.PingMsg.Created)
return msg
}

func toPongMsg(protoEnvMsg *Envelope_PongMsg) (msg *wire.PongMsg) {
msg = &wire.PongMsg{}
msg.Created = time.Unix(0, protoEnvMsg.PongMsg.Created)
return msg
}

func toShutdownMsg(protoEnvMsg *Envelope_ShutdownMsg) (msg *wire.ShutdownMsg) {
msg = &wire.ShutdownMsg{}
msg.Reason = protoEnvMsg.ShutdownMsg.Reason
return msg
}
17 changes: 17 additions & 0 deletions wire/protobuf/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package protobuf implements the wire serializer interface using the protocol
// buffers serialization protocol.
package protobuf // import "perun.network/go-perun/wire/protobuf"
22 changes: 22 additions & 0 deletions wire/protobuf/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package protobuf

// This command generates the bindings for serializing wire messages using
// protocol buffers serialization protocol.
//
// It requires protoc and protoc-gen-go to be installed.

//go:generate protoc --go_out=. wire.proto
Loading

0 comments on commit d698d08

Please sign in to comment.