Skip to content

[WIP] Add block justification support #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ target
*.nix
.tmp/
dev_conf.toml
**/.DS_Store
**/.vscode
**/*.log
11 changes: 8 additions & 3 deletions substrate-archive/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#,
);
Expand All @@ -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)
Expand All @@ -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())
Expand All @@ -168,15 +170,15 @@ 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#"
ON CONFLICT DO NOTHING
"#,
);
for b in self.inner {
batch.reserve(8)?;
batch.reserve(9)?;
if batch.current_num_arguments() > 0 {
batch.append(",");
}
Expand All @@ -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(",");
Expand All @@ -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?)
Expand Down
3 changes: 3 additions & 0 deletions substrate-archive/src/database/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct BlockModel {
pub digest: Vec<u8>,
pub ext: Vec<u8>,
pub spec: i32,
pub justification: Vec<u8>
}

impl BlockModel {
Expand All @@ -57,6 +58,8 @@ impl BlockModel {
let header = <B::Header as HeaderT>::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))
}
Expand Down
2 changes: 1 addition & 1 deletion substrate-archive/src/database/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add migration script here
ALTER TABLE blocks
ADD justification bytea;