mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-19 05:04:21 +01:00
* Fixes * Fixes to domain layer: * Add Leaf bool field to know to fix the returned list of leaves * Add non-persisted UnsignedForfeitTxs to RoundFinalizationStarted * Store only error msg when round fails instead of full error * Fix wallet interface: * Add Close() to close conn with wallet * Add GetAsset() to fix missing asset err when calling Transfer() * Fix gocron scheduler to correctly run/build the project * Fix badger repo implementation: * Fix datadirs of projection stores * Return error if current round not found * Fix round event deserialization * Fix TxBuilder interface & dummy impl: * Pass asp pubkey as arg of the defined functions * Fix connectorsToInputArgs to return the right number of ins * Fix getTxid() to return the id of an hex encoded tx too * Fix createConnectors() to return a tx if there's only 1 connector * Add leaf bool field to psetWithLevel in case a leaf is not in the last level * Fix node's isLeaf() check * Move to hex encoded pubkeys instead of ark encoded * Fix app layer: * Add Start() and Stop() to the interface & Expect raw pubkeys instead of strings as args * Source & cache pubkey from wallet at startup * Drop usage of scheduler and schedule next task based on occurred round events * Increase verbosity * Use hex instead of ark encoding to store receveirs' pubkeys * Lower faucet amount from 100k to 10k sats in total * Fix finalizeRound() to persist round events even if it failed * Add view() to forfeitTxMap to enrich RoundFinalizationEvent with unsigned forfeit txs * Add app config * Fix interface layer: * Remove repo manager from handler factory * Fix GetEventStream to forward events to stream once they arrive from app layer * Return missing unsigned forfeit txs in RoundFinalizationEvent * Fix extracting user pubkey from address * Add log interceptors * Add config struct * Add factory * Clean interface * Add config and launcher * Tidy deps & Set defaut round interval to 30secs for dev mode
248 lines
5.2 KiB
Go
248 lines
5.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
UndefinedStage RoundStage = iota
|
|
RegistrationStage
|
|
FinalizationStage
|
|
)
|
|
|
|
type RoundStage int
|
|
|
|
func (s RoundStage) String() string {
|
|
switch s {
|
|
case RegistrationStage:
|
|
return "REGISTRATION_STAGE"
|
|
case FinalizationStage:
|
|
return "FINALIZATION_STAGE"
|
|
default:
|
|
return "UNDEFINED_STAGE"
|
|
}
|
|
}
|
|
|
|
type Stage struct {
|
|
Code RoundStage
|
|
Ended bool
|
|
Failed bool
|
|
}
|
|
|
|
type Round struct {
|
|
Id string
|
|
StartingTimestamp int64
|
|
EndingTimestamp int64
|
|
Stage Stage
|
|
Payments map[string]Payment
|
|
Txid string
|
|
TxHex string
|
|
ForfeitTxs []string
|
|
CongestionTree CongestionTree
|
|
Connectors []string
|
|
DustAmount uint64
|
|
Version uint
|
|
changes []RoundEvent
|
|
}
|
|
|
|
func NewRound(dustAmount uint64) *Round {
|
|
return &Round{
|
|
Id: uuid.New().String(),
|
|
DustAmount: dustAmount,
|
|
Payments: make(map[string]Payment),
|
|
changes: make([]RoundEvent, 0),
|
|
}
|
|
}
|
|
|
|
func NewRoundFromEvents(events []RoundEvent) *Round {
|
|
r := &Round{}
|
|
|
|
for _, event := range events {
|
|
r.On(event, true)
|
|
}
|
|
|
|
r.changes = append([]RoundEvent{}, events...)
|
|
|
|
return r
|
|
}
|
|
|
|
func (r *Round) Events() []RoundEvent {
|
|
return r.changes
|
|
}
|
|
|
|
func (r *Round) On(event RoundEvent, replayed bool) {
|
|
switch e := event.(type) {
|
|
case RoundStarted:
|
|
r.Stage.Code = RegistrationStage
|
|
r.Id = e.Id
|
|
r.StartingTimestamp = e.Timestamp
|
|
case RoundFinalizationStarted:
|
|
r.Stage.Code = FinalizationStage
|
|
r.CongestionTree = e.CongestionTree
|
|
r.Connectors = append([]string{}, e.Connectors...)
|
|
r.TxHex = e.PoolTx
|
|
case RoundFinalized:
|
|
r.Stage.Ended = true
|
|
r.Txid = e.Txid
|
|
r.ForfeitTxs = append([]string{}, e.ForfeitTxs...)
|
|
r.EndingTimestamp = e.Timestamp
|
|
case RoundFailed:
|
|
r.Stage.Failed = true
|
|
r.EndingTimestamp = e.Timestamp
|
|
case PaymentsRegistered:
|
|
if r.Payments == nil {
|
|
r.Payments = make(map[string]Payment)
|
|
}
|
|
for _, p := range e.Payments {
|
|
r.Payments[p.Id] = p
|
|
}
|
|
}
|
|
|
|
if replayed {
|
|
r.Version++
|
|
}
|
|
}
|
|
|
|
func (r *Round) StartRegistration() ([]RoundEvent, error) {
|
|
empty := Stage{}
|
|
if r.Stage != empty {
|
|
return nil, fmt.Errorf("not in a valid stage to start payment registration")
|
|
}
|
|
|
|
event := RoundStarted{
|
|
Id: r.Id,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
r.raise(event)
|
|
|
|
return []RoundEvent{event}, nil
|
|
}
|
|
|
|
func (r *Round) RegisterPayments(payments []Payment) ([]RoundEvent, error) {
|
|
if r.Stage.Code != RegistrationStage || r.IsFailed() {
|
|
return nil, fmt.Errorf("not in a valid stage to register payments")
|
|
}
|
|
if len(payments) <= 0 {
|
|
return nil, fmt.Errorf("missing payments to register")
|
|
}
|
|
for _, p := range payments {
|
|
if err := p.validate(false); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
event := PaymentsRegistered{
|
|
Id: r.Id,
|
|
Payments: payments,
|
|
}
|
|
r.raise(event)
|
|
|
|
return []RoundEvent{event}, nil
|
|
}
|
|
|
|
func (r *Round) StartFinalization(connectors []string, tree CongestionTree, poolTx string) ([]RoundEvent, error) {
|
|
if len(connectors) <= 0 {
|
|
return nil, fmt.Errorf("missing list of connectors")
|
|
}
|
|
if len(tree) <= 0 {
|
|
return nil, fmt.Errorf("missing congestion tree")
|
|
}
|
|
if len(poolTx) <= 0 {
|
|
return nil, fmt.Errorf("missing unsigned pool tx")
|
|
}
|
|
if r.Stage.Code != RegistrationStage || r.IsFailed() {
|
|
return nil, fmt.Errorf("not in a valid stage to start payment finalization")
|
|
}
|
|
if len(r.Payments) <= 0 {
|
|
return nil, fmt.Errorf("no payments registered")
|
|
}
|
|
|
|
event := RoundFinalizationStarted{
|
|
Id: r.Id,
|
|
CongestionTree: tree,
|
|
Connectors: connectors,
|
|
PoolTx: poolTx,
|
|
}
|
|
r.raise(event)
|
|
|
|
return []RoundEvent{event}, nil
|
|
}
|
|
|
|
func (r *Round) EndFinalization(forfeitTxs []string, txid string) ([]RoundEvent, error) {
|
|
if len(forfeitTxs) <= 0 {
|
|
return nil, fmt.Errorf("missing list of signed forfeit txs")
|
|
}
|
|
if len(txid) <= 0 {
|
|
return nil, fmt.Errorf("missing pool txid")
|
|
}
|
|
if r.Stage.Code != FinalizationStage || r.IsFailed() {
|
|
return nil, fmt.Errorf("not in a valid stage to end payment finalization")
|
|
}
|
|
if r.Stage.Ended {
|
|
return nil, fmt.Errorf("round already finalized")
|
|
}
|
|
event := RoundFinalized{
|
|
Id: r.Id,
|
|
Txid: txid,
|
|
ForfeitTxs: forfeitTxs,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
r.raise(event)
|
|
|
|
return []RoundEvent{event}, nil
|
|
}
|
|
|
|
func (r *Round) Fail(err error) []RoundEvent {
|
|
if r.Stage.Failed {
|
|
return nil
|
|
}
|
|
event := RoundFailed{
|
|
Id: r.Id,
|
|
Err: err.Error(),
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
r.raise(event)
|
|
|
|
return []RoundEvent{event}
|
|
}
|
|
|
|
func (r *Round) IsStarted() bool {
|
|
empty := Stage{}
|
|
return !r.IsFailed() && !r.IsEnded() && r.Stage != empty
|
|
}
|
|
|
|
func (r *Round) IsEnded() bool {
|
|
return !r.IsFailed() && r.Stage.Code == FinalizationStage && r.Stage.Ended
|
|
}
|
|
|
|
func (r *Round) IsFailed() bool {
|
|
return r.Stage.Failed
|
|
}
|
|
|
|
func (r *Round) TotalInputAmount() uint64 {
|
|
totInputs := 0
|
|
for _, p := range r.Payments {
|
|
totInputs += len(p.Inputs)
|
|
}
|
|
return uint64(totInputs * int(r.DustAmount))
|
|
}
|
|
|
|
func (r *Round) TotalOutputAmount() uint64 {
|
|
tot := uint64(0)
|
|
for _, p := range r.Payments {
|
|
tot += p.TotalOutputAmount()
|
|
}
|
|
return tot
|
|
}
|
|
|
|
func (r *Round) raise(event RoundEvent) {
|
|
if r.changes == nil {
|
|
r.changes = make([]RoundEvent, 0)
|
|
}
|
|
r.changes = append(r.changes, event)
|
|
r.On(event, false)
|
|
}
|