Skip to content

Commit

Permalink
Add block timestamp info to stored txs
Browse files Browse the repository at this point in the history
  • Loading branch information
altafan committed Jan 31, 2024
1 parent b109ba0 commit 69a6a63
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 57 deletions.
2 changes: 1 addition & 1 deletion internal/core/application/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (as *AccountService) storeQueuedTransactions() {
as.log("received confirmed tx %s from channel", tx.TxID)

if _, err := txRepo.ConfirmTransaction(
ctx, tx.TxID, tx.BlockHash, tx.BlockHeight,
ctx, tx.TxID, tx.BlockHash, tx.BlockHeight, tx.BlockTime,
); err != nil {
as.warn(
err, "error while confirming transaction %s for account(s) %s",
Expand Down
7 changes: 4 additions & 3 deletions internal/core/application/notification_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ func testGetTxChannel(t *testing.T) {

time.Sleep(time.Second)

blockHash := randomHex(32)
blockHeight := uint64(randomIntInRange(1, 300))
blockhash := randomHex(32)
blockheight := uint64(randomIntInRange(1, 300))
blocktime := time.Now().Unix()
repoManager.TransactionRepository().ConfirmTransaction(
ctx, txid, blockHash, blockHeight,
ctx, txid, blockhash, blockheight, blocktime,
)

repoManager.TransactionRepository().UpdateTransaction(
Expand Down
6 changes: 5 additions & 1 deletion internal/core/domain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Transaction struct {
TxHex string
BlockHash string
BlockHeight uint64
BlockTime int64
Accounts map[string]struct{}
}

Expand All @@ -17,13 +18,16 @@ func (t *Transaction) IsConfirmed() bool {
}

// Confirm marks the tx as confirmed.
func (t *Transaction) Confirm(blockHash string, blockHeight uint64) {
func (t *Transaction) Confirm(
blockHash string, blockHeight uint64, blockTime int64,
) {
if t.IsConfirmed() {
return
}

t.BlockHash = blockHash
t.BlockHeight = blockHeight
t.BlockTime = blockTime
}

// AddAccount adds the given account to the map of those involved in the tx.
Expand Down
3 changes: 2 additions & 1 deletion internal/core/domain/transaction_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type TransactionRepository interface {
// Transaction identified by the given txid.
// Generates a TransactionConfirmed event if successful.
ConfirmTransaction(
ctx context.Context, txid, blockHash string, blockheight uint64,
ctx context.Context,
txid, blockHash string, blockheight uint64, blocktime int64,
) (bool, error)
// GetTransaction returns the Transaction identified by the given txid.
GetTransaction(ctx context.Context, txid string) (*Transaction, error)
Expand Down
3 changes: 2 additions & 1 deletion internal/core/domain/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/vulpemventures/ocean/internal/core/domain"
Expand All @@ -11,7 +12,7 @@ func TestConfirmTransaction(t *testing.T) {
tx := &domain.Transaction{}
require.False(t, tx.IsConfirmed())

tx.Confirm("fa84eb6806daf1b3c495ed30554d80573a39335b2993b66b3cc1afaa53816e47", 1728312)
tx.Confirm("fa84eb6806daf1b3c495ed30554d80573a39335b2993b66b3cc1afaa53816e47", 1728312, time.Now().Unix())
require.True(t, tx.IsConfirmed())
}

Expand Down
15 changes: 9 additions & 6 deletions internal/infrastructure/blockchain-scanner/electrum/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,19 +555,22 @@ func (s *service) dbEventHandler(event dbEvent) {
chTx := s.getTxChannelByAccount(event.account)
txHex, _ := tx.ToHex()

var hash string
var blockHeight uint64
var blockhash string
var blockheight uint64
var blocktime int64
if block != nil {
hash = block.hash().String()
blockHeight = uint64(event.tx.Height)
blockhash = block.hash().String()
blockheight = uint64(event.tx.Height)
blocktime = block.timestamp()
}

go func() {
chTx <- &domain.Transaction{
TxID: event.tx.Txid,
TxHex: txHex,
BlockHash: hash,
BlockHeight: blockHeight,
BlockHash: blockhash,
BlockHeight: blockheight,
BlockTime: blocktime,
Accounts: map[string]struct{}{event.account: {}},
}
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (r *transactionRepository) AddTransaction(
}

func (r *transactionRepository) ConfirmTransaction(
ctx context.Context, txid, blockHash string, blockheight uint64,
ctx context.Context,
txid, blockHash string, blockheight uint64, blocktime int64,
) (bool, error) {
tx, err := r.getTx(ctx, txid)
if err != nil {
Expand All @@ -66,7 +67,7 @@ func (r *transactionRepository) ConfirmTransaction(
return false, nil
}

tx.Confirm(blockHash, blockheight)
tx.Confirm(blockHash, blockheight, blocktime)

if err := r.updateTx(ctx, *tx); err != nil {
return false, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ func (r *txRepository) AddTransaction(
}

func (r *txRepository) ConfirmTransaction(
ctx context.Context, txid, blockHash string, blockheight uint64,
ctx context.Context,
txid, blockHash string, blockheight uint64, blocktime int64,
) (bool, error) {
r.store.lock.Lock()
defer r.store.lock.Unlock()

return r.confirmTx(ctx, txid, blockHash, blockheight)
return r.confirmTx(ctx, txid, blockHash, blockheight, blocktime)
}

func (r *txRepository) GetTransaction(
Expand Down Expand Up @@ -106,7 +107,8 @@ func (r *txRepository) addTx(
}

func (r *txRepository) confirmTx(
ctx context.Context, txid string, blockHash string, blockHeight uint64,
ctx context.Context,
txid string, blockHash string, blockHeight uint64, blocktime int64,
) (bool, error) {
tx, err := r.getTx(ctx, txid)
if err != nil {
Expand All @@ -117,7 +119,7 @@ func (r *txRepository) confirmTx(
return false, nil
}

tx.Confirm(blockHash, blockHeight)
tx.Confirm(blockHash, blockHeight, blocktime)

r.store.txs[txid] = tx

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE transaction DROP COLUMN block_time;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE transaction ADD COLUMN block_time BIGINT;

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

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

6 changes: 3 additions & 3 deletions internal/infrastructure/storage/db/postgres/sqlc/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ SELECT * FROM utxo WHERE account_name=$1;

/* TRANSACTION */
-- name: InsertTransaction :one
INSERT INTO transaction(tx_id,tx_hex,block_hash,block_height)
VALUES($1,$2,$3,$4) RETURNING *;
INSERT INTO transaction(tx_id,tx_hex,block_hash,block_height,block_time)
VALUES($1,$2,$3,$4,$5) RETURNING *;

-- name: InsertTransactionInputAccount :one
INSERT INTO tx_input_account(account_name, fk_tx_id)
VALUES($1,$2) RETURNING *;

-- name: UpdateTransaction :one
UPDATE transaction SET tx_hex=$1,block_hash=$2,block_height=$3 WHERE tx_id=$4 RETURNING *;
UPDATE transaction SET tx_hex=$1,block_hash=$2,block_height=$3,block_time=$4 WHERE tx_id=$5 RETURNING *;

-- name: DeleteTransactionInputAccounts :exec
DELETE FROM tx_input_account WHERE fk_tx_id=$1;
Expand Down
Loading

0 comments on commit 69a6a63

Please sign in to comment.