From a87528a3f6ac6199a1a4c64ee1740b41111ccec6 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 22 May 2022 22:39:45 +0100 Subject: [PATCH] Hook up transfers, show mining pool, show script messages & QOL improvements --- indexer/src/main.rs | 71 +++++++++++++++++++++++++++-------------------------------------------- migrations/up/V1__initial_schema.sql | 2 ++ frontend/src/lib/AsmScript.svelte | 21 +++++++++++++++++++++ frontend/src/lib/Blocks.svelte | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ frontend/src/lib/Time.svelte | 50 +++++++++++++++++++++++++------------------------- frontend/src/lib/Transaction.svelte | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- frontend/src/lib/TransactionInputInfo.svelte | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/src/lib/TransactionOutputInfo.svelte | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/src/lib/Transactions.svelte | 31 +++++++++++++++++++++++++++++++ frontend/src/lib/bitcoinScript.ts | 374 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ frontend/src/lib/dayjs.ts | 4 ++-- frontend/src/lib/i18n.ts | 22 ++++++++++------------ frontend/src/lib/store.ts | 19 +++++++++++++++++++ frontend/src/routes/__layout.svelte | 4 ++-- frontend/src/routes/index.svelte | 113 ++++++++++++++++++-------------------------------------------------------------- web-api/src/database/blocks.rs | 23 ++++++++++++++++++----- web-api/src/database/transactions.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ web-api/src/methods/address.rs | 20 +------------------- web-api/src/methods/block.rs | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- web-api/src/methods/mod.rs | 3 +++ web-api/src/methods/transaction.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ frontend/src/lib/i18n/en.json | 1 + frontend/src/routes/address/[address].svelte | 56 +++++++++++++++++++++++++++++--------------------------- frontend/src/routes/block/[id].svelte | 17 ++++++++++------- frontend/src/routes/block/index.svelte | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/src/routes/tx/[id].svelte | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- frontend/src/routes/tx/index.svelte | 40 +++++++++++++++++++++++++++++++++++++--- 27 files changed, 1284 insertions(+), 495 deletions(-) diff --git a/indexer/src/main.rs b/indexer/src/main.rs index 0aeae00..1c2c266 100644 --- a/indexer/src/main.rs +++ a/indexer/src/main.rs @@ -188,16 +188,12 @@ transaction: &Transaction, ) -> Result> { let query = " - WITH inserted AS ( - INSERT INTO transactions - (hash, block_id, version, lock_time, weight, coinbase, replace_by_fee) - VALUES ($1, $2, $3, $4, $5, $6, $7) - ON CONFLICT DO NOTHING - RETURNING id - ) SELECT COALESCE( - (SELECT id FROM inserted), - (SELECT id FROM transactions WHERE hash = $1) - ) AS id + INSERT INTO transactions + (hash, block_id, version, lock_time, weight, coinbase, replace_by_fee) + VALUES ($1, $2, $3, $4, $5, $6, $7) + ON CONFLICT (hash) DO UPDATE + SET block_id = excluded.block_id + RETURNING id "; Ok(tx @@ -223,17 +219,24 @@ transaction_id: i64, transaction_input: &TxIn, ) -> Result<(), Box> { - let previous_output = select_transaction_output( - tx, - &transaction_input.previous_output.txid.to_vec(), - transaction_input.previous_output.vout as i64, - ) - .await?; - let query = " INSERT INTO transaction_inputs - (transaction_id, index, previous_output, script) - VALUES ($1, $2, $3, $4) + (transaction_id, index, sequence, witness, script, previous_output) + VALUES ( + $1, + $2, + $3, + $4, + $5, + ( + SELECT transaction_outputs.id + FROM transactions + INNER JOIN transaction_outputs + ON transactions.id = transaction_outputs.transaction_id + WHERE transactions.hash = $6 + AND transaction_outputs.index = $7 + ) + ) ON CONFLICT DO NOTHING "; @@ -242,8 +245,11 @@ &[ &transaction_id, &index, - &previous_output, + &i64::from(transaction_input.sequence), + &transaction_input.witness.to_vec(), &transaction_input.script_sig.as_bytes(), + &transaction_input.previous_output.txid.to_vec(), + &(transaction_input.previous_output.vout as i64), ], ) .await?; @@ -279,31 +285,6 @@ .await?; Ok(()) -} - -// TODO: this is a _very_ efficient query involving just two index scans, right now we're inserting -// it alongside transaction_outputs, but we need sequential inserts for that to work. maybe we can -// just call this query on-demand? or figure out a way to sequentialise inserts - that's quite risky -// to our insert speed though. -async fn select_transaction_output( - tx: &tokio_postgres::Transaction<'_>, - transaction_hash: &[u8], - transaction_index: i64, -) -> Result, Box> { - let query = " - SELECT transaction_outputs.id AS output_id - FROM transactions - INNER JOIN transaction_outputs - ON transactions.id = transaction_outputs.transaction_id - WHERE transactions.hash = $1 - AND transaction_outputs.index = $2 - "; - - let row = tx - .query_opt(query, &[&transaction_hash, &transaction_index]) - .await?; - - Ok(row.map(|v| v.get("output_id"))) } #[derive(Parser, Debug)] diff --git a/migrations/up/V1__initial_schema.sql b/migrations/up/V1__initial_schema.sql index e7b97cb..01deae3 100644 --- a/migrations/up/V1__initial_schema.sql +++ a/migrations/up/V1__initial_schema.sql @@ -56,6 +56,8 @@ id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, transaction_id BIGINT NOT NULL, index BIGINT NOT NULL, + sequence BIGINT NOT NULL, + witness BYTEA[] NOT NULL, previous_output BIGINT, script BYTEA NOT NULL, CONSTRAINT fk_transaction_id diff --git a/frontend/src/lib/AsmScript.svelte b/frontend/src/lib/AsmScript.svelte new file mode 100644 index 0000000..2523e56 100644 --- /dev/null +++ a/frontend/src/lib/AsmScript.svelte @@ -1,0 +1,21 @@ + + + + {#each asm as opcode} + {#if opcode.startsWith("OP_")} + {` ${opcode} `} + {:else} + {` ${opcode} `} + {/if} + {/each} + + + diff --git a/frontend/src/lib/Blocks.svelte b/frontend/src/lib/Blocks.svelte new file mode 100644 index 0000000..dcbdd06 100644 --- /dev/null +++ a/frontend/src/lib/Blocks.svelte @@ -1,0 +1,48 @@ + + +
+ + + + + + + + + + + + + + {#each blocks as block} + + + + + + + + + + {/each} + +
{$_("home.latest_blocks.table.height")}{$_("home.latest_blocks.table.timestamp")}{$_("home.latest_blocks.table.pool")}{$_("home.latest_blocks.table.txns")}{$_("home.latest_blocks.table.size")}{$_("home.latest_blocks.table.weight")}
{block.height} + + + {block.mined_by?.pool || "Unknown"}{block.tx_count}{block.bits}{block.weight}
+
+ + diff --git a/frontend/src/lib/Time.svelte b/frontend/src/lib/Time.svelte index 3d99465..3f360b2 100644 --- a/frontend/src/lib/Time.svelte +++ a/frontend/src/lib/Time.svelte @@ -1,34 +1,34 @@ -