Files
ark/common/tree/congestion_tree.go
Louis Singer 01297ae38c Add support for covenant-less ASP (#214)
* scaffolding wallet

* remove wallet db, add loader instead

* wip

* implement some wallet methods

* signing and utxos

* renaming

* fee estimator

* chain source options

* config

* application service

* clark docker-compose

* CLI refactor

* v0 clark

* v0.1 clark

* fix SignTapscriptInput (btcwallet)

* wallet.Broadcast, send via explora

* fix ASP pubkey

* Use lnd's btcwallet & Add rpc to get wallet staus

* wip

* unilateral exit

* Fixes on watching for notifications and cli init

* handle non-final BIP68 errors

* Fixes

* Fixes

* Fix

* a

* fix onboard cosigners + revert tree validation

* fix covenant e2e tests

* fix covenantless e2e tests

* fix container naming

* fix lint error

* update REAME.md

* Add env var for wallet password

---------

Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
2024-07-30 20:57:52 +02:00

115 lines
2.4 KiB
Go

package tree
import (
"errors"
)
// Node is a struct embedding the transaction and the parent txid of a congestion tree node
type Node struct {
Txid string
Tx string
ParentTxid string
Leaf bool
}
var (
ErrParentNotFound = errors.New("parent not found")
ErrLeafNotFound = errors.New("leaf not found in congestion tree")
)
// CongestionTree is reprensented as a matrix of TreeNode struct
// the first level of the matrix is the root of the tree
type CongestionTree [][]Node
// Root returns the root node of the congestion tree
func (c CongestionTree) Root() (Node, error) {
if len(c) <= 0 {
return Node{}, errors.New("empty congestion tree")
}
if len(c[0]) <= 0 {
return Node{}, errors.New("empty congestion tree")
}
return c[0][0], nil
}
// Leaves returns the leaves of the congestion tree (the vtxos txs)
func (c CongestionTree) Leaves() []Node {
leaves := c[len(c)-1]
for _, level := range c[:len(c)-1] {
for _, node := range level {
if node.Leaf {
leaves = append(leaves, node)
}
}
}
return leaves
}
// Children returns all the nodes that have the given node as parent
func (c CongestionTree) Children(nodeTxid string) []Node {
var children []Node
for _, level := range c {
for _, node := range level {
if node.ParentTxid == nodeTxid {
children = append(children, node)
}
}
}
return children
}
// NumberOfNodes returns the total number of pset in the congestion tree
func (c CongestionTree) NumberOfNodes() int {
var count int
for _, level := range c {
count += len(level)
}
return count
}
// Branch returns the branch of the given vtxo txid from root to leaf in the order of the congestion tree
func (c CongestionTree) Branch(vtxoTxid string) ([]Node, error) {
branch := make([]Node, 0)
leaves := c.Leaves()
// check if the vtxo is a leaf
found := false
for _, leaf := range leaves {
if leaf.Txid == vtxoTxid {
found = true
branch = append(branch, leaf)
break
}
}
if !found {
return nil, ErrLeafNotFound
}
rootTxid := c[0][0].Txid
for branch[0].Txid != rootTxid {
parent, err := branch[0].findParent(c)
if err != nil {
return nil, err
}
branch = append([]Node{parent}, branch...)
}
return branch, nil
}
func (n Node) findParent(tree CongestionTree) (Node, error) {
for _, level := range tree {
for _, node := range level {
if node.Txid == n.ParentTxid {
return node, nil
}
}
}
return Node{}, ErrParentNotFound
}