2022-02-09 15:19:10 +00:00
|
|
|
package pg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-20 17:47:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/test_helpers"
|
2022-02-09 15:19:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/ipld"
|
|
|
|
|
2022-09-20 17:47:34 +00:00
|
|
|
fixt "github.com/cerc-io/ipld-eth-state-snapshot/fixture"
|
|
|
|
snapt "github.com/cerc-io/ipld-eth-state-snapshot/pkg/types"
|
|
|
|
"github.com/cerc-io/ipld-eth-state-snapshot/test"
|
2022-02-09 15:19:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pgConfig = test.DefaultPgConfig
|
|
|
|
nodeInfo = test.DefaultNodeInfo
|
|
|
|
// tables ordered according to fkey depedencies
|
|
|
|
allTables = []*snapt.Table{
|
|
|
|
&snapt.TableIPLDBlock,
|
|
|
|
&snapt.TableNodeInfo,
|
|
|
|
&snapt.TableHeader,
|
|
|
|
&snapt.TableStateNode,
|
|
|
|
&snapt.TableStorageNode,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-06-15 07:21:26 +00:00
|
|
|
func writeData(t *testing.T, db *postgres.DB) *publisher {
|
|
|
|
pub := NewPublisher(db)
|
2022-02-22 09:03:06 +00:00
|
|
|
test.NoError(t, pub.PublishHeader(&fixt.Block1_Header))
|
2022-02-09 15:19:10 +00:00
|
|
|
tx, err := pub.BeginTx()
|
|
|
|
test.NoError(t, err)
|
|
|
|
|
2022-02-22 09:03:06 +00:00
|
|
|
headerID := fixt.Block1_Header.Hash().String()
|
2022-05-13 08:30:40 +00:00
|
|
|
test.NoError(t, pub.PublishStateNode(&fixt.Block1_StateNode0, headerID, fixt.Block1_Header.Number, tx))
|
2022-02-09 15:19:10 +00:00
|
|
|
|
|
|
|
test.NoError(t, tx.Commit())
|
|
|
|
return pub
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: DB user requires role membership "pg_read_server_files"
|
|
|
|
func TestBasic(t *testing.T) {
|
|
|
|
test.NeedsDB(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2022-06-15 07:21:26 +00:00
|
|
|
driver, err := postgres.NewSQLXDriver(ctx, pgConfig, nodeInfo)
|
2022-02-09 15:19:10 +00:00
|
|
|
test.NoError(t, err)
|
2022-06-15 07:21:26 +00:00
|
|
|
db := postgres.NewPostgresDB(driver)
|
2022-02-09 15:19:10 +00:00
|
|
|
|
2022-09-20 17:47:34 +00:00
|
|
|
test_helpers.TearDownDB(t, db)
|
2022-02-09 15:19:10 +00:00
|
|
|
|
2022-06-15 07:21:26 +00:00
|
|
|
_ = writeData(t, db)
|
2022-02-09 15:19:10 +00:00
|
|
|
|
|
|
|
// check header was successfully committed
|
|
|
|
pgQueryHeader := `SELECT cid, block_hash
|
|
|
|
FROM eth.header_cids
|
|
|
|
WHERE block_number = $1`
|
|
|
|
type res struct {
|
|
|
|
CID string
|
|
|
|
BlockHash string
|
|
|
|
}
|
|
|
|
var header res
|
2022-06-15 07:21:26 +00:00
|
|
|
err = db.QueryRow(ctx, pgQueryHeader, fixt.Block1_Header.Number.Uint64()).Scan(
|
2022-02-09 15:19:10 +00:00
|
|
|
&header.CID, &header.BlockHash)
|
|
|
|
test.NoError(t, err)
|
|
|
|
|
2022-02-22 09:03:06 +00:00
|
|
|
headerNode, err := ipld.NewEthHeader(&fixt.Block1_Header)
|
2022-05-13 08:30:40 +00:00
|
|
|
test.NoError(t, err)
|
2022-02-09 15:19:10 +00:00
|
|
|
test.ExpectEqual(t, headerNode.Cid().String(), header.CID)
|
2022-02-22 09:03:06 +00:00
|
|
|
test.ExpectEqual(t, fixt.Block1_Header.Hash().String(), header.BlockHash)
|
2022-02-09 15:19:10 +00:00
|
|
|
}
|