Skip to content

Commit

Permalink
Update nonce calculations to use pallas
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Aug 21, 2024
1 parent dde41c3 commit 890d400
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 63 deletions.
69 changes: 47 additions & 22 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cncli"
version = "6.3.0"
version = "6.3.1"
authors = ["Andrew Westberg <[email protected]>"]
edition = "2018"
build = "build.rs"
Expand All @@ -18,8 +18,10 @@ blake2b_simd = "1.0"
byteorder = "1.5"
#pallas-network = { git = "https://github.com/AndrewWestberg/pallas", rev="35f693c57eec5f70c4f8e2f6a24445b14c6104b9" }
#pallas-traverse = { git = "https://github.com/AndrewWestberg/pallas", rev="35f693c57eec5f70c4f8e2f6a24445b14c6104b9" }
pallas-network = "0.29"
pallas-traverse = "0.29"
pallas-crypto = { git = "https://github.com/txpipe/pallas", rev = "d3084d6e3209ee0f507a413b1a2491e07abf3756" }
pallas-network = "0.30"
pallas-traverse = "0.30"
#pallas-crypto = "0.30.0"
chrono = "0.4"
chrono-tz = "0.9"
futures = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A community-based ```cardano-node``` CLI tool. It's a collection of utilities to enhance and extend beyond those available with the ```cardano-cli```.

![Build Status](https://github.com/cardano-community/cncli/actions/workflows/ci.yml/badge.svg?event=push)
![Build Status](https://github.com/cardano-community/cncli/actions/workflows/ci.yml/badge.svg?branch=develop)

## Installation

Expand Down
65 changes: 32 additions & 33 deletions src/nodeclient/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use std::io;
use std::path::Path;

use crate::nodeclient::sync::BlockHeader;
use blake2b_simd::Params;
use log::{debug, error, info};
use rusqlite::{named_params, Connection, Error};
use std::path::Path;
use pallas_crypto::hash::Hash;
use pallas_crypto::nonce::NonceGenerator;
use pallas_crypto::nonce::rolling_nonce::RollingNonceGenerator;
use rusqlite::{Connection, named_params};
use thiserror::Error;

use crate::nodeclient::sync::BlockHeader;

#[derive(Error, Debug)]
pub enum Error {
#[error("SQLite error: {0}")]
Sqlite(#[from] rusqlite::Error),

#[error("Nonce error: {0}")]
Nonce(#[from] pallas_crypto::nonce::Error),
}

pub trait BlockStore {
fn save_block(&mut self, pending_blocks: &mut Vec<BlockHeader>, shelley_genesis_hash: &str) -> io::Result<()>;
Expand Down Expand Up @@ -107,7 +121,7 @@ impl SqLiteBlockStore {
let mut stmt = tx.prepare("SELECT DISTINCT node_vkey FROM chain")?;
let vkeys = stmt
.query_map([], |row| {
let node_vkey_result: Result<String, Error> = row.get(0);
let node_vkey_result: Result<String, rusqlite::Error> = row.get(0);
let node_vkey = node_vkey_result?;
Ok(node_vkey)
})
Expand Down Expand Up @@ -172,7 +186,7 @@ impl SqLiteBlockStore {
let db = &mut self.db;

// get the last block eta_v (nonce) in the db
let mut prev_eta_v = {
let mut prev_eta_v: Hash<32> = Hash::from(
hex::decode(
match db.query_row(
"SELECT eta_v, block_number FROM chain WHERE block_number = ?1 and orphaned = 0",
Expand All @@ -188,9 +202,8 @@ impl SqLiteBlockStore {
shelley_genesis_hash.to_string()
}
},
)
.unwrap()
};
).unwrap().as_slice()
);

let tx = db.transaction()?;
{
Expand Down Expand Up @@ -251,7 +264,7 @@ impl SqLiteBlockStore {

if orphan_num > 0 {
// get the last block eta_v (nonce) in the db
prev_eta_v = {
prev_eta_v = Hash::from(
hex::decode(
match tx.query_row(
"SELECT eta_v, block_number FROM chain WHERE block_number = ?1 and orphaned = 0",
Expand All @@ -264,27 +277,13 @@ impl SqLiteBlockStore {
shelley_genesis_hash.to_string()
}
},
)
.unwrap()
};
).unwrap().as_slice()
);
}
// blake2b hash of eta_vrf_0
let mut block_eta_v = Params::new()
.hash_length(32)
.to_state()
.update(&block.eta_vrf_0)
.finalize()
.as_bytes()
.to_vec();
prev_eta_v.append(&mut block_eta_v);
// blake2b hash of prev_eta_v + block_eta_v
prev_eta_v = Params::new()
.hash_length(32)
.to_state()
.update(&prev_eta_v)
.finalize()
.as_bytes()
.to_vec();
// calculate rolling nonce (eta_v)
let mut rolling_nonce_generator = RollingNonceGenerator::new(prev_eta_v);
rolling_nonce_generator.apply_block(&block.eta_vrf_0)?;
prev_eta_v = rolling_nonce_generator.finalize()?;

// blake2b 224 of node_vkey is the pool_id
let pool_id = Params::new()
Expand All @@ -301,7 +300,7 @@ impl SqLiteBlockStore {
":hash" : hex::encode(block.hash),
":prev_hash" : hex::encode(block.prev_hash),
":pool_id" : hex::encode(pool_id),
":eta_v" : hex::encode(&prev_eta_v),
":eta_v" : hex::encode(prev_eta_v),
":node_vkey" : hex::encode(block.node_vkey),
":node_vrf_vkey" : hex::encode(block.node_vrf_vkey),
":block_vrf_0": hex::encode(block.block_vrf_0),
Expand Down Expand Up @@ -331,7 +330,7 @@ impl BlockStore for SqLiteBlockStore {
fn save_block(&mut self, pending_blocks: &mut Vec<BlockHeader>, shelley_genesis_hash: &str) -> io::Result<()> {
match self.sql_save_block(pending_blocks, shelley_genesis_hash) {
Ok(_) => Ok(()),
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Database error!")),
Err(error) => Err(io::Error::new(io::ErrorKind::Other, format!("Database error!: {:?}", error))),
}
}

Expand All @@ -342,8 +341,8 @@ impl BlockStore for SqLiteBlockStore {
.unwrap();
let blocks = stmt
.query_map([], |row| {
let slot_result: Result<i64, Error> = row.get(0);
let hash_result: Result<String, Error> = row.get(1);
let slot_result: Result<i64, rusqlite::Error> = row.get(0);
let hash_result: Result<String, rusqlite::Error> = row.get(1);
let slot = slot_result?;
let hash = hash_result?;
Ok((slot, hex::decode(hash).unwrap()))
Expand Down
8 changes: 4 additions & 4 deletions src/nodeclient/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ pub enum Error {
#[error("loggingobserver error occurred")]
LoggingObserverError(String),

#[error("pallas_traverse error occurred")]
#[error("pallas_traverse error occurred: {0}")]
PallasTraverseError(#[from] pallas_traverse::Error),

#[error("io error occurred")]
#[error("io error occurred: {0}")]
IoError(#[from] std::io::Error),

#[error("keepalive error occurred")]
#[error("keepalive error occurred: {0}")]
KeepAliveError(#[from] keepalive::ClientError),

#[error("chainsync error occurred")]
#[error("chainsync error occurred: {0}")]
ChainSyncError(#[from] chainsync::ClientError),

#[error("chainsync canceled")]
Expand Down

0 comments on commit 890d400

Please sign in to comment.