Skip to content

Commit

Permalink
explicitly using index to check for existing unconfirmed txs
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Aug 18, 2024
1 parent 97dc913 commit 67bfdcb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions database/bfgd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Database interface {
PopBasisInsertFull(ctx context.Context, pb *PopBasis) error
PopBasisInsertPopMFields(ctx context.Context, pb *PopBasis) error
PopBasisUpdateBTCFields(ctx context.Context, pb *PopBasis) (int64, error)
PopBasisExistsByBtcTxIdUnconfirmed(ctx context.Context, btcTxId [32]byte) (bool, error)

L2BTCFinalityMostRecent(ctx context.Context, limit uint32) ([]L2BTCFinality, error)
L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2KeystoneAbrevHashes []database.ByteArray) ([]L2BTCFinality, error)
Expand Down
17 changes: 17 additions & 0 deletions database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ func (p *pgdb) BtcBlockHeightByHash(ctx context.Context, hash [32]byte) (uint64,
return height, nil
}

func (p *pgdb) PopBasisExistsByBtcTxIdUnconfirmed(ctx context.Context, btcTxId [32]byte) (bool, error) {
log.Tracef("PopBasisInsertPopMFields")
defer log.Tracef("PopBasisInsertPopMFields exit")
const q = `
SELECT EXISTS(SELECT * FROM pop_basis WHERE btc_txid = $1 AND btc_block_hash IS NULL)
`
row := p.db.QueryRowContext(ctx, q, database.ByteArray(btcTxId[:]))

var exists bool

if err := row.Scan(&exists); err != nil {
return false, err
}

return exists, nil
}

func (p *pgdb) PopBasisInsertPopMFields(ctx context.Context, pb *bfgd.PopBasis) error {
log.Tracef("PopBasisInsertPopMFields")
defer log.Tracef("PopBasisInsertPopMFields exit")
Expand Down
42 changes: 29 additions & 13 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,37 @@ func (s *Server) handleBitcoinBroadcast(ctx context.Context, bbr *bfgapi.Bitcoin
}

go func() {
// retry up to 2 times, allowing only 5 second per try
// if we fail here it is ok for now
for range 2 {
insertCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.db.PopBasisInsertPopMFields(insertCtx, &bfgd.PopBasis{
BtcTxId: txHash,
BtcRawTx: database.ByteArray(bbr.Transaction),
PopMinerPublicKey: publicKeyUncompressed,
L2KeystoneAbrevHash: tl2.L2Keystone.Hash(),
}); err != nil {
log.Errorf("error occurred inserting pop basis: %s", err)
} else {
insertCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

if len(txHash) != 32 {
log.Errorf("tx hash %v invalid length of %d", len(txHash))
return
}

exists, err := s.db.PopBasisExistsByBtcTxIdUnconfirmed(insertCtx, [32]byte(txHash))
if err != nil {
log.Errorf("error checking if pop_basis exists: %v", err)
}

log.Infof("does unconfirmed txid exist? %s : %v", hex.EncodeToString(txHash), exists)

if exists {
return
}

if err := s.db.PopBasisInsertPopMFields(insertCtx, &bfgd.PopBasis{
BtcTxId: txHash,
BtcRawTx: database.ByteArray(bbr.Transaction),
PopMinerPublicKey: publicKeyUncompressed,
L2KeystoneAbrevHash: tl2.L2Keystone.Hash(),
}); err != nil {
log.Errorf("error occurred inserting pop basis: %s", err)
if errors.Is(err, database.ErrDuplicate) {
return
}
} else {
return
}
}()

Expand Down

0 comments on commit 67bfdcb

Please sign in to comment.