Skip to content

Commit

Permalink
multi: add aux leaf creator
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed May 14, 2024
1 parent d55ae17 commit c7135e7
Show file tree
Hide file tree
Showing 5 changed files with 564 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lightninglabs/taproot-assets/monitoring"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/tapchannel"
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tapgarden"
Expand Down Expand Up @@ -125,6 +126,8 @@ type Config struct {

UniverseStats universe.Telemetry

AuxLeafCreator *tapchannel.AuxLeafCreator

// UniversePublicAccess is flag which, If true, and the Universe server
// is on a public interface, valid proof from remote parties will be
// accepted, and proofs will be queryable by remote parties.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/lightningnetwork/lnd v0.17.0-beta.rc6.0.20240501135111-81970eac6a5a
github.com/lightningnetwork/lnd/cert v1.2.2
github.com/lightningnetwork/lnd/clock v1.1.1
github.com/lightningnetwork/lnd/fn v1.0.5
github.com/lightningnetwork/lnd/tlv v1.2.6
github.com/lightningnetwork/lnd/tor v1.1.2
github.com/ory/dockertest/v3 v3.10.0
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
github.com/lightninglabs/lightning-node-connect v0.2.5-alpha // indirect
github.com/lightninglabs/neutrino v0.16.0 // indirect
github.com/lightningnetwork/lightning-onion v1.2.1-0.20230823005744-06182b1d7d2f // indirect
github.com/lightningnetwork/lnd/fn v1.0.5 // indirect
github.com/lightningnetwork/lnd/healthcheck v1.2.4 // indirect
github.com/lightningnetwork/lnd/kvdb v1.4.6 // indirect
github.com/lightningnetwork/lnd/queue v1.1.1 // indirect
Expand Down
109 changes: 109 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ import (
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/channeldb"
lfn "github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/tlv"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"gopkg.in/macaroon-bakery.v2/bakery"
Expand Down Expand Up @@ -174,6 +179,11 @@ func (s *Server) initialize(interceptorChain *rpcperms.InterceptorChain) error {
return fmt.Errorf("unable to start RFQ manager: %w", err)
}

// Start the auxiliary leaf creator.
if err := s.cfg.AuxLeafCreator.Start(); err != nil {
return fmt.Errorf("unable to start aux leaf creator: %w", err)
}

if s.cfg.UniversePublicAccess {
err := s.cfg.UniverseFederation.SetAllowPublicAccess()
if err != nil {
Expand Down Expand Up @@ -610,6 +620,10 @@ func (s *Server) Stop() error {
return err
}

if err := s.cfg.AuxLeafCreator.Stop(); err != nil {
return err
}

if s.macaroonService != nil {
err := s.macaroonService.Stop()
if err != nil {
Expand All @@ -623,3 +637,98 @@ func (s *Server) Stop() error {

return nil
}

// A compile-time check to ensure that Server fully implements the
// lnwallet.AuxLeafStore interface.
var _ lnwallet.AuxLeafStore = (*Server)(nil)

// FetchLeavesFromView attempts to fetch the auxiliary leaves that correspond to
// the passed aux blob, and pending fully evaluated HTLC view.
//
// NOTE: This method is part of the lnwallet.AuxLeafStore interface.
func (s *Server) FetchLeavesFromView(chanState *channeldb.OpenChannel,
prevBlob tlv.Blob, view *lnwallet.HtlcView, isOurCommit bool,
ourBalance, theirBalance lnwire.MilliSatoshi,
keys lnwallet.CommitmentKeyRing) (lfn.Option[lnwallet.CommitAuxLeaves],
lnwallet.CommitSortFunc, error) {

srvrLog.Debugf("FetchLeavesFromView called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](), nil,
fmt.Errorf("tapd is shutting down")
}

return s.cfg.AuxLeafCreator.FetchLeavesFromView(
chanState, prevBlob, view, isOurCommit, ourBalance,
theirBalance, keys,
)
}

// FetchLeavesFromCommit attempts to fetch the auxiliary leaves that
// correspond to the passed aux blob, and an existing channel
// commitment.
//
// NOTE: This method is part of the lnwallet.AuxLeafStore interface.
func (s *Server) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
com channeldb.ChannelCommitment,
keys lnwallet.CommitmentKeyRing) (lfn.Option[lnwallet.CommitAuxLeaves],
error) {

srvrLog.Debugf("FetchLeavesFromCommit called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](),
fmt.Errorf("tapd is shutting down")
}

return s.cfg.AuxLeafCreator.FetchLeavesFromCommit(chanState, com, keys)
}

// FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
// from a channel revocation that stores balance + blob information.
//
// NOTE: This method is part of the lnwallet.AuxLeafStore interface.
func (s *Server) FetchLeavesFromRevocation(
rev *channeldb.RevocationLog) (lfn.Option[lnwallet.CommitAuxLeaves],
error) {

srvrLog.Debugf("FetchLeavesFromRevocation called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](),
fmt.Errorf("tapd is shutting down")
}

return s.cfg.AuxLeafCreator.FetchLeavesFromRevocation(rev)
}

// ApplyHtlcView serves as the state transition function for the custom
// channel's blob. Given the old blob, and an HTLC view, then a new
// blob should be returned that reflects the pending updates.
//
// NOTE: This method is part of the lnwallet.AuxLeafStore interface.
func (s *Server) ApplyHtlcView(chanState *channeldb.OpenChannel,
prevBlob tlv.Blob, originalView *lnwallet.HtlcView, isOurCommit bool,
ourBalance, theirBalance lnwire.MilliSatoshi,
keys lnwallet.CommitmentKeyRing) (lfn.Option[tlv.Blob], error) {

srvrLog.Debugf("ApplyHtlcView called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[tlv.Blob](), fmt.Errorf("tapd is shutting down")
}

return s.cfg.AuxLeafCreator.ApplyHtlcView(
chanState, prevBlob, originalView, isOurCommit, ourBalance,
theirBalance, keys,
)
}
8 changes: 8 additions & 0 deletions tapcfg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/tapchannel"
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
"github.com/lightninglabs/taproot-assets/tapfreighter"
Expand Down Expand Up @@ -340,6 +341,12 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
return nil, err
}

auxLeafCreator := tapchannel.NewAuxLeafCreator(
&tapchannel.LeafCreatorConfig{
ChainParams: &tapChainParams,
},
)

return &tap.Config{
DebugLevel: cfg.DebugLevel,
RuntimeID: runtimeID,
Expand Down Expand Up @@ -413,6 +420,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
UniverseQueriesPerSecond: cfg.Universe.UniverseQueriesPerSecond,
UniverseQueriesBurst: cfg.Universe.UniverseQueriesBurst,
RfqManager: rfqManager,
AuxLeafCreator: auxLeafCreator,
LogWriter: cfg.LogWriter,
DatabaseConfig: &tap.DatabaseConfig{
RootKeyStore: tapdb.NewRootKeyStore(rksDB),
Expand Down
Loading

0 comments on commit c7135e7

Please sign in to comment.