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 {
ID uint64
Address common.Address
Amount *big.Int
Success bool
Deposit ConfirmationInfo
Disbursement ConfirmationInfo
}
......@@ -65,7 +66,8 @@ CREATE TABLE IF NOT EXISTS disbursements (
id INT8 NOT NULL PRIMARY KEY REFERENCES deposits(id),
txn_hash VARCHAR 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) {
}
const markDisbursedStatement = `
INSERT INTO disbursements (id, txn_hash, block_number, block_timestamp)
VALUES ($1, $2, $3, $4)
INSERT INTO disbursements (id, txn_hash, block_number, block_timestamp, success)
VALUES ($1, $2, $3, $4, $5)
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
......@@ -338,6 +340,7 @@ func (d *Database) UpsertDisbursement(
txnHash common.Hash,
blockNumber uint64,
blockTimestamp time.Time,
success bool,
) error {
if blockTimestamp.IsZero() {
return ErrZeroTimestamp
......@@ -349,6 +352,7 @@ func (d *Database) UpsertDisbursement(
txnHash.String(),
blockNumber,
blockTimestamp,
success,
)
if err != nil {
if strings.Contains(err.Error(), "violates foreign key constraint") {
......@@ -369,7 +373,7 @@ func (d *Database) UpsertDisbursement(
const completedTeleportsQuery = `
SELECT
dep.id, dep.address, dep.amount,
dep.id, dep.address, dep.amount, dis.success,
dep.txn_hash, dep.block_number, dep.block_timestamp,
dis.txn_hash, dis.block_number, dis.block_timestamp
FROM deposits AS dep, disbursements AS dis
......@@ -397,6 +401,7 @@ func (d *Database) CompletedTeleports() ([]CompletedTeleport, error) {
&teleport.ID,
&addressStr,
&amountStr,
&teleport.Success,
&depTxnHashStr,
&teleport.Deposit.BlockNumber,
&teleport.Deposit.BlockTimestamp,
......
......@@ -235,7 +235,7 @@ func TestConfirmedDeposits(t *testing.T) {
require.Nil(t, err)
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)
deposits, err = d.ConfirmedDeposits(2, 1)
......@@ -259,11 +259,11 @@ func TestUpsertDisbursement(t *testing.T) {
disBlockNumber := uint64(2)
// 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)
// 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)
// Now, insert a real deposit that we will disburse.
......@@ -280,18 +280,48 @@ func TestUpsertDisbursement(t *testing.T) {
require.Nil(t, err)
// 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)
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.
err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp)
err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp, true)
require.Nil(t, err)
expTeleports := []db.CompletedTeleport{
expTeleports = []db.CompletedTeleport{
{
ID: 1,
Address: address,
Amount: amount,
Success: true,
Deposit: db.ConfirmationInfo{
TxnHash: depTxnHash,
BlockNumber: depBlockNumber,
......@@ -307,7 +337,7 @@ func TestUpsertDisbursement(t *testing.T) {
// Assert that the deposit now shows up in the CompletedTeleports method
// with both the L1 and L2 confirmation info.
teleports, err := d.CompletedTeleports()
teleports, err = d.CompletedTeleports()
require.Nil(t, err)
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