Merge pull request #2940 from poanetwork/vb-actualize-_additional_internal_transaction_constraints-script

Actualize additional_internal_transaction_constraints script
pull/2958/head
Victor Baranov 5 years ago committed by GitHub
commit fea396812c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 76
      apps/explorer/priv/repo/migrations/scripts/20181108205650_additional_internal_transaction_constraints.sql

@ -1,7 +1,7 @@
## Current ## Current
### Features ### Features
- [#2835](https://github.com/poanetwork/blockscout/pull/2835), [#2871](https://github.com/poanetwork/blockscout/pull/2871), [#2872](https://github.com/poanetwork/blockscout/pull/2872), [#2886](https://github.com/poanetwork/blockscout/pull/2886), [#2925](https://github.com/poanetwork/blockscout/pull/2925), [#2936](https://github.com/poanetwork/blockscout/pull/2936), [#2949](https://github.com/poanetwork/blockscout/pull/2949) - Add "block_hash" to logs, token_transfers and internal transactions and "pending blocks operations" approach - [#2835](https://github.com/poanetwork/blockscout/pull/2835), [#2871](https://github.com/poanetwork/blockscout/pull/2871), [#2872](https://github.com/poanetwork/blockscout/pull/2872), [#2886](https://github.com/poanetwork/blockscout/pull/2886), [#2925](https://github.com/poanetwork/blockscout/pull/2925), [#2936](https://github.com/poanetwork/blockscout/pull/2936), [#2949](https://github.com/poanetwork/blockscout/pull/2949), [#2940](https://github.com/poanetwork/blockscout/pull/2940) - Add "block_hash" to logs, token_transfers and internal transactions and "pending blocks operations" approach
- [#2926](https://github.com/poanetwork/blockscout/pull/2926) - API endpoint: sum balances except burnt address - [#2926](https://github.com/poanetwork/blockscout/pull/2926) - API endpoint: sum balances except burnt address
- [#2918](https://github.com/poanetwork/blockscout/pull/2918) - Add tokenID for tokentx API action explicitly - [#2918](https://github.com/poanetwork/blockscout/pull/2918) - Add tokenID for tokentx API action explicitly

@ -1,69 +1,77 @@
-- UPDATE (2020-08-01): use pending_block_operations table
DO $$ DO $$
DECLARE DECLARE
row_count integer := 1; row_count integer := 1;
batch_size integer := 50000; -- HOW MANY ITEMS WILL BE UPDATED AT TIME batch_size integer := 500; -- HOW MANY ITEMS WILL BE UPDATED AT TIME
iterator integer := batch_size; iterator integer := batch_size;
max_row_number integer; max_row_number integer;
next_iterator integer; next_iterator integer;
updated_transaction_count integer; updated_blocks_count integer;
deleted_internal_transaction_count integer; deleted_internal_transaction_count integer;
deleted_row_count integer; deleted_row_count integer;
BEGIN BEGIN
DROP TABLE IF EXISTS transactions_with_deprecated_internal_transactions; DROP TABLE IF EXISTS blocks_with_deprecated_internal_transactions;
-- CREATES TEMP TABLE TO STORE DATA TO BE UPDATED -- CREATES TEMP TABLE TO STORE DATA TO BE UPDATED
CREATE TEMP TABLE transactions_with_deprecated_internal_transactions( CREATE TEMP TABLE blocks_with_deprecated_internal_transactions(
hash bytea NOT NULL, block_number integer NOT NULL,
row_number integer row_number integer
); );
INSERT INTO transactions_with_deprecated_internal_transactions INSERT INTO blocks_with_deprecated_internal_transactions
SELECT DISTINCT ON (transaction_hash) SELECT DISTINCT ON (a.block_number)
transaction_hash, a.block_number,
ROW_NUMBER () OVER () ROW_NUMBER () OVER ()
FROM internal_transactions FROM (
WHERE SELECT DISTINCT i.block_number, i.transaction_index
-- call_has_call_type CONSTRAINT FROM internal_transactions i
(type = 'call' AND call_type IS NULL) OR WHERE
-- call_has_input CONSTRAINT i.block_number IS NOT NULL
(type = 'call' AND input IS NULL) OR AND
-- create_has_init CONSTRAINT -- call_has_call_type CONSTRAINT
(type = 'create' AND init is NULL) ((i.type = 'call' AND i.call_type IS NULL) OR
ORDER BY transaction_hash DESC; -- call_has_input CONSTRAINT
(i.type = 'call' AND i.input IS NULL) OR
max_row_number := (SELECT MAX(row_number) FROM transactions_with_deprecated_internal_transactions); -- create_has_init CONSTRAINT
RAISE NOTICE '% transactions to be updated', max_row_number + 1; (i.type = 'create' AND i.init is NULL))
ORDER BY i.block_number DESC, i.transaction_index
) a;
max_row_number := (SELECT MAX(row_number) FROM blocks_with_deprecated_internal_transactions);
RAISE NOTICE '% blocks to be updated', max_row_number + 1;
-- ITERATES THROUGH THE ITEMS UNTIL THE TEMP TABLE IS EMPTY -- ITERATES THROUGH THE ITEMS UNTIL THE TEMP TABLE IS EMPTY
WHILE iterator <= max_row_number LOOP WHILE iterator <= max_row_number LOOP
next_iterator := iterator + batch_size; next_iterator := iterator + batch_size;
RAISE NOTICE '-> transactions with deprecated internal transactions % to % to be updated', iterator, next_iterator - 1; RAISE NOTICE '-> blocks with deprecated internal transactions % to % to be updated', iterator, next_iterator - 1;
UPDATE transactions INSERT INTO pending_block_operations (block_hash, inserted_at, updated_at, fetch_internal_transactions)
SET internal_transactions_indexed_at = NULL, SELECT b.hash, NOW(), NOW(), true
error = NULL FROM blocks_with_deprecated_internal_transactions bd, blocks b
FROM transactions_with_deprecated_internal_transactions WHERE bd.block_number = b.number
WHERE transactions.hash = transactions_with_deprecated_internal_transactions.hash AND AND bd.row_number < next_iterator
transactions_with_deprecated_internal_transactions.row_number < next_iterator; AND b.consensus = true
ON CONFLICT (block_hash)
DO NOTHING;
GET DIAGNOSTICS updated_transaction_count = ROW_COUNT; GET DIAGNOSTICS updated_blocks_count = ROW_COUNT;
RAISE NOTICE '-> % transactions updated to refetch internal transactions', updated_transaction_count; RAISE NOTICE '-> % blocks updated to refetch internal transactions', updated_blocks_count;
DELETE FROM internal_transactions DELETE FROM internal_transactions
USING transactions_with_deprecated_internal_transactions USING blocks_with_deprecated_internal_transactions
WHERE internal_transactions.transaction_hash = transactions_with_deprecated_internal_transactions.hash AND WHERE internal_transactions.block_number = blocks_with_deprecated_internal_transactions.block_number AND
transactions_with_deprecated_internal_transactions.row_number < next_iterator; blocks_with_deprecated_internal_transactions.row_number < next_iterator;
GET DIAGNOSTICS deleted_internal_transaction_count = ROW_COUNT; GET DIAGNOSTICS deleted_internal_transaction_count = ROW_COUNT;
RAISE NOTICE '-> % internal transactions deleted', deleted_internal_transaction_count; RAISE NOTICE '-> % internal transactions deleted', deleted_internal_transaction_count;
DELETE FROM transactions_with_deprecated_internal_transactions DELETE FROM blocks_with_deprecated_internal_transactions
WHERE row_number < next_iterator; WHERE row_number < next_iterator;
GET DIAGNOSTICS deleted_row_count = ROW_COUNT; GET DIAGNOSTICS deleted_row_count = ROW_COUNT;
ASSERT updated_transaction_count = deleted_row_count; ASSERT updated_blocks_count = deleted_row_count;
-- COMMITS THE BATCH UPDATES -- COMMITS THE BATCH UPDATES
CHECKPOINT; CHECKPOINT;

Loading…
Cancel
Save