|
|
@ -6,6 +6,7 @@ |
|
|
|
-- IMPORTANT NOTE: after making all the corrections needed the script will NOT |
|
|
|
-- IMPORTANT NOTE: after making all the corrections needed the script will NOT |
|
|
|
-- run the constraint validations because this may be a very long and taxing |
|
|
|
-- run the constraint validations because this may be a very long and taxing |
|
|
|
-- operation. To validate the constraint one can run, after the script fininshed: |
|
|
|
-- operation. To validate the constraint one can run, after the script fininshed: |
|
|
|
|
|
|
|
-- UPDATE (2019-11-04): use pending_block_operations table instead of internal_transactions |
|
|
|
|
|
|
|
|
|
|
|
-- ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_call_type; |
|
|
|
-- ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_call_type; |
|
|
|
-- ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_input; |
|
|
|
-- ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_input; |
|
|
@ -14,56 +15,62 @@ |
|
|
|
DO $$ |
|
|
|
DO $$ |
|
|
|
DECLARE |
|
|
|
DECLARE |
|
|
|
batch_size integer := 10000; -- HOW MANY ITEMS WILL BE UPDATED AT A TIME |
|
|
|
batch_size integer := 10000; -- HOW MANY ITEMS WILL BE UPDATED AT A TIME |
|
|
|
last_transaction_hash bytea; -- WILL CHECK ONLY TRANSACTIONS FOLLOWING THIS HASH (DESC) |
|
|
|
last_block_number integer; -- WILL CHECK ONLY TRANSACTIONS FOLLOWING THIS HASH (DESC) |
|
|
|
last_fetched_batch_size integer; |
|
|
|
last_fetched_batch_size integer; |
|
|
|
BEGIN |
|
|
|
BEGIN |
|
|
|
RAISE NOTICE 'STARTING SCRIPT'; |
|
|
|
RAISE NOTICE 'STARTING SCRIPT'; |
|
|
|
CREATE TEMP TABLE transactions_with_deprecated_internal_transactions(hash bytea NOT NULL); |
|
|
|
CREATE TEMP TABLE blocks_with_deprecated_internal_transactions(block_number integer NOT NULL); |
|
|
|
|
|
|
|
|
|
|
|
LOOP |
|
|
|
LOOP |
|
|
|
RAISE NOTICE 'Fetching new batch of % transactions to correct', batch_size; |
|
|
|
RAISE NOTICE 'Fetching new batch of % transactions to correct', batch_size; |
|
|
|
|
|
|
|
|
|
|
|
INSERT INTO transactions_with_deprecated_internal_transactions |
|
|
|
INSERT INTO blocks_with_deprecated_internal_transactions |
|
|
|
SELECT DISTINCT transaction_hash |
|
|
|
SELECT DISTINCT a.block_number |
|
|
|
FROM internal_transactions |
|
|
|
FROM ( |
|
|
|
WHERE |
|
|
|
SELECT DISTINCT i.block_number, i.transaction_index |
|
|
|
(last_transaction_hash IS NULL OR transaction_hash < last_transaction_hash) AND |
|
|
|
FROM internal_transactions i |
|
|
|
-- call_has_call_type CONSTRAINT |
|
|
|
WHERE |
|
|
|
((type = 'call' AND call_type IS NULL) OR |
|
|
|
i.block_number IS NOT NULL |
|
|
|
-- call_has_input CONSTRAINT |
|
|
|
AND |
|
|
|
(type = 'call' AND input IS NULL) OR |
|
|
|
(last_block_number IS NULL OR i.block_number < last_block_number) AND |
|
|
|
-- create_has_init CONSTRAINT |
|
|
|
-- call_has_call_type CONSTRAINT |
|
|
|
(type = 'create' AND init is NULL)) |
|
|
|
((type = 'call' AND i.call_type IS NULL) OR |
|
|
|
ORDER BY transaction_hash DESC LIMIT batch_size; |
|
|
|
-- call_has_input CONSTRAINT |
|
|
|
|
|
|
|
(type = 'call' AND i.input IS NULL) OR |
|
|
|
|
|
|
|
-- create_has_init CONSTRAINT |
|
|
|
|
|
|
|
(type = 'create' AND i.init is NULL)) |
|
|
|
|
|
|
|
ORDER BY i.block_number DESC, i.transaction_index LIMIT batch_size |
|
|
|
|
|
|
|
) a; |
|
|
|
|
|
|
|
|
|
|
|
SELECT INTO last_fetched_batch_size count(*) FROM transactions_with_deprecated_internal_transactions; |
|
|
|
SELECT INTO last_fetched_batch_size count(block_number) FROM blocks_with_deprecated_internal_transactions; |
|
|
|
|
|
|
|
|
|
|
|
RAISE NOTICE 'Batch of % transactions was fetched, starting their deprecation', last_fetched_batch_size; |
|
|
|
RAISE NOTICE 'Batch of % transactions was fetched, starting their deprecation', last_fetched_batch_size; |
|
|
|
|
|
|
|
|
|
|
|
-- UPDATE TRANSACTIONS |
|
|
|
INSERT INTO pending_block_operations (block_hash, inserted_at, updated_at, fetch_internal_transactions) |
|
|
|
UPDATE transactions |
|
|
|
SELECT b.hash, NOW(), NOW(), true |
|
|
|
SET internal_transactions_indexed_at = NULL, |
|
|
|
FROM blocks_with_deprecated_internal_transactions bd, blocks b |
|
|
|
error = NULL |
|
|
|
WHERE bd.block_number = b.number |
|
|
|
FROM transactions_with_deprecated_internal_transactions |
|
|
|
AND b.consensus = true |
|
|
|
WHERE transactions.hash = transactions_with_deprecated_internal_transactions.hash; |
|
|
|
ON CONFLICT (block_hash) |
|
|
|
|
|
|
|
DO NOTHING; |
|
|
|
|
|
|
|
|
|
|
|
-- REMOVE THE DEPRECATED internal_transactions |
|
|
|
-- REMOVE THE DEPRECATED internal_transactions |
|
|
|
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; |
|
|
|
WHERE internal_transactions.block_number = blocks_with_deprecated_internal_transactions.block_number; |
|
|
|
|
|
|
|
|
|
|
|
-- COMMIT THE BATCH UPDATES |
|
|
|
-- COMMIT THE BATCH UPDATES |
|
|
|
CHECKPOINT; |
|
|
|
CHECKPOINT; |
|
|
|
|
|
|
|
|
|
|
|
-- UPDATE last_transaction_hash TO KEEP TRACK OF ROWS ALREADY CHECKED |
|
|
|
-- UPDATE last_block_number TO KEEP TRACK OF ROWS ALREADY CHECKED |
|
|
|
SELECT INTO last_transaction_hash hash |
|
|
|
SELECT INTO last_block_number block_number |
|
|
|
FROM transactions_with_deprecated_internal_transactions |
|
|
|
FROM blocks_with_deprecated_internal_transactions |
|
|
|
ORDER BY hash ASC LIMIT 1; |
|
|
|
ORDER BY block_number ASC LIMIT 1; |
|
|
|
|
|
|
|
|
|
|
|
RAISE NOTICE 'Last batch completed, last transaction hash: %', last_transaction_hash; |
|
|
|
RAISE NOTICE 'Last batch completed, last block number: %', last_block_number; |
|
|
|
|
|
|
|
|
|
|
|
-- CLEAR THE TEMP TABLE |
|
|
|
-- CLEAR THE TEMP TABLE |
|
|
|
DELETE FROM transactions_with_deprecated_internal_transactions; |
|
|
|
DELETE FROM blocks_with_deprecated_internal_transactions; |
|
|
|
|
|
|
|
|
|
|
|
-- EXIT IF ALL internal_transactions HAVE BEEN CHECKED ALREADY |
|
|
|
-- EXIT IF ALL internal_transactions HAVE BEEN CHECKED ALREADY |
|
|
|
EXIT WHEN last_fetched_batch_size != batch_size; |
|
|
|
EXIT WHEN last_fetched_batch_size != batch_size; |
|
|
@ -71,5 +78,5 @@ BEGIN |
|
|
|
|
|
|
|
|
|
|
|
RAISE NOTICE 'SCRIPT FINISHED, all affected transactions have been deprecated'; |
|
|
|
RAISE NOTICE 'SCRIPT FINISHED, all affected transactions have been deprecated'; |
|
|
|
|
|
|
|
|
|
|
|
DROP TABLE transactions_with_deprecated_internal_transactions; |
|
|
|
DROP TABLE blocks_with_deprecated_internal_transactions; |
|
|
|
END $$; |
|
|
|
END $$; |
|
|
|