Make change of async payment spendable (#324)

* Drop unused ComputeOutputScript & use ParseTaprootScript internally

* Add pending field to vtxo domain

* Add check to handle async change as claimed vtxo & Move check to prevent spending penidng vtxos to app level

* Rename utils.go to parser.go & Fixes

* Ignore sent-and-reversible vtxos in ListVtxos

* Fixes

Co-authored-by: Louis Singer <louisinger@users.noreply.github.com>

* Fix e2e test

Co-authored-by: Louis Singer <louisinger@users.noreply.github.com>
Co-authored-by: João Bordalo <bordalix@users.noreply.github.com>

* Fix

* Add PendingChange field to vtxo

* Add PendingChange field to Transaction

* Fixes

* Remove logs

---------

Co-authored-by: Louis Singer <louisinger@users.noreply.github.com>
Co-authored-by: João Bordalo <bordalix@users.noreply.github.com>
This commit is contained in:
Pietralberto Mazza
2024-09-19 19:44:22 +02:00
committed by GitHub
parent 10ef0dbffa
commit 5c2065ad47
34 changed files with 569 additions and 1054 deletions

View File

@@ -6,7 +6,6 @@ import (
"sync"
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
"github.com/ark-network/ark/common/tree"
"github.com/ark-network/ark/server/internal/core/application"
"github.com/ark-network/ark/server/internal/core/domain"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
@@ -115,7 +114,7 @@ func (h *handler) Ping(ctx context.Context, req *arkv1.PingRequest) (*arkv1.Ping
RoundFinalization: &arkv1.RoundFinalizationEvent{
Id: e.Id,
PoolTx: e.PoolTx,
CongestionTree: castCongestionTree(e.CongestionTree),
CongestionTree: congestionTree(e.CongestionTree).toProto(),
Connectors: e.Connectors,
MinRelayFeeRate: e.MinRelayFeeRate,
},
@@ -150,7 +149,7 @@ func (h *handler) Ping(ctx context.Context, req *arkv1.PingRequest) (*arkv1.Ping
RoundSigning: &arkv1.RoundSigningEvent{
Id: e.Id,
CosignersPubkeys: cosignersKeys,
UnsignedTree: castCongestionTree(e.UnsignedVtxoTree),
UnsignedTree: congestionTree(e.UnsignedVtxoTree).toProto(),
UnsignedRoundTx: e.UnsignedRoundTx,
},
},
@@ -246,10 +245,10 @@ func (h *handler) GetRound(ctx context.Context, req *arkv1.GetRoundRequest) (*ar
Start: round.StartingTimestamp,
End: round.EndingTimestamp,
PoolTx: round.UnsignedTx,
CongestionTree: castCongestionTree(round.CongestionTree),
CongestionTree: congestionTree(round.CongestionTree).toProto(),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
Stage: stage(round.Stage).toProto(),
},
}, nil
}
@@ -265,10 +264,10 @@ func (h *handler) GetRound(ctx context.Context, req *arkv1.GetRoundRequest) (*ar
Start: round.StartingTimestamp,
End: round.EndingTimestamp,
PoolTx: round.UnsignedTx,
CongestionTree: castCongestionTree(round.CongestionTree),
CongestionTree: congestionTree(round.CongestionTree).toProto(),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
Stage: stage(round.Stage).toProto(),
},
}, nil
}
@@ -292,10 +291,10 @@ func (h *handler) GetRoundById(
Start: round.StartingTimestamp,
End: round.EndingTimestamp,
PoolTx: round.UnsignedTx,
CongestionTree: castCongestionTree(round.CongestionTree),
CongestionTree: congestionTree(round.CongestionTree).toProto(),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
Stage: stage(round.Stage).toProto(),
},
}, nil
}
@@ -485,7 +484,7 @@ func (h *handler) listenToEvents() {
RoundFinalization: &arkv1.RoundFinalizationEvent{
Id: e.Id,
PoolTx: e.PoolTx,
CongestionTree: castCongestionTree(e.CongestionTree),
CongestionTree: congestionTree(e.CongestionTree).toProto(),
Connectors: e.Connectors,
MinRelayFeeRate: e.MinRelayFeeRate,
},
@@ -520,7 +519,7 @@ func (h *handler) listenToEvents() {
RoundSigning: &arkv1.RoundSigningEvent{
Id: e.Id,
CosignersPubkeys: cosignersKeys,
UnsignedTree: castCongestionTree(e.UnsignedVtxoTree),
UnsignedTree: congestionTree(e.UnsignedVtxoTree).toProto(),
UnsignedRoundTx: e.UnsignedRoundTx,
},
},
@@ -549,58 +548,3 @@ func (h *handler) listenToEvents() {
}
}
}
type vtxoList []domain.Vtxo
func (v vtxoList) toProto() []*arkv1.Vtxo {
list := make([]*arkv1.Vtxo, 0, len(v))
for _, vv := range v {
var pendingData *arkv1.PendingPayment
if vv.AsyncPayment != nil {
pendingData = &arkv1.PendingPayment{
RedeemTx: vv.AsyncPayment.RedeemTx,
UnconditionalForfeitTxs: vv.AsyncPayment.UnconditionalForfeitTxs,
}
}
list = append(list, &arkv1.Vtxo{
Outpoint: &arkv1.Outpoint{
Txid: vv.Txid,
Vout: vv.VOut,
},
Descriptor_: vv.Descriptor,
Amount: vv.Amount,
PoolTxid: vv.PoolTx,
Spent: vv.Spent,
ExpireAt: vv.ExpireAt,
SpentBy: vv.SpentBy,
Swept: vv.Swept,
PendingData: pendingData,
Pending: pendingData != nil,
})
}
return list
}
// castCongestionTree converts a tree.CongestionTree to a repeated arkv1.TreeLevel
func castCongestionTree(congestionTree tree.CongestionTree) *arkv1.Tree {
levels := make([]*arkv1.TreeLevel, 0, len(congestionTree))
for _, level := range congestionTree {
levelProto := &arkv1.TreeLevel{
Nodes: make([]*arkv1.Node, 0, len(level)),
}
for _, node := range level {
levelProto.Nodes = append(levelProto.Nodes, &arkv1.Node{
Txid: node.Txid,
Tx: node.Tx,
ParentTxid: node.ParentTxid,
})
}
levels = append(levels, levelProto)
}
return &arkv1.Tree{
Levels: levels,
}
}

View File

@@ -5,11 +5,14 @@ import (
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
"github.com/ark-network/ark/common"
"github.com/ark-network/ark/common/tree"
"github.com/ark-network/ark/server/internal/core/domain"
"github.com/ark-network/ark/server/internal/core/ports"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
// From interface type to app type
func parseAddress(addr string) (string, *secp256k1.PublicKey, *secp256k1.PublicKey, error) {
if len(addr) <= 0 {
return "", nil, nil, fmt.Errorf("missing address")
@@ -55,16 +58,77 @@ func parseReceivers(outs []*arkv1.Output) ([]domain.Receiver, error) {
return receivers, nil
}
func toRoundStage(stage domain.Stage) arkv1.RoundStage {
if stage.Failed {
// From app typeto interface type
type vtxoList []domain.Vtxo
func (v vtxoList) toProto() []*arkv1.Vtxo {
list := make([]*arkv1.Vtxo, 0, len(v))
for _, vv := range v {
var pendingData *arkv1.PendingPayment
if vv.AsyncPayment != nil {
pendingData = &arkv1.PendingPayment{
RedeemTx: vv.AsyncPayment.RedeemTx,
UnconditionalForfeitTxs: vv.AsyncPayment.UnconditionalForfeitTxs,
}
}
list = append(list, &arkv1.Vtxo{
Outpoint: &arkv1.Outpoint{
Txid: vv.Txid,
Vout: vv.VOut,
},
Descriptor_: vv.Descriptor,
Amount: vv.Amount,
PoolTxid: vv.PoolTx,
Spent: vv.Spent,
ExpireAt: vv.ExpireAt,
SpentBy: vv.SpentBy,
Swept: vv.Swept,
PendingData: pendingData,
Pending: pendingData != nil,
PendingChange: vv.PendingChange,
})
}
return list
}
type congestionTree tree.CongestionTree
func (t congestionTree) toProto() *arkv1.Tree {
levels := make([]*arkv1.TreeLevel, 0, len(t))
for _, level := range t {
levelProto := &arkv1.TreeLevel{
Nodes: make([]*arkv1.Node, 0, len(level)),
}
for _, node := range level {
levelProto.Nodes = append(levelProto.Nodes, &arkv1.Node{
Txid: node.Txid,
Tx: node.Tx,
ParentTxid: node.ParentTxid,
})
}
levels = append(levels, levelProto)
}
return &arkv1.Tree{
Levels: levels,
}
}
type stage domain.Stage
func (s stage) toProto() arkv1.RoundStage {
if s.Failed {
return arkv1.RoundStage_ROUND_STAGE_FAILED
}
switch stage.Code {
switch s.Code {
case domain.RegistrationStage:
return arkv1.RoundStage_ROUND_STAGE_REGISTRATION
case domain.FinalizationStage:
if stage.Ended {
if s.Ended {
return arkv1.RoundStage_ROUND_STAGE_FINALIZED
}
return arkv1.RoundStage_ROUND_STAGE_FINALIZATION