Skip to content

Commit 6ae7719

Browse files
committed
Use COALESCE to avoid possible NULL values from database
1 parent ecba34c commit 6ae7719

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

statediff/indexer/database/file/indexer_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ var (
5151
sqlxdb *sqlx.DB
5252
err error
5353
ind interfaces.StateDiffIndexer
54-
ipfsPgGet = `SELECT data FROM public.blocks
55-
WHERE key = $1`
54+
ipfsPgGet = `SELECT COALESCE(data, '') as data FROM public.blocks
55+
WHERE key = $1 AND block_number = $2`
5656
tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte
5757
mockBlock *types.Block
5858
headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid
@@ -262,7 +262,7 @@ func TestFileIndexer(t *testing.T) {
262262
mhKey := dshelp.MultihashToDsKey(dc.Hash())
263263
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
264264
var data []byte
265-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
265+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
266266
if err != nil {
267267
t.Fatal(err)
268268
}
@@ -301,7 +301,7 @@ func TestFileIndexer(t *testing.T) {
301301
mhKey := dshelp.MultihashToDsKey(dc.Hash())
302302
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
303303
var data []byte
304-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
304+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
305305
if err != nil {
306306
t.Fatal(err)
307307
}
@@ -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, log_cids.topic0, log_cids.topic1, data FROM eth.log_cids
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
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 data
483+
pgStr = `SELECT COALESCE(data, '') as 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`
@@ -506,7 +506,7 @@ func TestFileIndexer(t *testing.T) {
506506
mhKey := dshelp.MultihashToDsKey(dc.Hash())
507507
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
508508
var data []byte
509-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
509+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
510510
if err != nil {
511511
t.Fatal(err)
512512
}
@@ -580,7 +580,7 @@ func TestFileIndexer(t *testing.T) {
580580
}
581581
mhKey := dshelp.MultihashToDsKey(dc.Hash())
582582
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
583-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
583+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
584584
if err != nil {
585585
t.Fatal(err)
586586
}
@@ -641,7 +641,7 @@ func TestFileIndexer(t *testing.T) {
641641
mhKey := dshelp.MultihashToDsKey(dc.Hash())
642642
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
643643
require.Equal(t, shared.RemovedNodeMhKey, prefixedKey)
644-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
644+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
645645
if err != nil {
646646
t.Fatal(err)
647647
}
@@ -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, storage_cids.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, COALESCE(storage_cids.storage_path, '') as 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
@@ -694,15 +694,15 @@ func TestFileIndexer(t *testing.T) {
694694
}
695695
mhKey := dshelp.MultihashToDsKey(dc.Hash())
696696
prefixedKey := blockstore.BlockPrefix.String() + mhKey.String()
697-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
697+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
698698
if err != nil {
699699
t.Fatal(err)
700700
}
701701
require.Equal(t, mocks.StorageLeafNode, data)
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, storage_cids.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, COALESCE(storage_cids.storage_path, '') as 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
@@ -747,8 +747,8 @@ func TestFileIndexer(t *testing.T) {
747747
}
748748
mhKey = dshelp.MultihashToDsKey(dc.Hash())
749749
prefixedKey = blockstore.BlockPrefix.String() + mhKey.String()
750-
require.Equal(t, shared.RemovedNodeMhKey, prefixedKey)
751-
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey)
750+
require.Equal(t, shared.RemovedNodeMhKey, prefixedKey, mocks.BlockNumber.Uint64())
751+
err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64())
752752
if err != nil {
753753
t.Fatal(err)
754754
}

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 data FROM public.blocks
25+
ipfsPgGet = `SELECT COALESCE(data, '') as 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: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"math/big"
2222
"testing"
2323

24-
"github.com/lib/pq"
25-
2624
"github.com/ipfs/go-cid"
2725
blockstore "github.com/ipfs/go-ipfs-blockstore"
2826
dshelp "github.com/ipfs/go-ipfs-ds-help"
@@ -207,14 +205,6 @@ func TestPGXIndexer(t *testing.T) {
207205
if txRes.Value != transactions[3].Value().String() {
208206
t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value)
209207
}
210-
// AccessListElementModel is the db model for eth.access_list_entry
211-
type AccessListElementModel struct {
212-
BlockNumber string `db:"block_number"`
213-
Index int64 `db:"index"`
214-
TxID string `db:"tx_id"`
215-
Address string `db:"address"`
216-
StorageKeys pq.StringArray `db:"storage_keys"`
217-
}
218208
accessListElementModels := make([]models.AccessListElementModel, 0)
219209
pgStr = "SELECT cast(access_list_elements.block_number AS TEXT), access_list_elements.index, access_list_elements.tx_id, " +
220210
"access_list_elements.address, access_list_elements.storage_keys FROM eth.access_list_elements " +
@@ -267,7 +257,7 @@ func TestPGXIndexer(t *testing.T) {
267257
AND transaction_cids.header_id = header_cids.block_hash
268258
AND header_cids.block_number = $1
269259
ORDER BY transaction_cids.index`
270-
logsPgStr := `SELECT log_cids.index, log_cids.address, log_cids.topic0, log_cids.topic1, data FROM eth.log_cids
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
271261
INNER JOIN eth.receipt_cids ON (log_cids.rct_id = receipt_cids.tx_id)
272262
INNER JOIN public.blocks ON (log_cids.leaf_mh_key = blocks.key)
273263
WHERE receipt_cids.leaf_cid = $1 ORDER BY eth.log_cids.index ASC`
@@ -338,7 +328,7 @@ func TestPGXIndexer(t *testing.T) {
338328

339329
for idx, c := range rcts {
340330
result := make([]models.IPLDModel, 0)
341-
pgStr = `SELECT data
331+
pgStr = `SELECT COALESCE(data, '') as data
342332
FROM eth.receipt_cids
343333
INNER JOIN public.blocks ON (receipt_cids.leaf_mh_key = public.blocks.key)
344334
WHERE receipt_cids.leaf_cid = $1`
@@ -525,7 +515,7 @@ func TestPGXIndexer(t *testing.T) {
525515
defer checkTxClosure(t, 1, 0, 1)
526516
// check that storage nodes were properly indexed
527517
storageNodes := make([]models.StorageNodeWithStateKeyModel, 0)
528-
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
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
529519
FROM eth.storage_cids, eth.state_cids, eth.header_cids
530520
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
531521
AND state_cids.header_id = header_cids.block_hash
@@ -559,7 +549,7 @@ func TestPGXIndexer(t *testing.T) {
559549

560550
// check that Removed storage nodes were properly indexed
561551
storageNodes = make([]models.StorageNodeWithStateKeyModel, 0)
562-
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
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
563553
FROM eth.storage_cids, eth.state_cids, eth.header_cids
564554
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
565555
AND state_cids.header_id = header_cids.block_hash

statediff/indexer/database/sql/sqlx_indexer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func TestSQLXIndexer(t *testing.T) {
253253
AND transaction_cids.header_id = header_cids.block_hash
254254
AND header_cids.block_number = $1
255255
ORDER BY transaction_cids.index`
256-
logsPgStr := `SELECT log_cids.index, log_cids.address, log_cids.topic0, log_cids.topic1, data FROM eth.log_cids
256+
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
257257
INNER JOIN eth.receipt_cids ON (log_cids.rct_id = receipt_cids.tx_id)
258258
INNER JOIN public.blocks ON (log_cids.leaf_mh_key = blocks.key)
259259
WHERE receipt_cids.leaf_cid = $1 ORDER BY eth.log_cids.index ASC`
@@ -322,7 +322,7 @@ func TestSQLXIndexer(t *testing.T) {
322322

323323
for idx, c := range rcts {
324324
result := make([]models.IPLDModel, 0)
325-
pgStr = `SELECT data
325+
pgStr = `SELECT COALESCE(data, '') as data
326326
FROM eth.receipt_cids
327327
INNER JOIN public.blocks ON (receipt_cids.leaf_mh_key = public.blocks.key)
328328
WHERE receipt_cids.leaf_cid = $1`
@@ -508,7 +508,7 @@ func TestSQLXIndexer(t *testing.T) {
508508
defer checkTxClosure(t, 0, 0, 0)
509509
// check that storage nodes were properly indexed
510510
storageNodes := make([]models.StorageNodeWithStateKeyModel, 0)
511-
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
511+
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
512512
FROM eth.storage_cids, eth.state_cids, eth.header_cids
513513
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
514514
AND state_cids.header_id = header_cids.block_hash
@@ -542,7 +542,7 @@ func TestSQLXIndexer(t *testing.T) {
542542

543543
// check that Removed storage nodes were properly indexed
544544
storageNodes = make([]models.StorageNodeWithStateKeyModel, 0)
545-
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
545+
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
546546
FROM eth.storage_cids, eth.state_cids, eth.header_cids
547547
WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id)
548548
AND state_cids.header_id = header_cids.block_hash

0 commit comments

Comments
 (0)