Skip to content

Commit

Permalink
Separate wire address (hyperledger-labs#350)
Browse files Browse the repository at this point in the history
* Update .gitignore

`*.bin-runtime` can be removed as solidity files are no longer part of
the repository.

Signed-off-by: Matthias Geihs <[email protected]>

* Update wire.Address type

Signed-off-by: Matthias Geihs <[email protected]>

* Update codebase for new types

Signed-off-by: Matthias Geihs <[email protected]>

* Fix cache_internal_test timestamp difference

On fast machines, the timestamp was identical and the tests were
failing.

Signed-off-by: Matthias Geihs <[email protected]>

* Update backend/sim/wire/address.go

Signed-off-by: Matthias Geihs <[email protected]>

* Rename variables

Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs authored Jun 24, 2022
1 parent 7a3d451 commit 8d3b100
Show file tree
Hide file tree
Showing 53 changed files with 630 additions and 267 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go generate output
*.bin-runtime
1 change: 1 addition & 0 deletions backend/sim/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ package sim
import (
_ "perun.network/go-perun/backend/sim/channel" // backend init
_ "perun.network/go-perun/backend/sim/wallet" // backend init
_ "perun.network/go-perun/backend/sim/wire" // backend init
)
38 changes: 38 additions & 0 deletions backend/sim/wire/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 wire

import (
"math/rand"

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

// Account is a wire account.
type Account struct {
addr wire.Address
}

// Address returns the account's address.
func (acc *Account) Address() wire.Address {
return acc.addr
}

// NewRandomAccount generates a new random account.
func NewRandomAccount(rng *rand.Rand) *Account {
return &Account{
addr: NewRandomAddress(rng),
}
}
73 changes: 73 additions & 0 deletions backend/sim/wire/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 wire

import (
"bytes"
"math/rand"

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

// AddrLen is the length of an address in byte.
const AddrLen = 32

// Address is a wire address.
type Address [AddrLen]byte

// NewAddress returns a new address.
func NewAddress() *Address {
return &Address{}
}

// MarshalBinary marshals the address to binary.
func (a Address) MarshalBinary() (data []byte, err error) {
return a[:], nil
}

// UnmarshalBinary unmarshals an address from binary.
func (a *Address) UnmarshalBinary(data []byte) error {
copy(a[:], data)
return nil
}

// Equal returns whether the two addresses are equal.
func (a Address) Equal(b wire.Address) bool {
bTyped, ok := b.(*Address)
if !ok {
return false
}
return bytes.Equal(a[:], bTyped[:])
}

// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
// returns -1 if a < b, 0 if a == b, 1 if a > b.
func (a Address) Cmp(b wire.Address) int {
bTyped, ok := b.(*Address)
if !ok {
panic("wrong type")
}
return bytes.Compare(a[:], bTyped[:])
}

// NewRandomAddress returns a new random peer address.
func NewRandomAddress(rng *rand.Rand) *Address {
addr := Address{}
_, err := rng.Read(addr[:])
if err != nil {
panic(err)
}
return &addr
}
32 changes: 32 additions & 0 deletions backend/sim/wire/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 wire_test

import (
"math/rand"
"testing"

simwire "perun.network/go-perun/backend/sim/wire"
"perun.network/go-perun/wire"
"perun.network/go-perun/wire/test"
)

func TestAddress(t *testing.T) {
test.TestAddressImplementation(t, func() wire.Address {
return simwire.NewAddress()
}, func(rng *rand.Rand) wire.Address {
return simwire.NewRandomAddress(rng)
})
}
16 changes: 16 additions & 0 deletions backend/sim/wire/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 - 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 wire contains the implementation of the wire interfaces.
package wire // import "perun.network/go-perun/backend/sim/wire"
34 changes: 34 additions & 0 deletions backend/sim/wire/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 wire

import (
"math/rand"

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

func init() {
wire.SetNewAddressFunc(func() wire.Address {
return NewAddress()
})
test.SetNewRandomAddress(func(rng *rand.Rand) wire.Address {
return NewRandomAddress(rng)
})
test.SetNewRandomAccount(func(rng *rand.Rand) wire.Account {
return NewRandomAccount(rng)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

_ "perun.network/go-perun/backend/sim"
_ "perun.network/go-perun/backend/sim" // backend init
"perun.network/go-perun/channel/persistence/test"
"polycry.pt/poly-go/sortedkv"
"polycry.pt/poly-go/sortedkv/leveldb"
Expand Down
37 changes: 2 additions & 35 deletions channel/persistence/keyvalue/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package keyvalue
import (
"bytes"
"context"
"io"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -45,14 +44,14 @@ type ChannelIterator struct {
func (pr *PersistRestorer) ActivePeers(ctx context.Context) ([]wire.Address, error) {
it := sortedkv.NewTable(pr.db, prefix.PeerDB).NewIterator()

peermap := make(map[wallet.AddrKey]wire.Address)
peermap := make(map[wire.AddrKey]wire.Address)
for it.Next() {
addr := wire.NewAddress()
err := perunio.Decode(bytes.NewBufferString(it.Key()), addr)
if err != nil {
return nil, errors.WithMessagef(err, "decoding peer key (%x)", it.Key())
}
peermap[wallet.Key(addr)] = addr
peermap[wire.Key(addr)] = addr
}

peers := make([]wire.Address, 0, len(peermap))
Expand Down Expand Up @@ -133,38 +132,6 @@ func (pr *PersistRestorer) RestoreChannel(ctx context.Context, id channel.ID) (*
return nil, errors.Errorf("could not find channel %x", id)
}

// decodePeerChanID decodes the channel.ID and peer.Address from a key.
//nolint:deadcode,unused
func decodePeerChanID(key string) (wire.Address, channel.ID, error) {
buf := bytes.NewBufferString(key)
addr := wire.NewAddress()
err := perunio.Decode(buf, addr)
if err != nil {
return addr, channel.ID{}, errors.WithMessage(err, "decode peer address")
}

if err = eatExpect(buf, ":channel:"); err != nil {
return nil, channel.ID{}, errors.WithMessagef(err, "key: %x", key)
}

var id channel.ID
return addr, id, errors.WithMessage(perunio.Decode(buf, &id), "decode channel id")
}

// eatExpect consumes bytes from a Reader and asserts that they are equal to
// the expected string.
//nolint:unused
func eatExpect(r io.Reader, tok string) error {
buf := make([]byte, len(tok))
if _, err := io.ReadFull(r, buf); err != nil {
return errors.WithMessage(err, "reading")
}
if string(buf) != tok {
return errors.Errorf("expected %s, got %s", tok, string(buf))
}
return nil
}

type decOpts uint8

const (
Expand Down
6 changes: 3 additions & 3 deletions channel/persistence/test/persistrestorertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"perun.network/go-perun/channel"
"perun.network/go-perun/channel/persistence"
"perun.network/go-perun/log"
wtest "perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire"
"perun.network/go-perun/wire/test"
pkgtest "polycry.pt/poly-go/test"
)

Expand All @@ -46,7 +46,7 @@ const channelNumPeers = 2
func NewClient(ctx context.Context, t *testing.T, rng *rand.Rand, pr persistence.PersistRestorer) *Client {
t.Helper()
return &Client{
addr: wtest.NewRandomAddress(rng),
addr: test.NewRandomAddress(rng),
rng: rng,
pr: pr,
ctx: ctx,
Expand Down Expand Up @@ -94,7 +94,7 @@ func GenericPersistRestorerTest(

ct := pkgtest.NewConcurrent(t)
c := NewClient(ctx, t, rng, pr)
peers := wtest.NewRandomAddresses(rng, numPeers)
peers := test.NewRandomAddresses(rng, numPeers)

channels := make([]map[channel.ID]*Channel, numPeers)
var prevCh *Channel
Expand Down
4 changes: 2 additions & 2 deletions client/client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"github.com/stretchr/testify/assert"

channeltest "perun.network/go-perun/channel/test"
wallettest "perun.network/go-perun/wallet/test"
wiretest "perun.network/go-perun/wire/test"
"polycry.pt/poly-go/test"
)

func TestClient_Channel(t *testing.T) {
rng := test.Prng(t)
// dummy client that only has an id and a registry
c := &Client{
address: wallettest.NewRandomAddress(rng),
address: wiretest.NewRandomAddress(rng),
channels: makeChanRegistry(),
}

Expand Down
11 changes: 7 additions & 4 deletions client/client_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
chtest "perun.network/go-perun/channel/test"
"perun.network/go-perun/client"
ctest "perun.network/go-perun/client/test"
"perun.network/go-perun/wallet"
wtest "perun.network/go-perun/wallet/test"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewSetups(rng *rand.Rand, names []string) []ctest.RoleSetup {
}
setup[i] = ctest.RoleSetup{
Name: names[i],
Identity: wtest.NewRandomAccount(rng),
Identity: wiretest.NewRandomAccount(rng),
Bus: bus,
Funder: backend,
Adjudicator: backend,
Expand All @@ -73,18 +74,20 @@ func NewSetups(rng *rand.Rand, names []string) []ctest.RoleSetup {
type Client struct {
*client.Client
ctest.RoleSetup
WalletAddress wallet.Address
}

func NewClients(t *testing.T, rng *rand.Rand, setups []ctest.RoleSetup) []*Client {
t.Helper()
clients := make([]*Client, len(setups))
for i, setup := range setups {
setup.Identity = setup.Wallet.NewRandomAccount(rng)
setup.Identity = wiretest.NewRandomAccount(rng)
cl, err := client.New(setup.Identity.Address(), setup.Bus, setup.Funder, setup.Adjudicator, setup.Wallet, setup.Watcher)
assert.NoError(t, err)
clients[i] = &Client{
Client: cl,
RoleSetup: setup,
Client: cl,
RoleSetup: setup,
WalletAddress: setup.Wallet.NewRandomAccount(rng).Address(),
}
}
return clients
Expand Down
Loading

0 comments on commit 8d3b100

Please sign in to comment.