Skip to content

Commit ce43d36

Browse files
fix: rabbit/arkana comments
1 parent 8dfe974 commit ce43d36

7 files changed

Lines changed: 57 additions & 32 deletions

File tree

internal/infrastructure/db/postgres/asset_repo.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ func (r *assetRepository) GetAssets(
143143
}
144144

145145
if row.Metadata.Valid {
146-
ast.Metadata, err = asset.NewMetadataListFromString(row.Metadata.String)
147-
if err != nil {
148-
log.WithError(err).Warnf("failed to parse metadata for asset %s", row.ID)
146+
// Parsing metadata should never fail but if it does we just return an empty list
147+
// of metadata and log the error
148+
metadata, parseErr := asset.NewMetadataListFromString(row.Metadata.String)
149+
if parseErr != nil {
150+
log.WithError(parseErr).Warnf("failed to parse metadata for asset %s", row.ID)
151+
} else {
152+
ast.Metadata = metadata
149153
}
150154
}
151155

internal/infrastructure/db/sqlite/asset_repo.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,9 @@ func (r *assetRepository) GetAssets(
158158
indexByID[row.ID] = idx
159159
}
160160

161-
if !row.AssetAmount.Valid {
162-
continue
163-
}
164-
165-
amount, ok := new(big.Int).SetString(row.AssetAmount.String, 10)
161+
amount, ok := new(big.Int).SetString(row.AssetAmount, 10)
166162
if !ok {
167-
continue
163+
return nil, fmt.Errorf("invalid supply value: %s", row.AssetAmount)
168164
}
169165
assets[idx].Supply.Add(&assets[idx].Supply, amount)
170166
}

internal/infrastructure/db/sqlite/sqlc/queries/query.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/infrastructure/db/sqlite/sqlc/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ SELECT
453453
a.metadata_hash,
454454
a.metadata,
455455
a.control_asset_id,
456-
v.asset_amount
456+
COALESCE(v.asset_amount, '0') AS asset_amount
457457
FROM asset a
458458
LEFT JOIN vtxo_vw v
459459
ON v.asset_id = a.id

internal/infrastructure/db/sqlite/utils.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"time"
1414

1515
"github.com/arkade-os/arkd/internal/infrastructure/db/sqlite/sqlc/queries"
16-
_ "modernc.org/sqlite"
16+
log "github.com/sirupsen/logrus"
17+
sqlite "modernc.org/sqlite"
18+
sqlite3 "modernc.org/sqlite/lib"
1719
)
1820

1921
const (
@@ -74,20 +76,33 @@ func OpenDb(dbPath string) (SQLiteDB, error) {
7476
}
7577
writeDB.SetMaxOpenConns(1)
7678

77-
// Force connections to open now.
78-
if err := writeDB.Ping(); err != nil {
79+
// Use WAL so reads do not block writes
80+
if _, err := writeDB.Exec(`PRAGMA journal_mode = WAL;`); err != nil {
7981
_ = readDB.Close()
8082
_ = writeDB.Close()
81-
return nil, fmt.Errorf("failed to ping write db: %w", err)
83+
return nil, fmt.Errorf("failed to enable WAL: %w", err)
8284
}
8385

84-
// Use WAL so reads do not block writes
85-
if _, err := writeDB.Exec(`PRAGMA journal_mode = WAL;`); err != nil {
86+
// Use busy_timeout so reads/writes wait for WAL checkpointing instead of failing
87+
if _, err := writeDB.Exec(`PRAGMA busy_timeout = 5000;`); err != nil {
88+
_ = readDB.Close()
89+
_ = writeDB.Close()
90+
return nil, fmt.Errorf("failed to enable WAL: %w", err)
91+
}
92+
93+
if _, err := readDB.Exec(`PRAGMA busy_timeout = 5000;`); err != nil {
8694
_ = readDB.Close()
8795
_ = writeDB.Close()
8896
return nil, fmt.Errorf("failed to enable WAL: %w", err)
8997
}
9098

99+
// Check there are no errors when opening a connection
100+
if err := writeDB.Ping(); err != nil {
101+
_ = readDB.Close()
102+
_ = writeDB.Close()
103+
return nil, fmt.Errorf("failed to ping write db: %w", err)
104+
}
105+
91106
if err := readDB.Ping(); err != nil {
92107
_ = readDB.Close()
93108
_ = writeDB.Close()
@@ -164,7 +179,7 @@ func execTx(
164179
}
165180

166181
if err := closeConn(conn, false); err != nil {
167-
return err
182+
log.WithError(err).Warn("failed to close connection after successful commit")
168183
}
169184
return nil
170185
}
@@ -250,9 +265,8 @@ func closeConn(conn *sql.Conn, discard bool) error {
250265
return nil
251266
}
252267

253-
// isInterruptError reports whether err looks like a context cancellation or a
254-
// SQLite interrupt. SQLite interrupt detection is partly heuristic because the
255-
// driver may surface it as driver-specific error text.
268+
// isInterruptError reports whether err is a context cancellation or a SQLite
269+
// interrupt.
256270
func isInterruptError(ctx context.Context, err error) bool {
257271
if err == nil {
258272
return false
@@ -266,8 +280,13 @@ func isInterruptError(ctx context.Context, err error) bool {
266280
return true
267281
}
268282

269-
errMsg := strings.ToLower(err.Error())
270-
return strings.Contains(errMsg, "interrupted") || strings.Contains(errMsg, "sqlite_interrupt")
283+
var sqliteErr *sqlite.Error
284+
if errors.As(err, &sqliteErr) {
285+
code := sqliteErr.Code()
286+
return code == sqlite3.SQLITE_INTERRUPT || code&0xff == sqlite3.SQLITE_INTERRUPT
287+
}
288+
289+
return false
271290
}
272291

273292
func isConflictError(err error) bool {

internal/test/e2e/e2e_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5568,6 +5568,8 @@ func TestGetAssetQueryChurn(t *testing.T) {
55685568
var aliceRecvd, bobRecvd []types.Vtxo
55695569

55705570
for i := range supply {
5571+
completed := i + 1
5572+
55715573
sendWg := &sync.WaitGroup{}
55725574
sendWg.Add(2)
55735575
recvWg := &sync.WaitGroup{}
@@ -5607,14 +5609,14 @@ func TestGetAssetQueryChurn(t *testing.T) {
56075609
}()
56085610

56095611
sendWg.Wait()
5610-
require.NoErrorf(t, aliceSendErr, "send %d/%d failed", i, supply)
5611-
require.NoErrorf(t, bobSendErr, "send %d/%d failed", i, supply)
5612+
require.NoErrorf(t, aliceSendErr, "send %d/%d failed", completed, supply)
5613+
require.NoErrorf(t, bobSendErr, "send %d/%d failed", completed, supply)
56125614

56135615
recvWg.Wait()
56145616
require.NoError(t, aliceRecvErr, "receiving vtxos for send %s %d/%d failed",
5615-
aliceSendRes.Txid, i, supply)
5617+
aliceSendRes.Txid, completed, supply)
56165618
require.NoError(t, bobRecvErr, "receiving vtxos for send %s %d/%d failed",
5617-
bobSendRes.Txid, i, supply)
5619+
bobSendRes.Txid, completed, supply)
56185620

56195621
outpoints := make([]types.Outpoint, 0)
56205622
spentVtxos := make([]types.Outpoint, 0)
@@ -5666,7 +5668,7 @@ func TestGetAssetQueryChurn(t *testing.T) {
56665668
}
56675669

56685670
// start a batch after every batchInterval sends
5669-
if i != 0 && i%batchInterval == 0 {
5671+
if completed%batchInterval == 0 {
56705672
settleWg := &sync.WaitGroup{}
56715673
settleWg.Add(4)
56725674

@@ -5735,7 +5737,7 @@ func TestGetAssetQueryChurn(t *testing.T) {
57355737
require.NoError(t, bobGetCtxErr)
57365738
require.Len(t, bobCtx.Batches, 1, "failed to update completed round in database")
57375739
t.Logf("completed %d/%d offchain sends and batch %d/%d",
5738-
i, supply, i/batchInterval, supply/batchInterval)
5740+
completed, supply, completed/batchInterval, supply/batchInterval)
57395741
}
57405742
}
57415743

internal/test/e2e/utils_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,14 +811,18 @@ func isRetryableChurnError(err error) bool {
811811
return false
812812
}
813813

814-
func waitForVTXOs(ch <-chan indexer.ScriptEvent, atLeastN int, timeout time.Duration) ([]types.Vtxo, error) {
814+
func waitForVTXOs(
815+
ch <-chan indexer.ScriptEvent,
816+
atLeastN int,
817+
timeout time.Duration,
818+
) ([]types.Vtxo, error) {
815819
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(timeout))
816820
defer cancel()
817821
vtxos := make([]types.Vtxo, 0)
818822
for {
819823
select {
820824
case <-ctx.Done():
821-
return nil, fmt.Errorf("timed out - %d/%d recieved", len(vtxos), atLeastN)
825+
return nil, fmt.Errorf("timed out - %d/%d received", len(vtxos), atLeastN)
822826
case evt, ok := <-ch:
823827
if !ok {
824828
return nil, fmt.Errorf("vtxo event channel closed")

0 commit comments

Comments
 (0)