Skip to content
Closed
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
19 changes: 9 additions & 10 deletions clearnode/app_session_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAppSessionService_CreateApplication(t *testing.T) {
require.NoError(t, GetWalletLedger(db, userAddressB).Record(userAccountIDB, "usdc", decimal.NewFromInt(200)))

capturedNotifications := make(map[string][]Notification)
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestAppSessionService_CreateApplication(t *testing.T) {
require.NoError(t, db.Create(&SignerWallet{Signer: userAddressA.Hex(), Wallet: userAddressA.Hex()}).Error)
require.NoError(t, GetWalletLedger(db, userAddressA).Record(userAccountIDA, "usdc", decimal.NewFromInt(50))) // Not enough

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
params := &CreateAppSessionParams{
Definition: AppDefinition{
Protocol: "test-proto",
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestAppSessionService_CreateApplication(t *testing.T) {
Status: ChannelStatusChallenged,
}).Error)

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
params := &CreateAppSessionParams{
Definition: AppDefinition{
Protocol: "test-proto",
Expand All @@ -175,7 +175,7 @@ func TestAppSessionService_CreateApplication(t *testing.T) {
require.NoError(t, db.Create(&SignerWallet{Signer: userAddressA.Hex(), Wallet: userAddressA.Hex()}).Error)
require.NoError(t, GetWalletLedger(db, userAddressA).Record(userAccountIDA, "usdc", decimal.NewFromInt(100)))

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
params := &CreateAppSessionParams{
Definition: AppDefinition{
Protocol: "test-proto",
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestAppSessionService_SubmitAppState(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
session := &AppSession{
SessionID: "test-session",
Protocol: "test-proto",
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestAppSessionService_SubmitAppState(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
session := &AppSession{
SessionID: "test-session-negative",
Protocol: "test-proto",
Expand Down Expand Up @@ -307,8 +307,7 @@ func TestAppSessionService_CloseApplication(t *testing.T) {
defer cleanup()

capturedNotifications := make(map[string][]Notification)
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {
capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
eventType: EventType(method),
Expand Down Expand Up @@ -395,7 +394,7 @@ func TestAppSessionService_CloseApplication(t *testing.T) {
defer cleanup()

capturedNotifications := make(map[string][]Notification)
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -457,7 +456,7 @@ func TestAppSessionService_CloseApplication(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()

service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params ...any) {}, nil))
service := NewAppSessionService(db, NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, nil))
session := &AppSession{
SessionID: "test-session-close-negative",
Protocol: "test-proto",
Expand Down
33 changes: 10 additions & 23 deletions clearnode/channel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -114,13 +113,12 @@ func (s *ChannelService) RequestResize(logger Logger, params *ResizeChannelParam

// 6) Encode & sign the new state
channelIDHash := common.HexToHash(channel.ChannelID)
encodedState, err := nitrolite.EncodeState(channelIDHash, nitrolite.IntentRESIZE, big.NewInt(int64(channel.Version)+1), encodedIntentions, allocations)
packedState, err := nitrolite.PackState(channelIDHash, nitrolite.IntentRESIZE, big.NewInt(int64(channel.Version)+1), encodedIntentions, allocations)
if err != nil {
logger.Error("failed to encode state hash", "error", err)
return ResizeChannelResponse{}, RPCErrorf("failed to encode state hash")
logger.Error("failed to pack state", "error", err)
return ResizeChannelResponse{}, RPCErrorf("failed to pack state")
}
stateHash := crypto.Keccak256Hash(encodedState).Hex()
sig, err := s.signer.NitroSign(encodedState)
sig, err := s.signer.Sign(packedState)
if err != nil {
logger.Error("failed to sign state", "error", err)
return ResizeChannelResponse{}, RPCErrorf("failed to sign state")
Expand All @@ -131,12 +129,7 @@ func (s *ChannelService) RequestResize(logger Logger, params *ResizeChannelParam
Intent: uint8(nitrolite.IntentRESIZE),
Version: channel.Version + 1,
StateData: hexutil.Encode(encodedIntentions),
StateHash: stateHash,
Signature: Signature{
V: sig.V,
R: hexutil.Encode(sig.R[:]),
S: hexutil.Encode(sig.S[:]),
},
Signature: sig,
}

for _, alloc := range allocations {
Expand Down Expand Up @@ -217,13 +210,12 @@ func (s *ChannelService) RequestClose(logger Logger, params *CloseChannelParams,
logger.Error("failed to decode state data hex", "error", err)
return CloseChannelResponse{}, RPCErrorf("failed to decode state data hex")
}
encodedState, err := nitrolite.EncodeState(common.HexToHash(channel.ChannelID), nitrolite.IntentFINALIZE, big.NewInt(int64(channel.Version)+1), stateDataBytes, allocations)
packedState, err := nitrolite.PackState(common.HexToHash(channel.ChannelID), nitrolite.IntentFINALIZE, big.NewInt(int64(channel.Version)+1), stateDataBytes, allocations)
if err != nil {
logger.Error("failed to encode state hash", "error", err)
return CloseChannelResponse{}, RPCErrorf("failed to encode state hash")
logger.Error("failed to pack state", "error", err)
return CloseChannelResponse{}, RPCErrorf("failed to pack state")
}
stateHash := crypto.Keccak256Hash(encodedState).Hex()
sig, err := s.signer.NitroSign(encodedState)
sig, err := s.signer.Sign(packedState)
if err != nil {
logger.Error("failed to sign state", "error", err)
return CloseChannelResponse{}, RPCErrorf("failed to sign state")
Expand All @@ -234,12 +226,7 @@ func (s *ChannelService) RequestClose(logger Logger, params *CloseChannelParams,
Intent: uint8(nitrolite.IntentFINALIZE),
Version: channel.Version + 1,
StateData: stateDataHex,
StateHash: stateHash,
Signature: Signature{
V: sig.V,
R: hexutil.Encode(sig.R[:]),
S: hexutil.Encode(sig.S[:]),
},
Signature: sig,
}

for _, alloc := range allocations {
Expand Down
4 changes: 2 additions & 2 deletions clearnode/custody.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *Custody) Join(channelID string, lastStateData []byte) (common.Hash, err
// The broker will always join as participant with index 1 (second participant)
index := big.NewInt(1)

sig, err := c.signer.NitroSign(lastStateData)
sig, err := c.signer.Sign(lastStateData)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to sign data: %w", err)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ func (c *Custody) handleCreated(logger Logger, ev *nitrolite.CustodyCreated) {
return
}

encodedState, err := nitrolite.EncodeState(ev.ChannelId, nitrolite.IntentINITIALIZE, big.NewInt(0), ev.Initial.Data, ev.Initial.Allocations)
encodedState, err := nitrolite.PackState(ev.ChannelId, nitrolite.IntentINITIALIZE, big.NewInt(0), ev.Initial.Data, ev.Initial.Allocations)
if err != nil {
logger.Error("error encoding state hash", "error", err)
return
Expand Down
26 changes: 13 additions & 13 deletions clearnode/custody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func setupMockCustody(t *testing.T) (*Custody, *gorm.DB, func()) {
custody: contract,
chainID: uint32(chainID.Int64()),
adjudicatorAddress: newTestCommonAddress("0xAdjudicatorAddress"),
wsNotifier: NewWSNotifier(func(userID string, method string, params ...any) {}, logger),
wsNotifier: NewWSNotifier(func(userID string, method string, params RPCDataParams) {}, logger),
logger: logger,
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func createMockCreatedEvent(t *testing.T, signer *Signer, token string, amount *
Version: big.NewInt(0),
Data: []byte{},
Allocations: allocation,
Sigs: []nitrolite.Signature{},
Sigs: [][]byte{},
}

event := &nitrolite.CustodyCreated{
Expand Down Expand Up @@ -189,7 +189,7 @@ func createMockClosedEvent(t *testing.T, signer *Signer, token string, amount *b
Version: big.NewInt(1),
Data: []byte{},
Allocations: allocation,
Sigs: []nitrolite.Signature{},
Sigs: [][]byte{},
}

event := &nitrolite.CustodyClosed{
Expand Down Expand Up @@ -226,7 +226,7 @@ func createMockChallengedEvent(t *testing.T, signer *Signer, token string, amoun
Version: big.NewInt(2),
Data: []byte{},
Allocations: allocation,
Sigs: []nitrolite.Signature{},
Sigs: [][]byte{},
}

event := &nitrolite.CustodyChallenged{
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestHandleCreatedEvent(t *testing.T) {
Version: big.NewInt(0),
Data: []byte{},
Allocations: allocation,
Sigs: []nitrolite.Signature{},
Sigs: [][]byte{},
}

mockEvent := &nitrolite.CustodyCreated{
Expand All @@ -325,7 +325,7 @@ func TestHandleCreatedEvent(t *testing.T) {
}

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {
if capturedNotifications[userID] == nil {
capturedNotifications[userID] = make([]Notification, 0)
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestHandleJoinedEvent(t *testing.T) {
_, mockEvent := createMockJoinedEvent(t)

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestHandleJoinedEvent(t *testing.T) {
_, mockEvent := createMockJoinedEvent(t)

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestHandleClosedEvent(t *testing.T) {
_, mockEvent := createMockClosedEvent(t, custody.signer, tokenAddress, finalAmount)

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -755,7 +755,7 @@ func TestHandleClosedEvent(t *testing.T) {
_, mockEvent := createMockClosedEvent(t, custody.signer, tokenAddress, channelAmount.BigInt())

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -826,7 +826,7 @@ func TestHandleChallengedEvent(t *testing.T) {
_, mockEvent := createMockChallengedEvent(t, custody.signer, tokenAddress, amount.BigInt())

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -902,7 +902,7 @@ func TestHandleResizedEvent(t *testing.T) {
_, mockEvent := createMockResizedEvent(t, deltaAmount.BigInt())

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down Expand Up @@ -1062,7 +1062,7 @@ func TestHandleResizedEvent(t *testing.T) {
_, mockEvent := createMockResizedEvent(t, big.NewInt(500000))

capturedNotifications := make(map[string][]Notification)
custody.wsNotifier.notify = func(userID string, method string, params ...any) {
custody.wsNotifier.notify = func(userID string, method string, params RPCDataParams) {

capturedNotifications[userID] = append(capturedNotifications[userID], Notification{
userID: userID,
Expand Down
Loading