near / near-indexer-for-explorer

Watch NEAR network and store all the data from NEAR blockchain to PostgreSQL database
https://near-indexers.io/docs/projects/near-indexer-for-explorer
GNU General Public License v3.0
124 stars 56 forks source link

Running the explorer with `localnet` `init`'d config throws a foreign key constraint violation #167

Closed mieubrisse closed 2 years ago

mieubrisse commented 2 years ago

This seems to happen because: 1) The accounts table is empty by default 2) The localnet init'd config produces the following validator_key.json:

{
  "account_id": "test.near",
  "public_key": "ed25519:DzqwUpSJZvxtPf3yXDvhpqr7ytKLY2vX1EVxAV1uVudq",
  "secret_key": "ed25519:gWKyyAzFWkG17dXAtGjM8NXr4zQ97x7V6UDeQ7GKqi7jCyq11w6o9sRg9vXL4bp7R54v4G8Sh48BnsiFyCvAzrK"
}

3) There's a foreign key constraint on the accounts table that isn't getting fulfilled (from the Postgres logs):

2021-09-22 02:29:14.125 UTC [142] ERROR:  insert or update on table "account_changes" violates foreign key constraint "account_id_fk"
2021-09-22 02:29:14.125 UTC [142] DETAIL:  Key (affected_account_id)=(test.near) is not present in table "accounts".
2021-09-22 02:29:14.125 UTC [142] STATEMENT:  INSERT INTO "account_changes" ("affected_account_id", "changed_in_block_timestamp", "changed_in_block_hash", "caused_by_transaction_hash", "caused_by_receipt_id", "update_reason", "affected_account_nonstaked_balance", "affected_account_staked_balance", "affected_account_storage_usage", "index_in_block") VALUES ($1, $2, $3, DEFAULT, DEFAULT, $4, $5, $6, $7, $8) ON CONFLICT DO NOTHING
mieubrisse commented 2 years ago

Adding the --store-genesis flag fixes this; seems to be related to #146

frol commented 2 years ago

Well, --store-genesis is indeed crucial, though we need a special mode for localnet since neither sync-from-interruption / sync-from-latest / sync-from-block work great there (you want to start from genesis and continue from interruption afterwards), so we might consider splitting sync-from-interruption into sync-from-interruption-or-genesis and sync-from-interruption-or-latest (the current behavior)

mieubrisse commented 2 years ago

Yep that makes sense - seems like "sync-from-gensis-and-everything-after" would be the ideal (though sync-from-latest works when the Indexer is the only one running the network, which is the sneaky thing I did in my case 😄 )