Skip to content

Commit

Permalink
Split TransactionPoolAction::BestTipChanged in 2 actions
Browse files Browse the repository at this point in the history
The first fetches accounts from the ledger service
  • Loading branch information
sebastiencs committed Mar 28, 2024
1 parent f241635 commit 29e04a2
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 131 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion ledger/Cargo.toml
Expand Up @@ -70,7 +70,7 @@ crc32fast = "1"
chrono = "0.4"
serde_with = "3.6.1"
anyhow = "1.0.75"
fraction = { version = "0.15", default-features = false }
fraction = { version = "0.15", default-features = false, features = ["with-serde-support"] }

[target.'cfg(target_family = "wasm")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
18 changes: 13 additions & 5 deletions ledger/src/account/account.rs
Expand Up @@ -2,9 +2,13 @@ use std::{fmt::Write, io::Cursor, str::FromStr};

use ark_ff::{BigInteger256, One, UniformRand, Zero};
use mina_hasher::Fp;
use mina_p2p_messages::binprot::{BinProtRead, BinProtWrite};
use mina_p2p_messages::{
binprot::{BinProtRead, BinProtWrite},
v2,
};
use mina_signer::CompressedPubKey;
use rand::{prelude::ThreadRng, seq::SliceRandom, Rng};
use serde::{Deserialize, Serialize};

use crate::{
gen_compressed,
Expand Down Expand Up @@ -337,7 +341,7 @@ impl Permissions<AuthRequired> {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProofVerified {
N0,
N1,
Expand Down Expand Up @@ -385,7 +389,7 @@ impl ToFieldElements<Fp> for ProofVerified {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerificationKey {
pub max_proofs_verified: ProofVerified,
pub actual_wrap_domain_size: ProofVerified,
Expand Down Expand Up @@ -855,7 +859,9 @@ impl From<AccountIdOrderable> for AccountId {
}
}

#[derive(Clone, Eq)]
#[derive(Clone, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[serde(into = "v2::MinaBaseAccountIdStableV2")]
#[serde(from = "v2::MinaBaseAccountIdStableV2")]
pub struct AccountId {
pub public_key: CompressedPubKey,
pub token_id: TokenId,
Expand Down Expand Up @@ -1092,7 +1098,9 @@ pub struct PermsConst {
}

// https://github.com/MinaProtocol/mina/blob/1765ba6bdfd7c454e5ae836c49979fa076de1bea/src/lib/mina_base/account.ml#L368
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(into = "v2::MinaBaseAccountBinableArgStableV2")]
#[serde(from = "v2::MinaBaseAccountBinableArgStableV2")]
pub struct Account {
pub public_key: CompressedPubKey, // Public_key.Compressed.t
pub token_id: TokenId, // Token_id.t
Expand Down
77 changes: 53 additions & 24 deletions ledger/src/account/conv.rs
Expand Up @@ -131,22 +131,9 @@ impl From<&MinaBaseVerificationKeyWireStableV1> for VerificationKey {
let MinaBaseVerificationKeyWireStableV1 {
max_proofs_verified,
actual_wrap_domain_size,
wrap_index:
MinaBaseVerificationKeyWireStableV1WrapIndex {
sigma_comm,
coefficients_comm,
generic_comm,
psm_comm,
complete_add_comm,
mul_comm,
emul_comm,
endomul_scalar_comm,
},
wrap_index,
} = vk;

let sigma = array_into(sigma_comm);
let coefficients = array_into(coefficients_comm);

VerificationKey {
max_proofs_verified: match max_proofs_verified {
PicklesBaseProofsVerifiedStableV1::N0 => ProofVerified::N0,
Expand All @@ -158,21 +145,46 @@ impl From<&MinaBaseVerificationKeyWireStableV1> for VerificationKey {
PicklesBaseProofsVerifiedStableV1::N1 => ProofVerified::N1,
PicklesBaseProofsVerifiedStableV1::N2 => ProofVerified::N2,
},
wrap_index: PlonkVerificationKeyEvals {
sigma,
coefficients,
generic: generic_comm.into(),
psm: psm_comm.into(),
complete_add: complete_add_comm.into(),
mul: mul_comm.into(),
emul: emul_comm.into(),
endomul_scalar: endomul_scalar_comm.into(),
},
wrap_index: wrap_index.into(),
wrap_vk: None,
}
}
}

impl From<&MinaBaseVerificationKeyWireStableV1WrapIndex> for PlonkVerificationKeyEvals<Fp> {
fn from(value: &MinaBaseVerificationKeyWireStableV1WrapIndex) -> Self {
let MinaBaseVerificationKeyWireStableV1WrapIndex {
sigma_comm,
coefficients_comm,
generic_comm,
psm_comm,
complete_add_comm,
mul_comm,
emul_comm,
endomul_scalar_comm,
} = value;

let sigma = array_into(sigma_comm);
let coefficients = array_into(coefficients_comm);

PlonkVerificationKeyEvals {
sigma,
coefficients,
generic: generic_comm.into(),
psm: psm_comm.into(),
complete_add: complete_add_comm.into(),
mul: mul_comm.into(),
emul: emul_comm.into(),
endomul_scalar: endomul_scalar_comm.into(),
}
}
}
impl From<MinaBaseVerificationKeyWireStableV1WrapIndex> for PlonkVerificationKeyEvals<Fp> {
fn from(value: MinaBaseVerificationKeyWireStableV1WrapIndex) -> Self {
(&value).into()
}
}

impl From<&VerificationKey> for MinaBaseVerificationKeyWireStableV1 {
fn from(vk: &VerificationKey) -> Self {
let VerificationKey {
Expand Down Expand Up @@ -296,6 +308,11 @@ impl From<&Account> for mina_p2p_messages::v2::MinaBaseAccountBinableArgStableV2
}
}
}
impl From<Account> for mina_p2p_messages::v2::MinaBaseAccountBinableArgStableV2 {
fn from(account: Account) -> Self {
(&account).into()
}
}

impl From<&AuthRequired> for mina_p2p_messages::v2::MinaBasePermissionsAuthRequiredStableV2 {
fn from(perms: &AuthRequired) -> Self {
Expand Down Expand Up @@ -329,6 +346,13 @@ impl From<&PlonkVerificationKeyEvals<Fp>>
}
}
}
impl From<PlonkVerificationKeyEvals<Fp>>
for mina_p2p_messages::v2::MinaBaseVerificationKeyWireStableV1WrapIndex
{
fn from(value: PlonkVerificationKeyEvals<Fp>) -> Self {
(&value).into()
}
}

// // Following types were written manually

Expand Down Expand Up @@ -532,6 +556,11 @@ impl From<&MinaBaseAccountBinableArgStableV2> for Account {
}
}
}
impl From<MinaBaseAccountBinableArgStableV2> for Account {
fn from(account: MinaBaseAccountBinableArgStableV2) -> Self {
(&account).into()
}
}

impl From<AccountIndex> for MinaBaseAccountIndexStableV1 {
fn from(value: AccountIndex) -> Self {
Expand Down
19 changes: 19 additions & 0 deletions ledger/src/proofs/transaction.rs
Expand Up @@ -497,6 +497,25 @@ pub struct PlonkVerificationKeyEvals<F: FieldWitness> {
pub endomul_scalar: InnerCurve<F>,
}

impl<'de> serde::Deserialize<'de> for PlonkVerificationKeyEvals<Fp> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
v2::MinaBaseVerificationKeyWireStableV1WrapIndex::deserialize(deserializer).map(Self::from)
}
}

impl serde::Serialize for PlonkVerificationKeyEvals<Fp> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let v: v2::MinaBaseVerificationKeyWireStableV1WrapIndex = self.into();
v.serialize(serializer)
}
}

// Here cvars are not used correctly, but it's just temporary
#[derive(Clone, Debug)]
pub struct CircuitPlonkVerificationKeyEvals<F: FieldWitness> {
Expand Down
60 changes: 22 additions & 38 deletions ledger/src/scan_state/conv.rs
Expand Up @@ -1998,52 +1998,36 @@ impl binprot::BinProtWrite for LedgerProofWithSokMessage {
}
}

impl From<MinaBaseUserCommandStableV2> for transaction_logic::valid::UserCommand {
fn from(value: MinaBaseUserCommandStableV2) -> Self {
(&value).into()
}
}

impl From<&MinaBaseUserCommandStableV2> for transaction_logic::valid::UserCommand {
fn from(value: &MinaBaseUserCommandStableV2) -> Self {
match value {
MinaBaseUserCommandStableV2::ZkappCommand(cmd) => {
Self::ZkAppCommand(Box::new(zkapp_command::valid::ZkAppCommand {
zkapp_command: zkapp_command::ZkAppCommand {
fee_payer: (&cmd.fee_payer).into(),
account_updates: (&cmd.account_updates).into(),
memo: (&cmd.memo).into(),
},
zkapp_command: cmd.into(),
}))
}
MinaBaseUserCommandStableV2::SignedCommand(cmd) => {
Self::SignedCommand(Box::new(SignedCommand {
payload: transaction_logic::signed_command::SignedCommandPayload {
common: transaction_logic::signed_command::Common {
fee: (&cmd.payload.common.fee).into(),
fee_payer_pk: (&cmd.payload.common.fee_payer_pk).into(),
nonce: (&cmd.payload.common.nonce).into(),
valid_until: (&cmd.payload.common.valid_until).into(),
memo: (&cmd.payload.common.memo).into(),
},
body: match &cmd.payload.body {
MinaBaseSignedCommandPayloadBodyStableV2::Payment(payment) => {
transaction_logic::signed_command::Body::Payment(PaymentPayload {
receiver_pk: (&payment.receiver_pk).into(),
amount: payment.amount.clone().into(),
})
}
MinaBaseSignedCommandPayloadBodyStableV2::StakeDelegation(
delegation,
) => {
let MinaBaseStakeDelegationStableV2::SetDelegate { new_delegate } =
&delegation;

transaction_logic::signed_command::Body::StakeDelegation(
StakeDelegationPayload::SetDelegate {
new_delegate: new_delegate.into(),
},
)
}
},
},
signer: (&cmd.signer).into(),
signature: (&*cmd.signature).into(),
}))
Self::SignedCommand(Box::new(cmd.into()))
}
}
}
}

impl From<transaction_logic::valid::UserCommand> for MinaBaseUserCommandStableV2 {
fn from(value: transaction_logic::valid::UserCommand) -> Self {
match value {
transaction_logic::valid::UserCommand::SignedCommand(cmd) => {
MinaBaseUserCommandStableV2::SignedCommand((&*cmd).into())
}
transaction_logic::valid::UserCommand::ZkAppCommand(cmd) => {
let zkapp_command::valid::ZkAppCommand { zkapp_command } = &*cmd;
MinaBaseUserCommandStableV2::ZkappCommand(zkapp_command.into())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ledger/src/scan_state/currency.rs
Expand Up @@ -380,7 +380,7 @@ macro_rules! impl_number {
$(impl_number!({$name64, u64, as_u64, from_u64, next_u64, append_u64},);)+
};
($({ $name:ident, $inner:ty, $as_name:ident, $from_name:ident, $next_name:ident, $append_name:ident },)*) => ($(
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
pub struct $name(pub(super) $inner);

impl std::fmt::Debug for $name {
Expand Down
3 changes: 2 additions & 1 deletion ledger/src/scan_state/fee_rate.rs
@@ -1,8 +1,9 @@
use crate::scan_state::currency::Magnitude;

use super::currency::Fee;
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Hash)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct FeeRate {
q: fraction::Fraction,
}
Expand Down
14 changes: 9 additions & 5 deletions ledger/src/scan_state/transaction_logic.rs
Expand Up @@ -44,7 +44,7 @@ use super::{
};

/// https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/mina_base/transaction_status.ml#L9
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum TransactionFailure {
Predicate,
SourceNotPresent,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl ToString for TransactionFailure {
}

/// https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/mina_base/transaction_status.ml#L452
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum TransactionStatus {
Applied,
Failed(Vec<Vec<TransactionFailure>>),
Expand All @@ -186,7 +186,7 @@ impl TransactionStatus {
}

/// https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/mina_base/with_status.ml#L6
#[derive(Debug, Clone, PartialEq)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
pub struct WithStatus<T> {
pub data: T,
pub status: TransactionStatus,
Expand Down Expand Up @@ -260,7 +260,11 @@ pub mod valid {

pub type SignedCommand = super::signed_command::SignedCommand;

#[derive(Clone, Debug, PartialEq)]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(into = "MinaBaseUserCommandStableV2")]
#[serde(from = "MinaBaseUserCommandStableV2")]
pub enum UserCommand {
SignedCommand(Box<SignedCommand>),
ZkAppCommand(Box<super::zkapp_command::valid::ZkAppCommand>),
Expand Down Expand Up @@ -1299,7 +1303,7 @@ pub mod zkapp_command {
}
}

#[derive(Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct WithHash<T, H = Fp> {
pub data: T,
pub hash: H,
Expand Down

0 comments on commit 29e04a2

Please sign in to comment.