Documentation
¶
Overview ¶
Package consensus_channel manages a running ledger channel.
Index ¶
- Constants
- type Add
- type Balance
- type ConsensusChannel
- func (c *ConsensusChannel) Clone() *ConsensusChannel
- func (c *ConsensusChannel) ConsensusTurnNum() uint64
- func (c *ConsensusChannel) ConsensusVars() Vars
- func (c *ConsensusChannel) FixedPart() state.FixedPart
- func (c *ConsensusChannel) Follower() common.Address
- func (c *ConsensusChannel) FundingTargets() []types.Destination
- func (c *ConsensusChannel) HasRemovalBeenProposed(target types.Destination) bool
- func (c *ConsensusChannel) HasRemovalBeenProposedNext(target types.Destination) bool
- func (c *ConsensusChannel) Includes(g Guarantee) bool
- func (c *ConsensusChannel) IncludesTarget(target types.Destination) bool
- func (c *ConsensusChannel) IsFollower() bool
- func (c *ConsensusChannel) IsLeader() bool
- func (c *ConsensusChannel) IsProposed(g Guarantee) (bool, error)
- func (c *ConsensusChannel) IsProposedNext(g Guarantee) (bool, error)
- func (c *ConsensusChannel) Leader() common.Address
- func (c ConsensusChannel) MarshalJSON() ([]byte, error)
- func (c *ConsensusChannel) Participants() []types.Address
- func (c *ConsensusChannel) ProposalQueue() []SignedProposal
- func (c *ConsensusChannel) Propose(proposal Proposal, sk []byte) (SignedProposal, error)
- func (c *ConsensusChannel) Receive(sp SignedProposal) error
- func (c *ConsensusChannel) SignNextProposal(expectedProposal Proposal, sk []byte) (SignedProposal, error)
- func (c *ConsensusChannel) Signatures() [2]state.Signature
- func (cc *ConsensusChannel) SupportedSignedState() state.SignedState
- func (c *ConsensusChannel) UnmarshalJSON(data []byte) error
- type Guarantee
- type LedgerOutcome
- func (o *LedgerOutcome) AsOutcome() outcome.Exit
- func (lo *LedgerOutcome) Clone() LedgerOutcome
- func (lo *LedgerOutcome) Follower() Balance
- func (o LedgerOutcome) FundingTargets() []types.Destination
- func (o *LedgerOutcome) IncludesTarget(target types.Destination) bool
- func (lo *LedgerOutcome) Leader() Balance
- func (l LedgerOutcome) MarshalJSON() ([]byte, error)
- func (l *LedgerOutcome) UnmarshalJSON(data []byte) error
- type Proposal
- type ProposalType
- type Remove
- type SignedProposal
- type SignedVars
- type Vars
Constants ¶
const ( ErrIncorrectChannelID = types.ConstError("proposal ID and channel ID do not match") ErrIncorrectTurnNum = types.ConstError("incorrect turn number") ErrInvalidDeposit = types.ConstError("unable to divert to guarantee: invalid deposit") ErrInsufficientFunds = types.ConstError("insufficient funds") ErrDuplicateGuarantee = types.ConstError("duplicate guarantee detected") ErrGuaranteeNotFound = types.ConstError("guarantee not found") ErrInvalidAmount = types.ConstError("left amount is greater than the guarantee amount") )
const ( Leader ledgerIndex = 0 Follower ledgerIndex = 1 )
const ( ErrNotFollower = types.ConstError("method may only be called by channel follower") ErrNoProposals = types.ConstError("no proposals in the queue") ErrUnsupportedQueuedProposal = types.ConstError("only Add proposal is supported for queued proposals") ErrUnsupportedExpectedProposal = types.ConstError("only Add proposal is supported for expected update") ErrNonMatchingProposals = types.ConstError("expected proposal does not match first proposal in the queue") ErrInvalidProposalSignature = types.ConstError("invalid signature for proposal") ErrInvalidTurnNum = types.ConstError("the proposal turn number is not the next turn number") )
const ( ErrNotLeader = types.ConstError("method may only be called by the channel leader") ErrProposalQueueExhausted = types.ConstError("proposal queue exhausted") ErrWrongSigner = types.ConstError("proposal incorrectly signed") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Add ¶
type Add struct { Guarantee // LeftDeposit is the portion of the Add's amount that will be deducted from left participant's ledger balance. // // The right participant's deduction is computed as the difference between the guarantee amount and LeftDeposit. LeftDeposit *big.Int }
Add encodes a proposal to add a guarantee to a ConsensusChannel.
func (Add) MarshalJSON ¶
MarshalJSON returns a JSON representation of the Add
func (Add) RightDeposit ¶
RightDeposit computes the deposit from the right participant such that a.LeftDeposit + a.RightDeposit() fully funds a's guarantee.
func (*Add) UnmarshalJSON ¶
UnmarshalJSON populates the receiver with the json-encoded data
type Balance ¶
type Balance struct {
// contains filtered or unexported fields
}
Balance is a convenient, ergonomic representation of a single-asset Allocation of type 0, ie. a simple allocation.
func NewBalance ¶
func NewBalance(destination types.Destination, amount *big.Int) Balance
NewBalance returns a new Balance struct with the given destination and amount.
func (Balance) AsAllocation ¶
func (b Balance) AsAllocation() outcome.Allocation
AsAllocation converts a Balance struct into the on-chain outcome.Allocation type.
func (Balance) MarshalJSON ¶
MarshalJSON returns a JSON representation of the Balance
func (*Balance) UnmarshalJSON ¶
UnmarshalJSON populates the receiver with the json-encoded data
type ConsensusChannel ¶
type ConsensusChannel struct { Id types.Destination MyIndex ledgerIndex OnChainFunding types.Funds // contains filtered or unexported fields }
ConsensusChannel is used to manage states in a running ledger channel.
func NewFollowerChannel ¶
func NewFollowerChannel(fp state.FixedPart, turnNum uint64, outcome LedgerOutcome, signatures [2]state.Signature) (ConsensusChannel, error)
NewFollowerChannel constructs a new FollowerChannel
func NewLeaderChannel ¶
func NewLeaderChannel(fp state.FixedPart, turnNum uint64, outcome LedgerOutcome, signatures [2]state.Signature) (ConsensusChannel, error)
NewLeaderChannel constructs a new LeaderChannel
func (*ConsensusChannel) Clone ¶
func (c *ConsensusChannel) Clone() *ConsensusChannel
Clone returns a deep copy of the receiver.
func (*ConsensusChannel) ConsensusTurnNum ¶
func (c *ConsensusChannel) ConsensusTurnNum() uint64
ConsensusTurnNum returns the turn number of the current consensus state.
func (*ConsensusChannel) ConsensusVars ¶
func (c *ConsensusChannel) ConsensusVars() Vars
ConsensusVars returns the vars of the consensus state The consensus state is the latest state that has been signed by both parties.
func (*ConsensusChannel) FixedPart ¶
func (c *ConsensusChannel) FixedPart() state.FixedPart
FixedPart returns the fixed part of the channel.
func (*ConsensusChannel) Follower ¶
func (c *ConsensusChannel) Follower() common.Address
Follower returns the address of the participant who receives and contersigns proposals.
func (*ConsensusChannel) FundingTargets ¶
func (c *ConsensusChannel) FundingTargets() []types.Destination
FundingTargets returns a list of channels funded by the ConsensusChannel
func (*ConsensusChannel) HasRemovalBeenProposed ¶
func (c *ConsensusChannel) HasRemovalBeenProposed(target types.Destination) bool
HasRemovalBeenProposed returns whether or not a proposal exists to remove the guarantee for the target.
func (*ConsensusChannel) HasRemovalBeenProposedNext ¶
func (c *ConsensusChannel) HasRemovalBeenProposedNext(target types.Destination) bool
HasRemovalBeenProposedNext returns whether or not the next proposal in the queue is a remove proposal for the given target
func (*ConsensusChannel) Includes ¶
func (c *ConsensusChannel) Includes(g Guarantee) bool
Includes returns whether or not the consensus state includes the given guarantee.
func (*ConsensusChannel) IncludesTarget ¶
func (c *ConsensusChannel) IncludesTarget(target types.Destination) bool
IncludesTarget returns whether or not the consensus state includes a guarantee addressed to the given target.
func (*ConsensusChannel) IsFollower ¶
func (c *ConsensusChannel) IsFollower() bool
IsFollower returns true if the calling client is the follower of the channel, and false otherwise.
func (*ConsensusChannel) IsLeader ¶
func (c *ConsensusChannel) IsLeader() bool
IsLeader returns true if the calling client is the leader of the channel, and false otherwise.
func (*ConsensusChannel) IsProposed ¶
func (c *ConsensusChannel) IsProposed(g Guarantee) (bool, error)
IsProposed returns true if a proposal in the queue would lead to g being included in the receiver's outcome, and false otherwise.
Specific clarification: If the current outcome already includes g, IsProposed returns false.
func (*ConsensusChannel) IsProposedNext ¶
func (c *ConsensusChannel) IsProposedNext(g Guarantee) (bool, error)
IsProposedNext returns true if the next proposal in the queue would lead to g being included in the receiver's outcome, and false otherwise.
func (*ConsensusChannel) Leader ¶
func (c *ConsensusChannel) Leader() common.Address
Leader returns the address of the participant responsible for proposing.
func (ConsensusChannel) MarshalJSON ¶
func (c ConsensusChannel) MarshalJSON() ([]byte, error)
MarshalJSON returns a JSON representation of the ConsensusChannel
func (*ConsensusChannel) Participants ¶
func (c *ConsensusChannel) Participants() []types.Address
Participants returns the channel participants.
func (*ConsensusChannel) ProposalQueue ¶
func (c *ConsensusChannel) ProposalQueue() []SignedProposal
ProposalQueue returns the current queue of proposals, ordered by TurnNum.
func (*ConsensusChannel) Propose ¶
func (c *ConsensusChannel) Propose(proposal Proposal, sk []byte) (SignedProposal, error)
Propose is called by the Leader and receives a proposal to add or remove a guarantee, and generates and stores a SignedProposal in the queue, returning the resulting SignedProposal
func (*ConsensusChannel) Receive ¶
func (c *ConsensusChannel) Receive(sp SignedProposal) error
Receive accepts a proposal signed by the ConsensusChannel counterparty, validates its signature, and performs updates to the proposal queue and consensus state.
func (*ConsensusChannel) SignNextProposal ¶
func (c *ConsensusChannel) SignNextProposal(expectedProposal Proposal, sk []byte) (SignedProposal, error)
SignNextProposal is called by the follower and inspects whether the expected proposal matches the first proposal in the queue. If so, the proposal is removed from the queue and integrated into the channel state.
func (*ConsensusChannel) Signatures ¶
func (c *ConsensusChannel) Signatures() [2]state.Signature
Signatures returns the signatures on the currently supported state.
func (*ConsensusChannel) SupportedSignedState ¶
func (cc *ConsensusChannel) SupportedSignedState() state.SignedState
SupportedSignedState returns the latest supported signed state.
func (*ConsensusChannel) UnmarshalJSON ¶
func (c *ConsensusChannel) UnmarshalJSON(data []byte) error
UnmarshalJSON populates the receiver with the json-encoded data
type Guarantee ¶
type Guarantee struct {
// contains filtered or unexported fields
}
Guarantee is a convenient, ergonomic representation of a single-asset Allocation of type 1, ie. a guarantee.
func NewGuarantee ¶
func NewGuarantee(amount *big.Int, target types.Destination, left types.Destination, right types.Destination) Guarantee
NewGuarantee constructs a new guarantee.
func (Guarantee) AsAllocation ¶
func (g Guarantee) AsAllocation() outcome.Allocation
AsAllocation converts a Balance struct into the on-chain outcome.Allocation type
func (Guarantee) MarshalJSON ¶
MarshalJSON returns a JSON representation of the Guarantee
func (Guarantee) Target ¶
func (g Guarantee) Target() types.Destination
Target returns the target of the guarantee.
func (*Guarantee) UnmarshalJSON ¶
UnmarshalJSON populates the receiver with the json-encoded data
type LedgerOutcome ¶
type LedgerOutcome struct {
// contains filtered or unexported fields
}
LedgerOutcome encodes the outcome of a ledger channel involving a "leader" and "follower" participant.
This struct does not store items in sorted order. The conventional ordering of allocation items is: [leader, follower, ...guaranteesSortedByTargetDestination]
func FromExit ¶
func FromExit(sae outcome.SingleAssetExit) (LedgerOutcome, error)
FromExit creates a new LedgerOutcome from the given SingleAssetExit.
It makes the following assumptions about the exit:
- The first allocation entry is for the ledger leader
- The second allocation entry is for the ledger follower
- All other allocations are guarantees
func NewLedgerOutcome ¶
func NewLedgerOutcome(assetAddress types.Address, leader, follower Balance, guarantees []Guarantee) *LedgerOutcome
NewLedgerOutcome creates a new ledger outcome with the given asset address, balances, and guarantees.
func (*LedgerOutcome) AsOutcome ¶
func (o *LedgerOutcome) AsOutcome() outcome.Exit
AsOutcome converts a LedgerOutcome to an on-chain exit according to the following convention:
- the "leader" balance is first
- the "follower" balance is second
- guarantees follow, sorted according to their target destinations
func (*LedgerOutcome) Clone ¶
func (lo *LedgerOutcome) Clone() LedgerOutcome
Clone returns a deep copy of the receiver.
func (*LedgerOutcome) Follower ¶
func (lo *LedgerOutcome) Follower() Balance
Follower returns the follower's balance.
func (LedgerOutcome) FundingTargets ¶
func (o LedgerOutcome) FundingTargets() []types.Destination
FundingTargets returns a list of channels funded by the LedgerOutcome
func (*LedgerOutcome) IncludesTarget ¶
func (o *LedgerOutcome) IncludesTarget(target types.Destination) bool
IncludesTarget returns true when the receiver includes a guarantee that targets the given destination.
func (*LedgerOutcome) Leader ¶
func (lo *LedgerOutcome) Leader() Balance
Leader returns the leader's balance.
func (LedgerOutcome) MarshalJSON ¶
func (l LedgerOutcome) MarshalJSON() ([]byte, error)
MarshalJSON returns a JSON representation of the LedgerOutcome
func (*LedgerOutcome) UnmarshalJSON ¶
func (l *LedgerOutcome) UnmarshalJSON(data []byte) error
UnmarshalJSON populates the receiver with the json-encoded data
type Proposal ¶
type Proposal struct { // LedgerID is the ChannelID of the ConsensusChannel which should receive the proposal. // // The target virtual channel ID is contained in the Add / Remove struct. LedgerID types.Destination ToAdd Add ToRemove Remove }
Proposal is a proposal either to add or to remove a guarantee.
Exactly one of {toAdd, toRemove} should be non nil.
func NewAddProposal ¶
NewAddProposal constructs a proposal with a valid Add proposal and empty remove proposal.
func NewRemoveProposal ¶
func NewRemoveProposal(ledgerID types.Destination, target types.Destination, leftAmount *big.Int) Proposal
NewRemoveProposal constructs a proposal with a valid Remove proposal and empty Add proposal.
func (*Proposal) Equal ¶
Equal returns true if the supplied Proposal is deeply equal to the receiver, false otherwise.
func (Proposal) MarshalJSON ¶
MarshalJSON returns a JSON representation of the Proposal
func (*Proposal) Target ¶
func (p *Proposal) Target() types.Destination
Target returns the target channel of the proposal.
func (*Proposal) Type ¶
func (p *Proposal) Type() ProposalType
Type returns the type of the proposal based on whether it contains an Add or a Remove proposal.
func (*Proposal) UnmarshalJSON ¶
UnmarshalJSON populates the receiver with the json-encoded data
type ProposalType ¶
type ProposalType string
const ( AddProposal ProposalType = "AddProposal" RemoveProposal ProposalType = "RemoveProposal" )
type Remove ¶
type Remove struct { // Target is the address of the virtual channel being defunded Target types.Destination // LeftAmount is the amount to be credited (in the ledger channel) to the participant specified as the "left" in the guarantee. // // The amount for the "right" participant is calculated as the difference between the guarantee amount and LeftAmount. LeftAmount *big.Int }
Remove is a proposal to remove a guarantee for the given virtual channel.
func NewRemove ¶
func NewRemove(target types.Destination, leftAmount *big.Int) Remove
NewRemove constructs a new Remove proposal.
func (Remove) MarshalJSON ¶
MarshalJSON returns a JSON representation of the Remove
func (*Remove) UnmarshalJSON ¶
UnmarshalJSON populates the receiver with the json-encoded data
type SignedProposal ¶
SignedProposal is a Proposal with a signature on it.
func (SignedProposal) ChannelID ¶
func (p SignedProposal) ChannelID() types.Destination
ChannelID returns the id of the ConsensusChannel which receive the proposal.
func (*SignedProposal) Clone ¶
func (sp *SignedProposal) Clone() SignedProposal
Clone returns a deep copy of the receiver.
func (SignedProposal) SortInfo ¶
func (p SignedProposal) SortInfo() (types.Destination, uint64)
SortInfo returns the channelId and turn number so the proposal can be easily sorted.
type SignedVars ¶
SignedVars stores 0-2 signatures for some vars in a consensus channel.
type Vars ¶
type Vars struct { TurnNum uint64 Outcome LedgerOutcome }
Vars stores the turn number and outcome for a state in a consensus channel.
func (*Vars) Add ¶
Add mutates Vars by
- increasing the turn number by 1
- including the guarantee
- adjusting balances accordingly
An error is returned if:
- the turn number is not incremented
- the balances are incorrectly adjusted, or the deposits are too large
- the guarantee is already included in vars.Outcome
If an error is returned, the original vars is not mutated.
func (*Vars) HandleProposal ¶
HandleProposal handles a proposal to add or remove a guarantee. It will mutate Vars by calling Add or Remove for the proposal.
func (*Vars) Remove ¶
Remove mutates Vars by
- increasing the turn number by 1
- removing the guarantee for the Target channel
- adjusting balances accordingly based on LeftAmount and RightAmount
An error is returned if:
- the turn number is not incremented
- a guarantee is not found for the target
- the amounts are too large for the guarantee amount
If an error is returned, the original vars is not mutated.