Skip to content

Commit

Permalink
runtime: move actions back to near_primitives (#9332)
Browse files Browse the repository at this point in the history
Rather than keeping these types inside near-vm-runner where they don't belong conceptually and interfere with limited replayability due to being schemas for serialization on-chain, we move them to near_primitives and expose the necessary interfaces to the `near-vm-runner` via `Externals`.

With this none of the near-vm-runner code is on-chain schema sensitive, which is one less thing to worry about.

“But wait,” you ask “shouldn’t limited-replayability help with not needing to think about what changes are made in the first place?” Yes, indeed it should be that way, but it isn’t entirely clear to me if we don’t or won’t have intentional or accidental functionality where the data is not serialized, but rather deserialized, even without a need to invoke the runtime to execute the code. This might be as simple a tool as something that shows the stored blockchain structure (e.g. near explorer.)

(But more seriously, I have petitioned that it would make sense to have all schemas part of the limited replayability story as well, but that didn't really gain as much traction as I had hoped, so I’m just punting this problem upwards and away from the runtime crates.)

cc  #8197
  • Loading branch information
nagisa authored and nikurt committed Aug 28, 2023
1 parent 3e17f71 commit 781ea79
Show file tree
Hide file tree
Showing 30 changed files with 1,153 additions and 541 deletions.
2 changes: 2 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 chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ use near_network::types::{
};
use near_o11y::testonly::TracingCapture;
use near_o11y::WithSpanContextExt;
use near_primitives::action::delegate::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::block::{ApprovalInner, Block, GenesisId};
use near_primitives::delegate_action::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::epoch_manager::RngSeed;
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::merkle::{merklize, MerklePath, PartialMerkleTree};
Expand Down
6 changes: 3 additions & 3 deletions chain/rosetta-rpc/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,8 @@ impl TryFrom<Vec<crate::models::Operation>> for NearActions {
.try_set(&intitiate_signed_delegate_action_operation.sender_account)?;

let delegate_action: near_primitives::transaction::Action =
near_primitives::delegate_action::SignedDelegateAction {
delegate_action: near_primitives::delegate_action::DelegateAction {
near_primitives::action::delegate::SignedDelegateAction {
delegate_action: near_primitives::action::delegate::DelegateAction {
sender_id: initiate_delegate_action_operation
.sender_account
.address
Expand Down Expand Up @@ -826,7 +826,7 @@ mod tests {
use near_actix_test_utils::run_actix;
use near_client::test_utils::setup_no_network;
use near_crypto::{KeyType, SecretKey};
use near_primitives::delegate_action::{DelegateAction, SignedDelegateAction};
use near_primitives::action::delegate::{DelegateAction, SignedDelegateAction};
use near_primitives::runtime::config::RuntimeConfig;
use near_primitives::transaction::{Action, TransferAction};
use near_primitives::views::RuntimeConfigView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl TryFrom<crate::models::Operation> for DelegateActionOperation {
}
}

impl From<near_primitives::delegate_action::DelegateAction> for DelegateActionOperation {
fn from(delegate_action: near_primitives::delegate_action::DelegateAction) -> Self {
impl From<near_primitives::action::delegate::DelegateAction> for DelegateActionOperation {
fn from(delegate_action: near_primitives::action::delegate::DelegateAction) -> Self {
DelegateActionOperation {
receiver_id: delegate_action.receiver_id.into(),
max_block_height: delegate_action.max_block_height,
Expand Down
11 changes: 0 additions & 11 deletions core/primitives-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ pub type Compute = u64;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GasWeight(pub u64);

/// Result from a gas distribution among function calls with ratios.
#[must_use]
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum GasDistribution {
/// All remaining gas was distributed to functions.
All,
/// There were no function call actions with a ratio specified.
NoRatios,
}

/// Number of blocks in current group.
pub type NumBlocks = u64;
/// Number of shards in current group.
Expand Down
1 change: 1 addition & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ publish = true

[dependencies]
arbitrary.workspace = true
base64.workspace = true
borsh.workspace = true
bytesize.workspace = true
cfg-if.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! This is the module containing the types introduced for delegate actions.

pub use self::private_non_delegate_action::NonDelegateAction;
use super::action::Action;
use super::signable_message::{SignableMessage, SignableMessageType};
use super::Action;
use crate::signable_message::{SignableMessage, SignableMessageType};
use borsh::{BorshDeserialize, BorshSerialize};
use near_crypto::{PublicKey, Signature};
use near_primitives_core::hash::{hash, CryptoHash};
Expand Down Expand Up @@ -133,7 +133,7 @@ mod private_non_delegate_action {
#[cfg(test)]
mod tests {
use super::*;
use crate::logic::action::CreateAccountAction;
use crate::action::CreateAccountAction;
use near_crypto::KeyType;

/// A serialized `Action::Delegate(SignedDelegateAction)` for testing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod delegate;

use borsh::{BorshDeserialize, BorshSerialize};
use near_crypto::PublicKey;
use near_primitives_core::{
Expand Down Expand Up @@ -174,7 +176,7 @@ pub enum Action {
AddKey(AddKeyAction),
DeleteKey(DeleteKeyAction),
DeleteAccount(DeleteAccountAction),
Delegate(super::delegate_action::SignedDelegateAction),
Delegate(delegate::SignedDelegateAction),
}

impl Action {
Expand Down
3 changes: 2 additions & 1 deletion core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub use near_primitives_core::hash;
pub use near_primitives_core::num_rational;
pub use near_primitives_core::profile;
pub use near_primitives_core::serialize;
pub use near_vm_runner::logic::{delegate_action, signable_message};

pub mod action;
pub mod block;
pub mod block_header;
pub mod challenge;
Expand All @@ -21,6 +21,7 @@ pub mod runtime;
pub mod sandbox;
pub mod shard_layout;
pub mod sharding;
pub mod signable_message;
pub mod state;
pub mod state_part;
pub mod state_record;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod tests {
use near_crypto::{InMemorySigner, KeyType, PublicKey};

use super::*;
use crate::logic::delegate_action::{DelegateAction, SignedDelegateAction};
use crate::action::delegate::{DelegateAction, SignedDelegateAction};

// Note: this is currently a simplified copy of near-primitives::test_utils::create_user_test_signer
// TODO: consider whether it’s worth re-unifying them? it’s test-only code anyway.
Expand All @@ -241,10 +241,7 @@ mod tests {

let delegate_action = delegate_action(sender_id, receiver_id, signer.public_key());
let signable = SignableMessage::new(&delegate_action, SignableMessageType::DelegateAction);
let signed = SignedDelegateAction {
signature: signable.sign(&signer),
delegate_action: delegate_action,
};
let signed = SignedDelegateAction { signature: signable.sign(&signer), delegate_action };

assert!(signed.verify());
}
Expand All @@ -262,10 +259,7 @@ mod tests {
discriminant: MessageDiscriminant::new_on_chain(wrong_nep).unwrap(),
msg: &delegate_action,
};
let signed = SignedDelegateAction {
signature: signable.sign(&signer),
delegate_action: delegate_action,
};
let signed = SignedDelegateAction { signature: signable.sign(&signer), delegate_action };

assert!(!signed.verify());
}
Expand All @@ -282,10 +276,7 @@ mod tests {
// here we use it as an off-chain only signature
let wrong_discriminant = MessageDiscriminant::new_off_chain(correct_nep).unwrap();
let signable = SignableMessage { discriminant: wrong_discriminant, msg: &delegate_action };
let signed = SignedDelegateAction {
signature: signable.sign(&signer),
delegate_action: delegate_action,
};
let signed = SignedDelegateAction { signature: signable.sign(&signer), delegate_action };

assert!(!signed.verify());
}
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};

pub use near_vm_runner::logic::action::{
pub use crate::action::{
Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction,
DeployContractAction, FunctionCallAction, StakeAction, TransferAction,
};
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! type gets changed, the view should preserve the old shape and only re-map the necessary bits
//! from the source structure in the relevant `From<SourceStruct>` impl.
use crate::account::{AccessKey, AccessKeyPermission, Account, FunctionCallPermission};
use crate::action::delegate::{DelegateAction, SignedDelegateAction};
use crate::block::{Block, BlockHeader, Tip};
use crate::block_header::{
BlockHeaderInnerLite, BlockHeaderInnerRest, BlockHeaderInnerRestV2, BlockHeaderInnerRestV3,
Expand All @@ -13,7 +14,6 @@ use crate::block_header::{BlockHeaderInnerRestV4, BlockHeaderV4};
use crate::challenge::{Challenge, ChallengesResult};
use crate::checked_feature;
use crate::contract::ContractCode;
use crate::delegate_action::{DelegateAction, SignedDelegateAction};
use crate::errors::TxExecutionError;
use crate::hash::{hash, CryptoHash};
use crate::merkle::{combine_hash, MerklePath};
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::{future::LocalBoxFuture, FutureExt};
use near_crypto::{PublicKey, Signer};
use near_jsonrpc_primitives::errors::ServerError;
use near_primitives::account::AccessKey;
use near_primitives::delegate_action::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::action::delegate::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::hash::CryptoHash;
use near_primitives::receipt::Receipt;
use near_primitives::test_utils::create_user_test_signer;
Expand Down
Loading

0 comments on commit 781ea79

Please sign in to comment.