Skip to content

Commit

Permalink
feat: insert to the remaining neo_tables (#385)
Browse files Browse the repository at this point in the history
* feat: insert to the remaining neo_tables

* fmt

* lint
  • Loading branch information
carneiro-cw authored Mar 18, 2024
1 parent 97fbfd8 commit 0beb241
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.

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

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

43 changes: 43 additions & 0 deletions src/eth/storage/hybrid/query_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ pub async fn commit_eventually(pool: Arc<Pool<Postgres>>, 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::<sqlx::Pool<sqlx::Postgres>>::clone(&pool);
execute_with_retry(
|| async {
Expand Down Expand Up @@ -127,6 +142,34 @@ pub async fn commit_eventually(pool: Arc<Pool<Postgres>>, 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(())
},
Expand Down
3 changes: 2 additions & 1 deletion static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down

0 comments on commit 0beb241

Please sign in to comment.