Skip to content

Commit

Permalink
justification draft 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jakehemmerle committed Jun 29, 2021
1 parent 7688be0 commit 30a6089
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ target
*.nix
.tmp/
dev_conf.toml
**/.DS_Store
**/.vscode
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;

0 comments on commit 30a6089

Please sign in to comment.