mtg

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: Apache-2.0 Imports: 28 Imported by: 12

README

MTG

This module can bootstrap a MTG application very effortlessly, it's as simple as a few lines of code.

func (rw *RefundWorker) ProcessOutput(ctx context.Context, out *mtg.Output) {
	receivers := []string{out.Sender}
	traceId := mixin.UniqueConversationID(out.UTXOID, "refund")
	err := rw.grp.BuildTransaction(ctx, out.AssetID, receivers, 1, out.Amount.String(), "refund", traceId)
	if err != nil {
		panic(err)
	}
}

group, _ := mtg.BuildGroup(ctx, db, conf)
rw := NewRefundrWorker(ctx, group, conf)
group.AddWorker(rw)
group.Run(ctx)

The group will call every workers added, and the worker just needs to implement the ProcessOutput interface. The code above is a very simple worker that refunds all the payments received.

Documentation

Index

Constants

View Source
const (
	IterationActionAdd    = 11
	IterationActionRemove = 12
)
View Source
const (
	TransactionStateInitial  = 10
	TransactionStateSigned   = 12
	TransactionStateSnapshot = 13

	OutputsBatchSize = 36
	StorageAssetId   = "c94ac88f-4671-3976-b60a-09064f1811e8"
)

Variables

View Source
var SCHEMA string

Functions

func CheckRetryableError added in v0.7.0

func CheckRetryableError(err error) bool

func CheckTestEnvironment added in v0.7.0

func CheckTestEnvironment(ctx context.Context) bool

func DecodeMixinExtraBase64 added in v0.7.2

func DecodeMixinExtraBase64(extra string) (string, []byte)

func DecodeMixinExtraHEX added in v0.7.2

func DecodeMixinExtraHEX(memo string) (string, []byte)

func EnableTestEnvironment added in v0.7.0

func EnableTestEnvironment(ctx context.Context) context.Context

func EncodeMixinExtraBase64 added in v0.7.2

func EncodeMixinExtraBase64(appId string, extra []byte) string

func ExpandTilde added in v0.7.0

func ExpandTilde(path string) string

func GetKernelTransaction added in v0.9.0

func GetKernelTransaction(rpc, hash string) (*common.VersionedTransaction, string, error)

func NewMixAddress added in v0.7.0

func NewMixAddress(ctx context.Context, members []string, threshold byte) (*mixin.MixAddress, bool, error)

func ReplayCheck added in v0.7.6

func ReplayCheck(a *Action, txs1, txs2 []*Transaction, asset1, asset2 string)

func SerializeTransactions added in v0.7.5

func SerializeTransactions(txs []*Transaction) []byte

func UniqueId added in v0.7.0

func UniqueId(a, b string) string

Types

type Action

type Action struct {
	ActionState ActionState

	UnifiedOutput
	// contains filtered or unexported fields
}

func (*Action) BuildStorageTransaction added in v0.9.0

func (act *Action) BuildStorageTransaction(ctx context.Context, extra []byte) *Transaction

func (*Action) BuildTransaction added in v0.9.0

func (act *Action) BuildTransaction(ctx context.Context, traceId, opponentAppId, assetId, amount, memo string, receivers []string, threshold int) *Transaction

func (*Action) BuildTransactionWithReference added in v0.9.0

func (act *Action) BuildTransactionWithReference(ctx context.Context, traceId, opponentAppId, assetId, amount, memo string, receivers []string, threshold int, reference crypto.Hash) *Transaction

func (*Action) BuildTransactionWithStorageTraceId added in v0.9.0

func (act *Action) BuildTransactionWithStorageTraceId(ctx context.Context, traceId, opponentAppId, assetId, amount, memo string, receivers []string, threshold int, storageTraceId string) *Transaction

func (*Action) CheckAssetBalanceAt added in v0.9.0

func (act *Action) CheckAssetBalanceAt(ctx context.Context, assetId string) decimal.Decimal

this function or rpc should be used only in ProcessOutput

func (*Action) CheckAssetBalanceForStorageAt added in v0.9.0

func (act *Action) CheckAssetBalanceForStorageAt(ctx context.Context, extra []byte) bool

func (*Action) TestAttachActionToGroup added in v0.9.0

func (a *Action) TestAttachActionToGroup(g *Group)

type ActionState added in v0.7.0

type ActionState int
const (
	ActionStateInitial    ActionState = 10
	ActionStateDone       ActionState = 11
	ActionStateRestorable ActionState = 12
)

type Configuration

type Configuration struct {
	App struct {
		AppId             string `toml:"app-id"`
		SessionId         string `toml:"session-id"`
		SessionPrivateKey string `toml:"session-private-key"`
		ServerPublicKey   string `toml:"server-public-key"`
		SpendPrivateKey   string `toml:"spend-private-key"`
	} `toml:"app"`
	Genesis struct {
		Members   []string `toml:"members"`
		Threshold int      `toml:"threshold"`
		Epoch     uint64   `toml:"epoch"`
	} `toml:"genesis"`
	Project          string `toml:"project"`
	StoreDir         string `toml:"store-dir"`
	GroupSize        int    `toml:"group-size"`
	LoopWaitDuration int64  `toml:"loop-wait-duration"`
}

type DepositEntry added in v0.7.0

type DepositEntry struct {
	Destination string
	Tag         string
}

func (DepositEntry) UniqueKey added in v0.7.0

func (e DepositEntry) UniqueKey() string

type Group

type Group struct {
	GroupId string
	// contains filtered or unexported fields
}

func BuildGroup

func BuildGroup(ctx context.Context, store *SQLite3Store, conf *Configuration) (*Group, error)

func (*Group) AddNode

func (grp *Group) AddNode(ctx context.Context, id string, threshold int, epoch uint64) error

func (*Group) AttachWorker added in v0.7.0

func (grp *Group) AttachWorker(appId string, wkr Worker)

func (*Group) EnableDebug added in v0.7.5

func (grp *Group) EnableDebug()

func (*Group) FindAppByEntry added in v0.7.0

func (grp *Group) FindAppByEntry(entry string) string

func (*Group) FindWorker added in v0.7.0

func (grp *Group) FindWorker(appId string) Worker

func (*Group) GenesisId

func (grp *Group) GenesisId() string

func (*Group) GetMembers

func (grp *Group) GetMembers() []string

func (*Group) GetThreshold

func (grp *Group) GetThreshold() int

func (*Group) Index added in v0.7.0

func (grp *Group) Index() int

func (*Group) ListActiveNodes

func (grp *Group) ListActiveNodes(ctx context.Context) ([]string, int, uint64, error)

func (*Group) ListOutputsForAsset

func (grp *Group) ListOutputsForAsset(ctx context.Context, appId, assetId string, consumedUntil, sequence uint64, state SafeUtxoState, limit int) []*UnifiedOutput

func (*Group) ListOutputsForTransaction added in v0.4.1

func (grp *Group) ListOutputsForTransaction(ctx context.Context, traceId string, sequence uint64) []*UnifiedOutput

func (*Group) ReadKernelTransactionUntilSufficient added in v0.9.0

func (grp *Group) ReadKernelTransactionUntilSufficient(ctx context.Context, txHash string) (*common.VersionedTransaction, error)

func (*Group) RegisterDepositEntry added in v0.7.0

func (grp *Group) RegisterDepositEntry(appId string, entry DepositEntry)

func (*Group) Run

func (grp *Group) Run(ctx context.Context)

func (*Group) SetKernelRPC added in v0.9.0

func (grp *Group) SetKernelRPC(rpc string)

func (*Group) Synced added in v0.4.0

func (grp *Group) Synced(ctx context.Context) bool

type Iteration

type Iteration struct {
	Action    int
	NodeId    string
	Threshold int
	CreatedAt uint64
}

a node joins or leaves the group with an iteration this is for the evolution mechanism of MTG TODO not implemented yet

type Row added in v0.7.0

type Row interface {
	Scan(dest ...any) error
}

type SQLite3Store added in v0.7.0

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

func OpenSQLite3Store added in v0.7.0

func OpenSQLite3Store(path string) (*SQLite3Store, error)

func (*SQLite3Store) Close added in v0.7.0

func (s *SQLite3Store) Close() error

func (*SQLite3Store) FinishAction added in v0.7.0

func (s *SQLite3Store) FinishAction(ctx context.Context, id string, state ActionState, ts []*Transaction) error

func (*SQLite3Store) FinishTransaction added in v0.7.0

func (s *SQLite3Store) FinishTransaction(ctx context.Context, traceId string) error

func (*SQLite3Store) ListActions added in v0.7.0

func (s *SQLite3Store) ListActions(ctx context.Context, state ActionState, limit int) ([]*Action, error)

func (*SQLite3Store) ListIterations added in v0.7.0

func (s *SQLite3Store) ListIterations(ctx context.Context) ([]*Iteration, error)

func (*SQLite3Store) ListOutputsForAsset added in v0.7.0

func (s *SQLite3Store) ListOutputsForAsset(ctx context.Context, appId, assetId string, consumedUntil, sequence uint64, state SafeUtxoState, limit int) ([]*UnifiedOutput, error)

func (*SQLite3Store) ListOutputsForTransaction added in v0.7.0

func (s *SQLite3Store) ListOutputsForTransaction(ctx context.Context, traceId string, sequence uint64) ([]*UnifiedOutput, error)

func (*SQLite3Store) ListPreviousInitialTransactions added in v0.7.0

func (s *SQLite3Store) ListPreviousInitialTransactions(ctx context.Context, asset string, sequence uint64) ([]*Transaction, error)

func (*SQLite3Store) ListTransactions added in v0.7.0

func (s *SQLite3Store) ListTransactions(ctx context.Context, state, limit int) ([]*Transaction, map[string][]*Transaction, error)

func (*SQLite3Store) ReadCache added in v0.9.0

func (s *SQLite3Store) ReadCache(ctx context.Context, k string) (string, error)

func (*SQLite3Store) ReadOutputByHashAndIndex added in v0.8.0

func (s *SQLite3Store) ReadOutputByHashAndIndex(ctx context.Context, hash string, index uint) (*UnifiedOutput, error)

func (*SQLite3Store) ReadProperty added in v0.7.0

func (s *SQLite3Store) ReadProperty(ctx context.Context, k string) (string, error)

func (*SQLite3Store) ReadTransactionByHash added in v0.7.0

func (s *SQLite3Store) ReadTransactionByHash(ctx context.Context, hash crypto.Hash) (*Transaction, error)

func (*SQLite3Store) ReadTransactionByTraceId added in v0.7.0

func (s *SQLite3Store) ReadTransactionByTraceId(ctx context.Context, id string) (*Transaction, error)

func (*SQLite3Store) RestoreAction added in v0.7.0

func (s *SQLite3Store) RestoreAction(ctx context.Context, act *Action, t *Transaction) error

func (*SQLite3Store) UpdateTxWithOutputs added in v0.7.0

func (s *SQLite3Store) UpdateTxWithOutputs(ctx context.Context, t *Transaction, os []*UnifiedOutput) error

func (*SQLite3Store) WriteAction added in v0.7.0

func (s *SQLite3Store) WriteAction(ctx context.Context, out *UnifiedOutput, state ActionState) error

func (*SQLite3Store) WriteCache added in v0.9.0

func (s *SQLite3Store) WriteCache(ctx context.Context, k, v string) error

func (*SQLite3Store) WriteIteration added in v0.7.0

func (s *SQLite3Store) WriteIteration(ctx context.Context, ir *Iteration) error

func (*SQLite3Store) WriteProperty added in v0.7.0

func (s *SQLite3Store) WriteProperty(ctx context.Context, k, v string) error

type SafeDepositView added in v0.7.0

type SafeDepositView struct {
	DepositHash  string `json:"deposit_hash"`
	DepositIndex int64  `json:"deposit_index"`
	Sender       string `json:"sender"`
	Destination  string `json:"destination"`
	Tag          string `json:"tag"`
}

type SafeUtxoState added in v0.7.0

type SafeUtxoState string
const (
	OutputTypeSafeOutput = "kernel_output"

	SafeUtxoStateUnspent  SafeUtxoState = "unspent"
	SafeUtxoStateAssigned SafeUtxoState = "assigned"
	SafeUtxoStateSigned   SafeUtxoState = "signed"
	SafeUtxoStateSpent    SafeUtxoState = "spent"
)

type Transaction

type Transaction struct {
	TraceId       string
	AppId         string
	OpponentAppId string
	State         int
	AssetId       string
	Receivers     []string
	Threshold     int
	Amount        string
	Memo          string
	Raw           []byte
	Hash          crypto.Hash
	Sequence      uint64
	UpdatedAt     time.Time
	// contains filtered or unexported fields
}

func Deserialize added in v0.7.6

func Deserialize(rb []byte) (*Transaction, error)

func DeserializeTransactions added in v0.7.5

func DeserializeTransactions(tb []byte) ([]*Transaction, error)

func (*Transaction) Equal added in v0.7.5

func (t *Transaction) Equal(tx *Transaction) bool

func (*Transaction) RequestID added in v0.8.1

func (tx *Transaction) RequestID() string

func (*Transaction) Serialize added in v0.7.6

func (tx *Transaction) Serialize() []byte

type TransactionRecipient added in v0.7.0

type TransactionRecipient struct {
	MixAddress *mixin.MixAddress
	Amount     string
	UuidMember bool
}

type UnifiedOutput

type UnifiedOutput struct {
	Type                 string          `json:"type"`
	OutputId             string          `json:"output_id"`
	TransactionRequestId string          `json:"request_id,omitempty"`
	TransactionHash      string          `json:"transaction_hash"`
	OutputIndex          int             `json:"output_index"`
	AssetId              string          `json:"asset_id"`
	KernelAssetId        string          `json:"kernel_asset_id"`
	Amount               decimal.Decimal `json:"amount"`
	SendersHash          string          `json:"senders_hash"`
	SendersThreshold     int64           `json:"senders_threshold"`
	Senders              []string        `json:"senders"`
	ReceiversHash        string          `json:"receivers_hash"`
	ReceiversThreshold   int64           `json:"receivers_threshold"`
	Extra                string          `json:"extra"`
	State                SafeUtxoState   `json:"state"`
	Sequence             uint64          `json:"sequence"`
	Signers              []string        `json:"signers"`
	SignedBy             string          `json:"signed_by"`
	SequencerCreatedAt   time.Time       `json:"created_at"`

	TraceId string
	AppId   string
	// contains filtered or unexported fields
}

type Worker

type Worker interface {
	// process the action in a queue and return transactions
	// need to ensure enough balance with CheckAssetBalanceAt(ctx, a)
	// before return any transactions, otherwise the transactions
	// will be ignored when issuficient balance
	//
	// if we want to make a multi process worker, it's possible that
	// we pass some RPC handle to the process, or we could build a
	// whole state of the current sequence and send it to the process
	// i.e. ProcessOutput(StateAtSequence, Action) []*Transaction
	ProcessOutput(context.Context, *Action) ([]*Transaction, string)
}

Jump to

Keyboard shortcuts

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