Skip to content

Commit

Permalink
update sqls
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw committed Mar 8, 2024
1 parent 82cd0c4 commit 67a9e43
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/eth/storage/postgres/queries/insert_account.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
INSERT INTO accounts (address, latest_nonce, latest_balance, bytecode, creation_block)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO accounts (
address, bytecode, latest_balance, latest_nonce, creation_block, previous_balance, previous_nonce
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (address) DO
UPDATE
SET latest_nonce = EXCLUDED.latest_nonce,
latest_balance = EXCLUDED.latest_balance
WHERE accounts.latest_nonce = $6 AND accounts.latest_balance = $7
WHERE accounts.latest_nonce = excluded.previous_balance AND accounts.latest_balance = excluded.previous_balance
49 changes: 39 additions & 10 deletions src/eth/storage/postgres/queries/insert_account_batch.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
WITH account_updates
AS (SELECT *
FROM UNNEST($1::bytea[], $2::bytea[], $3::numeric[], $4::numeric[], $5::numeric[], $6::numeric[], $7::numeric[])
AS t(address, bytecode, new_balance, new_nonce, creation_block, original_balance, original_nonce)
WITH account_updates AS (
SELECT *
FROM
unnest(
$50::bytea [],
$51::bytea [],
$52::numeric [],
$53::numeric [],
$54::numeric [],
$55::numeric [],
$56::numeric []
)
AS t (
address,
bytecode,
new_balance,
new_nonce,
creation_block,
previous_balance,
previous_nonce
)
)
INSERT INTO accounts (address, bytecode, latest_balance, latest_nonce, creation_block)
SELECT address, bytecode, new_balance, new_nonce, creation_block

INSERT INTO accounts (
address, bytecode, latest_balance, latest_nonce, creation_block, previous_balance, previous_nonce
)
SELECT
address,
bytecode,
new_balance,
new_nonce,
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=accounts.address)
AND accounts.latest_balance = (SELECT original_balance FROM account_updates WHERE account_updates.address=accounts.address)
SET latest_nonce = excluded.latest_nonce,
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
5 changes: 3 additions & 2 deletions src/eth/storage/postgres/queries/insert_account_slot.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INSERT INTO account_slots VALUES ($1, $2, $3, $4)
INSERT INTO account_slots(idx, value, previous_value, account_address, creation_block)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (idx, account_address)
DO UPDATE SET value = EXCLUDED.value
WHERE account_slots.value = $5
WHERE account_slots.value = excluded.previous_value
34 changes: 22 additions & 12 deletions src/eth/storage/postgres/queries/insert_slot_batch.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
WITH slot_updates
AS (SELECT *
FROM UNNEST($1::bytea[], $2::bytea[], $3::bytea[], $4::numeric[], $5::bytea[])
AS t(idx, value, account_address, creation_block, original_value)
WITH slot_updates AS (
SELECT *
FROM
unnest(
$57::bytea [],
$58::bytea [],
$59::bytea [],
$60::numeric [],
$61::bytea []
)
AS t (idx, value, account_address, block_number, original_value)
)
INSERT INTO account_slots (idx, value, account_address, creation_block)
SELECT idx, value, account_address, creation_block

INSERT INTO account_slots (idx, value, previous_value, account_address, creation_block)
SELECT
idx,
value,
original_value,
account_address,
block_number
FROM slot_updates
ON CONFLICT (idx, account_address) DO
UPDATE
SET value = EXCLUDED.value
WHERE account_slots.value = (
SELECT original_value
FROM slot_updates
WHERE slot_updates.idx = EXCLUDED.idx
AND slot_updates.account_address = EXCLUDED.account_address)
SET value = excluded.value,
previous_value = excluded.previous_value
WHERE account_slots.value = excluded.previous_value

0 comments on commit 67a9e43

Please sign in to comment.