adjudicator

package
v0.0.0-...-8619d99 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownChannel = errors.New("unknown channel")

ErrUnknownChannel indicates that no information for a channel id could be found by the chaincode.

Functions

func FundingKey

func FundingKey(id channel.ID, addr wallet.Address) string

FundingKey creates the key used for storing a funding amount in the holdings map.

func IDKey

func IDKey(id channel.ID) string

IDKey creates the key used for storing the channel state in the states map.

func IsAdjudicatorError

func IsAdjudicatorError(err error) bool

IsAdjudicatorError returns true if the given error is one of the following: ValidationError, ChallengeTimeoutError, VersionError, UnderfundedError.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true if given err is a NotFoundError.

func SetNewTimestamp

func SetNewTimestamp(newts func() Timestamp)

SetNewTimestamp sets the Timestamp factory used during json unmarshaling of structs containing a Timestamp, e.g., StateRegs.

It is set to return Timestamp instances by default.

func ValidateChannel

func ValidateChannel(ch *SignedChannel) error

ValidateChannel checks if the given parameters in SignedChannel are in itself consistent.

func VerifySig

func VerifySig(signer wallet.Address, state State, sig wallet.Sig) (bool, error)

VerifySig verifies the signature on a State.

Types

type AccountID

type AccountID string

AccountID represents the ID of a client. It is used for minting, burning, receiving and sending funds. Ensure it is unique for every client interacting with Asset and no impersonation is possible.

type Adjudicator

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

Adjudicator is an abstract implementation of the adjudicator smart contract.

func NewAdjudicator

func NewAdjudicator(id string, ledger Ledger, asset Asset) *Adjudicator

NewAdjudicator generates a new Adjudicator with an identifier, holding ledger and asset ledger.

func (*Adjudicator) BalanceOfID

func (a *Adjudicator) BalanceOfID(id AccountID) (*big.Int, error)

BalanceOfID returns the asset token balance of the given user identifier.

func (*Adjudicator) Burn

func (a *Adjudicator) Burn(callee AccountID, amount *big.Int) error

Burn destroys the given amount of asset tokens for the callee.

func (*Adjudicator) Deposit

func (a *Adjudicator) Deposit(callee AccountID, chID channel.ID, part wallet.Address, amount *big.Int) error

Deposit transfers the given amount of coins from the callee to the channel with the specified channel ID. The funds are stored in the channel under the participant's wallet address.

func (*Adjudicator) Holding

func (a *Adjudicator) Holding(id channel.ID, part wallet.Address) (*big.Int, error)

Holding returns the current holding amount of the given participant in the channel.

func (*Adjudicator) Mint

func (a *Adjudicator) Mint(callee AccountID, amount *big.Int) error

Mint generates the given amount of asset tokens for the callee.

func (*Adjudicator) Register

func (a *Adjudicator) Register(ch *SignedChannel) error

Register verifies the given SignedChannel, updates the holdings and saves a new StateReg.

func (*Adjudicator) StateReg

func (a *Adjudicator) StateReg(id channel.ID) (*StateReg, error)

StateReg fetches the current state from the ledger and returns it. If no state is found under the given channel.ID an error is returned.

func (*Adjudicator) TotalHolding

func (a *Adjudicator) TotalHolding(id channel.ID, parts []wallet.Address) (*big.Int, error)

TotalHolding returns the sum of all participant holdings in the channel.

func (*Adjudicator) Transfer

func (a *Adjudicator) Transfer(sender AccountID, receiver AccountID, amount *big.Int) error

Transfer sends the given amount of asset tokens from the sender to the receiver.

func (*Adjudicator) Withdraw

func (a *Adjudicator) Withdraw(swr SignedWithdrawReq) (*big.Int, error)

Withdraw withdraws all funds of participant Part in the finalized channel id to the given Receiver. It returns the withdrawn amount.

type Asset

type Asset interface {
	// Mint creates the desired amount of token for the given id.
	// Note that id must be authenticated first.
	Mint(id AccountID, amount *big.Int) error

	// Burn removes the desired amount of token from the given id.
	// Note that id must be authenticated first.
	Burn(id AccountID, amount *big.Int) error

	// Transfer sends the desired amount of tokens from sender to receiver.
	// Note that sender must be authenticated first.
	Transfer(sender AccountID, receiver AccountID, amount *big.Int) error

	// BalanceOf returns the amount of tokens the given id holds.
	BalanceOf(id AccountID) (*big.Int, error)
}

Asset is a basic interface for creating tokens with.

type AssetHolder

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

AssetHolder tracks deposits and withdrawals of channel participants over a HoldingLedger.

func NewAssetHolder

func NewAssetHolder(ledger HoldingLedger) *AssetHolder

NewAssetHolder returns a new AssetHolder operating on the given ledger.

func (*AssetHolder) Deposit

func (a *AssetHolder) Deposit(id channel.ID, part wallet.Address, amount *big.Int) error

Deposit registers a deposit for channel `id` and participant `part` of amount `amount`, possibly adding to an already existent deposit.

Deposit throws an error if `amount` is negative.

Ledger access errors are propagated.

func (*AssetHolder) Holding

func (a *AssetHolder) Holding(id channel.ID, part wallet.Address) (*big.Int, error)

Holding returns the holdings of participant `part` in the channel of id `id`.

func (*AssetHolder) SetHolding

func (a *AssetHolder) SetHolding(id channel.ID, part wallet.Address, holding *big.Int) error

SetHolding sets the holding of part in channel id to holding.

Panics if `holding` is negative.

func (*AssetHolder) TotalHolding

func (a *AssetHolder) TotalHolding(id channel.ID, parts []wallet.Address) (*big.Int, error)

TotalHolding returns the total amount deposited into the channel specified by `params`.

func (*AssetHolder) Withdraw

func (a *AssetHolder) Withdraw(id channel.ID, part wallet.Address) (*big.Int, error)

Withdraw resets the holdings of participant `part` in the channel of id `id` to zero and returns the holdings before the reset.

type ChallengeTimeoutError

type ChallengeTimeoutError struct {
	Timeout Timestamp
	Now     Timestamp
}

ChallengeTimeoutError indicates that the challenge timeout passed.

func (ChallengeTimeoutError) Error

func (te ChallengeTimeoutError) Error() string

type HoldingLedger

type HoldingLedger interface {
	GetHolding(channel.ID, wallet.Address) (*big.Int, error) //nolint:forbidigo
	PutHolding(channel.ID, wallet.Address, *big.Int) error
}

HoldingLedger stores the channel's holdings.

type Ledger

type Ledger interface {
	StateLedger
	HoldingLedger
	Now() Timestamp
}

Ledger contains the read and write operations required by the Adjudicator.

type MemAsset

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

MemAsset is an in-memory asset for testing. As it is for testing there is no central banker.

func NewMemAsset

func NewMemAsset() *MemAsset

NewMemAsset generates a new in-memory Asset.

func (MemAsset) BalanceOf

func (m MemAsset) BalanceOf(id AccountID) (*big.Int, error)

BalanceOf returns the amount of tokens the given id holds. If the id is unknown, zero is returned.

func (MemAsset) Burn

func (m MemAsset) Burn(id AccountID, amount *big.Int) error

Burn removes the desired amount of token from the given id.

func (MemAsset) Mint

func (m MemAsset) Mint(id AccountID, amount *big.Int) error

Mint creates the desired amount of token for the given id.

func (MemAsset) Transfer

func (m MemAsset) Transfer(sender AccountID, receiver AccountID, amount *big.Int) error

Transfer checks if the proposed transfer is valid and transfers the given amount of coins from the sender to the receiver.

type MemLedger

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

MemLedger is a simple in-memory ledger, using Go maps. time.Time is used as Timestamps.

func NewMemLedger

func NewMemLedger() *MemLedger

NewMemLedger generates a new local in-memory ledger for testing purposes.

func (*MemLedger) GetHolding

func (m *MemLedger) GetHolding(id channel.ID, addr wallet.Address) (*big.Int, error)

GetHolding retrieves the current channel holding of the given address.

func (*MemLedger) GetState

func (m *MemLedger) GetState(id channel.ID) (*StateReg, error)

GetState retrieves the current channel state.

func (*MemLedger) Now

func (m *MemLedger) Now() Timestamp

Now returns time.Now() as a Timestamp.

func (*MemLedger) PutHolding

func (m *MemLedger) PutHolding(id channel.ID, addr wallet.Address, holding *big.Int) error

PutHolding overwrites the current address channel holdings with the given holding.

func (*MemLedger) PutState

func (m *MemLedger) PutState(s *StateReg) error

PutState overwrites the current channel state with the given one.

type NotFoundError

type NotFoundError struct {
	Key  string
	Type string
}

NotFoundError should be returned by getters of Ledger implementations if there's no entry under a given key.

func (*NotFoundError) Error

func (err *NotFoundError) Error() string

type Params

type Params struct {
	ChallengeDuration uint64           `json:"challengeDuration"`
	Parts             []wallet.Address `json:"parts"`
	Nonce             channel.Nonce    `json:"nonce"`
}

Params are the parameters of a state channel.

func (Params) Clone

func (p Params) Clone() Params

Clone duplicates the params.

func (Params) CoreParams

func (p Params) CoreParams() *channel.Params

CoreParams returns the equivalent representation of p as channel.Params. The returned Params is set to have no App, LedgerChannel is set to true and VirtualChannel is set to false.

It is not a deep copy, e.g., field Parts references the same participants.

func (Params) ID

func (p Params) ID() channel.ID

ID return the params channel id.

func (*Params) UnmarshalJSON

func (p *Params) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom unmarshalling for Params.

type SignedChannel

type SignedChannel struct {
	Params Params       `json:"params"`
	State  State        `json:"state"`
	Sigs   []wallet.Sig `json:"sigs"`
}

SignedChannel contains signatures on Params and State and is used for registering new states.

func ConvertToSignedChannel

func ConvertToSignedChannel(req channel.AdjudicatorReq) (*SignedChannel, error)

ConvertToSignedChannel takes a AdjudicatorReq and generates a SignedChannel from it.

func SignChannel

func SignChannel(params Params, state State, accs []wallet.Account) (*SignedChannel, error)

SignChannel creates signatures on the provided channel state for each provided account. The signatures are in the same order as the accounts.

func (*SignedChannel) Clone

func (ch *SignedChannel) Clone() *SignedChannel

Clone duplicates a SignedChannel.

type SignedWithdrawReq

type SignedWithdrawReq struct {
	Req WithdrawReq `json:"req"`
	Sig wallet.Sig  `json:"sig"`
}

SignedWithdrawReq contains a signature over a WithdrawReq to check its validity.

func SignWithdrawRequest

func SignWithdrawRequest(acc wallet.Account, channel channel.ID, receiver AccountID) (*SignedWithdrawReq, error)

SignWithdrawRequest generates a WithdrawReq and signs it with the given account to return a SignedWithdrawReq.

func (SignedWithdrawReq) Verify

func (swr SignedWithdrawReq) Verify(addr wallet.Address) (bool, error)

Verify verifies that the provided signature on the signed withdraw request belongs to the provided address.

type State

type State struct {
	ID       channel.ID    `json:"id"`
	Version  uint64        `json:"version"`
	Balances []channel.Bal `json:"balances"`
	IsFinal  bool          `json:"final"`
}

State is a state of a state channel.

func (State) Clone

func (s State) Clone() State

Clone duplicates the State.

func (State) CoreState

func (s State) CoreState() *channel.State

CoreState returns the equivalent representation of s as channel.State. The returned State is set to have no App, no Data, contains one asset that is default initialized and this first assets' balances are set to the Balances of s.

Use the State returned by CoreState to create or verify signatures with the go-perun channel backend.

It is not a deep copy, e.g., field Balances references the same balances slice.

func (State) Sign

func (s State) Sign(acc wallet.Account) (wallet.Sig, error)

Sign signs the State with a given account.

func (State) Total

func (s State) Total() channel.Bal

Total returns the total balance of the State.

type StateLedger

type StateLedger interface {
	GetState(channel.ID) (*StateReg, error) //nolint:forbidigo
	PutState(*StateReg) error
}

StateLedger stores the channel's state.

type StateReg

type StateReg struct {
	State   `json:"state"`
	Timeout Timestamp `json:"timeout"`
}

StateReg adds a Timeout to the State to indicate the states challenge timeout.

func (*StateReg) Clone

func (s *StateReg) Clone() *StateReg

Clone duplicates the StateReg.

func (*StateReg) Equal

func (s *StateReg) Equal(sr StateReg) bool

Equal checks if the given StateReg is equal.

func (*StateReg) IsFinalizedAt

func (s *StateReg) IsFinalizedAt(ts Timestamp) bool

IsFinalizedAt checks if the registered state is final. This is the case if either the isFinal flag is true or the timeout passed.

type Timestamp

type Timestamp time.Time

A Timestamp is a point in time that can be compared to others.

func NewTimestamp

func NewTimestamp() Timestamp

NewTimestamp returns new Timestamp instances used during json unmarshaling of structs containing a Timestamp, e.g., StateRegs.

It returns Timestamp instances by default.

func StdNow

func StdNow() Timestamp

StdNow returns the current time as Timestamp.

func (Timestamp) Add

func (t Timestamp) Add(d uint64) Timestamp

Add adds the given amount in seconds onto the Timestamp.

func (Timestamp) After

func (t Timestamp) After(other Timestamp) bool

After evaluates if the given Timestamp is after the Timestamp it is being called on.

func (Timestamp) Before

func (t Timestamp) Before(other Timestamp) bool

Before evaluates if the given Timestamp is before the Timestamp it is being called on.

func (Timestamp) Clone

func (t Timestamp) Clone() Timestamp

Clone duplicates the Timestamp.

func (Timestamp) Equal

func (t Timestamp) Equal(other Timestamp) bool

Equal compares the given Timestamps.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Timestamp as time.Time.

func (Timestamp) Time

func (t Timestamp) Time() time.Time

Time returns the Timestamp as time.Time.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the Timestamp as time.Time.

type UnderfundedError

type UnderfundedError struct {
	Version uint64
	Total   *big.Int
	Funded  *big.Int
}

UnderfundedError indicates that the sum of the proposed balances are higher than the actual funding.

func (UnderfundedError) Error

func (ue UnderfundedError) Error() string

type ValidationError

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

ValidationError indicates that the given arguments could not be validated successfully.

func (ValidationError) Unwrap

func (ve ValidationError) Unwrap() error

type VersionError

type VersionError struct {
	Registered uint64
	Tried      uint64
}

VersionError indicates that the chaincode holds a newer version of the proposed channel state.

func (VersionError) Error

func (ve VersionError) Error() string

type WithdrawReq

type WithdrawReq struct {
	ID       channel.ID     `json:"id"`
	Part     wallet.Address `json:"part"`
	Receiver AccountID      `json:"receiver"`
}

WithdrawReq are parameters needed to withdraw funds from a state channel.

func (WithdrawReq) Decode

func (wr WithdrawReq) Decode(r io.Reader) error

Decode decodes a withdraw request from an `io.Reader` or returns an `error`.

func (WithdrawReq) Encode

func (wr WithdrawReq) Encode(w io.Writer) error

Encode encodes a withdraw request into an `io.Writer` or returns an `error`.

func (*WithdrawReq) MarshalJSON

func (wr *WithdrawReq) MarshalJSON() ([]byte, error)

MarshalJSON implements custom marshalling for WithdrawReq to deal with custom data types.

func (WithdrawReq) Sign

func (wr WithdrawReq) Sign(acc wallet.Account) (wallet.Sig, error)

Sign signs a withdraw request with the given Account. Returns the signature or an error.

func (*WithdrawReq) UnmarshalJSON

func (wr *WithdrawReq) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom unmarshalling for WithdrawReq to deal with custom data types.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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