Skip to content
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

NRD kernel variant (compile against grin master) #408

Merged
merged 4 commits into from
May 29, 2020
Merged
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
16 changes: 8 additions & 8 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions libwallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ pub enum ErrorKind {
#[fail(display = "Unknown Kernel Feature: {}", _0)]
UnknownKernelFeatures(u8),

/// Invalid Kernel Feature
#[fail(display = "Invalid Kernel Feature: {}", _0)]
InvalidKernelFeatures(u8),

/// Invalid Slatepack Data
#[fail(display = "Invalid Slatepack Data: {}", _0)]
InvalidSlatepackData(String),
Expand Down
48 changes: 40 additions & 8 deletions libwallet/src/slate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use crate::error::{Error, ErrorKind};
use crate::grin_core::core::amount_to_hr_string;
use crate::grin_core::core::transaction::{
Input, KernelFeatures, Output, OutputFeatures, Transaction, TransactionBody, TxKernel,
Weighting,
Input, KernelFeatures, NRDRelativeHeight, Output, OutputFeatures, Transaction, TransactionBody,
TxKernel, Weighting,
};
use crate::grin_core::core::verifier_cache::LruVerifierCache;
use crate::grin_core::libtx::{aggsig, build, proof::ProofBuild, tx_fee};
Expand Down Expand Up @@ -114,7 +114,11 @@ pub struct Slate {
/// should refuse to process the transaction and unlock all
/// associated outputs
pub ttl_cutoff_height: u64,
/// Kernel Features flag, if any
/// Kernel Features flag -
/// 0: plain
/// 1: coinbase (invalid)
/// 2: height_locked
/// 3: NRD
pub kernel_features: u8,
/// Offset, needed when posting of tranasction is deferred
pub offset: BlindingFactor,
Expand Down Expand Up @@ -160,7 +164,7 @@ pub enum SlateState {
#[derive(Debug, Clone, PartialEq, Eq)]
/// Kernel features arguments definition
pub struct KernelFeaturesArgs {
/// Lock height, for HeightLocked
/// Lock height, for HeightLocked (also NRD relative lock height)
pub lock_height: u64,
}

Expand Down Expand Up @@ -346,12 +350,17 @@ impl Slate {
Ok(())
}

// Construct the appropriate kernel features based on our fee and lock_height.
// If lock_height is 0 then its a plain kernel, otherwise its a height locked kernel.
// Build kernel features based on variant and associated data.
// 0: plain
// 1: coinbase (invalid)
// 2: height_locked (with associated lock_height)
// 3: NRD (with associated relative_height)
// Any other value is invalid.
fn kernel_features(&self) -> Result<KernelFeatures, Error> {
match self.kernel_features {
0 => Ok(KernelFeatures::Plain { fee: self.fee }),
1 => Ok(KernelFeatures::HeightLocked {
1 => Err(ErrorKind::InvalidKernelFeatures(1).into()),
2 => Ok(KernelFeatures::HeightLocked {
fee: self.fee,
lock_height: match &self.kernel_features_args {
Some(a) => a.lock_height,
Expand All @@ -360,7 +369,16 @@ impl Slate {
}
},
}),
n => return Err(ErrorKind::UnknownKernelFeatures(n).into()),
3 => Ok(KernelFeatures::NoRecentDuplicate {
fee: self.fee,
relative_height: match &self.kernel_features_args {
Some(a) => NRDRelativeHeight::new(a.lock_height)?,
None => {
return Err(ErrorKind::KernelFeaturesMissing(format!("lock_height")).into())
}
},
}),
n => Err(ErrorKind::UnknownKernelFeatures(n).into()),
}
}

Expand Down Expand Up @@ -996,6 +1014,14 @@ impl From<&TxKernel> for TxKernelV4 {
KernelFeatures::HeightLocked { fee, lock_height } => {
(CompatKernelFeatures::HeightLocked, fee, lock_height)
}
KernelFeatures::NoRecentDuplicate {
fee,
relative_height,
} => (
CompatKernelFeatures::NoRecentDuplicate,
fee,
relative_height.into(),
),
};
TxKernelV4 {
features,
Expand Down Expand Up @@ -1257,6 +1283,11 @@ impl From<&TxKernelV4> for TxKernel {
CompatKernelFeatures::Plain => KernelFeatures::Plain { fee },
CompatKernelFeatures::Coinbase => KernelFeatures::Coinbase,
CompatKernelFeatures::HeightLocked => KernelFeatures::HeightLocked { fee, lock_height },
CompatKernelFeatures::NoRecentDuplicate => KernelFeatures::NoRecentDuplicate {
fee,
relative_height: NRDRelativeHeight::new(lock_height)
.expect("a valid NRD relative height"),
},
};
TxKernel {
features,
Expand All @@ -1271,4 +1302,5 @@ pub enum CompatKernelFeatures {
Plain,
Coinbase,
HeightLocked,
NoRecentDuplicate,
}
23 changes: 22 additions & 1 deletion libwallet/src/slate_versions/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,27 @@ impl TryFrom<&SlateV4> for SlateV3 {
let id = *id;
let amount = *amount;
let fee = *fee;

// Match on kernel feature variant:
// 0: plain
// 1: coinbase (invalid)
// 2: height locked (with associated lock_height)
// 3: NRD (with associated relative_height)
// Anything else is unknown.
let lock_height = match feat {
0 => 0,
1 => match feat_args {
1 => return Err(ErrorKind::InvalidKernelFeatures(1).into()),
2 => match feat_args {
None => return Err(ErrorKind::KernelFeaturesMissing("lock_hgt".to_owned()).into()),
Some(h) => h.lock_hgt,
},
3 => match feat_args {
None => return Err(ErrorKind::KernelFeaturesMissing("lock_hgt".to_owned()).into()),
Some(h) => h.lock_hgt,
},
n => return Err(ErrorKind::UnknownKernelFeatures(*n).into()),
};

let participant_data = map_vec!(participant_data, |data| ParticipantDataV3::from(data));
let version_info = VersionCompatInfoV3::from(ver);
let payment_proof = match payment_proof {
Expand Down Expand Up @@ -783,6 +796,14 @@ impl From<&SlateV4> for Option<TransactionV3> {
out_lock_height = lock_height;
CompatKernelFeatures::HeightLocked
}
KernelFeatures::NoRecentDuplicate {
fee,
relative_height,
} => {
out_fee = fee;
out_lock_height = relative_height.into();
CompatKernelFeatures::NoRecentDuplicate
}
},
fee: out_fee,
lock_height: out_lock_height,
Expand Down
12 changes: 6 additions & 6 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ grin_api = { git = "https://github.com/mimblewimble/grin", branch = "master" }
grin_store = { git = "https://github.com/mimblewimble/grin", branch = "master" }

# For local testing
#grin_core = { path = "../../grin/core"}
#grin_keychain = { path = "../../grin/keychain"}
#grin_chain = { path = "../../grin/chain"}
#grin_util = { path = "../../grin/util"}
#grin_api = { path = "../../grin/api"}
#grin_store = { path = "../../grin/store"}
# grin_core = { path = "../../grin/core"}
# grin_keychain = { path = "../../grin/keychain"}
# grin_chain = { path = "../../grin/chain"}
# grin_util = { path = "../../grin/util"}
# grin_api = { path = "../../grin/api"}
# grin_store = { path = "../../grin/store"}

[dev-dependencies]
pretty_assertions = "0.5.1"