Skip to content

Commit

Permalink
chore(all): Use named BackendID and update license date
Browse files Browse the repository at this point in the history
Signed-off-by: Sophia Koehler <[email protected]>
  • Loading branch information
sophia1ch committed Jan 28, 2025
1 parent 1cba30f commit 5cf3234
Show file tree
Hide file tree
Showing 39 changed files with 118 additions and 108 deletions.
9 changes: 5 additions & 4 deletions channel/adjudicator_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand All @@ -15,6 +15,7 @@
package channel

import (
"github.com/perun-network/perun-eth-backend/wallet"

Check failure on line 18 in channel/adjudicator_internal_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
"math/rand"
"testing"

Check failure on line 20 in channel/adjudicator_internal_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 21 in channel/adjudicator_internal_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Expand All @@ -38,13 +39,13 @@ func Test_toEthSubStates(t *testing.T) {
{
title: "nil map gives nil slice",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
return channeltest.NewRandomState(rng, channeltest.WithBackend(1)), nil, nil
return channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID)), nil, nil
},
},
{
title: "fresh map gives nil slice",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
return channeltest.NewRandomState(rng, channeltest.WithBackend(1)), nil, nil
return channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID)), nil, nil
},
},
{
Expand Down Expand Up @@ -84,7 +85,7 @@ func Test_toEthSubStates(t *testing.T) {
func genStates(rng *rand.Rand, n int) (states []*channel.State) {
states = make([]*channel.State, n)
for i := range states {
states[i] = channeltest.NewRandomState(rng, channeltest.WithBackend(1))
states[i] = channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID))
}
return
}
Expand Down
8 changes: 4 additions & 4 deletions channel/adjudicator_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down Expand Up @@ -47,7 +47,7 @@ func testSignState(t *testing.T, accounts []*keystore.Account, state *channel.St
func signState(accounts []*keystore.Account, state *channel.State) (channel.Transaction, error) {
sigs := make([][]byte, len(accounts))
for i := range accounts {
sig, err := channel.Sign(accounts[i], state, 1)
sig, err := channel.Sign(accounts[i], state, ethwallettest.BackendID)
if err != nil {
return channel.Transaction{}, errors.WithMessagef(err, "signing with account %d", i)
}
Expand All @@ -68,7 +68,7 @@ func TestSubscribeRegistered(t *testing.T) {
rng,
channeltest.WithChallengeDuration(uint64(100*time.Second)),
channeltest.WithParts(s.Parts),
channeltest.WithBackend(1),
channeltest.WithBackend(ethwallettest.BackendID),
channeltest.WithAssets(s.Asset),
channeltest.WithIsFinal(false),
channeltest.WithLedgerChannel(true),
Expand All @@ -94,7 +94,7 @@ func TestSubscribeRegistered(t *testing.T) {
tx := testSignState(t, s.Accs, state)
req := channel.AdjudicatorReq{
Params: params,
Acc: map[wallet.BackendID]wallet.Account{1: s.Accs[0]},
Acc: map[wallet.BackendID]wallet.Account{ethwallettest.BackendID: s.Accs[0]},
Idx: channel.Index(0),
Tx: tx,
}
Expand Down
4 changes: 2 additions & 2 deletions channel/asset.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down Expand Up @@ -54,7 +54,7 @@ func MakeAssetID(id *big.Int) multi.AssetID {
if id.Sign() < 0 {
panic("must not be smaller than zero")
}
return AssetID{backendID: 1, ledgerID: MakeChainID(id)}
return AssetID{backendID: wallet.BackendID, ledgerID: MakeChainID(id)}
}

// UnmarshalBinary unmarshals the chainID from its binary representation.
Expand Down
2 changes: 1 addition & 1 deletion channel/asset_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
2 changes: 1 addition & 1 deletion channel/backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
19 changes: 10 additions & 9 deletions channel/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand All @@ -16,6 +16,7 @@ package channel_test

import (
"context"
"github.com/perun-network/perun-eth-backend/wallet"

Check failure on line 19 in channel/backend_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
"math/rand"
"testing"
"time"
Expand All @@ -42,7 +43,7 @@ func TestState_ToAndFromEth(t *testing.T) {
rng := pkgtest.Prng(t)

for i := 0; i < 100; i++ {
state := test.NewRandomState(rng, test.WithBackend(1))
state := test.NewRandomState(rng, test.WithBackend(wallet.BackendID))
testToAndFromEthState(t, state)
}
})
Expand All @@ -51,7 +52,7 @@ func TestState_ToAndFromEth(t *testing.T) {
rng := pkgtest.Prng(t)

for i := 0; i < 100; i++ {
state := test.NewRandomState(rng, test.WithBackend(1), test.WithNumLocked(int(rng.Int31n(10))))
state := test.NewRandomState(rng, test.WithBackend(wallet.BackendID), test.WithNumLocked(int(rng.Int31n(10))))
testToAndFromEthState(t, state)
}
})
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestAdjudicator_PureFunctions(t *testing.T) {
func testCalcID(t *testing.T, rng *rand.Rand, contr *adjudicator.Adjudicator, opts *bind.CallOpts) {
t.Helper()
for i := 0; i < 100; i++ {
opt := test.WithBackend(1)
opt := test.WithBackend(wallet.BackendID)
params := test.NewRandomParams(rng, opt)
ethParams := channel.ToEthParams(params)
ethID, err := contr.ChannelID(opts, ethParams)
Expand All @@ -108,7 +109,7 @@ func testCalcID(t *testing.T, rng *rand.Rand, contr *adjudicator.Adjudicator, op
func testHashState(t *testing.T, rng *rand.Rand, contr *adjudicator.Adjudicator, opts *bind.CallOpts) {
t.Helper()
for i := 0; i < 100; i++ {
state := test.NewRandomState(rng, test.WithBackend(1))
state := test.NewRandomState(rng, test.WithBackend(wallet.BackendID))
ethState := channel.ToEthState(state)
ethHash, err := contr.HashState(opts, ethState)
require.NoError(t, err)
Expand All @@ -130,19 +131,19 @@ func TestGenericTests(t *testing.T) {
}

func newChannelSetup(rng *rand.Rand) *test.Setup {
params, state := test.NewRandomParamsAndState(rng, test.WithNumLocked(int(rng.Int31n(4)+1)), test.WithBackend(1))
params2, state2 := test.NewRandomParamsAndState(rng, test.WithIsFinal(!state.IsFinal), test.WithNumLocked(int(rng.Int31n(4)+1)), test.WithBackend(1))
params, state := test.NewRandomParamsAndState(rng, test.WithNumLocked(int(rng.Int31n(4)+1)), test.WithBackend(wallet.BackendID))
params2, state2 := test.NewRandomParamsAndState(rng, test.WithIsFinal(!state.IsFinal), test.WithNumLocked(int(rng.Int31n(4)+1)), test.WithBackend(wallet.BackendID))

createAddr := func() map[perunwallet.BackendID]perunwallet.Address {
return map[perunwallet.BackendID]perunwallet.Address{1: wallettest.NewRandomAddress(rng, 1)}
return map[perunwallet.BackendID]perunwallet.Address{wallet.BackendID: wallettest.NewRandomAddress(rng, wallet.BackendID)}
}

return &test.Setup{
Params: params,
Params2: params2,
State: state,
State2: state2,
Account: wallettest.NewRandomAccount(rng, 1),
Account: wallettest.NewRandomAccount(rng, wallet.BackendID),
RandomAddress: createAddr,
}
}
2 changes: 1 addition & 1 deletion channel/conclude.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
13 changes: 7 additions & 6 deletions channel/conclude_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand All @@ -16,6 +16,7 @@ package channel_test

import (
"context"
test2 "github.com/perun-network/perun-eth-backend/wallet/test"
"math/rand"
"testing"

Check failure on line 22 in channel/conclude_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Expand Down Expand Up @@ -50,7 +51,7 @@ func testConcludeFinal(t *testing.T, numParts int) {
rng,
channeltest.WithParts(s.Parts),
channeltest.WithAssets(s.Asset),
channeltest.WithBackend(1),
channeltest.WithBackend(test2.BackendID),
channeltest.WithIsFinal(true),
channeltest.WithLedgerChannel(true),
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func testConcludeFinal(t *testing.T, numParts int) {
go ct.StageN("register", numParts, func(t pkgtest.ConcT) {
req := channel.AdjudicatorReq{
Params: params,
Acc: map[wallet.BackendID]wallet.Account{1: s.Accs[i]},
Acc: map[wallet.BackendID]wallet.Account{test2.BackendID: s.Accs[i]},
Idx: channel.Index(i),
Tx: tx,
Secondary: (i != initiator),
Expand Down Expand Up @@ -204,7 +205,7 @@ func makeRandomChannel(rng *rand.Rand, participants []map[wallet.BackendID]walle
rng,
channeltest.WithParts(participants),
channeltest.WithAssets(asset),
channeltest.WithBackend(1),
channeltest.WithBackend(test2.BackendID),
channeltest.WithIsFinal(false),
channeltest.WithNumLocked(0),
channeltest.WithoutApp(),
Expand Down Expand Up @@ -257,7 +258,7 @@ func register(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystor

req := channel.AdjudicatorReq{
Params: ch.params,
Acc: map[wallet.BackendID]wallet.Account{1: accounts[0]},
Acc: map[wallet.BackendID]wallet.Account{test2.BackendID: accounts[0]},
Idx: 0,
Tx: tx,
Secondary: false,
Expand All @@ -280,7 +281,7 @@ func withdraw(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystor
for i, a := range accounts {
req := channel.AdjudicatorReq{
Params: c.params,
Acc: map[wallet.BackendID]wallet.Account{1: a},
Acc: map[wallet.BackendID]wallet.Account{test2.BackendID: a},
Idx: channel.Index(i),
Tx: tx,
Secondary: i != 0,
Expand Down
2 changes: 1 addition & 1 deletion channel/contractbackend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
23 changes: 12 additions & 11 deletions channel/contractbackend_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand All @@ -16,6 +16,7 @@ package channel_test

import (
"context"
test2 "github.com/perun-network/perun-eth-backend/wallet/test"
"math/big"
"testing"
"time"
Expand All @@ -42,35 +43,35 @@ func Test_calcFundingIDs(t *testing.T) {
tests := []struct {
name string
participants []map[wallet.BackendID]wallet.Address
channelID map[wallet.BackendID][32]byte
channelID [32]byte
want [][32]byte
}{
{"Test nil array, empty channelID", nil, map[wallet.BackendID][32]byte{}, make([][32]byte, 0)},
{"Test nil array, non-empty channelID", nil, map[wallet.BackendID][32]byte{1: {1}}, make([][32]byte, 0)},
{"Test empty array, non-empty channelID", []map[wallet.BackendID]wallet.Address{}, map[wallet.BackendID][32]byte{1: {1}}, make([][32]byte, 0)},
{"Test nil array, empty channelID", nil, [32]byte{}, make([][32]byte, 0)},
{"Test nil array, non-empty channelID", nil, [32]byte{1}, make([][32]byte, 0)},
{"Test empty array, non-empty channelID", []map[wallet.BackendID]wallet.Address{}, [32]byte{1}, make([][32]byte, 0)},
// Tests based on actual data from contracts.
{
name: "Test non-empty array, empty channelID",
participants: []map[wallet.BackendID]wallet.Address{{1: &ethwallet.Address{}}},
participants: []map[wallet.BackendID]wallet.Address{{test2.BackendID: &ethwallet.Address{}}},
want: [][32]byte{{173, 50, 40, 182, 118, 247, 211, 205, 66, 132, 165, 68, 63, 23, 241, 150, 43, 54, 228, 145, 179, 10, 64, 178, 64, 88, 73, 229, 151, 186, 95, 181}},
},
{
"Test non-empty array, non-empty channelID",
[]map[wallet.BackendID]wallet.Address{{1: &ethwallet.Address{}}},
map[wallet.BackendID][32]byte{1: {1}},
[]map[wallet.BackendID]wallet.Address{{test2.BackendID: &ethwallet.Address{}}},
[32]byte{1},
[][32]byte{{130, 172, 39, 157, 178, 106, 32, 109, 155, 165, 169, 76, 7, 255, 148, 10, 234, 75, 59, 253, 232, 130, 14, 201, 95, 78, 250, 10, 207, 208, 213, 188}},
},
{
"Test non-empty array, non-empty channelID",
[]map[wallet.BackendID]wallet.Address{{1: fromEthAddr(common.BytesToAddress([]byte{}))}},
map[wallet.BackendID][32]byte{1: {1}},
[]map[wallet.BackendID]wallet.Address{{test2.BackendID: fromEthAddr(common.BytesToAddress([]byte{}))}},
[32]byte{1},
[][32]byte{{130, 172, 39, 157, 178, 106, 32, 109, 155, 165, 169, 76, 7, 255, 148, 10, 234, 75, 59, 253, 232, 130, 14, 201, 95, 78, 250, 10, 207, 208, 213, 188}},
},
}
for _, _tt := range tests {
tt := _tt
t.Run(tt.name, func(t *testing.T) {
got := ethchannel.FundingIDs(tt.channelID[1], tt.participants...)
got := ethchannel.FundingIDs(tt.channelID, tt.participants...)
assert.Equal(t, got, tt.want, "FundingIDs not as expected")
})
}
Expand Down
2 changes: 1 addition & 1 deletion channel/erc20_depositor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
2 changes: 1 addition & 1 deletion channel/eth_depositor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
2 changes: 1 addition & 1 deletion channel/funder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand Down
14 changes: 7 additions & 7 deletions channel/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newFunderSetup(rng *rand.Rand) (
) {
n := 2
simBackend := test.NewSimulatedBackend()
ksWallet := wallettest.RandomWallet(1).(*keystore.Wallet)
ksWallet := wallettest.RandomWallet(ethwallet.BackendID).(*keystore.Wallet)
cb := ethchannel.NewContractBackend(
simBackend,
ethchannel.MakeChainID(simBackend.ChainID()),
Expand All @@ -94,12 +94,12 @@ func newFunderSetup(rng *rand.Rand) (

for i := 0; i < n; i++ {
assets[i] = *test.NewRandomAsset(rng)
accs[i] = accounts.Account{Address: ethwallet.AsEthAddr(wallettest.NewRandomAddress(rng, 1))}
accs[i] = accounts.Account{Address: ethwallet.AsEthAddr(wallettest.NewRandomAddress(rng, ethwallet.BackendID))}
}
// Use an ETH depositor with random addresses at index 0.
depositors[0] = ethchannel.NewETHDepositor(txETHGasLimit)
// Use an ERC20 depositor with random addresses at index 1.
token := wallettest.NewRandomAddress(rng, 1)
token := wallettest.NewRandomAddress(rng, ethwallet.BackendID)
depositors[1] = ethchannel.NewERC20Depositor(ethwallet.AsEthAddr(token), txERC20GasLimit)
return funder, assets, depositors, accs
}
Expand Down Expand Up @@ -444,7 +444,7 @@ func newNFunders(
// Start the auto-mining of blocks.
simBackend.StartMining(blockInterval)
t.Cleanup(simBackend.StopMining)
ksWallet := wallettest.RandomWallet(1).(*keystore.Wallet)
ksWallet := wallettest.RandomWallet(ethwallet.BackendID).(*keystore.Wallet)

deployAccount := &ksWallet.NewRandomAccount(rng).(*keystore.Account).Account
simBackend.FundAddress(ctx, deployAccount.Address)
Expand Down Expand Up @@ -475,7 +475,7 @@ func newNFunders(
funders = make([]*ethchannel.Funder, n)
for i := range parts {
acc := ksWallet.NewRandomAccount(rng).(*keystore.Account).Account
parts[i] = map[wallet.BackendID]wallet.Address{1: ethwallet.AsWalletAddr(acc.Address)}
parts[i] = map[wallet.BackendID]wallet.Address{ethwallet.BackendID: ethwallet.AsWalletAddr(acc.Address)}

simBackend.FundAddress(ctx, ethwallet.AsEthAddr(parts[i][1]))
err = fundERC20(ctx, cb, *tokenAcc, ethwallet.AsEthAddr(parts[i][1]), token, *asset2)
Expand All @@ -493,12 +493,12 @@ func newNFunders(
rng,
channeltest.WithParts(parts),
channeltest.WithChallengeDuration(uint64(n)*40000),
channeltest.WithBackend(1),
channeltest.WithBackend(ethwallet.BackendID),
)
allocation = channeltest.NewRandomAllocation(
rng,
channeltest.WithNumParts(n),
channeltest.WithBackend(1),
channeltest.WithBackend(ethwallet.BackendID),
channeltest.WithAssets(
ethchannel.NewAsset(chainID, assetAddr1),
ethchannel.NewAsset(chainID, assetAddr2),
Expand Down
5 changes: 3 additions & 2 deletions channel/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 - See NOTICE file for copyright holders.
// Copyright 2024 - 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.
Expand All @@ -15,9 +15,10 @@
package channel

import (
"github.com/perun-network/perun-eth-backend/wallet"
"perun.network/go-perun/channel"
)

func init() {
channel.SetBackend(new(Backend), 1)
channel.SetBackend(new(Backend), wallet.BackendID)
}
Loading

0 comments on commit 5cf3234

Please sign in to comment.