Skip to content

Commit

Permalink
add the previous nonce and balance to the accounts schema
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw committed Mar 8, 2024
1 parent f199d0a commit f6930ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 11 additions & 17 deletions src/eth/storage/postgres/queries/insert_entire_block.sql
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,31 @@ account_insert AS (
new_balance,
new_nonce,
creation_block,
original_balance,
original_nonce
previous_balance,
previous_nonce
)
)

INSERT INTO accounts (
address, bytecode, latest_balance, latest_nonce, creation_block
address, bytecode, latest_balance, latest_nonce, creation_block, previous_balance, previous_nonce
)
SELECT
address,
bytecode,
new_balance,
new_nonce,
creation_block
creation_block,
previous_balance,
previous_nonce
FROM account_updates
ON CONFLICT (address) DO
UPDATE
SET latest_nonce = excluded.latest_nonce,
latest_balance = excluded.latest_balance
WHERE accounts.latest_nonce
= (
SELECT original_nonce
FROM account_updates
WHERE account_updates.address = excluded.address
)
AND accounts.latest_balance
= (
SELECT original_balance
FROM account_updates
WHERE account_updates.address = excluded.address
)
latest_balance = excluded.latest_balance,
previous_balance = excluded.previous_balance,
previous_nonce = excluded.previous_nonce
WHERE accounts.latest_nonce = excluded.previous_nonce
AND accounts.latest_balance = excluded.previous_balance
RETURNING 1 as res
),

Expand Down
2 changes: 2 additions & 0 deletions static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CREATE TABLE IF NOT EXISTS accounts (
bytecode BYTEA,
latest_balance NUMERIC NOT NULL CHECK (latest_balance >= 0),
latest_nonce NUMERIC NOT NULL CHECK (latest_nonce >= 0),
previous_balance NUMERIC CHECK (latest_balance >= 0),
previous_nonce NUMERIC CHECK (latest_balance >= 0),
creation_block NUMERIC NOT NULL REFERENCES blocks (number) ON DELETE CASCADE,
PRIMARY KEY (address)
);
Expand Down

0 comments on commit f6930ea

Please sign in to comment.