forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate wire address (hyperledger-labs#350)
* 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
1 parent
7a3d451
commit 8d3b100
Showing
53 changed files
with
630 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,3 @@ | |
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Go generate output | ||
*.bin-runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.