Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting claim transaction fee #57

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
344 changes: 182 additions & 162 deletions boltzrpc/boltzrpc.pb.go

Large diffs are not rendered by default.

192 changes: 80 additions & 112 deletions boltzrpc/boltzrpc.pb.gw.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions boltzrpc/boltzrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ message ReverseSwapInfo {
uint32 timeout_block_height = 11;
string lockup_transaction_id = 12;
string claim_transaction_id = 13;
uint32 claim_fee_per_vbyte = 14;
}

message GetInfoRequest {}
Expand Down Expand Up @@ -211,9 +212,14 @@ message CreateChannelRequest {

message CreateReverseSwapRequest {
int64 amount = 1;

// If no value is set, the daemon will query a new P2WKH address from LND
string address = 2;

bool accept_zero_conf = 3;

// sat/vByte fee for the claim transaction; if not set, a reasonable fee will be queried from mempool.space or LND
uint32 sat_per_vbyte_fee = 4;
}
message CreateReverseSwapResponse {
string id = 1;
Expand Down
12 changes: 2 additions & 10 deletions boltzrpc/boltzrpc_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package build

var Commit string

const version = "1.2.7"
const version = "1.2.8"

func GetVersion() string {
basicVersion := "v" + version
Expand Down
83 changes: 57 additions & 26 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,62 @@ import (

// TODO: prepare insert statements only once

const (
createTables = `
CREATE TABLE IF NOT EXISTS version (
version INT
);

CREATE TABLE IF NOT EXISTS macaroons (
id VARCHAR PRIMARY KEY,
rootKey VARCHAR
);

CREATE TABLE IF NOT EXISTS swaps (
id VARCHAR PRIMARY KEY,
state INT,
error VARCHAR,
status VARCHAR,
privateKey VARCHAR,
preimage VARCHAR,
redeemScript VARCHAR,
invoice VARCHAR,
address VARCHAR,
expectedAmount INT,
timeoutBlockheight INTEGER,
lockupTransactionId VARCHAR,
refundTransactionId VARCHAR
);

CREATE TABLE IF NOT EXISTS reverseSwaps (
id VARCHAR PRIMARY KEY,
state INT,
error VARCHAR,
status VARCHAR,
acceptZeroConf BOOLEAN,
privateKey VARCHAR,
preimage VARCHAR,
redeemScript VARCHAR,
invoice VARCHAR,
claimAddress VARCHAR,
expectedAmount INT,
timeoutBlockheight INTEGER,
lockupTransactionId VARCHAR,
claimFeePerVbyte INTEGER,
claimTransactionId VARCHAR
);

CREATE TABLE IF NOT EXISTS channelCreations (
swapId VARCHAR PRIMARY KEY,
status VARCHAR,
inboundLiquidity INT,
private BOOLEAN,
fundingTransactionId VARCHAR,
fundingTransactionVout INT
);
`
)

type Database struct {
Path string `long:"database.path" description:"Path to the database file"`

Expand All @@ -36,32 +92,7 @@ func (database *Database) Connect() error {
}

func (database *Database) createTables() error {
_, err := database.db.Exec("CREATE TABLE IF NOT EXISTS version (version INT)")

if err != nil {
return err
}

_, err = database.db.Exec("CREATE TABLE IF NOT EXISTS macaroons (id VARCHAR PRIMARY KEY, rootKey VARCHAR)")

if err != nil {
return err
}

_, err = database.db.Exec("CREATE TABLE IF NOT EXISTS swaps (id VARCHAR PRIMARY KEY, state INT, error VARCHAR, status VARCHAR, privateKey VARCHAR, preimage VARCHAR, redeemScript VARCHAR, invoice VARCHAR, address VARCHAR, expectedAmount INT, timeoutBlockheight INTEGER, lockupTransactionId VARCHAR, refundTransactionId VARCHAR)")

if err != nil {
return err
}

_, err = database.db.Exec("CREATE TABLE IF NOT EXISTS reverseSwaps (id VARCHAR PRIMARY KEY, state INT, error VARCHAR, status VARCHAR, acceptZeroConf BOOLEAN, privateKey VARCHAR, preimage VARCHAR, redeemScript VARCHAR, invoice VARCHAR, claimAddress VARCHAR, expectedAmount INT, timeoutBlockheight INTEGER, lockupTransactionId VARCHAR, claimTransactionId VARCHAR)")

if err != nil {
return err
}

_, err = database.db.Exec("CREATE TABLE IF NOT EXISTS channelCreations (swapId VARCHAR PRIMARY KEY, status VARCHAR, inboundLiquidity INT, private BOOLEAN, fundingTransactionId VARCHAR, fundingTransactionVout INT)")

_, err := database.db.Exec(createTables)
return err
}

Expand Down
42 changes: 30 additions & 12 deletions database/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type swapStatus struct {
status string
}

const latestSchemaVersion = 2
const latestSchemaVersion = 3

func (database *Database) migrate() error {
version, err := database.queryVersion()
Expand All @@ -36,12 +36,11 @@ func (database *Database) migrate() error {
return database.performMigration(version)
}

func (database *Database) performMigration(fromVersion int) error {
switch fromVersion {
func (database *Database) performMigration(oldVersion int) error {
switch oldVersion {
case 1:
logger.Info("Updating database from version 1 to 2")

logger.Info("Migrating table \"swaps\"")
logMigration(oldVersion)
logMigratingTable("swaps")

_, err := database.db.Exec("ALTER TABLE swaps ADD COLUMN state INT")

Expand Down Expand Up @@ -114,7 +113,7 @@ func (database *Database) performMigration(fromVersion int) error {
}
}

logger.Info("Migrating table \"reverseSwaps\"")
logMigratingTable("reverseSwaps")

_, err = database.db.Exec("ALTER TABLE reverseSwaps ADD COLUMN state INT")

Expand Down Expand Up @@ -177,27 +176,38 @@ func (database *Database) performMigration(fromVersion int) error {
}
}

_, err = database.db.Exec("UPDATE version SET version = 2 WHERE version = 1")
return database.postMigration(oldVersion)

case 2:
logMigration(oldVersion)

_, err := database.db.Exec("ALTER TABLE reverseSwaps ADD COLUMN claimFeePerVbyte INTEGER NOT NULL DEFAULT 0")
if err != nil {
return err
}

logger.Info("Update to database version 2 completed")
return database.postMigration(fromVersion)
return database.postMigration(oldVersion)

case latestSchemaVersion:
logger.Info("Database already at latest schema version: " + strconv.Itoa(latestSchemaVersion))

default:
return errors.New("found unexpected database schema version: " + strconv.Itoa(fromVersion))
return errors.New("found unexpected database schema version: " + strconv.Itoa(oldVersion))
}

return nil
}

func (database *Database) postMigration(fromVersion int) error {
_, err := database.db.Exec("UPDATE version SET version = ? WHERE version = ?", fromVersion+1, fromVersion)
if err != nil {
return err
}

logger.Infof("Update to database version %d completed", fromVersion+1)

if fromVersion+1 < latestSchemaVersion {
logger.Info("Running migration again")
logger.Info("Running next database migration")
return database.migrate()
}

Expand All @@ -215,3 +225,11 @@ func (database *Database) queryVersion() (int, error) {

return version, err
}

func logMigration(oldVersion int) {
logger.Infof("Updating database from version %d to %d", oldVersion, oldVersion+1)
}

func logMigratingTable(table string) {
logger.Infof("Migrating table \"%s\"", table)
}
34 changes: 31 additions & 3 deletions database/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ReverseSwap struct {
OnchainAmount uint64
TimeoutBlockHeight uint32
LockupTransactionId string
ClaimFeePerVbyte uint32
ClaimTransactionId string
}

Expand All @@ -42,6 +43,7 @@ type ReverseSwapSerialized struct {
OnchainAmount uint64
TimeoutBlockHeight uint32
LockupTransactionId string
ClaimFeePerVbyte uint32
ClaimTransactionId string
}

Expand All @@ -60,6 +62,7 @@ func (reverseSwap *ReverseSwap) Serialize() ReverseSwapSerialized {
OnchainAmount: reverseSwap.OnchainAmount,
TimeoutBlockHeight: reverseSwap.TimeoutBlockHeight,
LockupTransactionId: reverseSwap.LockupTransactionId,
ClaimFeePerVbyte: reverseSwap.ClaimFeePerVbyte,
ClaimTransactionId: reverseSwap.ClaimTransactionId,
}
}
Expand Down Expand Up @@ -88,6 +91,7 @@ func parseReverseSwap(rows *sql.Rows) (*ReverseSwap, error) {
"expectedAmount": &reverseSwap.OnchainAmount,
"timeoutBlockheight": &reverseSwap.TimeoutBlockHeight,
"lockupTransactionId": &reverseSwap.LockupTransactionId,
"claimFeePerVbyte": &reverseSwap.ClaimFeePerVbyte,
"claimTransactionId": &reverseSwap.ClaimTransactionId,
},
)
Expand Down Expand Up @@ -169,7 +173,24 @@ func (database *Database) QueryPendingReverseSwaps() ([]ReverseSwap, error) {
}

func (database *Database) CreateReverseSwap(reverseSwap ReverseSwap) error {
insertStatement := "INSERT INTO reverseSwaps (id, state, error, status, acceptZeroConf, privateKey, preimage, redeemScript, invoice, claimAddress, expectedAmount, timeoutBlockheight, lockupTransactionId, claimTransactionId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
insertStatement := `
INSERT INTO reverseSwaps (
id,
state,
error,
status,
acceptZeroConf,
privateKey,
preimage,
redeemScript,
invoice,
claimAddress,
expectedAmount,
timeoutBlockheight,
lockupTransactionId,
claimFeePerVbyte,
claimTransactionId
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
statement, err := database.db.Prepare(insertStatement)

if err != nil {
Expand All @@ -190,6 +211,7 @@ func (database *Database) CreateReverseSwap(reverseSwap ReverseSwap) error {
reverseSwap.OnchainAmount,
reverseSwap.TimeoutBlockHeight,
reverseSwap.LockupTransactionId,
reverseSwap.ClaimFeePerVbyte,
reverseSwap.ClaimTransactionId,
)

Expand Down Expand Up @@ -222,9 +244,15 @@ func (database *Database) SetReverseSwapLockupTransactionId(reverseSwap *Reverse
return err
}

func (database *Database) SetReverseSwapClaimTransactionId(reverseSwap *ReverseSwap, claimTransactionId string) error {
func (database *Database) SetReverseSwapClaimTransactionId(reverseSwap *ReverseSwap, claimTransactionId string, satPerVbyte int64) error {
reverseSwap.ClaimTransactionId = claimTransactionId
reverseSwap.ClaimFeePerVbyte = uint32(satPerVbyte)

_, err := database.db.Exec("UPDATE reverseSwaps SET claimTransactionId = ? WHERE id = ?", claimTransactionId, reverseSwap.Id)
_, err := database.db.Exec(
"UPDATE reverseSwaps SET claimTransactionId = ?, claimFeePerVbyte = ? WHERE id = ?",
reverseSwap.ClaimTransactionId,
reverseSwap.ClaimFeePerVbyte,
reverseSwap.Id,
)
return err
}
5 changes: 1 addition & 4 deletions database/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
// TODO: optimize (query columns only once per query (improves speed by like a microsecond per row so low priority))
func scanRow(row *sql.Rows, rowValues map[string]interface{}) error {
columns, err := row.Columns()

if err != nil {
return err
}
Expand All @@ -18,7 +17,5 @@ func scanRow(row *sql.Rows, rowValues map[string]interface{}) error {
values = append(values, rowValues[column])
}

err = row.Scan(values...)

return err
return row.Scan(values...)
}
2 changes: 2 additions & 0 deletions docs/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| `amount` | [`int64`](#int64) | | |
| `address` | [`string`](#string) | | If no value is set, the daemon will query a new P2WKH address from LND |
| `accept_zero_conf` | [`bool`](#bool) | | |
| `sat_per_vbyte_fee` | [`uint32`](#uint32) | | sat/vByte fee for the claim transaction; if not set, a reasonable fee will be queried from mempool.space or LND |



Expand Down Expand Up @@ -356,6 +357,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| `timeout_block_height` | [`uint32`](#uint32) | | |
| `lockup_transaction_id` | [`string`](#string) | | |
| `claim_transaction_id` | [`string`](#string) | | |
| `claim_fee_per_vbyte` | [`uint32`](#uint32) | | |



Expand Down
Loading