From 0beb2412aff6dec54e5562b93c85438798b6ee46 Mon Sep 17 00:00:00 2001 From: carneiro-cw <156914855+carneiro-cw@users.noreply.github.com> Date: Mon, 18 Mar 2024 12:53:03 -0300 Subject: [PATCH] feat: insert to the remaining neo_tables (#385) * feat: insert to the remaining neo_tables * fmt * lint --- ...68c2f47d09b1325cc53035929d60c89559171.json | 16 +++++++ ...fa84f3d62c5ab9d0c75f4e2b32a8232b48d33.json | 18 ++++++++ src/eth/storage/hybrid/query_executor.rs | 43 +++++++++++++++++++ static/schema/001-init.sql | 3 +- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 .sqlx/query-0b15e5777d491cb4a4fea24045f68c2f47d09b1325cc53035929d60c89559171.json create mode 100644 .sqlx/query-673c1b1108abf013843370ec99afa84f3d62c5ab9d0c75f4e2b32a8232b48d33.json diff --git a/.sqlx/query-0b15e5777d491cb4a4fea24045f68c2f47d09b1325cc53035929d60c89559171.json b/.sqlx/query-0b15e5777d491cb4a4fea24045f68c2f47d09b1325cc53035929d60c89559171.json new file mode 100644 index 000000000..7c714bfaa --- /dev/null +++ b/.sqlx/query-0b15e5777d491cb4a4fea24045f68c2f47d09b1325cc53035929d60c89559171.json @@ -0,0 +1,16 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO public.neo_transactions (block_number, hash, transaction_data)\n SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::jsonb[])\n AS t(block_number, hash, transaction_data);", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array", + "ByteaArray", + "JsonbArray" + ] + }, + "nullable": [] + }, + "hash": "0b15e5777d491cb4a4fea24045f68c2f47d09b1325cc53035929d60c89559171" +} diff --git a/.sqlx/query-673c1b1108abf013843370ec99afa84f3d62c5ab9d0c75f4e2b32a8232b48d33.json b/.sqlx/query-673c1b1108abf013843370ec99afa84f3d62c5ab9d0c75f4e2b32a8232b48d33.json new file mode 100644 index 000000000..c2f6acd1e --- /dev/null +++ b/.sqlx/query-673c1b1108abf013843370ec99afa84f3d62c5ab9d0c75f4e2b32a8232b48d33.json @@ -0,0 +1,18 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO public.neo_logs (block_number, hash, address, log_idx, log_data)\n SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::bytea[], $4::numeric[], $5::jsonb[])\n AS t(block_number, hash, address, log_idx, log_data);", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8Array", + "ByteaArray", + "ByteaArray", + "NumericArray", + "JsonbArray" + ] + }, + "nullable": [] + }, + "hash": "673c1b1108abf013843370ec99afa84f3d62c5ab9d0c75f4e2b32a8232b48d33" +} diff --git a/src/eth/storage/hybrid/query_executor.rs b/src/eth/storage/hybrid/query_executor.rs index 2ad5f503d..2bcbe7716 100644 --- a/src/eth/storage/hybrid/query_executor.rs +++ b/src/eth/storage/hybrid/query_executor.rs @@ -82,6 +82,21 @@ pub async fn commit_eventually(pool: Arc>, block_task: BlockTask) accounts_changes.4.push(nonce); } + let mut transaction_batch = (Vec::new(), Vec::new(), Vec::new()); + let mut logs_batch = (Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new()); + for transaction in block_task.block_data.transactions { + for log in transaction.logs.iter() { + logs_batch.0.push(transaction.block_number); + logs_batch.1.push(transaction.input.hash.clone()); + logs_batch.2.push(log.log.address.clone()); + logs_batch.3.push(log.log_index); + logs_batch.4.push(serde_json::to_value(log).unwrap()); + } + transaction_batch.0.push(transaction.block_number); + transaction_batch.1.push(transaction.input.hash.clone()); + transaction_batch.2.push(serde_json::to_value(transaction).unwrap()); + } + let pool_clone = Arc::>::clone(&pool); execute_with_retry( || async { @@ -127,6 +142,34 @@ pub async fn commit_eventually(pool: Arc>, block_task: BlockTask) .await?; } + if !transaction_batch.0.is_empty() { + sqlx::query!( + "INSERT INTO public.neo_transactions (block_number, hash, transaction_data) + SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::jsonb[]) + AS t(block_number, hash, transaction_data);", + transaction_batch.0 as _, + transaction_batch.1 as _, + transaction_batch.2 as _, + ) + .execute(&mut *tx) + .await?; + } + + if !logs_batch.0.is_empty() { + sqlx::query!( + "INSERT INTO public.neo_logs (block_number, hash, address, log_idx, log_data) + SELECT * FROM UNNEST($1::bigint[], $2::bytea[], $3::bytea[], $4::numeric[], $5::jsonb[]) + AS t(block_number, hash, address, log_idx, log_data);", + logs_batch.0 as _, + logs_batch.1 as _, + logs_batch.2 as _, + logs_batch.3 as _, + logs_batch.4 as _, + ) + .execute(&mut *tx) + .await?; + } + tx.commit().await?; Ok(()) }, diff --git a/static/schema/001-init.sql b/static/schema/001-init.sql index 338eadac6..99ab80b6d 100644 --- a/static/schema/001-init.sql +++ b/static/schema/001-init.sql @@ -764,9 +764,10 @@ CREATE TABLE public.neo_logs ( block_number BIGINT NOT NULL, hash BYTEA NOT NULL, address BYTEA NOT NULL, + log_idx numeric NOT NULL, log_data JSONB NOT NULL, created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(), - PRIMARY KEY (hash, address, block_number), + PRIMARY KEY (hash, block_number, log_idx), FOREIGN KEY (block_number) REFERENCES public.neo_blocks(block_number) );