From 30a6089095f0313be61584b97b88cd363a1ac654 Mon Sep 17 00:00:00 2001 From: Jake Hemmerle Date: Sun, 27 Jun 2021 11:54:43 -0400 Subject: [PATCH] justification draft 1 --- .gitignore | 2 ++ substrate-archive/src/database.rs | 11 ++++++++--- substrate-archive/src/database/models.rs | 3 +++ substrate-archive/src/database/queries.rs | 2 +- .../20210611075511_add_column_justification.sql | 3 +++ 5 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 substrate-archive/src/migrations/20210611075511_add_column_justification.sql diff --git a/.gitignore b/.gitignore index 5bab26be..c8246439 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ target *.nix .tmp/ dev_conf.toml +**/.DS_Store +**/.vscode diff --git a/substrate-archive/src/database.rs b/substrate-archive/src/database.rs index b15e3749..450b510e 100644 --- a/substrate-archive/src/database.rs +++ b/substrate-archive/src/database.rs @@ -129,7 +129,7 @@ where ); let query = sqlx::query( r#" - INSERT INTO blocks (parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec) VALUES($1, $2, $3, $4, $5, $6, $7, $8) + INSERT INTO blocks (parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec, justification) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT DO NOTHING "#, ); @@ -140,6 +140,7 @@ where let extrinsics_root = self.inner.block.header().extrinsics_root().as_ref(); let digest = self.inner.block.header().digest().encode(); let extrinsics = self.inner.block.extrinsics().encode(); + let justifications = self.inner.justifications.encode(); query .bind(parent_hash) @@ -150,6 +151,7 @@ where .bind(digest.as_slice()) .bind(extrinsics.as_slice()) .bind(self.spec) + .bind(justifications.as_slice()) .execute(conn) .await .map(|d| d.rows_affected()) @@ -168,7 +170,7 @@ where "blocks", r#" INSERT INTO "blocks" ( - parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec + parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec, justification ) VALUES "#, r#" @@ -176,7 +178,7 @@ where "#, ); for b in self.inner { - batch.reserve(8)?; + batch.reserve(9)?; if batch.current_num_arguments() > 0 { batch.append(","); } @@ -187,6 +189,7 @@ where let extrinsics_root = b.inner.block.header().extrinsics_root().as_ref(); let digest = b.inner.block.header().digest().encode(); let extrinsics = b.inner.block.extrinsics().encode(); + let justifications = b.inner.justifications.encode(); batch.append("("); batch.bind(parent_hash)?; batch.append(","); @@ -203,6 +206,8 @@ where batch.bind(extrinsics.as_slice())?; batch.append(","); batch.bind(b.spec)?; + batch.append(","); + batch.bind(justifications.as_slice())?; batch.append(")"); } Ok(batch.execute(conn).await?) diff --git a/substrate-archive/src/database/models.rs b/substrate-archive/src/database/models.rs index c2134a11..d9f2306b 100644 --- a/substrate-archive/src/database/models.rs +++ b/substrate-archive/src/database/models.rs @@ -44,6 +44,7 @@ pub struct BlockModel { pub digest: Vec, pub ext: Vec, pub spec: i32, + pub justification: Vec } impl BlockModel { @@ -57,6 +58,8 @@ impl BlockModel { let header = ::new(block_num, extrinsics_root, state_root, parent_hash, digest); let spec = self.spec as u32; + // do I need to include justifications here? + // let justification = Decode::decode(&mut self.justification.as_slice())?; Ok((B::new(header, ext), spec)) } diff --git a/substrate-archive/src/database/queries.rs b/substrate-archive/src/database/queries.rs index 875fa2a1..28841a74 100644 --- a/substrate-archive/src/database/queries.rs +++ b/substrate-archive/src/database/queries.rs @@ -94,7 +94,7 @@ pub(crate) async fn get_full_block_by_number(conn: &mut sqlx::PgConnection, bloc sqlx::query_as!( BlockModel, " - SELECT id, parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec + SELECT id, parent_hash, hash, block_num, state_root, extrinsics_root, digest, ext, spec, justification FROM blocks WHERE block_num = $1 ", diff --git a/substrate-archive/src/migrations/20210611075511_add_column_justification.sql b/substrate-archive/src/migrations/20210611075511_add_column_justification.sql new file mode 100644 index 00000000..0b6f352e --- /dev/null +++ b/substrate-archive/src/migrations/20210611075511_add_column_justification.sql @@ -0,0 +1,3 @@ +-- Add migration script here +alter table blocks + add justification bytea; \ No newline at end of file