Skip to content

Commit 2caedb0

Browse files
committed
Changes for using single-node v4 db schema
1 parent 328ab95 commit 2caedb0

File tree

8 files changed

+46
-49
lines changed

8 files changed

+46
-49
lines changed

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ services:
55
restart: on-failure
66
depends_on:
77
- access-node
8-
image: migrations-test
8+
image: vulcanize/ipld-eth-db:v4.1.1-alpha
99
environment:
10-
DATABASE_USER: "postgres"
10+
DATABASE_USER: "vdbm"
1111
DATABASE_NAME: "vulcanize_testing_v4"
1212
DATABASE_PASSWORD: "password"
1313
DATABASE_HOSTNAME: "access-node"

scripts/run_unit_test.sh

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22

33
set -e
44

5-
# Build database image
6-
docker build -t migrations-test https://github.com/vulcanize/ipld-eth-db.git#sharding
7-
85
mkdir -p out
96

10-
# Remove existing docker-multi-node directory
11-
rm -rf out/docker-multi-node/
7+
# Remove existing docker-tsdb directory
8+
rm -rf out/docker-tsdb/
129

13-
# Copy over files to setup multi-node database
14-
ID=$(docker create migrations-test)
15-
docker cp $ID:/app/docker-multi-node out/docker-multi-node/
10+
# Copy over files to setup TimescaleDB
11+
ID=$(docker create vulcanize/ipld-eth-db:v4.1.1-alpha)
12+
docker cp $ID:/app/docker-tsdb out/docker-tsdb/
1613
docker rm -v $ID
1714

18-
# Spin up multi-node database
19-
docker-compose -f out/docker-multi-node/docker-compose.test.yml -f docker-compose.yml up ipld-eth-db
20-
sleep 20
15+
# Spin up TimescaleDB
16+
docker-compose -f out/docker-tsdb/docker-compose.test.yml -f docker-compose.yml up ipld-eth-db
17+
sleep 45
2118

2219
# Run unit tests
2320
go clean -testcache
2421
make statedifftest
2522

2623
# Clean up
27-
docker-compose -f out/docker-multi-node/docker-compose.test.yml -f docker-compose.yml down --remove-orphans --volumes
28-
rm -rf out/docker-multi-node/
24+
docker-compose -f out/docker-tsdb/docker-compose.test.yml -f docker-compose.yml down --remove-orphans --volumes
25+
rm -rf out/docker-tsdb/

statediff/indexer/database/file/indexer_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var (
5151
sqlxdb *sqlx.DB
5252
err error
5353
ind interfaces.StateDiffIndexer
54-
ipfsPgGet = `SELECT COALESCE(data, '') as data FROM public.blocks
54+
ipfsPgGet = `SELECT data FROM public.blocks
5555
WHERE key = $1 AND block_number = $2`
5656
tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte
5757
mockBlock *types.Block
@@ -425,7 +425,7 @@ func TestFileIndexer(t *testing.T) {
425425
}
426426
for i := range rcts {
427427
results := make([]logIPLD, 0)
428-
pgStr = `SELECT log_cids.index, log_cids.address, COALESCE(log_cids.topic0, '') as topic0, COALESCE(log_cids.topic1, '') as topic1, data FROM eth.log_cids
428+
pgStr = `SELECT log_cids.index, log_cids.address, log_cids.topic0, log_cids.topic1, data FROM eth.log_cids
429429
INNER JOIN eth.receipt_cids ON (log_cids.rct_id = receipt_cids.tx_id)
430430
INNER JOIN public.blocks ON (log_cids.leaf_mh_key = blocks.key)
431431
WHERE receipt_cids.leaf_cid = $1 ORDER BY eth.log_cids.index ASC`
@@ -480,7 +480,7 @@ func TestFileIndexer(t *testing.T) {
480480

481481
for idx, c := range rcts {
482482
result := make([]models.IPLDModel, 0)
483-
pgStr = `SELECT COALESCE(data, '') as data
483+
pgStr = `SELECT data
484484
FROM eth.receipt_cids
485485
INNER JOIN public.blocks ON (receipt_cids.leaf_mh_key = public.blocks.key)
486486
WHERE receipt_cids.leaf_cid = $1`
@@ -510,7 +510,7 @@ func TestFileIndexer(t *testing.T) {
510510
if err != nil {
511511
t.Fatal(err)
512512
}
513-
postStatePgStr := `SELECT COALESCE(post_state, '') as post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
513+
postStatePgStr := `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
514514
switch c {
515515
case rct1CID.String():
516516
require.Equal(t, rctLeaf1, data)
@@ -564,7 +564,7 @@ func TestFileIndexer(t *testing.T) {
564564

565565
// check that state nodes were properly indexed and published
566566
stateNodes := make([]models.StateNodeModel, 0)
567-
pgStr := `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, COALESCE(state_cids.state_path, '') as state_path, state_cids.header_id
567+
pgStr := `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, state_cids.state_path, state_cids.header_id
568568
FROM eth.state_cids INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.block_hash)
569569
WHERE header_cids.block_number = $1 AND node_type != 3`
570570
err = sqlxdb.Select(&stateNodes, pgStr, mocks.BlockNumber.Uint64())
@@ -584,7 +584,7 @@ func TestFileIndexer(t *testing.T) {
584584
if err != nil {
585585
t.Fatal(err)
586586
}
587-
pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, COALESCE(code_hash, '') as code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2`
587+
pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2`
588588
var account models.StateAccountModel
589589
err = sqlxdb.Get(&account, pgStr, stateNode.HeaderID, stateNode.Path)
590590
if err != nil {
@@ -668,7 +668,7 @@ func TestFileIndexer(t *testing.T) {
668668

669669
// check that storage nodes were properly indexed
670670
storageNodes := make([]models.StorageNodeWithStateKeyModel, 0)
671-
pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, COALESCE(storage_cids.storage_path, '') as storage_path
671+
pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path
672672
FROM eth.storage_cids, eth.state_cids, eth.header_cids
673673
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
674674
AND state_cids.header_id = header_cids.block_hash
@@ -702,7 +702,7 @@ func TestFileIndexer(t *testing.T) {
702702

703703
// check that Removed storage nodes were properly indexed
704704
storageNodes = make([]models.StorageNodeWithStateKeyModel, 0)
705-
pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, COALESCE(storage_cids.storage_path, '') as storage_path
705+
pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path
706706
FROM eth.storage_cids, eth.state_cids, eth.header_cids
707707
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
708708
AND state_cids.header_id = header_cids.block_hash

statediff/indexer/database/sql/indexer_shared_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
db sql.Database
2323
err error
2424
ind interfaces.StateDiffIndexer
25-
ipfsPgGet = `SELECT COALESCE(data, '') as data FROM public.blocks
25+
ipfsPgGet = `SELECT data FROM public.blocks
2626
WHERE key = $1 AND block_number = $2`
2727
tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte
2828
mockBlock *types.Block

statediff/indexer/database/sql/pgx_indexer_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestPGXIndexer(t *testing.T) {
257257
AND transaction_cids.header_id = header_cids.block_hash
258258
AND header_cids.block_number = $1
259259
ORDER BY transaction_cids.index`
260-
logsPgStr := `SELECT log_cids.index, log_cids.address, COALESCE(log_cids.topic0, '') as topic0, COALESCE(log_cids.topic1, '') as topic1, data FROM eth.log_cids
260+
logsPgStr := `SELECT log_cids.index, log_cids.address, log_cids.topic0, log_cids.topic1, data FROM eth.log_cids
261261
INNER JOIN eth.receipt_cids ON (log_cids.rct_id = receipt_cids.tx_id)
262262
INNER JOIN public.blocks ON (log_cids.leaf_mh_key = blocks.key)
263263
WHERE receipt_cids.leaf_cid = $1 ORDER BY eth.log_cids.index ASC`
@@ -328,7 +328,7 @@ func TestPGXIndexer(t *testing.T) {
328328

329329
for idx, c := range rcts {
330330
result := make([]models.IPLDModel, 0)
331-
pgStr = `SELECT COALESCE(data, '') as data
331+
pgStr = `SELECT data
332332
FROM eth.receipt_cids
333333
INNER JOIN public.blocks ON (receipt_cids.leaf_mh_key = public.blocks.key)
334334
WHERE receipt_cids.leaf_cid = $1`
@@ -359,7 +359,7 @@ func TestPGXIndexer(t *testing.T) {
359359
t.Fatal(err)
360360
}
361361

362-
postStatePgStr := `SELECT COALESCE(post_state, '') as post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
362+
postStatePgStr := `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
363363
switch c {
364364
case rct1CID.String():
365365
require.Equal(t, rctLeaf1, data)
@@ -412,7 +412,7 @@ func TestPGXIndexer(t *testing.T) {
412412
defer checkTxClosure(t, 1, 0, 1)
413413
// check that state nodes were properly indexed and published
414414
stateNodes := make([]models.StateNodeModel, 0)
415-
pgStr := `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, COALESCE(state_cids.state_path, '') as state_path, state_cids.header_id
415+
pgStr := `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, state_cids.state_path, state_cids.header_id
416416
FROM eth.state_cids INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.block_hash)
417417
WHERE header_cids.block_number = $1 AND node_type != 3`
418418
err = db.Select(context.Background(), &stateNodes, pgStr, mocks.BlockNumber.Uint64())
@@ -432,7 +432,7 @@ func TestPGXIndexer(t *testing.T) {
432432
if err != nil {
433433
t.Fatal(err)
434434
}
435-
pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, COALESCE(code_hash, '') as code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2`
435+
pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2`
436436
var account models.StateAccountModel
437437
err = db.Get(context.Background(), &account, pgStr, stateNode.HeaderID, stateNode.Path)
438438
if err != nil {
@@ -472,7 +472,7 @@ func TestPGXIndexer(t *testing.T) {
472472

473473
// check that Removed state nodes were properly indexed and published
474474
stateNodes = make([]models.StateNodeModel, 0)
475-
pgStr = `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, COALESCE(state_cids.state_path, '') as state_path, state_cids.header_id
475+
pgStr = `SELECT state_cids.cid, state_cids.state_leaf_key, state_cids.node_type, state_cids.state_path, state_cids.header_id
476476
FROM eth.state_cids INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.block_hash)
477477
WHERE header_cids.block_number = $1 AND node_type = 3`
478478
err = db.Select(context.Background(), &stateNodes, pgStr, mocks.BlockNumber.Uint64())
@@ -515,7 +515,7 @@ func TestPGXIndexer(t *testing.T) {
515515
defer checkTxClosure(t, 1, 0, 1)
516516
// check that storage nodes were properly indexed
517517
storageNodes := make([]models.StorageNodeWithStateKeyModel, 0)
518-
pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, COALESCE(storage_cids.storage_path, '') as storage_path
518+
pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path
519519
FROM eth.storage_cids, eth.state_cids, eth.header_cids
520520
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
521521
AND state_cids.header_id = header_cids.block_hash
@@ -549,7 +549,7 @@ func TestPGXIndexer(t *testing.T) {
549549

550550
// check that Removed storage nodes were properly indexed
551551
storageNodes = make([]models.StorageNodeWithStateKeyModel, 0)
552-
pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, COALESCE(storage_cids.storage_path, '') as storage_path
552+
pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path
553553
FROM eth.storage_cids, eth.state_cids, eth.header_cids
554554
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
555555
AND state_cids.header_id = header_cids.block_hash

statediff/indexer/database/sql/postgres/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var DefaultConfig = Config{
5050
Hostname: "localhost",
5151
Port: 8066,
5252
DatabaseName: "vulcanize_testing_v4",
53-
Username: "postgres",
53+
Username: "vdbm",
5454
Password: "password",
5555
}
5656

statediff/indexer/database/sql/postgres/database.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,55 +40,55 @@ type DB struct {
4040
func (db *DB) InsertHeaderStm() string {
4141
return `INSERT INTO eth.header_cids (block_number, block_hash, parent_hash, cid, td, node_id, reward, state_root, tx_root, receipt_root, uncle_root, bloom, timestamp, mh_key, times_validated, coinbase)
4242
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
43-
ON CONFLICT DO NOTHING`
43+
ON CONFLICT (block_hash, block_number) DO UPDATE SET (parent_hash, cid, td, node_id, reward, state_root, tx_root, receipt_root, uncle_root, bloom, timestamp, mh_key, times_validated, coinbase) = ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, eth.header_cids.times_validated + 1, $16)`
4444
}
4545

4646
// InsertUncleStm satisfies the sql.Statements interface
4747
func (db *DB) InsertUncleStm() string {
4848
return `INSERT INTO eth.uncle_cids (block_number, block_hash, header_id, parent_hash, cid, reward, mh_key) VALUES ($1, $2, $3, $4, $5, $6, $7)
49-
ON CONFLICT DO NOTHING`
49+
ON CONFLICT (block_hash, block_number) DO NOTHING`
5050
}
5151

5252
// InsertTxStm satisfies the sql.Statements interface
5353
func (db *DB) InsertTxStm() string {
5454
return `INSERT INTO eth.transaction_cids (block_number, header_id, tx_hash, cid, dst, src, index, mh_key, tx_data, tx_type, value) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
55-
ON CONFLICT DO NOTHING`
55+
ON CONFLICT (tx_hash, block_number) DO NOTHING`
5656
}
5757

5858
// InsertAccessListElementStm satisfies the sql.Statements interface
5959
func (db *DB) InsertAccessListElementStm() string {
6060
return `INSERT INTO eth.access_list_elements (block_number, tx_id, index, address, storage_keys) VALUES ($1, $2, $3, $4, $5)
61-
ON CONFLICT DO NOTHING`
61+
ON CONFLICT (tx_id, index, block_number) DO NOTHING`
6262
}
6363

6464
// InsertRctStm satisfies the sql.Statements interface
6565
func (db *DB) InsertRctStm() string {
6666
return `INSERT INTO eth.receipt_cids (block_number, tx_id, leaf_cid, contract, contract_hash, leaf_mh_key, post_state, post_status, log_root) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
67-
ON CONFLICT DO NOTHING`
67+
ON CONFLICT (tx_id, block_number) DO NOTHING`
6868
}
6969

7070
// InsertLogStm satisfies the sql.Statements interface
7171
func (db *DB) InsertLogStm() string {
7272
return `INSERT INTO eth.log_cids (block_number, leaf_cid, leaf_mh_key, rct_id, address, index, topic0, topic1, topic2, topic3, log_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
73-
ON CONFLICT DO NOTHING`
73+
ON CONFLICT (rct_id, index, block_number) DO NOTHING`
7474
}
7575

7676
// InsertStateStm satisfies the sql.Statements interface
7777
func (db *DB) InsertStateStm() string {
7878
return `INSERT INTO eth.state_cids (block_number, header_id, state_leaf_key, cid, state_path, node_type, diff, mh_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
79-
ON CONFLICT DO NOTHING`
79+
ON CONFLICT (header_id, state_path, block_number) DO UPDATE SET (block_number, state_leaf_key, cid, node_type, diff, mh_key) = ($1, $3, $4, $6, $7, $8)`
8080
}
8181

8282
// InsertAccountStm satisfies the sql.Statements interface
8383
func (db *DB) InsertAccountStm() string {
8484
return `INSERT INTO eth.state_accounts (block_number, header_id, state_path, balance, nonce, code_hash, storage_root) VALUES ($1, $2, $3, $4, $5, $6, $7)
85-
ON CONFLICT DO NOTHING`
85+
ON CONFLICT (header_id, state_path, block_number) DO NOTHING`
8686
}
8787

8888
// InsertStorageStm satisfies the sql.Statements interface
8989
func (db *DB) InsertStorageStm() string {
9090
return `INSERT INTO eth.storage_cids (block_number, header_id, state_path, storage_leaf_key, cid, storage_path, node_type, diff, mh_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
91-
ON CONFLICT DO NOTHING`
91+
ON CONFLICT (header_id, state_path, storage_path, block_number) DO UPDATE SET (block_number, storage_leaf_key, cid, node_type, diff, mh_key) = ($1, $4, $5, $7, $8, $9)`
9292
}
9393

9494
// InsertIPLDStm satisfies the sql.Statements interface

0 commit comments

Comments
 (0)