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

Use keypair version to generate secret key in massa client #4011

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 18 additions & 8 deletions massa-client/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,27 +782,37 @@ impl Command {
resp.entry
}
Err(e) => {
// FIXME: Should we default to the last known version - default to 0?
grpc_error!(e)
}
};

// Note: unused for now as AddressFactory is not yet merged
let _address_version = versioning_status
// Note: this is a bit duplicated with KeyPairFactory code but it avoids
// sending the whole mip store through the API
let keypair_version = versioning_status
.into_iter()
.rev()
.find_map(|entry| {
let is_about_keypair = match entry.mip_info {
None => false,
Some(mip_info) => mip_info.components.iter().any(|st_entry| {
grpc::MipComponent::from_i32(st_entry.kind)
.unwrap_or(grpc::MipComponent::Unspecified)
== grpc::MipComponent::Keypair
}),
};

let state = grpc::ComponentStateId::from_i32(entry.state_id)
.unwrap_or(grpc::ComponentStateId::Error);
match state {
grpc::ComponentStateId::Active => Some(entry.state_id),
_ => None,

if is_about_keypair && state == grpc::ComponentStateId::Active {
Some(entry.state_id)
} else {
None
}
})
.unwrap_or(0);
println!("Should create address with version: {}", _address_version);
let key = KeyPair::generate(keypair_version as u64).unwrap();
Leo-Besancon marked this conversation as resolved.
Show resolved Hide resolved

let key = KeyPair::generate(0).unwrap();
let ad = wallet.add_keypairs(vec![key])?[0];
if json {
Ok(Box::new(ad.to_string()))
Expand Down
9 changes: 8 additions & 1 deletion massa-proto/proto/massa/api/v1/versioning.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ message MipInfo {
}

message MipComponentEntry {
fixed32 kind = 1;
MipComponent kind = 1;
fixed32 version = 2;
}

Expand All @@ -46,3 +46,10 @@ enum ComponentStateId {
COMPONENT_STATE_ID_ACTIVE = 5;
COMPONENT_STATE_ID_FAILED = 6;
}

// Same as MipComponent enum in versioning package
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In proto files, always copy/paste the doc on Rust struct. This is will be added to autogenerated documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

enum MipComponent {
MIP_COMPONENT_UNSPECIFIED = 0;
MIP_COMPONENT_ADDRESS = 1;
MIP_COMPONENT_KEYPAIR = 2;
}
Binary file modified massa-proto/src/api.bin
Binary file not shown.
34 changes: 32 additions & 2 deletions massa-proto/src/massa.api.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ pub struct MipInfo {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MipComponentEntry {
#[prost(fixed32, tag = "1")]
pub kind: u32,
#[prost(enumeration = "MipComponent", tag = "1")]
pub kind: i32,
#[prost(fixed32, tag = "2")]
pub version: u32,
}
Expand Down Expand Up @@ -1074,6 +1074,36 @@ impl ComponentStateId {
}
}
}
/// Same as MipComponent enum in versioning package
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum MipComponent {
Unspecified = 0,
Address = 1,
Keypair = 2,
}
impl MipComponent {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
MipComponent::Unspecified => "MIP_COMPONENT_UNSPECIFIED",
MipComponent::Address => "MIP_COMPONENT_ADDRESS",
MipComponent::Keypair => "MIP_COMPONENT_KEYPAIR",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"MIP_COMPONENT_UNSPECIFIED" => Some(Self::Unspecified),
"MIP_COMPONENT_ADDRESS" => Some(Self::Address),
"MIP_COMPONENT_KEYPAIR" => Some(Self::Keypair),
_ => None,
}
}
}
/// GetBlocksRequest holds request for GetBlocks
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
14 changes: 12 additions & 2 deletions massa-versioning/src/grpc_mapping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023 MASSA LABS <info@massa.net>
use crate::versioning::{ComponentStateTypeId, MipInfo};
use crate::versioning::{ComponentStateTypeId, MipComponent, MipInfo};
use massa_proto::massa::api::v1 as grpc;

impl From<&ComponentStateTypeId> for grpc::ComponentStateId {
Expand All @@ -15,13 +15,23 @@ impl From<&ComponentStateTypeId> for grpc::ComponentStateId {
}
}

impl From<&MipComponent> for grpc::MipComponent {
fn from(value: &MipComponent) -> Self {
match value {
MipComponent::KeyPair => grpc::MipComponent::Keypair,
MipComponent::Address => grpc::MipComponent::Address,
_ => grpc::MipComponent::Unspecified,
}
}
}

aoudiamoncef marked this conversation as resolved.
Show resolved Hide resolved
impl From<&MipInfo> for grpc::MipInfo {
fn from(value: &MipInfo) -> Self {
let components = value
.components
.iter()
.map(|(mip_component, version)| grpc::MipComponentEntry {
kind: u32::from(mip_component.clone()),
kind: grpc::MipComponent::from(mip_component).into(),
version: *version,
})
.collect();
Expand Down