Commit 11fe70a9 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: add success field to teleportr disbursement table

parent 2ece2172
...@@ -45,6 +45,7 @@ type CompletedTeleport struct { ...@@ -45,6 +45,7 @@ type CompletedTeleport struct {
ID uint64 ID uint64
Address common.Address Address common.Address
Amount *big.Int Amount *big.Int
Success bool
Deposit ConfirmationInfo Deposit ConfirmationInfo
Disbursement ConfirmationInfo Disbursement ConfirmationInfo
} }
...@@ -65,7 +66,8 @@ CREATE TABLE IF NOT EXISTS disbursements ( ...@@ -65,7 +66,8 @@ CREATE TABLE IF NOT EXISTS disbursements (
id INT8 NOT NULL PRIMARY KEY REFERENCES deposits(id), id INT8 NOT NULL PRIMARY KEY REFERENCES deposits(id),
txn_hash VARCHAR NOT NULL, txn_hash VARCHAR NOT NULL,
block_number INT8 NOT NULL, block_number INT8 NOT NULL,
block_timestamp TIMESTAMPTZ NOT NULL block_timestamp TIMESTAMPTZ NOT NULL,
success BOOL NOT NULL
); );
` `
...@@ -325,10 +327,10 @@ func (d *Database) LatestDisbursementID() (*uint64, error) { ...@@ -325,10 +327,10 @@ func (d *Database) LatestDisbursementID() (*uint64, error) {
} }
const markDisbursedStatement = ` const markDisbursedStatement = `
INSERT INTO disbursements (id, txn_hash, block_number, block_timestamp) INSERT INTO disbursements (id, txn_hash, block_number, block_timestamp, success)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO UPDATE ON CONFLICT (id) DO UPDATE
SET (txn_hash, block_number, block_timestamp) = ($2, $3, $4) SET (txn_hash, block_number, block_timestamp, success) = ($2, $3, $4, $5)
` `
// UpsertDisbursement inserts a disbursement, or updates an existing record // UpsertDisbursement inserts a disbursement, or updates an existing record
...@@ -338,6 +340,7 @@ func (d *Database) UpsertDisbursement( ...@@ -338,6 +340,7 @@ func (d *Database) UpsertDisbursement(
txnHash common.Hash, txnHash common.Hash,
blockNumber uint64, blockNumber uint64,
blockTimestamp time.Time, blockTimestamp time.Time,
success bool,
) error { ) error {
if blockTimestamp.IsZero() { if blockTimestamp.IsZero() {
return ErrZeroTimestamp return ErrZeroTimestamp
...@@ -349,6 +352,7 @@ func (d *Database) UpsertDisbursement( ...@@ -349,6 +352,7 @@ func (d *Database) UpsertDisbursement(
txnHash.String(), txnHash.String(),
blockNumber, blockNumber,
blockTimestamp, blockTimestamp,
success,
) )
if err != nil { if err != nil {
if strings.Contains(err.Error(), "violates foreign key constraint") { if strings.Contains(err.Error(), "violates foreign key constraint") {
...@@ -369,7 +373,7 @@ func (d *Database) UpsertDisbursement( ...@@ -369,7 +373,7 @@ func (d *Database) UpsertDisbursement(
const completedTeleportsQuery = ` const completedTeleportsQuery = `
SELECT SELECT
dep.id, dep.address, dep.amount, dep.id, dep.address, dep.amount, dis.success,
dep.txn_hash, dep.block_number, dep.block_timestamp, dep.txn_hash, dep.block_number, dep.block_timestamp,
dis.txn_hash, dis.block_number, dis.block_timestamp dis.txn_hash, dis.block_number, dis.block_timestamp
FROM deposits AS dep, disbursements AS dis FROM deposits AS dep, disbursements AS dis
...@@ -397,6 +401,7 @@ func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) { ...@@ -397,6 +401,7 @@ func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) {
&teleport.ID, &teleport.ID,
&addressStr, &addressStr,
&amountStr, &amountStr,
&teleport.Success,
&depTxnHashStr, &depTxnHashStr,
&teleport.Deposit.BlockNumber, &teleport.Deposit.BlockNumber,
&teleport.Deposit.BlockTimestamp, &teleport.Deposit.BlockTimestamp,
......
...@@ -235,7 +235,7 @@ func TestConfirmedDeposits(t *testing.T) { ...@@ -235,7 +235,7 @@ func TestConfirmedDeposits(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, []db.Deposit{deposit1, deposit2, deposit3}, deposits) require.Equal(t, []db.Deposit{deposit1, deposit2, deposit3}, deposits)
err = d.UpsertDisbursement(deposit1.ID, common.HexToHash("0xdd01"), 1, testTimestamp) err = d.UpsertDisbursement(deposit1.ID, common.HexToHash("0xdd01"), 1, testTimestamp, true)
require.Nil(t, err) require.Nil(t, err)
deposits, err = d.ConfirmedDeposits(2, 1) deposits, err = d.ConfirmedDeposits(2, 1)
...@@ -259,11 +259,11 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -259,11 +259,11 @@ func TestUpsertDisbursement(t *testing.T) {
disBlockNumber := uint64(2) disBlockNumber := uint64(2)
// Calling UpsertDisbursement with the zero timestamp should fail. // Calling UpsertDisbursement with the zero timestamp should fail.
err := d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, time.Time{}) err := d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, time.Time{}, true)
require.Equal(t, db.ErrZeroTimestamp, err) require.Equal(t, db.ErrZeroTimestamp, err)
// Calling UpsertDisbursement with an unknown id should fail. // Calling UpsertDisbursement with an unknown id should fail.
err = d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, testTimestamp) err = d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, testTimestamp, true)
require.Equal(t, db.ErrUnknownDeposit, err) require.Equal(t, db.ErrUnknownDeposit, err)
// Now, insert a real deposit that we will disburse. // Now, insert a real deposit that we will disburse.
...@@ -280,18 +280,48 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -280,18 +280,48 @@ func TestUpsertDisbursement(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
// Mark the deposit as disbursed with some temporary info. // Mark the deposit as disbursed with some temporary info.
err = d.UpsertDisbursement(1, common.HexToHash("0xee00"), 1, testTimestamp) tempDisTxnHash := common.HexToHash("0xee00")
tempDisBlockNumber := uint64(1)
err = d.UpsertDisbursement(
1, tempDisTxnHash, tempDisBlockNumber, testTimestamp, false,
)
require.Nil(t, err) require.Nil(t, err)
expTeleports := []db.CompletedTeleport{
{
ID: 1,
Address: address,
Amount: amount,
Success: false,
Deposit: db.ConfirmationInfo{
TxnHash: depTxnHash,
BlockNumber: depBlockNumber,
BlockTimestamp: testTimestamp,
},
Disbursement: db.ConfirmationInfo{
TxnHash: tempDisTxnHash,
BlockNumber: tempDisBlockNumber,
BlockTimestamp: testTimestamp,
},
},
}
// Assert that the deposit shows up in the CompletedTeleports method with
// both the L1 and temp L2 confirmation info.
teleports, err := d.CompletedTeleports()
require.Nil(t, err)
require.Equal(t, expTeleports, teleports)
// Overwrite the disbursement info with the final values. // Overwrite the disbursement info with the final values.
err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp) err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp, true)
require.Nil(t, err) require.Nil(t, err)
expTeleports := []db.CompletedTeleport{ expTeleports = []db.CompletedTeleport{
{ {
ID: 1, ID: 1,
Address: address, Address: address,
Amount: amount, Amount: amount,
Success: true,
Deposit: db.ConfirmationInfo{ Deposit: db.ConfirmationInfo{
TxnHash: depTxnHash, TxnHash: depTxnHash,
BlockNumber: depBlockNumber, BlockNumber: depBlockNumber,
...@@ -307,7 +337,7 @@ func TestUpsertDisbursement(t *testing.T) { ...@@ -307,7 +337,7 @@ func TestUpsertDisbursement(t *testing.T) {
// Assert that the deposit now shows up in the CompletedTeleports method // Assert that the deposit now shows up in the CompletedTeleports method
// with both the L1 and L2 confirmation info. // with both the L1 and L2 confirmation info.
teleports, err := d.CompletedTeleports() teleports, err = d.CompletedTeleports()
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, expTeleports, teleports) require.Equal(t, expTeleports, teleports)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment