Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions backend/ethereum/channel/adjudicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ import (

const defaultTxTimeout = 2 * time.Second

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

func signState(accounts []*keystore.Account, params *channel.Params, state *channel.State) (channel.Transaction, error) {
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], params, state)
sig, err := channel.Sign(accounts[i], state)
if err != nil {
return channel.Transaction{}, errors.WithMessagef(err, "signing with account %d", i)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestSubscribeRegistered(t *testing.T) {
require.NoError(t, err)
defer sub.Close()
// Now test the register function
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)
req := channel.AdjudicatorReq{
Params: params,
Acc: s.Accs[0],
Expand Down
15 changes: 6 additions & 9 deletions backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func (*Backend) CalcID(p *channel.Params) (id channel.ID) {
}

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

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

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

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

// Verify verifies that a state was signed correctly.
func Verify(addr wallet.Address, p *channel.Params, s *channel.State, sig wallet.Sig) (bool, error) {
if err := s.Valid(); err != nil {
return false, errors.WithMessage(err, "invalid state")
}
func Verify(addr wallet.Address, s *channel.State, sig wallet.Sig) (bool, error) {
state := ToEthState(s)
enc, err := EncodeState(&state)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions backend/ethereum/channel/conclude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testConcludeFinal(t *testing.T, numParts int) {
})
}
ct.Wait("funding loop")
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)

ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
defer cancel()
Expand Down Expand Up @@ -238,7 +238,7 @@ func fund(ctx context.Context, funders []*ethchannel.Funder, c paramsAndState) e
func register(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystore.Account, ch paramsAndState, subChannels []paramsAndState) error {
sub := make([]channel.SignedState, len(subChannels))
for i, subCh := range subChannels {
tx, err := signState(accounts, subCh.params, subCh.state)
tx, err := signState(accounts, subCh.state)
if err != nil {
return err
}
Expand All @@ -250,7 +250,7 @@ func register(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystor
}
}

tx, err := signState(accounts, ch.params, ch.state)
tx, err := signState(accounts, ch.state)
if err != nil {
return err
}
Expand All @@ -272,7 +272,7 @@ func addSubStates(subStates channel.StateMap, channels ...paramsAndState) {
}

func withdraw(ctx context.Context, adj *test.SimAdjudicator, accounts []*keystore.Account, c paramsAndState, subStates channel.StateMap) error {
tx, err := signState(accounts, c.params, c.state)
tx, err := signState(accounts, c.state)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions backend/ethereum/channel/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func registerMultiple(t *testing.T, numParts int, parallel bool) {
subs := make([]channel.AdjudicatorSubscription, numParts)
for i := 0; i < numParts; i++ {
state.Version = uint64(int(state.Version) + i) // manipulate the state
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)
txs[i] = &tx

sleepDuration := time.Duration(rng.Int63n(10)+1) * time.Millisecond
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestRegister_FinalState(t *testing.T) {
require.NoError(t, err)
defer sub.Close()
// register
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)
req := channel.AdjudicatorReq{
Params: params,
Acc: s.Accs[0],
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestRegister_CancelledContext(t *testing.T) {
require.NoError(t, err)
defer sub.Close()
// register
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)
req := channel.AdjudicatorReq{
Params: params,
Acc: s.Accs[0],
Expand Down
8 changes: 4 additions & 4 deletions backend/ethereum/channel/withdraw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func withdrawMultipleConcurrentFinal(t *testing.T, numParts int, parallel bool)
ct.Wait("funding loop")
// manipulate the state
state.IsFinal = true
tx := testSignState(t, s.Accs, params, state)
tx := testSignState(t, s.Accs, state)

// Now test the withdraw function
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
Expand Down Expand Up @@ -148,7 +148,7 @@ func testWithdrawZeroBalance(t *testing.T, n int) {
req := channel.AdjudicatorReq{
Params: params,
Acc: s.Accs[0],
Tx: testSignState(t, s.Accs, params, state),
Tx: testSignState(t, s.Accs, state),
Idx: 0,
}
require.NoError(t, s.Adjs[0].Register(context.Background(), req, nil))
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestWithdraw(t *testing.T) {
testWithdraw := func(t *testing.T, shouldWork bool) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout)
defer cancel()
req.Tx = testSignState(t, s.Accs, params, state)
req.Tx = testSignState(t, s.Accs, state)
err := s.Adjs[0].Withdraw(ctx, req, nil)

if shouldWork {
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestWithdrawNonFinal(t *testing.T) {
Params: params,
Acc: s.Accs[0],
Idx: 0,
Tx: testSignState(t, s.Accs, params, state),
Tx: testSignState(t, s.Accs, state),
}
require.NoError(t, adj.Register(ctx, req, nil))
reg := sub.Next()
Expand Down
9 changes: 2 additions & 7 deletions backend/sim/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (*backend) CalcID(p *channel.Params) (id channel.ID) {
}

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

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

// Verify verifies the signature for `state`.
func (b *backend) Verify(addr wallet.Address, _params *channel.Params, state *channel.State, sig []byte) (bool, error) {
if err := state.Valid(); err != nil {
return false, errors.Wrap(err, "verifying invalid state")
}
log.WithFields(log.Fields{"channel": state.ID, "version": state.Version}).Tracef("Verifying state")

func (b *backend) Verify(addr wallet.Address, state *channel.State, sig []byte) (bool, error) {
buff := new(bytes.Buffer)
if err := state.Encode(buff); err != nil {
return false, errors.WithMessage(err, "pack state")
Expand Down
16 changes: 6 additions & 10 deletions channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ type Backend interface {

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

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

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

// Sign creates a signature from the account a on state s.
func Sign(a wallet.Account, p *Params, s *State) (wallet.Sig, error) {
return backend.Sign(a, p, s)
func Sign(a wallet.Account, s *State) (wallet.Sig, error) {
return backend.Sign(a, s)
}

// Verify verifies that a signature was a valid signature from addr on a state.
func Verify(addr wallet.Address, params *Params, state *State, sig wallet.Sig) (bool, error) {
return backend.Verify(addr, params, state, sig)
func Verify(addr wallet.Address, state *State, sig wallet.Sig) (bool, error) {
return backend.Verify(addr, state, sig)
}

// DecodeAsset decodes an Asset from an io.Reader.
Expand Down
4 changes: 2 additions & 2 deletions channel/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (m *machine) Sig() (sig wallet.Sig, err error) {
}

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

if ok, err := Verify(m.params.Parts[idx], &m.params, m.stagingTX.State, sig); err != nil {
if ok, err := Verify(m.params.Parts[idx], m.stagingTX.State, sig); err != nil {
return err
} else if !ok {
return errors.Errorf("invalid signature for idx %d (ID: %x)", idx, m.params.id)
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/statemachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestStateMachine(t *testing.T) {
tpr.AssertEqual(csm)
// remote signers
for i := 1; i < n; i++ {
sig, err := channel.Sign(accs[i], params, csm.StagingState())
sig, err := channel.Sign(accs[i], csm.StagingState())
require.NoError(err)
sm.AddSig(nil, channel.Index(i), sig)
tpr.AssertEqual(csm)
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/test/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *Channel) SignAll(t require.TestingT) {
c.AssertPersisted(c.ctx, t)
// remote signers
for i := range c.accounts {
sig, err := channel.Sign(c.accounts[i], c.Params(), c.StagingState())
sig, err := channel.Sign(c.accounts[i], c.StagingState())
require.NoError(t, err)
c.AddSig(nil, channel.Index(i), sig)
c.AssertPersisted(c.ctx, t)
Expand Down
2 changes: 1 addition & 1 deletion channel/statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *StateMachine) CheckUpdate(
return err
}

if ok, err := Verify(m.params.Parts[sigIdx], &m.params, state, sig); err != nil {
if ok, err := Verify(m.params.Parts[sigIdx], state, sig); err != nil {
return errors.WithMessagef(err, "verifying signature[%d]", sigIdx)
} else if !ok {
return errors.Errorf("invalid signature[%d]", sigIdx)
Expand Down
Loading