event_handlers

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandlerService

type EventHandlerService struct {
	// contains filtered or unexported fields
}

EventHandlerService processes blockchain events and updates the local database state accordingly. It handles events from both home channels (user state channels) and escrow channels (temporary lock channels). All handlers execute within database transactions provided by useStoreInTx to ensure atomicity.

func NewEventHandlerService

func NewEventHandlerService(useStoreInTx StoreTxProvider, logger log.Logger) *EventHandlerService

NewEventHandlerService creates a new EventHandlerService instance. The useStoreInTx parameter wraps all store operations in database transactions. The logger is used for structured logging of event processing.

func (*EventHandlerService) HandleEscrowDepositChallenged

func (s *EventHandlerService) HandleEscrowDepositChallenged(ctx context.Context, event *core.EscrowDepositChallengedEvent) error

HandleEscrowDepositChallenged processes the EscrowDepositChallenged event emitted when an escrow deposit is challenged on-chain. Similar to home channel challenges, it marks the channel as Challenged, sets the expiration time, and automatically schedules a checkpoint with the latest signed state to resolve the challenge.

func (*EventHandlerService) HandleEscrowDepositFinalized

func (s *EventHandlerService) HandleEscrowDepositFinalized(ctx context.Context, event *core.EscrowDepositFinalizedEvent) error

HandleEscrowDepositFinalized processes the EscrowDepositFinalized event emitted when an escrow deposit is successfully finalized on-chain. It updates the channel status to Closed and sets the final state version, completing the deposit lifecycle.

func (*EventHandlerService) HandleEscrowDepositInitiated

func (s *EventHandlerService) HandleEscrowDepositInitiated(ctx context.Context, event *core.EscrowDepositInitiatedEvent) error

HandleEscrowDepositInitiated processes the EscrowDepositInitiated event emitted when an escrow deposit operation begins on-chain. It updates the escrow channel status to Open, sets the state version, and schedules a checkpoint to finalize the deposit if a matching state exists in the database.

func (*EventHandlerService) HandleEscrowWithdrawalChallenged

func (s *EventHandlerService) HandleEscrowWithdrawalChallenged(ctx context.Context, event *core.EscrowWithdrawalChallengedEvent) error

HandleEscrowWithdrawalChallenged processes the EscrowWithdrawalChallenged event emitted when an escrow withdrawal is challenged on-chain. It marks the channel as Challenged, sets the expiration time, and schedules a checkpoint for escrow withdrawal with the latest signed state to resolve the challenge.

func (*EventHandlerService) HandleEscrowWithdrawalFinalized

func (s *EventHandlerService) HandleEscrowWithdrawalFinalized(ctx context.Context, event *core.EscrowWithdrawalFinalizedEvent) error

HandleEscrowWithdrawalFinalized processes the EscrowWithdrawalFinalized event emitted when an escrow withdrawal is successfully finalized on-chain. It updates the channel status to Closed and sets the final state version, completing the withdrawal lifecycle.

func (*EventHandlerService) HandleEscrowWithdrawalInitiated

func (s *EventHandlerService) HandleEscrowWithdrawalInitiated(ctx context.Context, event *core.EscrowWithdrawalInitiatedEvent) error

HandleEscrowWithdrawalInitiated processes the EscrowWithdrawalInitiated event emitted when an escrow withdrawal operation begins on-chain. It updates the escrow channel status to Open and sets the state version to reflect the initiated withdrawal.

func (*EventHandlerService) HandleHomeChannelChallenged

func (s *EventHandlerService) HandleHomeChannelChallenged(ctx context.Context, event *core.HomeChannelChallengedEvent) error

HandleHomeChannelChallenged processes the HomeChannelChallenged event emitted when a potentially stale state is submitted on-chain. It updates the channel status to Challenged, sets the challenge expiration time, and automatically schedules a checkpoint of the latest signed state if available to resolve the challenge.

func (*EventHandlerService) HandleHomeChannelCheckpointed

func (s *EventHandlerService) HandleHomeChannelCheckpointed(ctx context.Context, event *core.HomeChannelCheckpointedEvent) error

HandleHomeChannelCheckpointed processes the HomeChannelCheckpointed event emitted when a channel state is successfully checkpointed on-chain. It updates the channel's state version and clears the Challenged status if present, returning the channel to Open status.

func (*EventHandlerService) HandleHomeChannelClosed

func (s *EventHandlerService) HandleHomeChannelClosed(ctx context.Context, event *core.HomeChannelClosedEvent) error

HandleHomeChannelClosed processes the HomeChannelClosed event emitted when a home channel is finalized and closed on-chain. It updates the channel status to Closed and sets the final state version. Once closed, no further state updates are possible for this channel.

func (*EventHandlerService) HandleHomeChannelCreated

func (s *EventHandlerService) HandleHomeChannelCreated(ctx context.Context, event *core.HomeChannelCreatedEvent) error

HandleHomeChannelCreated processes the HomeChannelCreated event emitted when a home channel is successfully created on-chain. It updates the channel status to Open and sets the state version. The channel must exist in the database with type ChannelTypeHome, otherwise a warning is logged.

func (*EventHandlerService) HandleHomeChannelMigrated

func (s *EventHandlerService) HandleHomeChannelMigrated(ctx context.Context, event *core.HomeChannelMigratedEvent) error

HandleHomeChannelMigrated processes the HomeChannelMigrated event emitted when a home channel is migrated to a new version or blockchain. This is currently not implemented and logs a warning. TODO: Implement HomeChannelMigrated handler logic

func (*EventHandlerService) HandleUserLockedBalanceUpdated

func (s *EventHandlerService) HandleUserLockedBalanceUpdated(ctx context.Context, event *core.UserLockedBalanceUpdatedEvent) error

type MockStore

type MockStore struct {
	mock.Mock
}

MockStore is a mock implementation of the Store interface for testing

func (*MockStore) GetChannelByID

func (m *MockStore) GetChannelByID(channelID string) (*core.Channel, error)

GetChannelByID mocks retrieving a channel by its ID

func (*MockStore) GetLastStateByChannelID

func (m *MockStore) GetLastStateByChannelID(channelID string, signed bool) (*core.State, error)

GetLastStateByChannelID mocks retrieving the last state for a channel

func (*MockStore) GetStateByChannelIDAndVersion

func (m *MockStore) GetStateByChannelIDAndVersion(channelID string, version uint64) (*core.State, error)

GetStateByChannelIDAndVersion mocks retrieving a specific state version for a channel

func (*MockStore) ScheduleCheckpoint

func (m *MockStore) ScheduleCheckpoint(stateID string, chainID uint64) error

ScheduleCheckpoint mocks scheduling a checkpoint operation

func (*MockStore) ScheduleFinalizeEscrowDeposit

func (m *MockStore) ScheduleFinalizeEscrowDeposit(stateID string, chainID uint64) error

ScheduleFinalizeEscrowDeposit mocks scheduling an escrow deposit checkpoint

func (*MockStore) ScheduleFinalizeEscrowWithdrawal

func (m *MockStore) ScheduleFinalizeEscrowWithdrawal(stateID string, chainID uint64) error

ScheduleFinalizeEscrowWithdrawal mocks scheduling an escrow withdrawal checkpoint

func (*MockStore) ScheduleInitiateEscrowDeposit

func (m *MockStore) ScheduleInitiateEscrowDeposit(stateID string, chainID uint64) error

ScheduleInitiateEscrowDeposit mocks scheduling an escrow deposit checkpoint

func (*MockStore) UpdateChannel

func (m *MockStore) UpdateChannel(channel core.Channel) error

UpdateChannel mocks updating a channel in the database

func (*MockStore) UpdateUserStaked

func (m *MockStore) UpdateUserStaked(wallet string, blockchainID uint64, amount decimal.Decimal) error

UpdateUserStaked mocks updating the total staked amount for a user

type Store

type Store interface {
	// GetLastStateByChannelID retrieves the most recent state for a given channel.
	// If signed is true, only returns states with both user and node signatures.
	// Returns nil if no matching state exists.
	GetLastStateByChannelID(channelID string, signed bool) (*core.State, error)

	// GetStateByChannelIDAndVersion retrieves a specific state version for a channel.
	// Returns nil if the state with the specified version does not exist.
	GetStateByChannelIDAndVersion(channelID string, version uint64) (*core.State, error)

	// UpdateChannel persists changes to a channel's metadata (status, version, etc).
	// The channel must already exist in the database.
	UpdateChannel(channel core.Channel) error

	// GetChannelByID retrieves a channel by its unique identifier.
	// Returns nil if the channel does not exist.
	GetChannelByID(channelID string) (*core.Channel, error)

	// ScheduleCheckpoint schedules a checkpoint operation for a home channel state.
	// This queues the state to be submitted on-chain to update the channel's on-chain state.
	ScheduleCheckpoint(stateID string, chainID uint64) error

	// ScheduleInitiateEscrowDeposit schedules an initiate for an escrow deposit operation.
	// This queues the state to be submitted on-chain to finalize an escrow deposit.
	ScheduleInitiateEscrowDeposit(stateID string, chainID uint64) error

	// ScheduleFinalizeEscrowDeposit schedules a finalize for an escrow deposit operation.
	// This queues the state to be submitted on-chain to finalize an escrow deposit.
	ScheduleFinalizeEscrowDeposit(stateID string, chainID uint64) error

	// ScheduleFinalizeEscrowWithdrawal schedules a checkpoint for an escrow withdrawal operation.
	// This queues the state to be submitted on-chain to finalize an escrow withdrawal.
	ScheduleFinalizeEscrowWithdrawal(stateID string, chainID uint64) error

	// UpdateUserStaked updates the total staked amount for a user on a specific blockchain.
	UpdateUserStaked(wallet string, blockchainID uint64, amount decimal.Decimal) error
}

Store defines the persistence layer interface for channel and state data. All methods should be implemented to work within database transactions. Implementations are typically provided by the database layer and wrapped by StoreTxProvider.

type StoreTxHandler

type StoreTxHandler func(Store) error

StoreTxHandler is a function that executes Store operations within a transaction. If the handler returns an error, the transaction is rolled back; otherwise it's committed.

type StoreTxProvider

type StoreTxProvider func(StoreTxHandler) error

StoreTxProvider wraps Store operations in a database transaction. It accepts a StoreTxHandler and manages transaction lifecycle (begin, commit, rollback). Returns an error if the handler fails or the transaction cannot be committed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL