Skip to content

Commit ffd2dfa

Browse files
authored
🔀 Merge pull request #196 from perun-network/195-remove-params
Channel.Backend: Remove Params from Sign and Verify
2 parents 5b134c1 + c952b67 commit ffd2dfa

File tree

16 files changed

+103
-89
lines changed

16 files changed

+103
-89
lines changed

backend/ethereum/channel/adjudicator_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ import (
3535

3636
const defaultTxTimeout = 2 * time.Second
3737

38-
func testSignState(t *testing.T, accounts []*keystore.Account, params *channel.Params, state *channel.State) channel.Transaction {
39-
tx, err := signState(accounts, params, state)
38+
func testSignState(t *testing.T, accounts []*keystore.Account, state *channel.State) channel.Transaction {
39+
tx, err := signState(accounts, state)
4040
assert.NoError(t, err, "Sign should not return error")
4141
return tx
4242
}
4343

44-
func signState(accounts []*keystore.Account, params *channel.Params, state *channel.State) (channel.Transaction, error) {
44+
func signState(accounts []*keystore.Account, state *channel.State) (channel.Transaction, error) {
4545
sigs := make([][]byte, len(accounts))
4646
for i := range accounts {
47-
sig, err := channel.Sign(accounts[i], params, state)
47+
sig, err := channel.Sign(accounts[i], state)
4848
if err != nil {
4949
return channel.Transaction{}, errors.WithMessagef(err, "signing with account %d", i)
5050
}
@@ -87,7 +87,7 @@ func TestSubscribeRegistered(t *testing.T) {
8787
require.NoError(t, err)
8888
defer sub.Close()
8989
// Now test the register function
90-
tx := testSignState(t, s.Accs, params, state)
90+
tx := testSignState(t, s.Accs, state)
9191
req := channel.AdjudicatorReq{
9292
Params: params,
9393
Acc: s.Accs[0],

backend/ethereum/channel/backend.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ func (*Backend) CalcID(p *channel.Params) (id channel.ID) {
9090
}
9191

9292
// Sign signs the channel state as needed by the ethereum smart contracts.
93-
func (*Backend) Sign(acc wallet.Account, p *channel.Params, s *channel.State) (wallet.Sig, error) {
94-
return Sign(acc, p, s)
93+
func (*Backend) Sign(acc wallet.Account, s *channel.State) (wallet.Sig, error) {
94+
return Sign(acc, s)
9595
}
9696

9797
// Verify verifies that a state was signed correctly.
98-
func (*Backend) Verify(addr wallet.Address, p *channel.Params, s *channel.State, sig wallet.Sig) (bool, error) {
99-
return Verify(addr, p, s, sig)
98+
func (*Backend) Verify(addr wallet.Address, s *channel.State, sig wallet.Sig) (bool, error) {
99+
return Verify(addr, s, sig)
100100
}
101101

102102
// DecodeAsset decodes an asset from a stream.
@@ -126,7 +126,7 @@ func HashState(s *channel.State) (id channel.ID) {
126126
}
127127

128128
// Sign signs the channel state as needed by the ethereum smart contracts.
129-
func Sign(acc wallet.Account, p *channel.Params, s *channel.State) (wallet.Sig, error) {
129+
func Sign(acc wallet.Account, s *channel.State) (wallet.Sig, error) {
130130
state := ToEthState(s)
131131
enc, err := EncodeState(&state)
132132
if err != nil {
@@ -136,10 +136,7 @@ func Sign(acc wallet.Account, p *channel.Params, s *channel.State) (wallet.Sig,
136136
}
137137

138138
// Verify verifies that a state was signed correctly.
139-
func Verify(addr wallet.Address, p *channel.Params, s *channel.State, sig wallet.Sig) (bool, error) {
140-
if err := s.Valid(); err != nil {
141-
return false, errors.WithMessage(err, "invalid state")
142-
}
139+
func Verify(addr wallet.Address, s *channel.State, sig wallet.Sig) (bool, error) {
143140
state := ToEthState(s)
144141
enc, err := EncodeState(&state)
145142
if err != nil {

backend/ethereum/channel/conclude_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func testConcludeFinal(t *testing.T, numParts int) {
7171
})
7272
}
7373
ct.Wait("funding loop")
74-
tx := testSignState(t, s.Accs, params, state)
74+
tx := testSignState(t, s.Accs, state)
7575

7676
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
7777
defer cancel()
@@ -238,7 +238,7 @@ func fund(ctx context.Context, funders []*ethchannel.Funder, c paramsAndState) e
238238
func register(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystore.Account, ch paramsAndState, subChannels []paramsAndState) error {
239239
sub := make([]channel.SignedState, len(subChannels))
240240
for i, subCh := range subChannels {
241-
tx, err := signState(accounts, subCh.params, subCh.state)
241+
tx, err := signState(accounts, subCh.state)
242242
if err != nil {
243243
return err
244244
}
@@ -250,7 +250,7 @@ func register(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystor
250250
}
251251
}
252252

253-
tx, err := signState(accounts, ch.params, ch.state)
253+
tx, err := signState(accounts, ch.state)
254254
if err != nil {
255255
return err
256256
}
@@ -272,7 +272,7 @@ func addSubStates(subStates channel.StateMap, channels ...paramsAndState) {
272272
}
273273

274274
func withdraw(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystore.Account, c paramsAndState, subStates channel.StateMap) error {
275-
tx, err := signState(accounts, c.params, c.state)
275+
tx, err := signState(accounts, c.state)
276276
if err != nil {
277277
return err
278278
}

backend/ethereum/channel/register_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func registerMultiple(t *testing.T, numParts int, parallel bool) {
8989
subs := make([]channel.AdjudicatorSubscription, numParts)
9090
for i := 0; i < numParts; i++ {
9191
state.Version = uint64(int(state.Version) + i) // manipulate the state
92-
tx := testSignState(t, s.Accs, params, state)
92+
tx := testSignState(t, s.Accs, state)
9393
txs[i] = &tx
9494

9595
sleepDuration := time.Duration(rng.Int63n(10)+1) * time.Millisecond
@@ -162,7 +162,7 @@ func TestRegister_FinalState(t *testing.T) {
162162
require.NoError(t, err)
163163
defer sub.Close()
164164
// register
165-
tx := testSignState(t, s.Accs, params, state)
165+
tx := testSignState(t, s.Accs, state)
166166
req := channel.AdjudicatorReq{
167167
Params: params,
168168
Acc: s.Accs[0],
@@ -198,7 +198,7 @@ func TestRegister_CancelledContext(t *testing.T) {
198198
require.NoError(t, err)
199199
defer sub.Close()
200200
// register
201-
tx := testSignState(t, s.Accs, params, state)
201+
tx := testSignState(t, s.Accs, state)
202202
req := channel.AdjudicatorReq{
203203
Params: params,
204204
Acc: s.Accs[0],

backend/ethereum/channel/withdraw_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func withdrawMultipleConcurrentFinal(t *testing.T, numParts int, parallel bool)
6969
ct.Wait("funding loop")
7070
// manipulate the state
7171
state.IsFinal = true
72-
tx := testSignState(t, s.Accs, params, state)
72+
tx := testSignState(t, s.Accs, state)
7373

7474
// Now test the withdraw function
7575
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
@@ -148,7 +148,7 @@ func testWithdrawZeroBalance(t *testing.T, n int) {
148148
req := channel.AdjudicatorReq{
149149
Params: params,
150150
Acc: s.Accs[0],
151-
Tx: testSignState(t, s.Accs, params, state),
151+
Tx: testSignState(t, s.Accs, state),
152152
Idx: 0,
153153
}
154154
require.NoError(t, s.Adjs[0].Register(context.Background(), req, nil))
@@ -194,7 +194,7 @@ func TestWithdraw(t *testing.T) {
194194
testWithdraw := func(t *testing.T, shouldWork bool) {
195195
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
196196
defer cancel()
197-
req.Tx = testSignState(t, s.Accs, params, state)
197+
req.Tx = testSignState(t, s.Accs, state)
198198
err := s.Adjs[0].Withdraw(ctx, req, nil)
199199

200200
if shouldWork {
@@ -252,7 +252,7 @@ func TestWithdrawNonFinal(t *testing.T) {
252252
Params: params,
253253
Acc: s.Accs[0],
254254
Idx: 0,
255-
Tx: testSignState(t, s.Accs, params, state),
255+
Tx: testSignState(t, s.Accs, state),
256256
}
257257
require.NoError(t, adj.Register(ctx, req, nil))
258258
reg := sub.Next()

backend/sim/channel/backend.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (*backend) CalcID(p *channel.Params) (id channel.ID) {
5555
}
5656

5757
// Sign signs `state`.
58-
func (b *backend) Sign(addr wallet.Account, _params *channel.Params, state *channel.State) ([]byte, error) {
58+
func (b *backend) Sign(addr wallet.Account, state *channel.State) ([]byte, error) {
5959
log.WithFields(log.Fields{"channel": state.ID, "version": state.Version}).Tracef("Signing state")
6060

6161
buff := new(bytes.Buffer)
@@ -66,12 +66,7 @@ func (b *backend) Sign(addr wallet.Account, _params *channel.Params, state *chan
6666
}
6767

6868
// Verify verifies the signature for `state`.
69-
func (b *backend) Verify(addr wallet.Address, _params *channel.Params, state *channel.State, sig []byte) (bool, error) {
70-
if err := state.Valid(); err != nil {
71-
return false, errors.Wrap(err, "verifying invalid state")
72-
}
73-
log.WithFields(log.Fields{"channel": state.ID, "version": state.Version}).Tracef("Verifying state")
74-
69+
func (b *backend) Verify(addr wallet.Address, state *channel.State, sig []byte) (bool, error) {
7570
buff := new(bytes.Buffer)
7671
if err := state.Encode(buff); err != nil {
7772
return false, errors.WithMessage(err, "pack state")

channel/backend.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@ type Backend interface {
3636

3737
// Sign signs a channel's State with the given Account.
3838
// Returns the signature or an error.
39-
// The framework guarantees to not pass nil Account, *Params or *State, that
40-
// the IDs of them match and that Params.ID = ChannelID(Params).
41-
Sign(wallet.Account, *Params, *State) (wallet.Sig, error)
39+
Sign(wallet.Account, *State) (wallet.Sig, error)
4240

4341
// Verify verifies that the provided signature on the state belongs to the
4442
// provided address.
45-
// The framework guarantees to not pass nil Address, *Params or *State, that
46-
// the IDs of them match and that Params.ID = ChannelID(Params).
47-
Verify(addr wallet.Address, params *Params, state *State, sig wallet.Sig) (bool, error)
43+
Verify(addr wallet.Address, state *State, sig wallet.Sig) (bool, error)
4844

4945
// DecodeAsset decodes an asset from a stream.
5046
DecodeAsset(io.Reader) (Asset, error)
@@ -65,13 +61,13 @@ func CalcID(p *Params) ID {
6561
}
6662

6763
// Sign creates a signature from the account a on state s.
68-
func Sign(a wallet.Account, p *Params, s *State) (wallet.Sig, error) {
69-
return backend.Sign(a, p, s)
64+
func Sign(a wallet.Account, s *State) (wallet.Sig, error) {
65+
return backend.Sign(a, s)
7066
}
7167

7268
// Verify verifies that a signature was a valid signature from addr on a state.
73-
func Verify(addr wallet.Address, params *Params, state *State, sig wallet.Sig) (bool, error) {
74-
return backend.Verify(addr, params, state, sig)
69+
func Verify(addr wallet.Address, state *State, sig wallet.Sig) (bool, error) {
70+
return backend.Verify(addr, state, sig)
7571
}
7672

7773
// DecodeAsset decodes an Asset from an io.Reader.

channel/machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (m *machine) Sig() (sig wallet.Sig, err error) {
209209
}
210210

211211
if m.stagingTX.Sigs[m.idx] == nil {
212-
sig, err = Sign(m.acc, &m.params, m.stagingTX.State)
212+
sig, err = Sign(m.acc, m.stagingTX.State)
213213
if err != nil {
214214
return
215215
}
@@ -275,7 +275,7 @@ func (m *machine) AddSig(idx Index, sig wallet.Sig) error {
275275
return errors.Errorf("signature for idx %d already present (ID: %x)", idx, m.params.id)
276276
}
277277

278-
if ok, err := Verify(m.params.Parts[idx], &m.params, m.stagingTX.State, sig); err != nil {
278+
if ok, err := Verify(m.params.Parts[idx], m.stagingTX.State, sig); err != nil {
279279
return err
280280
} else if !ok {
281281
return errors.Errorf("invalid signature for idx %d (ID: %x)", idx, m.params.id)

channel/persistence/statemachine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestStateMachine(t *testing.T) {
6262
tpr.AssertEqual(csm)
6363
// remote signers
6464
for i := 1; i < n; i++ {
65-
sig, err := channel.Sign(accs[i], params, csm.StagingState())
65+
sig, err := channel.Sign(accs[i], csm.StagingState())
6666
require.NoError(err)
6767
sm.AddSig(nil, channel.Index(i), sig)
6868
tpr.AssertEqual(csm)

channel/persistence/test/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (c *Channel) SignAll(t require.TestingT) {
173173
c.AssertPersisted(c.ctx, t)
174174
// remote signers
175175
for i := range c.accounts {
176-
sig, err := channel.Sign(c.accounts[i], c.Params(), c.StagingState())
176+
sig, err := channel.Sign(c.accounts[i], c.StagingState())
177177
require.NoError(t, err)
178178
c.AddSig(nil, channel.Index(i), sig)
179179
c.AssertPersisted(c.ctx, t)

0 commit comments

Comments
 (0)