Documentation
¶
Index ¶
- func NewMockSigner() sign.Signer
- func VerifyNodeSignature(t *testing.T, nodeAddr string, data []byte, sigHex string)
- type Handler
- func (h *Handler) GetChannels(c *rpc.Context)
- func (h *Handler) GetEscrowChannel(c *rpc.Context)
- func (h *Handler) GetHomeChannel(c *rpc.Context)
- func (h *Handler) GetLastKeyStates(c *rpc.Context)
- func (h *Handler) GetLatestState(c *rpc.Context)
- func (h *Handler) RequestCreation(c *rpc.Context)
- func (h *Handler) SubmitSessionKeyState(c *rpc.Context)
- func (h *Handler) SubmitState(c *rpc.Context)
- type MemoryStore
- type MockAssetStore
- type MockMemoryStore
- type MockSigValidator
- type MockStatePacker
- type MockStore
- func (m *MockStore) BeginTx() (Store, func() error, func() error)
- func (m *MockStore) CheckActiveChannel(wallet, asset string) (string, *core.ChannelStatus, error)
- func (m *MockStore) CountSessionKeysForUser(userAddress string) (uint32, error)
- func (m *MockStore) CreateChannel(channel core.Channel) error
- func (m *MockStore) EnsureNoOngoingEscrowOperation(wallet, asset string) error
- func (m *MockStore) EnsureNoOngoingStateTransitions(wallet, asset string) error
- func (m *MockStore) GetActiveHomeChannel(wallet, asset string) (*core.Channel, error)
- func (m *MockStore) GetChannelByID(channelID string) (*core.Channel, error)
- func (m *MockStore) GetLastChannelSessionKeyStates(wallet string, sessionKey *string, includeInactive bool, limit, offset uint32) ([]core.ChannelSessionKeyStateV1, uint32, error)
- func (m *MockStore) GetLastChannelSessionKeyVersion(wallet, sessionKey string) (uint64, error)
- func (m *MockStore) GetLastUserState(wallet, asset string, signed bool) (*core.State, error)
- func (m *MockStore) GetNotClosedHomeChannel(wallet, asset string) (*core.Channel, error)
- func (m *MockStore) GetUserChannels(wallet string, status *core.ChannelStatus, asset *string, ...) ([]core.Channel, uint32, error)
- func (m *MockStore) HasNonClosedHomeChannel(wallet, asset string) (bool, error)
- func (m *MockStore) LockSessionKeyState(userAddress, sessionKey string, kind database.SessionKeyKind) (uint64, time.Time, error)
- func (m *MockStore) LockUserState(wallet, asset string) (decimal.Decimal, error)
- func (m *MockStore) RecordTransaction(tx core.Transaction, applicationID string) error
- func (m *MockStore) ScheduleInitiateEscrowWithdrawal(stateID string, chainID uint64) error
- func (m *MockStore) StoreChannelSessionKeyState(state core.ChannelSessionKeyStateV1) error
- func (m *MockStore) StoreUserState(state core.State, applicationID string) error
- func (m *MockStore) UpdateChannel(channel core.Channel) error
- func (m *MockStore) ValidateChannelSessionKeyForAsset(wallet, sessionKey, asset, metadataHash string) (bool, error)
- type SigValidator
- type SigValidatorType
- type Store
- type StoreTxHandler
- type StoreTxProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMockSigner ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler manages channel state transitions and provides RPC endpoints for state submission.
func NewHandler ¶
func NewHandler( useStoreInTx StoreTxProvider, memoryStore MemoryStore, nodeSigner *core.ChannelDefaultSigner, stateAdvancer core.StateAdvancer, statePacker core.StatePacker, nodeAddress string, minChallenge, maxChallenge uint32, m metrics.RuntimeMetricExporter, maxSessionKeyIDs int, maxSessionKeysPerUser int, ) *Handler
NewHandler creates a new Handler instance with the provided dependencies.
func (*Handler) GetChannels ¶
GetChannels retrieves all channels for a user with optional status/asset/type filtering and pagination.
func (*Handler) GetEscrowChannel ¶
GetEscrowChannel retrieves current on-chain escrow channel information.
Note: when the escrow channel has been closed by the on-chain purge queue (no signed FINALIZE_ESCROW_DEPOSIT was received before expiry), StateVersion on the returned channel reflects the initiate version (N) and does not advance to the finalize version (N+1).
func (*Handler) GetHomeChannel ¶
GetHomeChannel retrieves current on-chain home channel information.
func (*Handler) GetLastKeyStates ¶
GetLastKeyStates retrieves the latest channel session key states for a user with optional filtering by session key. Mandatory pagination caps response size to prevent unbounded reads.
func (*Handler) GetLatestState ¶
GetLatestState retrieves the current state of the user stored on the Node.
func (*Handler) RequestCreation ¶
RequestCreation processes channel creation requests from users. It validates the channel definition and initial state, checks for existing channels, signs the state with the node's key, and persists the new pending state and channel.
func (*Handler) SubmitSessionKeyState ¶
SubmitSessionKeyState processes channel session key state submissions for registration and updates.
func (*Handler) SubmitState ¶
SubmitState processes user-submitted state transitions, validates them against the current state, verifies user signatures, signs the new state with the node's key, and persists changes. For transfer transitions, it automatically creates corresponding receiver states. For certain transitions (escrow lock, etc.), it schedules blockchain actions.
type MemoryStore ¶
type MemoryStore interface {
// IsAssetSupported checks if a given asset (token) is supported on the specified blockchain.
IsAssetSupported(asset, tokenAddress string, blockchainID uint64) (bool, error)
// GetAssetDecimals checks if an asset exists and returns its decimals in YN
GetAssetDecimals(asset string) (uint8, error)
// GetTokenDecimals returns the decimals for a token on a specific blockchain
GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)
}
type MockAssetStore ¶
MockAssetStore is a mock implementation of the core.AssetStore interface
func (*MockAssetStore) GetAssetDecimals ¶
func (m *MockAssetStore) GetAssetDecimals(asset string) (uint8, error)
func (*MockAssetStore) GetTokenDecimals ¶
func (m *MockAssetStore) GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)
type MockMemoryStore ¶
MockMemoryStore is a mock implementation of the MemoryStore interface
func (*MockMemoryStore) GetAssetDecimals ¶
func (m *MockMemoryStore) GetAssetDecimals(asset string) (uint8, error)
func (*MockMemoryStore) GetTokenDecimals ¶
func (m *MockMemoryStore) GetTokenDecimals(blockchainID uint64, tokenAddress string) (uint8, error)
func (*MockMemoryStore) IsAssetSupported ¶
func (m *MockMemoryStore) IsAssetSupported(asset, tokenAddress string, blockchainID uint64) (bool, error)
type MockSigValidator ¶
MockSigValidator is a mock implementation of the SigValidator interface
type MockStatePacker ¶
type MockStore ¶
MockStore is a mock implementation of the Store interface
func (*MockStore) CheckActiveChannel ¶
func (*MockStore) CountSessionKeysForUser ¶
func (*MockStore) EnsureNoOngoingEscrowOperation ¶
func (*MockStore) EnsureNoOngoingStateTransitions ¶
func (*MockStore) GetActiveHomeChannel ¶
func (*MockStore) GetChannelByID ¶
func (*MockStore) GetLastChannelSessionKeyStates ¶
func (*MockStore) GetLastChannelSessionKeyVersion ¶
func (*MockStore) GetLastUserState ¶
func (*MockStore) GetNotClosedHomeChannel ¶
func (*MockStore) GetUserChannels ¶
func (*MockStore) HasNonClosedHomeChannel ¶
func (*MockStore) LockSessionKeyState ¶
func (*MockStore) LockUserState ¶
func (*MockStore) RecordTransaction ¶
func (m *MockStore) RecordTransaction(tx core.Transaction, applicationID string) error
func (*MockStore) ScheduleInitiateEscrowWithdrawal ¶
func (*MockStore) StoreChannelSessionKeyState ¶
func (m *MockStore) StoreChannelSessionKeyState(state core.ChannelSessionKeyStateV1) error
func (*MockStore) StoreUserState ¶
type SigValidator ¶
type SigValidator interface {
// Verify checks that the signature is valid for the given data and wallet address.
// Returns an error if the signature is invalid or cannot be verified.
Verify(wallet string, data, sig []byte) error
}
SigValidator validates cryptographic signatures on state transitions.
type SigValidatorType ¶
type SigValidatorType string
SigValidatorType identifies the signature validation algorithm to use.
const EcdsaSigValidatorType SigValidatorType = "ecdsa"
EcdsaSigValidatorType represents the ECDSA (Elliptic Curve Digital Signature Algorithm) validator, used for Ethereum-style signature verification.
type Store ¶
type Store interface {
// LockUserState locks a user's balance for update, must be used within a transaction.
// Returns the current balance.
LockUserState(wallet, asset string) (decimal.Decimal, error)
// GetLastUserState retrieves the most recent state for a user's asset.
// If signed is true, only returns states with both user and node signatures.
// Returns nil state if no matching state exists.
GetLastUserState(wallet, asset string, signed bool) (*core.State, error)
// CheckActiveChannel verifies if a user has an active home channel for the given asset
// and returns its approved signature validators and current status. A nil status means
// no active channel exists. "Active" includes Void (DB-only, awaiting onchain confirmation)
// and Open (materialized onchain); callers needing onchain materialization must additionally
// require Status == core.ChannelStatusOpen.
CheckActiveChannel(wallet, asset string) (string, *core.ChannelStatus, error)
// StoreUserState persists a new user state to the database.
// applicationID is the client-declared origin tag (rpc.ApplicationIDQueryParam);
// empty string is persisted as NULL.
StoreUserState(state core.State, applicationID string) error
// EnsureNoOngoingStateTransitions validates that no blockchain operations are pending
// that would conflict with submitting a new state transition.
EnsureNoOngoingStateTransitions(wallet, asset string) error
// EnsureNoOngoingEscrowOperation validates that the user has no in-flight escrow
// operation (escrow_lock, mutual_lock, or unfinalized escrow_deposit/escrow_withdraw)
// that would prevent issuing a receiver-side state.
EnsureNoOngoingEscrowOperation(wallet, asset string) error
// ScheduleInitiateEscrowWithdrawal queues a blockchain action to initiate
// withdrawal from an escrow channel (triggered by escrow_lock transition).
ScheduleInitiateEscrowWithdrawal(stateID string, chainID uint64) error
// RecordTransaction creates a transaction record linking state transitions
// to track the history of operations (deposits, withdrawals, transfers, etc.).
// applicationID is the client-declared origin tag (rpc.ApplicationIDQueryParam);
// empty string is persisted as NULL.
RecordTransaction(tx core.Transaction, applicationID string) error
// CreateChannel creates a new channel entity in the database.
// This is called during channel creation before the channel exists on-chain.
// The channel starts with OnChainStateVersion=0 to indicate it's pending blockchain confirmation.
CreateChannel(channel core.Channel) error
// GetChannelByID retrieves a channel by its unique identifier.
// Returns nil if the channel doesn't exist.
GetChannelByID(channelID string) (*core.Channel, error)
// GetActiveHomeChannel retrieves the active home channel for a user's wallet and asset.
// Returns nil if no home channel exists for the given wallet and asset.
GetActiveHomeChannel(wallet, asset string) (*core.Channel, error)
// GetNotClosedHomeChannel retrieves the home channel regardless of status as long as it
// is not Closed. Used by GetHomeChannel so the endpoint stays functional after an
// off-chain Finalize flips the channel to Closing.
GetNotClosedHomeChannel(wallet, asset string) (*core.Channel, 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
// HasNonClosedHomeChannel returns true if any home channel for (wallet, asset) has a
// status other than Closed, meaning a channel lifecycle is still in progress.
HasNonClosedHomeChannel(wallet, asset string) (bool, error)
// GetUserChannels retrieves all channels for a user with optional status, asset, and type filters.
GetUserChannels(wallet string, status *core.ChannelStatus, asset *string, channelType *core.ChannelType, limit, offset uint32) ([]core.Channel, uint32, error)
// LockSessionKeyState locks the (user, session_key, kind) pointer row for the surrounding
// transaction, returning the current version (0 if newly created) and the expires_at of
// the matching history row (zero time when version is 0). Callers use the expires_at to
// distinguish a reactivation (prev inactive → submitted active) from a rotation, so the
// per-user cap can be re-checked when a revoked slot is brought back.
LockSessionKeyState(userAddress, sessionKey string, kind database.SessionKeyKind) (latestVersion uint64, latestExpiresAt time.Time, err error)
// CountSessionKeysForUser returns the number of distinct session keys for the wallet
// across both kinds, used to enforce the per-user cap at submit time.
CountSessionKeysForUser(userAddress string) (uint32, error)
// StoreChannelSessionKeyState persists a channel session key state.
StoreChannelSessionKeyState(state core.ChannelSessionKeyStateV1) error
// GetLastChannelSessionKeyVersion returns the latest version for a (wallet, sessionKey) pair.
// Returns 0 if no state exists.
GetLastChannelSessionKeyVersion(wallet, sessionKey string) (uint64, error)
// GetLastChannelSessionKeyStates retrieves the latest channel session key states for a user,
// optionally filtered by session key. When includeInactive is false, only non-expired latest
// states are returned; when true, all latest states are returned regardless of expiry.
// Results are paginated.
GetLastChannelSessionKeyStates(wallet string, sessionKey *string, includeInactive bool, limit, offset uint32) ([]core.ChannelSessionKeyStateV1, uint32, error)
// ValidateChannelSessionKeyForAsset checks that a valid, non-expired session key state
// exists at its latest version for the (wallet, sessionKey) pair, includes the given asset,
// and matches the metadata hash.
ValidateChannelSessionKeyForAsset(wallet, sessionKey, asset, metadataHash string) (bool, error)
}
Store defines the persistence layer interface for channel state management. All methods should be implemented to work within database transactions.
type StoreTxHandler ¶
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.