Skip to content

Commit

Permalink
[rooch-networkgh-771] support simple on-chain random number.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Oct 27, 2023
1 parent d61d644 commit fdec613
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ExecutorActor {
let resolved_sender = self.resolve_or_generate(multi_chain_address_sender.clone())?;
let authenticator = tx.authenticator_info()?;

let mut moveos_tx = tx.construct_moveos_transaction(resolved_sender)?;
let mut moveos_tx = tx.construct_moveos_transaction(resolved_sender, H256::zero())?; // TODO: tx accumulator root

let vm_result = self.validate_authenticator(&moveos_tx.ctx, authenticator)?;

Expand Down
1 change: 1 addition & 0 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl GenesisPackage {
RoochTransaction::new_genesis_tx(
genesis_account.into(),
genesis_ctx.chain_id,
H256::zero(), // TODO tx accumulator root
MoveAction::ModuleBundle(bundle),
)
})
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-relayer/src/actor/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Relayer, TxSubmiter};
use anyhow::Result;
use async_trait::async_trait;
use coerce::actor::{context::ActorContext, message::Handler, Actor};
use moveos_types::{gas_config::GasConfig, transaction::MoveAction};
use moveos_types::{gas_config::GasConfig, h256::H256, transaction::MoveAction};
use rooch_rpc_api::jsonrpc_types::KeptVMStatusView;
use rooch_rpc_client::ClientBuilder;
use rooch_types::{
Expand Down Expand Up @@ -71,6 +71,7 @@ impl RelayerActor {
sequence_number,
self.chain_id,
self.max_gas_amount,
H256::zero(), // TODO: accumulator root
action,
);
let tx = tx_data.sign(&self.relayer_key);
Expand Down
2 changes: 2 additions & 0 deletions crates/rooch-rpc-client/src/wallet_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::Client;
use anyhow::anyhow;
use move_core_types::account_address::AccountAddress;
use moveos_types::gas_config::GasConfig;
use moveos_types::h256::H256;
use moveos_types::transaction::MoveAction;
use rooch_config::config::{Config, PersistedConfig};
use rooch_config::server_config::ServerConfig;
Expand Down Expand Up @@ -122,6 +123,7 @@ impl WalletContext {
sequence_number,
chain_id,
GasConfig::DEFAULT_MAX_GAS_AMOUNT,
H256::zero(), // TODO: tx accumulator root
action,
);
Ok(tx_data)
Expand Down
9 changes: 8 additions & 1 deletion crates/rooch-types/src/transaction/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,18 @@ impl AbstractTransaction for EthereumTransactionData {
fn construct_moveos_transaction(
self,
resolved_sender: AccountAddress,
tx_accumulator_root: H256,
) -> Result<MoveOSTransaction> {
let action = self.decode_calldata_to_action()?;
let sequence_number = self.0.nonce.as_u64();
let gas = self.0.gas.as_u64();
let tx_ctx = TxContext::new(resolved_sender, sequence_number, gas, self.tx_hash());
let tx_ctx = TxContext::new(
resolved_sender,
sequence_number,
gas,
self.tx_hash(),
tx_accumulator_root,
);
Ok(MoveOSTransaction::new(tx_ctx, action))
}

Expand Down
10 changes: 8 additions & 2 deletions crates/rooch-types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub trait AbstractTransaction {
fn construct_moveos_transaction(
self,
resolved_sender: AccountAddress,
tx_accumulator_root: H256,
) -> Result<MoveOSTransaction>;
}

Expand Down Expand Up @@ -134,10 +135,15 @@ impl AbstractTransaction for TypedTransaction {
fn construct_moveos_transaction(
self,
resolved_sender: AccountAddress,
tx_accumulator_root: H256,
) -> Result<moveos_types::transaction::MoveOSTransaction> {
match self {
TypedTransaction::Rooch(tx) => tx.construct_moveos_transaction(resolved_sender),
TypedTransaction::Ethereum(tx) => tx.construct_moveos_transaction(resolved_sender),
TypedTransaction::Rooch(tx) => {
tx.construct_moveos_transaction(resolved_sender, tx_accumulator_root)
}
TypedTransaction::Ethereum(tx) => {
tx.construct_moveos_transaction(resolved_sender, tx_accumulator_root)
}
}
}

Expand Down
32 changes: 29 additions & 3 deletions crates/rooch-types/src/transaction/rooch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct RoochTransactionData {
pub chain_id: u64,
// The max gas to be used.
pub max_gas_amount: u64,
/// The transaction accumulator root.
pub tx_accumulator_root: H256,
// The MoveAction to execute.
pub action: MoveAction,
}
Expand All @@ -37,23 +39,31 @@ impl RoochTransactionData {
sequence_number: u64,
chain_id: u64,
max_gas_amount: u64,
tx_accumulator_root: H256,
action: MoveAction,
) -> Self {
Self {
sender,
sequence_number,
chain_id,
max_gas_amount,
tx_accumulator_root,
action,
}
}

pub fn new_for_test(sender: RoochAddress, sequence_number: u64, action: MoveAction) -> Self {
pub fn new_for_test(
sender: RoochAddress,
sequence_number: u64,
tx_accumulator_root: H256,
action: MoveAction,
) -> Self {
Self {
sender,
sequence_number,
chain_id: RoochChainID::LOCAL.chain_id().id(),
max_gas_amount: GasConfig::DEFAULT_MAX_GAS_AMOUNT,
tx_accumulator_root,
action,
}
}
Expand Down Expand Up @@ -91,10 +101,18 @@ impl RoochTransaction {
pub fn new_genesis_tx(
genesis_address: RoochAddress,
chain_id: u64,
tx_accumulator_root: H256,
action: MoveAction,
) -> Self {
Self {
data: RoochTransactionData::new(genesis_address, 0, chain_id, u64::max_value(), action),
data: RoochTransactionData::new(
genesis_address,
0,
chain_id,
u64::max_value(),
tx_accumulator_root,
action,
),
authenticator: Authenticator::rooch(Signature::Ed25519RoochSignature(
Ed25519RoochSignature::default(),
)),
Expand Down Expand Up @@ -132,6 +150,7 @@ impl RoochTransaction {

let sender: RoochAddress = RoochAddress::random();
let sequence_number = 0;
let tx_accumulator_root = H256::zero();
let payload = MoveAction::new_function_call(
FunctionId::new(
ModuleId::new(AccountAddress::random(), Identifier::new("test").unwrap()),
Expand All @@ -141,7 +160,12 @@ impl RoochTransaction {
vec![],
);

let transaction_data = RoochTransactionData::new_for_test(sender, sequence_number, payload);
let transaction_data = RoochTransactionData::new_for_test(
sender,
sequence_number,
tx_accumulator_root,
payload,
);
let mut rng = rand::thread_rng();
let ed25519_keypair: Ed25519KeyPair = Ed25519KeyPair::generate(&mut rng);
let auth =
Expand All @@ -158,6 +182,7 @@ impl From<RoochTransaction> for MoveOSTransaction {
tx.data.sequence_number,
tx.data.max_gas_amount,
tx_hash,
tx.data.tx_accumulator_root,
);
MoveOSTransaction::new(tx_ctx, tx.data.action)
}
Expand Down Expand Up @@ -195,6 +220,7 @@ impl AbstractTransaction for RoochTransaction {
fn construct_moveos_transaction(
self,
resolved_sender: AccountAddress,
_tx_accumulator_root: H256,
) -> Result<MoveOSTransaction> {
debug_assert!(self.sender() == resolved_sender.into());
Ok(self.into())
Expand Down
17 changes: 16 additions & 1 deletion moveos/moveos-types/src/moveos_std/tx_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct TxContext {
pub tx_hash: Vec<u8>,
/// Number of `ObjectID`'s generated during execution of the current transaction
pub ids_created: u64,
/// TODO: tx_accumulator_root
pub tx_accumulator_root: Vec<u8>,
/// A map for storing context data
pub map: SimpleMap<MoveString, Any>,
}
Expand All @@ -45,6 +47,7 @@ impl std::fmt::Debug for TxContext {
.field("max_gas_amount", &self.max_gas_amount)
.field("tx_hash", &self.tx_hash)
.field("ids_created", &self.ids_created)
.field("tx_accumulator_root", &self.tx_accumulator_root)
.field("map", &self.map)
.finish()
}
Expand All @@ -56,21 +59,29 @@ impl TxContext {
sequence_number: u64,
max_gas_amount: u64,
tx_hash: H256,
tx_accumulator_root: H256,
) -> Self {
Self {
sender,
sequence_number,
max_gas_amount,
tx_hash: tx_hash.0.to_vec(),
ids_created: 0,
tx_accumulator_root: tx_accumulator_root.0.to_vec(),
map: SimpleMap::create(),
}
}

/// Create a new TxContext with a zero tx_hash for read-only function call cases
pub fn new_readonly_ctx(sender: AccountAddress) -> Self {
//TODO define read-only function gas limit
Self::new(sender, 0, GasConfig::DEFAULT_MAX_GAS_AMOUNT, H256::zero())
Self::new(
sender,
0,
GasConfig::DEFAULT_MAX_GAS_AMOUNT,
H256::zero(),
H256::zero(),
)
}

/// Spawn a new TxContext with a new `ids_created` counter and empty map
Expand All @@ -81,6 +92,7 @@ impl TxContext {
max_gas_amount: self.max_gas_amount,
tx_hash: self.tx_hash,
ids_created: 0,
tx_accumulator_root: self.tx_accumulator_root,
map: env,
}
}
Expand All @@ -94,6 +106,7 @@ impl TxContext {
max_gas_amount: GasConfig::DEFAULT_MAX_GAS_AMOUNT,
tx_hash: vec![0u8; h256::LENGTH],
ids_created: 0,
tx_accumulator_root: vec![0u8; h256::LENGTH],
map: SimpleMap::create(),
}
}
Expand Down Expand Up @@ -126,6 +139,7 @@ impl TxContext {
0,
GasConfig::DEFAULT_MAX_GAS_AMOUNT,
H256::random(),
H256::random(),
)
}

Expand Down Expand Up @@ -165,6 +179,7 @@ impl MoveStructState for TxContext {
MoveTypeLayout::U64,
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
MoveTypeLayout::U64,
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
MoveTypeLayout::Struct(SimpleMap::<MoveString, Any>::struct_layout()),
])
}
Expand Down
1 change: 1 addition & 0 deletions moveos/moveos-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl MoveOSTransaction {
0,
GasConfig::DEFAULT_MAX_GAS_AMOUNT,
tx_hash,
H256::zero(), // TODO: tx accumulator root
);
Self::new(ctx, sender_and_action.1)
}
Expand Down

0 comments on commit fdec613

Please sign in to comment.