interpreter

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const KEPT_ADDR = "<kept>"

Variables

View Source
var ErrScalingNotSupported = errors.New("scaling is not supported")

Functions

func CalculateMaxSafeWithdraw

func CalculateMaxSafeWithdraw(balance *big.Int, overdraft *big.Int) *big.Int

PRE: ovedraft != nil, balance != nil PRE: ovedraft >= 0 POST: $out >= 0

func CalculateSafeWithdraw

func CalculateSafeWithdraw(
	balance *big.Int,
	overdraft *big.Int,
	requestedAmount *big.Int,
) *big.Int

PRE: ovedraft != nil, balance != nil PRE: ovedraft >= 0 PRE: requestedAmount >= 0 POST: $out >= 0

func CompareBalances

func CompareBalances(b1 Balances, b2 Balances) bool

func CompareBalancesIncluding

func CompareBalancesIncluding(b1 Balances, b2 Balances) bool

Returns whether the first value is a subset of the second one.

func CompareSetAccountsMetadata added in v0.0.25

func CompareSetAccountsMetadata(a SetAccountsMetadata, b SetAccountsMetadata) bool

CompareSetAccountsMetadata reports whether two lists hold the same rows, ignoring order but respecting multiplicity (so [x, x] != [x, y]). Values are compared on their canonical source form.

func NewAccountAddress

func NewAccountAddress(src string) (AccountAddress, InterpreterError)

func NewArgsParser

func NewArgsParser(args []Value) *argsParser

func NewAsset

func NewAsset(src string) (Asset, InterpreterError)

func PrettyPrintMeta

func PrettyPrintMeta(meta Metadata) string

func PrettyPrintPostings

func PrettyPrintPostings(postings []Posting) string

func RunProgram

func RunProgram(
	ctx context.Context,
	program parser.Program,
	vars map[string]string,
	store Store,
	featureFlags map[string]struct{},
) (*ExecutionResult, InterpreterError)

Types

type AccountAddress

type AccountAddress struct {
	Name  string
	Scope string
}

AccountAddress is an account, optionally partitioned by a scope. The scope is a separate dimension of the account rather than part of its name, so it is modeled as its own field instead of being encoded into the name string.

func (AccountAddress) MarshalJSON added in v0.0.25

func (v AccountAddress) MarshalJSON() ([]byte, error)

func (AccountAddress) String

func (v AccountAddress) String() string

type AccountBalance

type AccountBalance struct {
	Asset  string
	Color  string
	Amount *big.Int
}

A single balance entry for an account: an (asset, color) pair and its amount.

type AccountDependency added in v0.0.25

type AccountDependency struct {
	Account string
	Scope   string
	Color   string
	Asset   string
}

type AccountMetadataRow added in v0.0.25

type AccountMetadataRow struct {
	Account string `json:"account"`
	Key     string `json:"key"`
	Value   string `json:"value"`
	Scope   string `json:"scope,omitempty"`
}

type AccountsMetadata

type AccountsMetadata []AccountMetadataRow

AccountsMetadata is the external, serialized representation of account metadata. The runtime works with the in-memory InternalAccountsMetadata and converts to this at the boundaries (store queries, execution result).

func (AccountsMetadata) FirstDuplicate added in v0.0.25

func (rows AccountsMetadata) FirstDuplicate() (AccountMetadataRow, bool)

FirstDuplicate returns the first row whose (account, key, scope) key already appeared earlier in the list, if any. That triple is the identity of a metadata entry and the value is its content, so a repeated key is an ambiguous, malformed input.

func (AccountsMetadata) PrettyPrint

func (m AccountsMetadata) PrettyPrint() string

type Asset

type Asset string

func (Asset) GetBaseAndScale added in v0.0.25

func (asset Asset) GetBaseAndScale() (string, int64)

func (Asset) MarshalJSON added in v0.0.25

func (v Asset) MarshalJSON() ([]byte, error)

func (Asset) String

func (v Asset) String() string

type BadArityErr

type BadArityErr struct {
	parser.Range
	ExpectedArity  int
	GivenArguments int
}

func (BadArityErr) Error

func (e BadArityErr) Error() string

type BadPortionParsingErr

type BadPortionParsingErr struct {
	parser.Range
	Source string
	Reason string
}

func (BadPortionParsingErr) Error

func (e BadPortionParsingErr) Error() string

type BalanceQuery

type BalanceQuery []BalanceQueryItem

type BalanceQueryItem added in v0.0.25

type BalanceQueryItem struct {
	Account string
	Asset   string
	Color   string
	Scope   string
}

For each account, list of the needed assets

type BalanceRow added in v0.0.25

type BalanceRow struct {
	Account string   `json:"account"`
	Asset   string   `json:"asset"`
	Amount  *big.Int `json:"amount"`
	Color   string   `json:"color,omitempty"`
	Scope   string   `json:"scope,omitempty"`
}

type Balances

type Balances []BalanceRow

func (Balances) FirstDuplicate added in v0.0.25

func (rows Balances) FirstDuplicate() (BalanceRow, bool)

FirstDuplicate returns the first row whose (account, asset, color) key already appeared earlier in the list, if any. That triple is the identity of a balance entry and the amount is its value, so a repeated key is an ambiguous, malformed input.

func (Balances) PrettyPrint

func (rows Balances) PrettyPrint() string

type CannotCastScopedAccountToString added in v0.0.25

type CannotCastScopedAccountToString struct {
	parser.Range
	Account string
	Scope   string
}

func (CannotCastScopedAccountToString) Error added in v0.0.25

type CannotCastToString

type CannotCastToString struct {
	parser.Range
	Value Value
}

func (CannotCastToString) Error

func (e CannotCastToString) Error() string

type CannotStoreScopedAccountInMeta added in v0.0.25

type CannotStoreScopedAccountInMeta struct {
	parser.Range
	Account string
	Scope   string
}

func (CannotStoreScopedAccountInMeta) Error added in v0.0.25

type DivideByZero

type DivideByZero struct {
	parser.Range
	Numerator *big.Int
}

func (DivideByZero) Error

func (e DivideByZero) Error() string

type ExecutionResult

type ExecutionResult struct {
	Postings []Posting `json:"postings"`

	Metadata Metadata `json:"txMeta"`

	AccountsMetadata SetAccountsMetadata `json:"accountsMeta"`
}

type ExperimentalFeature

type ExperimentalFeature struct {
	parser.Range
	FlagName string
}

func (ExperimentalFeature) Error

func (e ExperimentalFeature) Error() string

type InternalAccountsMetadata added in v0.0.25

type InternalAccountsMetadata map[AccountAddress]map[string]string

An internal representation of the account metadata. Used to cache metadata we get from external store. Whereas the external representation (interpreter.AccountsMetadata) is user-facing and a stable contract, this one is used internally by the runtime, and could change over time, for example to add more indexes for faster lookups. It mirrors InternalBalances: keyed by the (account, scope) pair, holding that account's (key -> value) entries.

func (InternalAccountsMetadata) Get added in v0.0.25

func (m InternalAccountsMetadata) Get(account AccountAddress, key string) (string, bool)

Get the (account, key) metadata value from the cache.

func (InternalAccountsMetadata) Merge added in v0.0.25

Merge the queried metadata rows into the cache.

func (InternalAccountsMetadata) Set added in v0.0.25

func (m InternalAccountsMetadata) Set(account AccountAddress, key string, value string)

Set assigns value to the (account, key) metadata entry.

type InternalBalances added in v0.0.25

type InternalBalances map[AccountAddress][]AccountBalance

An internal representation of the balances. Used to cache balances we get from external store. Whereas the external representation (interpreter.Balances) is user-facing and be a stable contract, (for example, allowing more columns if we need an higher level of fungibility), this one is used internally by the runtime, and could change over time, for example to add more indexes for faster lookups

func FromBalancesRows added in v0.0.25

func FromBalancesRows(b Balances) InternalBalances

func (InternalBalances) DeepClone added in v0.0.25

func (b InternalBalances) DeepClone() InternalBalances

func (InternalBalances) Merge added in v0.0.25

func (b InternalBalances) Merge(update []BalanceRow)

Merge the queried balance rows into the cache

func (InternalBalances) Set added in v0.0.25

func (b InternalBalances) Set(account AccountAddress, asset string, color string, amount *big.Int)

Set assigns amount to the (account, asset, color) balance.

type InternalError

type InternalError struct {
	parser.Range
	Posting Posting
}

func (InternalError) Error

func (e InternalError) Error() string

type InterpreterError

type InterpreterError interface {
	error
	parser.Ranged
}

func ParsePortionSpecific

func ParsePortionSpecific(input string) (*big.Rat, InterpreterError)

type InvalidAccountName

type InvalidAccountName struct {
	parser.Range
	Name string
}

func (InvalidAccountName) Error

func (e InvalidAccountName) Error() string

type InvalidAllotmentInSendAll

type InvalidAllotmentInSendAll struct {
	parser.Range
}

func (InvalidAllotmentInSendAll) Error

type InvalidAllotmentSum

type InvalidAllotmentSum struct {
	parser.Range
	ActualSum big.Rat
}

func (InvalidAllotmentSum) Error

func (e InvalidAllotmentSum) Error() string

type InvalidAsset

type InvalidAsset struct {
	parser.Range
	Name string
}

func (InvalidAsset) Error

func (e InvalidAsset) Error() string

type InvalidColor

type InvalidColor struct {
	parser.Range
	Color string
}

func (InvalidColor) Error

func (e InvalidColor) Error() string

type InvalidFeature

type InvalidFeature struct {
	parser.Range
	Feature string
}

func (InvalidFeature) Error

func (e InvalidFeature) Error() string

type InvalidMonetaryLiteral

type InvalidMonetaryLiteral struct {
	parser.Range
	Source string
}

func (InvalidMonetaryLiteral) Error

func (e InvalidMonetaryLiteral) Error() string

type InvalidNestedMeta

type InvalidNestedMeta struct {
	parser.Range
}

func (InvalidNestedMeta) Error

func (InvalidNestedMeta) Error() string

type InvalidNumberLiteral

type InvalidNumberLiteral struct {
	parser.Range
	Source string
}

func (InvalidNumberLiteral) Error

func (e InvalidNumberLiteral) Error() string

type InvalidOperatorErr added in v0.0.25

type InvalidOperatorErr struct {
	parser.Range
	Operator string
}

func (InvalidOperatorErr) Error added in v0.0.25

func (e InvalidOperatorErr) Error() string

type InvalidScope added in v0.0.25

type InvalidScope struct {
	parser.Range
	Scope string
}

func (InvalidScope) Error added in v0.0.25

func (e InvalidScope) Error() string

type InvalidTypeErr

type InvalidTypeErr struct {
	parser.Range
	Name string
}

func (InvalidTypeErr) Error

func (e InvalidTypeErr) Error() string

type InvalidUnboundedAddressInScalingAddress

type InvalidUnboundedAddressInScalingAddress struct {
	parser.Range
}

func (InvalidUnboundedAddressInScalingAddress) Error

type InvalidUnboundedInSendAll

type InvalidUnboundedInSendAll struct {
	parser.Range
	Name  string
	Scope string
}

func (InvalidUnboundedInSendAll) Error

type MetaDependency added in v0.0.25

type MetaDependency struct {
	Account string
	Scope   string
	Key     string
}

type Metadata

type Metadata = map[string]Value

type MetadataNotFound

type MetadataNotFound struct {
	parser.Range
	Account string
	Scope   string
	Key     string
}

func (MetadataNotFound) Error

func (e MetadataNotFound) Error() string

type MetadataQuery

type MetadataQuery []MetadataQueryItem

type MetadataQueryItem added in v0.0.25

type MetadataQueryItem = struct {
	Account string
	Scope   string
	Keys    []string
}

type MismatchedCurrencyError

type MismatchedCurrencyError struct {
	parser.Range
	Expected string
	Got      string
}

func (MismatchedCurrencyError) Error

func (e MismatchedCurrencyError) Error() string

type MissingFundsErr

type MissingFundsErr struct {
	parser.Range
	Asset     string
	Needed    big.Int
	Available big.Int
}

func (MissingFundsErr) Error

func (e MissingFundsErr) Error() string

func (MissingFundsErr) Is

func (e MissingFundsErr) Is(target error) bool

type MissingVariableErr

type MissingVariableErr struct {
	parser.Range
	Name string
}

func (MissingVariableErr) Error

func (e MissingVariableErr) Error() string

type Monetary

type Monetary struct {
	Amount MonetaryInt
	Asset  Asset
}

func NewMonetary

func NewMonetary(asset string, n int64) Monetary

func (Monetary) MarshalJSON

func (v Monetary) MarshalJSON() ([]byte, error)

func (Monetary) String

func (v Monetary) String() string

type MonetaryInt

type MonetaryInt big.Int

func NewMonetaryInt

func NewMonetaryInt(n int64) MonetaryInt

func NewMonetaryIntBig added in v0.0.25

func NewMonetaryIntBig(n *big.Int) MonetaryInt

func (MonetaryInt) Add

func (m MonetaryInt) Add(other MonetaryInt) MonetaryInt

func (MonetaryInt) MarshalJSON

func (v MonetaryInt) MarshalJSON() ([]byte, error)

func (MonetaryInt) String

func (v MonetaryInt) String() string

func (MonetaryInt) Sub

func (m MonetaryInt) Sub(other MonetaryInt) MonetaryInt

type NegativeAmountErr

type NegativeAmountErr struct {
	parser.Range
	Amount MonetaryInt
}

func (NegativeAmountErr) Error

func (e NegativeAmountErr) Error() string

type NegativeBalanceError

type NegativeBalanceError struct {
	parser.Range
	Account string
	Scope   string
	Amount  big.Int
}

func (NegativeBalanceError) Error

func (e NegativeBalanceError) Error() string

type Portion

type Portion big.Rat

func (Portion) MarshalJSON

func (v Portion) MarshalJSON() ([]byte, error)

func (Portion) String

func (p Portion) String() string

type Posting

type Posting struct {
	Source           string   `json:"source"`
	SourceScope      string   `json:"sourceScope,omitempty"`
	Destination      string   `json:"destination"`
	DestinationScope string   `json:"destinationScope,omitempty"`
	Amount           *big.Int `json:"amount"`
	Asset            string   `json:"asset"`
	Color            string   `json:"color,omitempty"`
}

type QueryBalanceError

type QueryBalanceError struct {
	parser.Range
	WrappedError error
}

func (QueryBalanceError) Error

func (e QueryBalanceError) Error() string

func (QueryBalanceError) Unwrap added in v0.0.25

func (e QueryBalanceError) Unwrap() error

type QueryMetadataError

type QueryMetadataError struct {
	parser.Range
	WrappedError error
}

func (QueryMetadataError) Error

func (e QueryMetadataError) Error() string

func (QueryMetadataError) Unwrap added in v0.0.25

func (e QueryMetadataError) Unwrap() error

type ResolvedDependencies added in v0.0.25

type ResolvedDependencies struct {
	AccountsReads  map[AccountDependency]struct{}
	AccountsWrites map[AccountDependency]struct{}
	MetaReads      map[MetaDependency]struct{}
	MetaWrites     map[MetaDependency]struct{}
	TxMetaWrites   map[string]struct{}
}

func ResolveDependencies added in v0.0.25

func ResolveDependencies(ctx context.Context, store Store, vars map[string]string, program parser.Program) (ResolvedDependencies, error)

type Sender

type Sender struct {
	Account AccountAddress
	Amount  *big.Int
	Color   string
}

type SetAccountMetadataRow added in v0.0.25

type SetAccountMetadataRow struct {
	Account string `json:"account"`
	Key     string `json:"key"`
	Value   Value  `json:"value"`
	Scope   string `json:"scope,omitempty"`
}

SetAccountMetadataRow is a single piece of account metadata set by the script during execution. Unlike the input metadata (which is opaque and string-valued, since its serialization format isn't always known), the set value's type is known, so it is carried as a typed Value and serialized in the tagged form.

func (*SetAccountMetadataRow) UnmarshalJSON added in v0.0.25

func (r *SetAccountMetadataRow) UnmarshalJSON(data []byte) error

type SetAccountsMetadata added in v0.0.25

type SetAccountsMetadata []SetAccountMetadataRow

SetAccountsMetadata is the account metadata produced by the script (the execution result's accountsMeta, and a spec's expect.metadata).

type StaticStore

type StaticStore struct {
	Balances Balances
	Meta     AccountsMetadata
}

func (StaticStore) GetAccountsMetadata

func (s StaticStore) GetAccountsMetadata(_ context.Context, q MetadataQuery) (AccountsMetadata, error)

func (StaticStore) GetBalances

func (s StaticStore) GetBalances(_ context.Context, q BalanceQuery) (Balances, error)

type Store

type Store interface {
	// Returns the batched balances for a given batched query.
	//
	// Note: the "Balances" result is expected not to have duplicate entries.
	// Malformed Balances will result it undefined behaviour, and the implementation doesn't guarantee keys are deduped.
	GetBalances(context.Context, BalanceQuery) (Balances, error)
	GetAccountsMetadata(context.Context, MetadataQuery) (AccountsMetadata, error)
}

type String

type String string

func (String) MarshalJSON added in v0.0.25

func (v String) MarshalJSON() ([]byte, error)

func (String) String

func (v String) String() string

type TypeError

type TypeError struct {
	parser.Range
	Expected string
	Value    Value
}

func (TypeError) Error

func (e TypeError) Error() string

type UnboundFunctionErr

type UnboundFunctionErr struct {
	parser.Range
	Name string
}

func (UnboundFunctionErr) Error

func (e UnboundFunctionErr) Error() string

type UnboundVariableErr

type UnboundVariableErr struct {
	parser.Range
	Name string
}

func (UnboundVariableErr) Error

func (e UnboundVariableErr) Error() string

type UnhandledError added in v0.0.25

type UnhandledError struct {
	parser.Range
	Node string
}

UnhandledError is returned instead of panicking when expression evaluation or dependency resolution meets an AST node it doesn't handle (e.g. a language construct added after this code was written).

func (UnhandledError) Error added in v0.0.25

func (e UnhandledError) Error() string

type Value

type Value interface {
	String() string
	// contains filtered or unexported methods
}

func ParseTaggedValue added in v0.0.25

func ParseTaggedValue(data []byte) (Value, error)

ParseTaggedValue decodes the tagged-JSON representation of a Value. It reads the "type" discriminator (json can't unmarshal directly into the Value interface), then decodes into the struct shared with that type's MarshalJSON.

type VariablesMap

type VariablesMap map[string]string

Jump to

Keyboard shortcuts

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