Skip to content

Commit 5726969

Browse files
committed
WIP: supplycommit: push supply commitment to remote uni during CommitFinalizeState
1 parent 5b0795b commit 5726969

File tree

6 files changed

+206
-34
lines changed

6 files changed

+206
-34
lines changed

tapcfg/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
626626
AssetLookup: tapdbAddrBook,
627627
KeyRing: keyRing,
628628
Chain: chainBridge,
629+
SupplySyncer: &supplySyncer,
629630
DaemonAdapters: lndFsmDaemonAdapters,
630631
StateLog: supplyCommitStore,
631632
ChainParams: *tapChainParams.Params,

universe/supplycommit/env.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/sha256"
66
"fmt"
7+
"net/url"
78

89
"github.com/btcsuite/btcd/btcec/v2"
910
"github.com/btcsuite/btcd/btcutil"
@@ -100,6 +101,39 @@ type SupplyLeaves struct {
100101
IgnoreLeafEntries []NewIgnoreEvent
101102
}
102103

104+
// NewSupplyLeavesFromEvents creates a SupplyLeaves instance from a slice of
105+
// SupplyUpdateEvent instances.
106+
func NewSupplyLeavesFromEvents(events []SupplyUpdateEvent) (SupplyLeaves,
107+
error) {
108+
109+
var leaves SupplyLeaves
110+
for idx := range events {
111+
event := events[idx]
112+
113+
switch e := event.(type) {
114+
case *NewMintEvent:
115+
leaves.IssuanceLeafEntries = append(
116+
leaves.IssuanceLeafEntries, *e,
117+
)
118+
119+
case *NewBurnEvent:
120+
leaves.BurnLeafEntries = append(
121+
leaves.BurnLeafEntries, *e,
122+
)
123+
124+
case *NewIgnoreEvent:
125+
leaves.IgnoreLeafEntries = append(
126+
leaves.IgnoreLeafEntries, *e,
127+
)
128+
129+
default:
130+
return leaves, fmt.Errorf("unknown event type: %T", e)
131+
}
132+
}
133+
134+
return leaves, nil
135+
}
136+
103137
// AssetLookup is an interface that allows us to query for asset
104138
// information, such as asset groups and asset metadata.
105139
type AssetLookup interface {
@@ -498,6 +532,17 @@ type StateMachineStore interface {
498532
asset.Specifier) ([]SupplyUpdateEvent, error)
499533
}
500534

535+
// SupplySyncer is an interface that allows the state machine to insert
536+
// supply commitments into the remote universe server.
537+
type SupplySyncer interface {
538+
// PushSupplyCommitment pushes a supply commitment to the remote
539+
// universe server. This function should block until the sync insertion
540+
// is complete.
541+
PushSupplyCommitment(ctx context.Context, assetSpec asset.Specifier,
542+
commitment RootCommitment, updateLeaves SupplyLeaves,
543+
chainProof ChainProof, canonicalUniverses []url.URL) error
544+
}
545+
501546
// Environment is a set of dependencies that a state machine may need to carry
502547
// out the logic for a given state transition. All fields are to be considered
503548
// immutable, and will be fixed for the lifetime of the state machine.
@@ -518,6 +563,10 @@ type Environment struct {
518563
// Wallet is the main wallet interface used to managed PSBT packets.
519564
Wallet Wallet
520565

566+
// AssetLookup is used to look up asset information such as asset groups
567+
// and asset metadata.
568+
AssetLookup AssetLookup
569+
521570
// KeyRing is the main key ring interface used to manage keys.
522571
KeyRing KeyRing
523572

@@ -526,6 +575,10 @@ type Environment struct {
526575
// TODO(roasbeef): can make a slimmer version of
527576
Chain tapgarden.ChainBridge
528577

578+
// SupplySyncer is used to insert supply commitments into the remote
579+
// universe server.
580+
SupplySyncer SupplySyncer
581+
529582
// StateLog is the main state log that is used to track the state of the
530583
// state machine. This is used to persist the state of the state machine
531584
// across restarts.

universe/supplycommit/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type ManagerCfg struct {
6464
// TODO(roasbeef): can make a slimmer version of
6565
Chain tapgarden.ChainBridge
6666

67+
// SupplySyncer is used to insert supply commitments into the remote
68+
// universe server.
69+
SupplySyncer SupplySyncer
70+
6771
// DaemonAdapters is a set of adapters that allow the state machine to
6872
// interact with external daemons whilst processing internal events.
6973
DaemonAdapters DaemonAdapters
@@ -227,8 +231,10 @@ func (m *Manager) fetchStateMachine(
227231
TreeView: m.cfg.TreeView,
228232
Commitments: m.cfg.Commitments,
229233
Wallet: m.cfg.Wallet,
234+
AssetLookup: m.cfg.AssetLookup,
230235
KeyRing: m.cfg.KeyRing,
231236
Chain: m.cfg.Chain,
237+
SupplySyncer: m.cfg.SupplySyncer,
232238
StateLog: m.cfg.StateLog,
233239
CommitConfTarget: DefaultCommitConfTarget,
234240
ChainParams: m.cfg.ChainParams,

universe/supplycommit/mock.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package supplycommit
22

33
import (
44
"context"
5+
"net/url"
56
"sync"
67

78
"github.com/btcsuite/btcd/btcec/v2"
@@ -451,3 +452,18 @@ func (m *mockAssetLookup) FetchInternalKeyLocator(ctx context.Context,
451452
args := m.Called(ctx, rawKey)
452453
return args.Get(0).(keychain.KeyLocator), args.Error(1)
453454
}
455+
456+
// mockSupplySyncer is a mock implementation of the SupplySyncer interface.
457+
type mockSupplySyncer struct {
458+
mock.Mock
459+
}
460+
461+
func (m *mockSupplySyncer) PushSupplyCommitment(ctx context.Context,
462+
assetSpec asset.Specifier, commitment RootCommitment,
463+
updateLeaves SupplyLeaves, chainProof ChainProof,
464+
canonicalUniverses []url.URL) error {
465+
466+
args := m.Called(ctx, assetSpec, commitment, updateLeaves, chainProof,
467+
canonicalUniverses)
468+
return args.Error(0)
469+
}

0 commit comments

Comments
 (0)