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

feat(zkapp): continue zkapp ledger diff prep #1694

Merged
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
2 changes: 2 additions & 0 deletions rust/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod previous_state_hash;
pub mod store;
pub mod vrf_output;

mod post_hardfork;

use self::{precomputed::PrecomputedBlock, vrf_output::VrfOutput};
use crate::{
canonicity::Canonicity,
Expand Down
64 changes: 64 additions & 0 deletions rust/src/block/post_hardfork/account_accessed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::{
ledger::account::{Account, Permissions},
mina_blocks::v2,
};
use serde::{Deserialize, Serialize};

#[derive(Default, Clone, Serialize, Deserialize)]
pub struct AccountAccessed {
pub account: Account,
// mina ledger index
pub index: u64,
}

impl From<(u64, v2::AccountAccessed)> for AccountAccessed {
fn from(value: (u64, v2::AccountAccessed)) -> Self {
let index = value.0;
let public_key = value.1.public_key.to_owned();

let timing = match value.1.timing {
v2::AccountAccessedTiming::Untimed(_) => None,
v2::AccountAccessedTiming::Timed((_, timing)) => Some(timing.into()),
};

let account = Account {
public_key: public_key.to_owned(),
balance: value.1.balance.into(),
nonce: Some(value.1.nonce.into()),
delegate: value.1.delegate.unwrap_or(public_key),
genesis_account: false,
token: Some(value.1.token_id),
receipt_chain_hash: Some(value.1.receipt_chain_hash),
voting_for: Some(value.1.voting_for),
permissions: Some(value.1.permissions.into()),
timing,
zkapp: value.1.zkapp,
username: None,
};

Self { account, index }
}
}

impl From<v2::Permissions> for Permissions {
fn from(value: v2::Permissions) -> Self {
Self {
edit_state: value.edit_state.0.into(),
access: value.access.0.into(),
send: value.send.0.into(),
receive: value.receive.0.into(),
set_delegate: value.set_delegate.0.into(),
set_permissions: value.set_permissions.0.into(),
set_verification_key: (
value.set_verification_key.0 .0.into(),
value.set_verification_key.1,
),
set_zkapp_uri: value.set_zkapp_uri.0.into(),
edit_action_state: value.edit_action_state.0.into(),
set_token_symbol: value.set_token_symbol.0.into(),
increment_nonce: value.increment_nonce.0.into(),
set_voting_for: value.set_voting_for.0.into(),
set_timing: value.set_timing.0.into(),
}
}
}
26 changes: 26 additions & 0 deletions rust/src/block/post_hardfork/account_created.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{
ledger::{amount::Amount, public_key::PublicKey, token::TokenAddress},
mina_blocks::v2,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountCreated {
pub public_key: PublicKey,
pub token: TokenAddress,
pub creation_fee: Amount,
}

impl From<v2::AccountCreated> for AccountCreated {
fn from(value: v2::AccountCreated) -> Self {
let public_key = value.0 .0;
let token = value.0 .1;
let creation_fee = value.1.parse().expect("account creation fee");

Self {
public_key,
token,
creation_fee,
}
}
}
3 changes: 3 additions & 0 deletions rust/src/block/post_hardfork/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod account_accessed;
pub mod account_created;
pub mod token_used;
31 changes: 31 additions & 0 deletions rust/src/block/post_hardfork/token_used.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{
ledger::{public_key::PublicKey, token::TokenAddress},
mina_blocks::v2,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenUsed {
pub used_token: TokenAddress,
pub token_owner: Option<PublicKey>,
pub payment_token: Option<TokenAddress>,
}

impl From<v2::TokenUsed> for TokenUsed {
fn from(value: v2::TokenUsed) -> Self {
let used_token = value.0;
let mut token_owner = None;
let mut payment_token = None;

if let Some((owner, token)) = value.1 {
token_owner = Some(owner);
payment_token = Some(token);
}

Self {
used_token,
token_owner,
payment_token,
}
}
}
60 changes: 59 additions & 1 deletion rust/src/block/precomputed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! Indexer internal precomputed block representation

use super::{epoch_data::EpochSeed, extract_network_height_hash, Block, BlockHash, VrfOutput};
use super::{
epoch_data::EpochSeed,
extract_network_height_hash,
post_hardfork::{
account_accessed::AccountAccessed, account_created::AccountCreated, token_used::TokenUsed,
},
Block, BlockHash, VrfOutput,
};
use crate::{
canonicity::Canonicity,
chain::Network,
Expand Down Expand Up @@ -64,6 +71,11 @@ pub struct BlockFileDataV2 {

protocol_state: v2::protocol_state::ProtocolState,
staged_ledger_diff: v2::staged_ledger_diff::StagedLedgerDiff,

// new post-hardfork data
tokens_used: Vec<v2::TokenUsed>,
accounts_accessed: Vec<(u64, v2::AccountAccessed)>,
accounts_created: Vec<v2::AccountCreated>,
}

fn berkeley_genesis_timestamp() -> u64 {
Expand Down Expand Up @@ -106,6 +118,10 @@ pub struct PrecomputedBlockV2 {
pub scheduled_time: u64,
pub protocol_state: v2::protocol_state::ProtocolState,
pub staged_ledger_diff: v2::staged_ledger_diff::StagedLedgerDiff,
// new post-hardfork data
pub tokens_used: Vec<v2::TokenUsed>,
pub accounts_accessed: Vec<(u64, v2::AccountAccessed)>,
pub accounts_created: Vec<v2::AccountCreated>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -169,6 +185,9 @@ impl PrecomputedBlock {
scheduled_time,
protocol_state,
staged_ledger_diff,
tokens_used,
accounts_accessed,
accounts_created,
},
} = serde_json::from_slice(&block_file_contents.contents)?;
Ok(Self::V2(PrecomputedBlockV2 {
Expand All @@ -178,6 +197,9 @@ impl PrecomputedBlock {
network: block_file_contents.network,
protocol_state,
staged_ledger_diff,
tokens_used,
accounts_accessed,
accounts_created,
}))
}
}
Expand Down Expand Up @@ -234,6 +256,42 @@ impl PrecomputedBlock {
}
}

pub fn accounts_accessed(&self) -> Vec<AccountAccessed> {
match self {
Self::V1(_v1) => vec![],
Self::V2(v2) => v2
.accounts_accessed
.iter()
.cloned()
.map(AccountAccessed::from)
.collect(),
}
}

pub fn accounts_created_v2(&self) -> Vec<AccountCreated> {
match self {
Self::V1(_v1) => vec![],
Self::V2(v2) => v2
.accounts_created
.iter()
.cloned()
.map(AccountCreated::from)
.collect(),
}
}

pub fn tokens_used(&self) -> Vec<TokenUsed> {
match self {
Self::V1(_v1) => vec![],
Self::V2(v2) => v2
.tokens_used
.iter()
.cloned()
.map(TokenUsed::from)
.collect(),
}
}

////////////////////////
// Staged ledger diff //
////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub const HARDFORK_PROTOCOL_TXN_VERSION_DIGEST: &str = "eccbc87e4b5ce2fe28308fd9
pub const HARDFORK_CHAIN_ID: &str =
"a7351abc7ddf2ea92d1b38cc8e636c271c1dfd2c081c637f62ebc2af34eb7cc1";

pub const ZKAPP_STATE_FIELD_ELEMENTS_NUM: usize = 8;

// Name service constants
pub const MINA_EXPLORER_NAME_SERVICE_ADDRESS: &str =
"B62qjzJvc59DdG9ahht9rwxkEz7GedKuUMsnaVTuXFUeANKqfBeWpRE";
Expand Down
42 changes: 25 additions & 17 deletions rust/src/ledger/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::{
block::{genesis::GenesisBlock, BlockHash},
constants::MAINNET_ACCOUNT_CREATION_FEE,
ledger::{diff::account::PaymentDiff, public_key::PublicKey},
mina_blocks::v2::ZkappAccount,
mina_blocks::v2::{self, ZkappAccount},
};
use mina_serialization_proc_macros::AutoFrom;
use serde::{Deserialize, Serialize};

#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -26,8 +27,7 @@ pub struct Account {
pub genesis_account: bool,

// optional
pub token: Option<u64>,
pub token_permissions: Option<TokenPermissions>,
pub token: Option<TokenAddress>,
pub receipt_chain_hash: Option<ReceiptChainHash>,
pub voting_for: Option<BlockHash>,
pub permissions: Option<Permissions>,
Expand All @@ -42,23 +42,34 @@ pub struct Account {

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Permissions {
stake: bool,
edit_state: Permission,
send: Permission,
set_delegate: Permission,
set_permissions: Permission,
set_verification_key: Permission,
pub edit_state: Permission,
pub access: Permission,
pub send: Permission,
pub receive: Permission,
pub set_delegate: Permission,
pub set_permissions: Permission,
pub set_verification_key: (Permission, String),
pub set_zkapp_uri: Permission,
pub edit_action_state: Permission,
pub set_token_symbol: Permission,
pub increment_nonce: Permission,
pub set_voting_for: Permission,
pub set_timing: Permission,
}

#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AutoFrom)]
#[auto_from(v2::PermissionKind)]
pub enum Permission {
#[default]
Signature,
None,
Either,
Proof,
Signature,
Impossible,
}

#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AutoFrom)]
#[auto_from(v2::Timing)]
pub struct Timing {
pub initial_minimum_balance: u64,
pub cliff_time: u32,
Expand All @@ -67,9 +78,6 @@ pub struct Timing {
pub vesting_increment: u64,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TokenPermissions {}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReceiptChainHash(pub String);

Expand Down Expand Up @@ -114,7 +122,7 @@ impl Account {
Account {
public_key: public_key.clone(),
delegate: public_key,
token: Some(token.into()),
token: Some(token),
..Default::default()
}
}
Expand Down
17 changes: 16 additions & 1 deletion rust/src/ledger/amount.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{constants::MINA_SCALE, utility::functions::nanomina_to_mina};
use crate::{
constants::{MINA_SCALE, MINA_SCALE_DEC},
utility::functions::nanomina_to_mina,
};
use anyhow::anyhow;
use rust_decimal::{prelude::ToPrimitive, Decimal};
use serde::{Deserialize, Serialize};
use std::ops::{Add, Sub};

Expand Down Expand Up @@ -63,6 +68,16 @@ impl From<u64> for Amount {
}
}

impl std::str::FromStr for Amount {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse::<Decimal>()
.map(|amt| Self((amt * MINA_SCALE_DEC).to_u64().expect("currency amount")))
.map_err(|e| anyhow!("{e}"))
}
}

// display

impl std::fmt::Display for Amount {
Expand Down
Loading