Add support for announcing market hours (#380)

* feat: add market hour configuration for optimal payment timing

This commit adds market hour configuration to help users determine optimal
times for making payments with lower fees. The configuration is managed
through environment variables and exposed via the GetInfo RPC.

Changes:
- Add MarketHour message type to protobuf service definition
- Add market hour configuration fields to Config struct
- Update covenant and covenantless services to handle market hour data
- Extend GetInfo RPC response to include market hour information
- Set default market hour period to 24 hours
- Initialize market hour fields after other service fields

Configuration:
- ARK_FIRST_MARKET_HOUR: Initial market hour timestamp
  (default: current server start time)
- ARK_MARKET_HOUR_PERIOD: Time between market hours in seconds
  (default: 86400)
- ARK_MARKET_HOUR_ROUND_LIFETIME: Round lifetime for market hours
  (default: 0, falls back to ARK_ROUND_LIFETIME)

* feat: add admin RPC for updating market hour configuration

Add new UpdateMarketHour RPC to AdminService for configuring market hour parameters:
- Add request/response messages to admin.proto
- Add UpdateMarketHour method to Service interface
- Implement market hour updates in covenant and covenantless services
- Add validation for market hour parameters
- Implement admin gRPC handler

The RPC allows updating:
- First market hour timestamp
- Market hour period
- Market hour round lifetime (optional, defaults to round lifetime

* feat: add market hour persistence with sqlite

- Add MarketHourRepo interface in domain layer
- Implement market hour persistence using SQLite
- Add market hour queries to sqlc/query.sql
- Update service initialization to load market hours from DB
- Add fallback to config values if no DB entry exists
- Update RepoManager interface with new MarketHourRepo method
This commit is contained in:
Dusan Sekulic
2024-11-22 10:36:51 +01:00
committed by GitHub
parent d6b8508f6d
commit ae3ccb3579
31 changed files with 2464 additions and 827 deletions

View File

@@ -26,6 +26,8 @@ import (
log "github.com/sirupsen/logrus"
)
const marketHourDelta = 5 * time.Minute
type covenantlessService struct {
network common.Network
pubkey *secp256k1.PublicKey
@@ -59,17 +61,32 @@ type covenantlessService struct {
func NewCovenantlessService(
network common.Network,
roundInterval, roundLifetime, unilateralExitDelay, boardingExitDelay int64,
defaultNostrRelays []string,
nostrDefaultRelays []string,
walletSvc ports.WalletService, repoManager ports.RepoManager,
builder ports.TxBuilder, scanner ports.BlockchainScanner,
scheduler ports.SchedulerService,
notificationPrefix string,
noteUriPrefix string,
marketHourStartTime, marketHourEndTime time.Time,
marketHourPeriod, marketHourRoundInterval time.Duration,
) (Service, error) {
pubkey, err := walletSvc.GetPubkey(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to fetch pubkey: %s", err)
}
// Try to load market hours from DB first
marketHour, err := repoManager.MarketHourRepo().Get(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get market hours from db: %w", err)
}
if marketHour == nil {
marketHour = domain.NewMarketHour(marketHourStartTime, marketHourEndTime, marketHourPeriod, marketHourRoundInterval)
if err := repoManager.MarketHourRepo().Upsert(context.Background(), *marketHour); err != nil {
return nil, fmt.Errorf("failed to upsert initial market hours to db: %w", err)
}
}
svc := &covenantlessService{
network: network,
pubkey: pubkey,
@@ -80,7 +97,7 @@ func NewCovenantlessService(
repoManager: repoManager,
builder: builder,
scanner: scanner,
sweeper: newSweeper(walletSvc, repoManager, builder, scheduler, notificationPrefix),
sweeper: newSweeper(walletSvc, repoManager, builder, scheduler, noteUriPrefix),
paymentRequests: newPaymentsMap(),
forfeitTxs: newForfeitTxsMap(builder),
eventsCh: make(chan domain.RoundEvent),
@@ -89,7 +106,7 @@ func NewCovenantlessService(
asyncPaymentsCache: make(map[string]asyncPaymentData),
treeSigningSessions: make(map[string]*musigSigningSession),
boardingExitDelay: boardingExitDelay,
nostrDefaultRelays: defaultNostrRelays,
nostrDefaultRelays: nostrDefaultRelays,
}
repoManager.RegisterEventsHandler(
@@ -693,6 +710,22 @@ func (s *covenantlessService) GetInfo(ctx context.Context) (*ServiceInfo, error)
return nil, fmt.Errorf("failed to get forfeit address: %s", err)
}
marketHourConfig, err := s.repoManager.MarketHourRepo().Get(ctx)
if err != nil {
return nil, err
}
marketHourNextStart, marketHourNextEnd, err := calcNextMarketHour(
marketHourConfig.StartTime,
marketHourConfig.EndTime,
marketHourConfig.Period,
marketHourDelta,
time.Now(),
)
if err != nil {
return nil, err
}
return &ServiceInfo{
PubKey: pubkey,
RoundLifetime: s.roundLifetime,
@@ -701,9 +734,61 @@ func (s *covenantlessService) GetInfo(ctx context.Context) (*ServiceInfo, error)
Network: s.network.Name,
Dust: dust,
ForfeitAddress: forfeitAddr,
NextMarketHour: &NextMarketHour{
StartTime: marketHourNextStart,
EndTime: marketHourNextEnd,
Period: marketHourConfig.Period,
RoundInterval: marketHourConfig.RoundInterval,
},
}, nil
}
func calcNextMarketHour(marketHourStartTime, marketHourEndTime time.Time, period, marketHourDelta time.Duration, now time.Time) (time.Time, time.Time, error) {
// Validate input parameters
if period <= 0 {
return time.Time{}, time.Time{}, fmt.Errorf("period must be greater than 0")
}
if !marketHourEndTime.After(marketHourStartTime) {
return time.Time{}, time.Time{}, fmt.Errorf("market hour end time must be after start time")
}
// Calculate the duration of the market hour
duration := marketHourEndTime.Sub(marketHourStartTime)
// Calculate the number of periods since the initial marketHourStartTime
elapsed := now.Sub(marketHourStartTime)
var n int64
if elapsed >= 0 {
n = int64(elapsed / period)
} else {
n = int64((elapsed - period + 1) / period)
}
// Calculate the current market hour start and end times
currentStartTime := marketHourStartTime.Add(time.Duration(n) * period)
currentEndTime := currentStartTime.Add(duration)
// Adjust if now is before the currentStartTime
if now.Before(currentStartTime) {
n -= 1
currentStartTime = marketHourStartTime.Add(time.Duration(n) * period)
currentEndTime = currentStartTime.Add(duration)
}
timeUntilEnd := currentEndTime.Sub(now)
if !now.Before(currentStartTime) && now.Before(currentEndTime) && timeUntilEnd >= marketHourDelta {
// Return the current market hour
return currentStartTime, currentEndTime, nil
} else {
// Move to the next market hour
n += 1
nextStartTime := marketHourStartTime.Add(time.Duration(n) * period)
nextEndTime := nextStartTime.Add(duration)
return nextStartTime, nextEndTime, nil
}
}
func (s *covenantlessService) RegisterCosignerPubkey(ctx context.Context, paymentId string, pubkey string) error {
pubkeyBytes, err := hex.DecodeString(pubkey)
if err != nil {
@@ -1478,9 +1563,11 @@ func (s *covenantlessService) getNewVtxos(round *domain.Round) []domain.Vtxo {
continue
}
vtxoPubkey := hex.EncodeToString(schnorr.SerializePubKey(vtxoTapKey))
vtxos = append(vtxos, domain.Vtxo{
VtxoKey: domain.VtxoKey{Txid: node.Txid, VOut: uint32(i)},
Pubkey: hex.EncodeToString(schnorr.SerializePubKey(vtxoTapKey)),
Pubkey: vtxoPubkey,
Amount: uint64(out.Value),
RoundTxid: round.Txid,
CreatedAt: createdAt,
@@ -1739,3 +1826,24 @@ func newMusigSigningSession(nbCosigners int) *musigSigningSession {
nbCosigners: nbCosigners,
}
}
func (s *covenantlessService) GetMarketHourConfig(ctx context.Context) (*domain.MarketHour, error) {
return s.repoManager.MarketHourRepo().Get(ctx)
}
func (s *covenantlessService) UpdateMarketHourConfig(
ctx context.Context,
marketHourStartTime, marketHourEndTime time.Time, period, roundInterval time.Duration,
) error {
marketHour := domain.NewMarketHour(
marketHourStartTime,
marketHourEndTime,
period,
roundInterval,
)
if err := s.repoManager.MarketHourRepo().Upsert(ctx, *marketHour); err != nil {
return fmt.Errorf("failed to upsert market hours: %w", err)
}
return nil
}