Skip to content

Commit

Permalink
Add automatic data-migration support for Docker deployment. (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
spilin authored Dec 27, 2021
1 parent 751e58a commit 33a7702
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions .docker/.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20211221121213 node lib/data_migrations/2021-12-14-empty-blocks.js
8 changes: 4 additions & 4 deletions .docker/docker-entrypoint-initdb.d/init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ CREATE TYPE block_result AS (
"size" int4,
"gasLimit" int8,
"gasUsed" int8,
"mixHash" hash,
"timestamp" int4
"timestamp" int4,
"mixHash" hash
);
DROP TYPE IF EXISTS filter_result CASCADE;

Expand Down Expand Up @@ -233,8 +233,8 @@ BEGIN
size, -- size
gas_limit, -- gasLimit
gas_used, -- gasUsed
repeat('\000', 32)::hash, -- mixHash
EXTRACT(EPOCH FROM timestamp) -- timestamp
COALESCE(EXTRACT(EPOCH FROM timestamp), 0)::int4, -- timestamp
repeat('\000', 32)::hash -- mixHash
FROM block
WHERE id = block_id
LIMIT 1
Expand Down
5 changes: 0 additions & 5 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 2021-12-14

- Run `node lib/data_migrations/2021-12-14-empty-blocks.js` to reindex all historical empty blocks.
In addition, reload the following stored procedures:

- `etc/schema/functions/eth_getBlockByNumber.sql`

or

```bash
$ cd migrations
Expand Down
4 changes: 2 additions & 2 deletions etc/schema/functions/eth_getBlockByNumber.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ BEGIN
size, -- size
gas_limit, -- gasLimit
gas_used, -- gasUsed
repeat('\000', 32)::hash, -- mixHash
COALESCE(EXTRACT(EPOCH FROM timestamp), 0)::int4 -- timestamp
COALESCE(EXTRACT(EPOCH FROM timestamp), 0)::int4, -- timestamp
repeat('\000', 32)::hash -- mixHash
FROM block
WHERE id = block_id
LIMIT 1
Expand Down
4 changes: 2 additions & 2 deletions etc/schema/types/block_result.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ CREATE TYPE block_result AS (
"size" int4,
"gasLimit" int8,
"gasUsed" int8,
"mixHash" hash,
"timestamp" int4
"timestamp" int4,
"mixHash" hash
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- +goose Up
ALTER TABLE event ADD COLUMN "from" address;
ALTER TABLE event ADD COLUMN IF NOT EXISTS "from" address;
-- +goose StatementBegin
SELECT 'up SQL query';
-- +goose StatementEnd
Expand Down
71 changes: 71 additions & 0 deletions migrations/20211222141956_reload_block_result_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- +goose Up
-- +goose StatementBegin
--Make sure it will not fail on db
ALTER TYPE block_result DROP ATTRIBUTE IF EXISTS "mixHash"; --Make sure it will not fail on db
ALTER TYPE block_result ADD ATTRIBUTE "mixHash" hash;
-- Move mixHash to the end, since order is important
CREATE OR REPLACE FUNCTION eth_getBlockByNumber(block_id blockno) RETURNS block_result AS $$
DECLARE
result block_result;
BEGIN
SELECT
id, -- number
hash, -- hash
parent_hash, -- parentHash
repeat('\000', 8)::bytea, -- nonce
'\x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', -- sha3Uncles keccak256(rlp.encode([]))
repeat('\000', 256)::bytea, -- logsBloom
transactions_root, -- transactionsRoot
state_root, -- stateRoot
receipts_root, -- receiptsRoot
repeat('\000', 20)::bytea, -- miner
0, -- difficulty
0, -- totalDifficulty
''::bytea, -- extraData
size, -- size
gas_limit, -- gasLimit
gas_used, -- gasUsed
COALESCE(EXTRACT(EPOCH FROM timestamp), 0)::int4, -- timestamp
repeat('\000', 32)::hash -- mixHash
FROM block
WHERE id = block_id
LIMIT 1
INTO STRICT result;
RETURN result;
END;
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TYPE block_result DROP ATTRIBUTE IF EXISTS "mixHash";
CREATE OR REPLACE FUNCTION eth_getBlockByNumber(block_id blockno) RETURNS block_result AS $$
DECLARE
result block_result;
BEGIN
SELECT
id, -- number
hash, -- hash
parent_hash, -- parentHash
repeat('\000', 8)::bytea, -- nonce
'\x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', -- sha3Uncles keccak256(rlp.encode([]))
repeat('\000', 256)::bytea, -- logsBloom
transactions_root, -- transactionsRoot
state_root, -- stateRoot
receipts_root, -- receiptsRoot
repeat('\000', 20)::bytea, -- miner
0, -- difficulty
0, -- totalDifficulty
''::bytea, -- extraData
size, -- size
gas_limit, -- gasLimit
gas_used, -- gasUsed
COALESCE(EXTRACT(EPOCH FROM timestamp), 0)::int4 -- timestamp
FROM block
WHERE id = block_id
LIMIT 1
INTO STRICT result;
RETURN result;
END;
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
-- +goose StatementEnd

0 comments on commit 33a7702

Please sign in to comment.