db_test.go 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package db_test

import (
	"database/sql"
	"fmt"
	"math/big"
	"testing"
	"time"

	"github.com/ethereum-optimism/optimism/go/teleportr/db"
	"github.com/ethereum/go-ethereum/common"
	"github.com/google/uuid"
	"github.com/stretchr/testify/require"
)

var (
	testTimestamp = time.Unix(time.Now().Unix(), 0)
)

func newDatabase(t *testing.T) *db.Database {
	dbName := uuid.NewString()
	cfg := db.Config{
		Host:     "0.0.0.0",
		Port:     5432,
		User:     "postgres",
		Password: "password",
		DBName:   dbName,
	}

	conn, err := sql.Open("postgres", cfg.WithoutDB())
	require.Nil(t, err)

	_, err = conn.Exec(fmt.Sprintf("CREATE DATABASE \"%s\";", dbName))
	require.Nil(t, err)

	err = conn.Close()
	require.Nil(t, err)

	db, err := db.Open(cfg)
	require.Nil(t, err)

	err = db.Migrate()
	require.Nil(t, err)

	return db
}

// TestOpenClose asserts that we are able to open and close the database
// connection.
func TestOpenClose(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	err := d.Close()
	require.Nil(t, err)
}

58 59
// TestUpsertEmptyDeposits empty deposits asserts that it is safe to call
// UpsertDeposits with an empty list.
60 61 62 63 64 65
func TestUpsertEmptyDeposits(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

66
	err := d.UpsertDeposits(nil, 0)
67 68
	require.Nil(t, err)

69
	err = d.UpsertDeposits([]db.Deposit{}, 0)
70 71 72 73 74 75 76 77 78 79 80
	require.Nil(t, err)
}

// TestUpsertDepositWithZeroTimestampFails asserts that trying to insert a
// deposit with a zero-timestamp fails.
func TestUpsertDepositWithZeroTimestampFails(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

81
	err := d.UpsertDeposits([]db.Deposit{{}}, 0)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	require.Equal(t, db.ErrZeroTimestamp, err)
}

// TestUpsertDeposits asserts that UpsertDeposits properly overwrites an
// existing entry with the same ID.
func TestUpsertDeposits(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	deposit1 := db.Deposit{
		ID:             1,
		TxnHash:        common.HexToHash("0xff01"),
		BlockNumber:    1,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa01"),
		Amount:         big.NewInt(1),
	}

102
	err := d.UpsertDeposits([]db.Deposit{deposit1}, 0)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	require.Nil(t, err)

	deposits, err := d.ConfirmedDeposits(1, 1)
	require.Nil(t, err)
	require.Equal(t, deposits, []db.Deposit{deposit1})

	deposit2 := db.Deposit{
		ID:             1,
		TxnHash:        common.HexToHash("0xff02"),
		BlockNumber:    2,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa02"),
		Amount:         big.NewInt(2),
	}

118
	err = d.UpsertDeposits([]db.Deposit{deposit2}, 0)
119 120 121 122 123 124 125
	require.Nil(t, err)

	deposits, err = d.ConfirmedDeposits(2, 1)
	require.Nil(t, err)
	require.Equal(t, deposits, []db.Deposit{deposit2})
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
// TestUpsertDepositsRecordsLastProcessedBlock asserts that calling
// UpsertDeposits properly records the last processed block.
func TestUpsertDepositsRecordsLastProcessedBlock(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	uint64Ptr := func(x uint64) *uint64 {
		return &x
	}

	// Should be empty initially.
	lastProcessedBlock, err := d.LastProcessedBlock()
	require.Nil(t, err)
	require.Nil(t, lastProcessedBlock)

	// Insert nil deposits through block 1.
	err = d.UpsertDeposits(nil, 1)
	require.Nil(t, err)

	// Check that LastProcessedBlock returns 1.
	lastProcessedBlock, err = d.LastProcessedBlock()
	require.Nil(t, err)
	require.Equal(t, uint64Ptr(1), lastProcessedBlock)

	// Insert empty deposits through block 2.
	err = d.UpsertDeposits([]db.Deposit{}, 2)
	require.Nil(t, err)

	// Check that LastProcessedBlock returns 2.
	lastProcessedBlock, err = d.LastProcessedBlock()
	require.Nil(t, err)
	require.Equal(t, uint64Ptr(2), lastProcessedBlock)

	// Insert real deposit in block 3 with last processed at 4.
	deposit := db.Deposit{
		ID:             0,
		TxnHash:        common.HexToHash("0xff03"),
		BlockNumber:    3,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa03"),
		Amount:         big.NewInt(3),
	}
	err = d.UpsertDeposits([]db.Deposit{deposit}, 4)
	require.Nil(t, err)

	// Check that LastProcessedBlock returns 2.
	lastProcessedBlock, err = d.LastProcessedBlock()
	require.Nil(t, err)
	require.Equal(t, uint64Ptr(4), lastProcessedBlock)
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
// TestConfirmedDeposits asserts that ConfirmedDeposits properly returns the set
// of deposits that have sufficient confirmation, but do not have a recorded
// disbursement.
func TestConfirmedDeposits(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	deposits, err := d.ConfirmedDeposits(1e9, 1)
	require.Nil(t, err)
	require.Equal(t, int(0), len(deposits))

	deposit1 := db.Deposit{
		ID:             1,
		TxnHash:        common.HexToHash("0xff01"),
		BlockNumber:    1,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa01"),
		Amount:         big.NewInt(1),
	}
	deposit2 := db.Deposit{
		ID:             2,
		TxnHash:        common.HexToHash("0xff21"),
		BlockNumber:    2,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa21"),
		Amount:         big.NewInt(2),
	}
	deposit3 := db.Deposit{
		ID:             3,
		TxnHash:        common.HexToHash("0xff22"),
		BlockNumber:    2,
		BlockTimestamp: testTimestamp,
		Address:        common.HexToAddress("0xaa22"),
		Amount:         big.NewInt(2),
	}

	err = d.UpsertDeposits([]db.Deposit{
		deposit1, deposit2, deposit3,
219
	}, 0)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	require.Nil(t, err)

	// First deposit only has 1 conf, should not be found using 2 confs at block
	// 1.
	deposits, err = d.ConfirmedDeposits(1, 2)
	require.Nil(t, err)
	require.Equal(t, int(0), len(deposits))

	// First deposit should be returned when querying for 1 conf at block 1.
	deposits, err = d.ConfirmedDeposits(1, 1)
	require.Nil(t, err)
	require.Equal(t, []db.Deposit{deposit1}, deposits)

	// All deposits should be returned when querying for 1 conf at block 2.
	deposits, err = d.ConfirmedDeposits(2, 1)
	require.Nil(t, err)
	require.Equal(t, []db.Deposit{deposit1, deposit2, deposit3}, deposits)

238
	err = d.UpsertDisbursement(deposit1.ID, common.HexToHash("0xdd01"), 1, testTimestamp, true)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	require.Nil(t, err)

	deposits, err = d.ConfirmedDeposits(2, 1)
	require.Nil(t, err)
	require.Equal(t, []db.Deposit{deposit2, deposit3}, deposits)
}

// TestUpsertDisbursement asserts that UpsertDisbursement properly inserts new
// disbursements or overwrites existing ones.
func TestUpsertDisbursement(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	address := common.HexToAddress("0xaa01")
	amount := big.NewInt(1)
	depTxnHash := common.HexToHash("0xdd01")
257
	depBlockNumber := uint64(1)
258
	disTxnHash := common.HexToHash("0xee02")
259
	disBlockNumber := uint64(2)
260 261

	// Calling UpsertDisbursement with the zero timestamp should fail.
262
	err := d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, time.Time{}, true)
263 264 265
	require.Equal(t, db.ErrZeroTimestamp, err)

	// Calling UpsertDisbursement with an unknown id should fail.
266
	err = d.UpsertDisbursement(0, common.HexToHash("0xdd00"), 0, testTimestamp, true)
267 268 269 270 271 272 273 274 275 276 277 278
	require.Equal(t, db.ErrUnknownDeposit, err)

	// Now, insert a real deposit that we will disburse.
	err = d.UpsertDeposits([]db.Deposit{
		{
			ID:             1,
			TxnHash:        depTxnHash,
			BlockNumber:    depBlockNumber,
			BlockTimestamp: testTimestamp,
			Address:        address,
			Amount:         amount,
		},
279
	}, 0)
280 281 282
	require.Nil(t, err)

	// Mark the deposit as disbursed with some temporary info.
283 284 285 286 287
	tempDisTxnHash := common.HexToHash("0xee00")
	tempDisBlockNumber := uint64(1)
	err = d.UpsertDisbursement(
		1, tempDisTxnHash, tempDisBlockNumber, testTimestamp, false,
	)
288 289
	require.Nil(t, err)

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	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)

315
	// Overwrite the disbursement info with the final values.
316
	err = d.UpsertDisbursement(1, disTxnHash, disBlockNumber, testTimestamp, true)
317 318
	require.Nil(t, err)

319
	expTeleports = []db.CompletedTeleport{
320 321 322 323
		{
			ID:      1,
			Address: address,
			Amount:  amount,
324
			Success: true,
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			Deposit: db.ConfirmationInfo{
				TxnHash:        depTxnHash,
				BlockNumber:    depBlockNumber,
				BlockTimestamp: testTimestamp,
			},
			Disbursement: db.ConfirmationInfo{
				TxnHash:        disTxnHash,
				BlockNumber:    disBlockNumber,
				BlockTimestamp: testTimestamp,
			},
		},
	}

	// Assert that the deposit now shows up in the CompletedTeleports method
	// with both the L1 and L2 confirmation info.
340
	teleports, err = d.CompletedTeleports()
341 342 343
	require.Nil(t, err)
	require.Equal(t, expTeleports, teleports)
}
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

// TestUpsertPendingTxs asserts that UpsertPendingTx properly records a pending
// tx, and that it appears in ListPendingTxs on subsequent calls.
func TestUpsertPendingTxs(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	// Should be empty at first.
	pendingTxs, err := d.ListPendingTxs()
	require.Nil(t, err)
	require.Nil(t, pendingTxs)

	// Add first pending tx.
	pendingTx1 := db.PendingTx{
		TxHash:  common.HexToHash("0x11"),
		StartID: 0,
		EndID:   1,
	}
	err = d.UpsertPendingTx(pendingTx1)
	require.Nil(t, err)

	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx1}, pendingTxs)

	// Add second pending tx.
	pendingTx2 := db.PendingTx{
		TxHash:  common.HexToHash("0x22"),
		StartID: 0,
		EndID:   1,
	}
	err = d.UpsertPendingTx(pendingTx2)
	require.Nil(t, err)

	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx1, pendingTx2}, pendingTxs)

	// Readd duplciate pending tx.
	err = d.UpsertPendingTx(pendingTx2)
	require.Nil(t, err)

	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx1, pendingTx2}, pendingTxs)

	// Add third pending tx.
	pendingTx3 := db.PendingTx{
		TxHash:  common.HexToHash("0x33"),
		StartID: 1,
		EndID:   2,
	}
	err = d.UpsertPendingTx(pendingTx3)
	require.Nil(t, err)

	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3, pendingTx1, pendingTx2}, pendingTxs)
}

// TestDeletePendingTx asserts that DeletePendingTx properly cleans up the
// pending_txs table when provided with various start/end ids.
func TestDeletePendingTx(t *testing.T) {
	t.Parallel()

	d := newDatabase(t)
	defer d.Close()

	pendingTx1 := db.PendingTx{
		TxHash:  common.HexToHash("0x11"),
		StartID: 0,
		EndID:   1,
	}
	pendingTx2 := db.PendingTx{
		TxHash:  common.HexToHash("0x22"),
		StartID: 0,
		EndID:   1,
	}
	pendingTx3 := db.PendingTx{
		TxHash:  common.HexToHash("0x33"),
		StartID: 1,
		EndID:   2,
	}

	err := d.UpsertPendingTx(pendingTx1)
	require.Nil(t, err)
	err = d.UpsertPendingTx(pendingTx2)
	require.Nil(t, err)
	err = d.UpsertPendingTx(pendingTx3)
	require.Nil(t, err)

	pendingTxs, err := d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3, pendingTx1, pendingTx2}, pendingTxs)

	// Delete with indexes that do not match any start/end, no effect.
	err = d.DeletePendingTx(3, 4)
	require.Nil(t, err)
	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3, pendingTx1, pendingTx2}, pendingTxs)

	// Delete with indexes that matches start but no end, no effect.
	err = d.DeletePendingTx(1, 3)
	require.Nil(t, err)
	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3, pendingTx1, pendingTx2}, pendingTxs)

	// Delete with indexes that matches end but no start, no effect.
	err = d.DeletePendingTx(0, 2)
	require.Nil(t, err)
	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3, pendingTx1, pendingTx2}, pendingTxs)

	// Delete with indexes that matches start and end, should remove both.
	err = d.DeletePendingTx(0, 1)
	require.Nil(t, err)
	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Equal(t, []db.PendingTx{pendingTx3}, pendingTxs)

	// Delete with indexes that matches start and end, no empty.
	err = d.DeletePendingTx(1, 2)
	require.Nil(t, err)
	pendingTxs, err = d.ListPendingTxs()
	require.Nil(t, err)
	require.Nil(t, pendingTxs)
}