Skip to content

Commit

Permalink
Add jcli support for EVM mapping certificates (#3887)
Browse files Browse the repository at this point in the history
* update chain-libs

* remove total_stake field from PrivateTallyState::Encrypted

this field has been removed from chain-libs already

* add EvmMapping variant in certificate match patterns

* add EvmMapping variant in fragment match patterns

* remove max_stake step from TallyVotePlanWithAllShares

math for this is performed in the EncryptedTally type
so this code should not be necessary, or it should be
simplified

* add EvmMapping variant in match patterns jcli, explorer,
prometheus_exporter

* fix encrypted_tally cloning

* remove unneded code

* update chain-libs to latest master

* remove max_stake calculation that is now redundant

* add missing read impl

Co-authored-by: Giacomo Pasini <[email protected]>
  • Loading branch information
saibatizoku and zeegomo authored Mar 18, 2022
1 parent 3c96fc5 commit 2fa5178
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 61 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions explorer/src/api/graphql/certificates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Certificate {
UpdateProposal(UpdateProposal),
UpdateVote(UpdateVote),
MintToken(MintToken),
EvmMapping(EvmMapping),
}

pub struct StakeDelegation(certificate::StakeDelegation);
Expand All @@ -46,6 +47,8 @@ pub struct UpdateVote(certificate::UpdateVote);

pub struct MintToken(certificate::MintToken);

pub struct EvmMapping(certificate::EvmMapping);

#[Object]
impl StakeDelegation {
// FIXME: Maybe a new Account type would be better?
Expand Down Expand Up @@ -262,6 +265,13 @@ impl MintToken {
}
}

#[Object]
impl EvmMapping {
pub async fn address(&self) -> String {
format!("{:?}", self.0)
}
}

/*------------------------------*/
/*------- Conversions ---------*/
/*----------------------------*/
Expand Down Expand Up @@ -290,6 +300,7 @@ impl From<chain_impl_mockchain::certificate::Certificate> for Certificate {
}
certificate::Certificate::UpdateVote(c) => Certificate::UpdateVote(UpdateVote(c)),
certificate::Certificate::MintToken(c) => Certificate::MintToken(MintToken(c)),
certificate::Certificate::EvmMapping(c) => Certificate::EvmMapping(EvmMapping(c)),
}
}
}
Expand Down Expand Up @@ -347,3 +358,9 @@ impl From<certificate::UpdateVote> for UpdateVote {
UpdateVote(update_vote)
}
}

impl From<certificate::EvmMapping> for EvmMapping {
fn from(evm_mapping: certificate::EvmMapping) -> Self {
EvmMapping(evm_mapping)
}
}
3 changes: 2 additions & 1 deletion jcli/src/jcli_lib/certificate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod sign;
mod weighted_pool_ids;

pub(crate) use self::sign::{
committee_vote_plan_sign, committee_vote_tally_sign, pool_owner_sign,
committee_vote_plan_sign, committee_vote_tally_sign, evm_mapping_sign, pool_owner_sign,
stake_delegation_account_binding_sign, update_proposal_sign, update_vote_sign,
};

Expand Down Expand Up @@ -254,6 +254,7 @@ fn read_cert_or_signed_cert(input: Option<&Path>) -> Result<interfaces::Certific
SignedCertificate::VoteTally(vt, _) => Certificate::VoteTally(vt),
SignedCertificate::UpdateProposal(vt, _) => Certificate::UpdateProposal(vt),
SignedCertificate::UpdateVote(vt, _) => Certificate::UpdateVote(vt),
SignedCertificate::EvmMapping(vt, _) => Certificate::EvmMapping(vt),
};

Ok(interfaces::Certificate(cert))
Expand Down
30 changes: 27 additions & 3 deletions jcli/src/jcli_lib/certificate/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::jcli_lib::{
use chain_crypto::{Ed25519, PublicKey};
use chain_impl_mockchain::{
certificate::{
BftLeaderBindingSignature, Certificate, PoolOwnersSigned, PoolRegistration, PoolSignature,
SignedCertificate, StakeDelegation, TallyProof, UpdateProposal, UpdateVote, VotePlan,
VotePlanProof, VoteTally,
BftLeaderBindingSignature, Certificate, EvmMapping, PoolOwnersSigned, PoolRegistration,
PoolSignature, SignedCertificate, StakeDelegation, TallyProof, UpdateProposal, UpdateVote,
VotePlan, VotePlanProof, VoteTally,
},
key::EitherEd25519SecretKey,
transaction::{
Expand Down Expand Up @@ -125,6 +125,16 @@ impl Sign {
})??
}
Certificate::MintToken(_) => return Err(Error::MintTokenDoesntNeedSignature),
Certificate::EvmMapping(uv) => {
let txbuilder = Transaction::block0_payload_builder(&uv);
keys_str
.len()
.eq(&1)
.then(|| evm_mapping_sign(uv, &keys_str[0], txbuilder))
.ok_or(Error::ExpectingOnlyOneSigningKey {
got: keys_str.len(),
})??
}
};
write_signed_cert(self.output.as_deref(), signedcert.into())
}
Expand Down Expand Up @@ -269,3 +279,17 @@ pub(crate) fn update_vote_sign<P: Payload>(

Ok(SignedCertificate::UpdateVote(update_vote, signature))
}

pub(crate) fn evm_mapping_sign(
evm_mapping: EvmMapping,
key_str: &str,
builder: TxBuilderState<SetAuthData<EvmMapping>>,
) -> Result<SignedCertificate, Error> {
let private_key = parse_ed25519_secret_key(key_str.trim())?;

let signature = SingleAccountBindingSignature::new(&builder.get_auth_data(), |d| {
private_key.sign_slice(d.0)
});

Ok(SignedCertificate::EvmMapping(evm_mapping, signature))
}
27 changes: 25 additions & 2 deletions jcli/src/jcli_lib/transaction/staging.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::jcli_lib::{
certificate::{
self, committee_vote_plan_sign, committee_vote_tally_sign, pool_owner_sign,
stake_delegation_account_binding_sign, update_proposal_sign, update_vote_sign,
self, committee_vote_plan_sign, committee_vote_tally_sign, evm_mapping_sign,
pool_owner_sign, stake_delegation_account_binding_sign, update_proposal_sign,
update_vote_sign,
},
transaction::Error,
utils::io,
Expand Down Expand Up @@ -274,6 +275,19 @@ impl Staging {
.map_err(|error| Error::CertificateError { error })??;
self.extra_authed = Some(sc.into())
}
Certificate::EvmMapping(uv) => {
let builder = self.builder_after_witness(TxBuilder::new().set_payload(&uv))?;
let sc = keys
.len()
.eq(&1)
.then(|| {
evm_mapping_sign(uv, &keys[0], builder)
.map_err(|e| Error::CertificateError { error: e })
})
.ok_or(certificate::Error::ExpectingOnlyOneSigningKey { got: keys.len() })
.map_err(|error| Error::CertificateError { error })??;
self.extra_authed = Some(sc.into())
}
Certificate::MintToken(_) => unreachable!(),
},
};
Expand Down Expand Up @@ -408,6 +422,9 @@ impl Staging {
Certificate::MintToken(vt) => {
self.finalize_payload(&vt, fee_algorithm, output_policy)
}
Certificate::EvmMapping(vt) => {
self.finalize_payload(&vt, fee_algorithm, output_policy)
}

Certificate::OwnerStakeDelegation(c) => {
let balance = self.finalize_payload(&c, fee_algorithm, output_policy)?;
Expand Down Expand Up @@ -559,6 +576,9 @@ impl Staging {
SignedCertificate::UpdateVote(vt, a) => {
self.make_fragment(&vt, &a, Fragment::UpdateVote)
}
SignedCertificate::EvmMapping(vt, a) => {
self.make_fragment(&vt, &a, Fragment::EvmMapping)
}
}
}
}
Expand Down Expand Up @@ -634,6 +654,9 @@ impl Staging {
Certificate::MintToken(vt) => {
self.transaction_sign_data_hash_on(TxBuilder::new().set_payload(&vt))
}
Certificate::EvmMapping(vt) => {
self.transaction_sign_data_hash_on(TxBuilder::new().set_payload(&vt))
}
},
};

Expand Down
19 changes: 0 additions & 19 deletions jcli/src/jcli_lib/vote/tally/decrypt_tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,6 @@ impl TallyVotePlanWithAllShares {
Some(self.threshold),
)?
.try_into()?;
let mut max_stake = 0;
// We need a first iteration to get the max stake used
for proposal in &mut vote_plan.proposals {
match &proposal.tally {
Tally::Private {
state: PrivateTallyState::Encrypted { total_stake, .. },
} => {
max_stake = std::cmp::max(u64::from(*total_stake), max_stake);
}
other => {
let found = match other {
Tally::Public { .. } => "public tally",
Tally::Private { .. } => "private decrypted tally",
};
return Err(Error::PrivateTallyExpected { found });
}
}
}

let committee_member_keys = vote_plan.committee_member_keys.clone();

let validated_tallies = (&vote_plan.proposals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ fn pack_certificate_in_empty_tx_fragment(cert: &SignedCertificate) -> Vec<Fragme
certificate::SignedCertificate::UpdateVote(c, a) => {
Fragment::UpdateVote(empty_auth_tx(c, a))
}
certificate::SignedCertificate::EvmMapping(c, a) => {
Fragment::EvmMapping(empty_auth_tx(c, a))
}
}]
}

Expand Down
19 changes: 19 additions & 0 deletions jormungandr-lib/src/interfaces/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl SignedCertificate {
certificate::SignedCertificate::UpdateVote(c, _) => {
Certificate(certificate::Certificate::UpdateVote(c))
}
certificate::SignedCertificate::EvmMapping(c, _) => {
Certificate(certificate::Certificate::EvmMapping(c))
}
}
}
}
Expand Down Expand Up @@ -108,6 +111,10 @@ impl property::Serialize for Certificate {
writer.write_all(&[11])?;
writer.write_all(c.serialize().as_slice())?;
}
certificate::Certificate::EvmMapping(c) => {
writer.write_all(&[15])?;
writer.write_all(c.serialize().as_slice())?;
}
};
Ok(())
}
Expand Down Expand Up @@ -217,6 +224,11 @@ impl property::Serialize for SignedCertificate {
writer.write_all(c.serialize().as_slice())?;
writer.write_all(a.serialize_in(ByteBuilder::new()).finalize().as_slice())?;
}
certificate::SignedCertificate::EvmMapping(c, a) => {
writer.write_all(&[10])?;
writer.write_all(c.serialize().as_slice())?;
writer.write_all(a.serialize_in(ByteBuilder::new()).finalize().as_slice())?;
}
};
Ok(())
}
Expand Down Expand Up @@ -287,6 +299,13 @@ impl Readable for SignedCertificate {
certificate::SignedCertificate::UpdateVote(cert, auth),
))
}
10 => {
let cert = certificate::EvmMapping::read(buf)?;
let auth = Readable::read(buf)?;
Ok(SignedCertificate(
certificate::SignedCertificate::EvmMapping(cert, auth),
))
}
t => Err(ReadError::UnknownTag(t as u32)),
}
}
Expand Down
Loading

0 comments on commit 2fa5178

Please sign in to comment.