Skip to content

Commit 6768958

Browse files
committed
Add bootloader initial gas to vm
1 parent 0424f0e commit 6768958

File tree

1 file changed

+144
-143
lines changed
  • core/lib/multivm/src/versions/era_vm

1 file changed

+144
-143
lines changed

core/lib/multivm/src/versions/era_vm/vm.rs

Lines changed: 144 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
9292
.to_fixed_bytes(),
9393
vm_hook_position,
9494
true,
95+
system_env.bootloader_gas_limit,
9596
);
9697
let pre_contract_storage = Rc::new(RefCell::new(HashMap::new()));
9798
pre_contract_storage.borrow_mut().insert(
@@ -315,11 +316,11 @@ impl<S: ReadStorage + 'static> Vm<S> {
315316
heap.read((word * 32) as u32)
316317
}
317318

318-
#[cfg(test)]
319-
/// Returns the current state of the VM in a format that can be compared for equality.
320-
pub(crate) fn dump_state(&self) -> Execution {
321-
self.inner.execution.clone()
322-
}
319+
// #[cfg(test)]
320+
// /// Returns the current state of the VM in a format that can be compared for equality.
321+
// pub(crate) fn dump_state(&self) -> Execution {
322+
// self.inner.execution.clone()
323+
// }
323324

324325
fn write_to_bootloader_heap(&mut self, memory: impl IntoIterator<Item = (usize, U256)>) {
325326
assert!(self.inner.execution.running_contexts.len() == 1); // No on-going far calls
@@ -564,141 +565,141 @@ impl<S: ReadStorage> era_vm::store::Storage for World<S> {
564565
}
565566
}
566567

567-
#[cfg(test)]
568-
mod tests {
569-
use std::{cell::RefCell, path::PathBuf, rc::Rc};
570-
571-
use once_cell::sync::Lazy;
572-
use zksync_contracts::{deployer_contract, BaseSystemContracts};
573-
use zksync_state::{InMemoryStorage, StorageView};
574-
use zksync_types::{
575-
block::L2BlockHasher,
576-
ethabi::{encode, Token},
577-
fee::Fee,
578-
fee_model::BatchFeeInput,
579-
helpers::unix_timestamp_ms,
580-
l2::L2Tx,
581-
utils::storage_key_for_eth_balance,
582-
Address, K256PrivateKey, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId,
583-
Transaction, CONTRACT_DEPLOYER_ADDRESS, H256, U256,
584-
};
585-
use zksync_utils::bytecode::hash_bytecode;
586-
587-
use super::*;
588-
use crate::{
589-
era_vm::vm::Vm,
590-
interface::{L2BlockEnv, TxExecutionMode, VmExecutionMode, VmInterface},
591-
utils::get_max_gas_per_pubdata_byte,
592-
vm_latest::constants::BATCH_COMPUTATIONAL_GAS_LIMIT,
593-
};
594-
/// Bytecodes have consist of an odd number of 32 byte words
595-
/// This function "fixes" bytecodes of wrong length by cutting off their end.
596-
pub fn cut_to_allowed_bytecode_size(bytes: &[u8]) -> Option<&[u8]> {
597-
let mut words = bytes.len() / 32;
598-
if words == 0 {
599-
return None;
600-
}
601-
if words & 1 == 0 {
602-
words -= 1;
603-
}
604-
Some(&bytes[..32 * words])
605-
}
606-
607-
static PRIVATE_KEY: Lazy<K256PrivateKey> =
608-
Lazy::new(|| K256PrivateKey::from_bytes(H256([42; 32])).expect("invalid key bytes"));
609-
static SYSTEM_CONTRACTS: Lazy<BaseSystemContracts> =
610-
Lazy::new(BaseSystemContracts::load_from_disk);
611-
static STORAGE: Lazy<InMemoryStorage> = Lazy::new(|| {
612-
let mut storage = InMemoryStorage::with_system_contracts(hash_bytecode);
613-
614-
// Give `PRIVATE_KEY` some money
615-
let key = storage_key_for_eth_balance(&PRIVATE_KEY.address());
616-
storage.set_value(key, zksync_utils::u256_to_h256(U256([0, 0, 1, 0])));
617-
618-
storage
619-
});
620-
static CREATE_FUNCTION_SIGNATURE: Lazy<[u8; 4]> = Lazy::new(|| {
621-
deployer_contract()
622-
.function("create")
623-
.unwrap()
624-
.short_signature()
625-
});
626-
627-
pub fn get_deploy_tx(code: &[u8]) -> Transaction {
628-
let params = [
629-
Token::FixedBytes(vec![0u8; 32]),
630-
Token::FixedBytes(hash_bytecode(code).0.to_vec()),
631-
Token::Bytes([].to_vec()),
632-
];
633-
let calldata = CREATE_FUNCTION_SIGNATURE
634-
.iter()
635-
.cloned()
636-
.chain(encode(&params))
637-
.collect();
638-
639-
let mut signed = L2Tx::new_signed(
640-
CONTRACT_DEPLOYER_ADDRESS,
641-
calldata,
642-
Nonce(0),
643-
Fee {
644-
gas_limit: U256::from(30000000u32),
645-
max_fee_per_gas: U256::from(250_000_000),
646-
max_priority_fee_per_gas: U256::from(0),
647-
gas_per_pubdata_limit: U256::from(get_max_gas_per_pubdata_byte(
648-
ProtocolVersionId::latest().into(),
649-
)),
650-
},
651-
U256::zero(),
652-
L2ChainId::from(270),
653-
&PRIVATE_KEY,
654-
vec![code.to_vec()], // maybe not needed?
655-
Default::default(),
656-
)
657-
.expect("should create a signed execute transaction");
658-
659-
signed.set_input(H256::random().as_bytes().to_vec(), H256::random());
660-
661-
signed.into()
662-
}
663-
664-
#[test]
665-
fn test_vm() {
666-
let path = PathBuf::from("./src/versions/era_vm/test_contract/storage");
667-
let test_contract = std::fs::read(&path).expect("failed to read file");
668-
let code = cut_to_allowed_bytecode_size(&test_contract).unwrap();
669-
let tx = get_deploy_tx(code);
670-
let timestamp = unix_timestamp_ms();
671-
let mut vm = Vm::new(
672-
crate::interface::L1BatchEnv {
673-
previous_batch_hash: None,
674-
number: L1BatchNumber(1),
675-
timestamp,
676-
fee_input: BatchFeeInput::l1_pegged(
677-
50_000_000_000, // 50 gwei
678-
250_000_000, // 0.25 gwei
679-
),
680-
fee_account: Address::random(),
681-
enforced_base_fee: None,
682-
first_l2_block: L2BlockEnv {
683-
number: 1,
684-
timestamp,
685-
prev_block_hash: L2BlockHasher::legacy_hash(L2BlockNumber(0)),
686-
max_virtual_blocks_to_create: 100,
687-
},
688-
},
689-
crate::interface::SystemEnv {
690-
zk_porter_available: false,
691-
version: ProtocolVersionId::latest(),
692-
base_system_smart_contracts: SYSTEM_CONTRACTS.clone(),
693-
bootloader_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
694-
execution_mode: TxExecutionMode::VerifyExecute,
695-
default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
696-
chain_id: L2ChainId::from(270),
697-
},
698-
Rc::new(RefCell::new(StorageView::new(&*STORAGE))),
699-
);
700-
vm.push_transaction(tx);
701-
let a = vm.execute(VmExecutionMode::OneTx);
702-
println!("{:?}", a.result);
703-
}
704-
}
568+
// #[cfg(test)]
569+
// mod tests {
570+
// use std::{cell::RefCell, path::PathBuf, rc::Rc};
571+
572+
// use once_cell::sync::Lazy;
573+
// use zksync_contracts::{deployer_contract, BaseSystemContracts};
574+
// use zksync_state::{InMemoryStorage, StorageView};
575+
// use zksync_types::{
576+
// block::L2BlockHasher,
577+
// ethabi::{encode, Token},
578+
// fee::Fee,
579+
// fee_model::BatchFeeInput,
580+
// helpers::unix_timestamp_ms,
581+
// l2::L2Tx,
582+
// utils::storage_key_for_eth_balance,
583+
// Address, K256PrivateKey, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId,
584+
// Transaction, CONTRACT_DEPLOYER_ADDRESS, H256, U256,
585+
// };
586+
// use zksync_utils::bytecode::hash_bytecode;
587+
588+
// use super::*;
589+
// use crate::{
590+
// era_vm::vm::Vm,
591+
// interface::{L2BlockEnv, TxExecutionMode, VmExecutionMode, VmInterface},
592+
// utils::get_max_gas_per_pubdata_byte,
593+
// vm_latest::constants::BATCH_COMPUTATIONAL_GAS_LIMIT,
594+
// };
595+
// /// Bytecodes have consist of an odd number of 32 byte words
596+
// /// This function "fixes" bytecodes of wrong length by cutting off their end.
597+
// pub fn cut_to_allowed_bytecode_size(bytes: &[u8]) -> Option<&[u8]> {
598+
// let mut words = bytes.len() / 32;
599+
// if words == 0 {
600+
// return None;
601+
// }
602+
// if words & 1 == 0 {
603+
// words -= 1;
604+
// }
605+
// Some(&bytes[..32 * words])
606+
// }
607+
608+
// static PRIVATE_KEY: Lazy<K256PrivateKey> =
609+
// Lazy::new(|| K256PrivateKey::from_bytes(H256([42; 32])).expect("invalid key bytes"));
610+
// static SYSTEM_CONTRACTS: Lazy<BaseSystemContracts> =
611+
// Lazy::new(BaseSystemContracts::load_from_disk);
612+
// static STORAGE: Lazy<InMemoryStorage> = Lazy::new(|| {
613+
// let mut storage = InMemoryStorage::with_system_contracts(hash_bytecode);
614+
615+
// // Give `PRIVATE_KEY` some money
616+
// let key = storage_key_for_eth_balance(&PRIVATE_KEY.address());
617+
// storage.set_value(key, zksync_utils::u256_to_h256(U256([0, 0, 1, 0])));
618+
619+
// storage
620+
// });
621+
// static CREATE_FUNCTION_SIGNATURE: Lazy<[u8; 4]> = Lazy::new(|| {
622+
// deployer_contract()
623+
// .function("create")
624+
// .unwrap()
625+
// .short_signature()
626+
// });
627+
628+
// pub fn get_deploy_tx(code: &[u8]) -> Transaction {
629+
// let params = [
630+
// Token::FixedBytes(vec![0u8; 32]),
631+
// Token::FixedBytes(hash_bytecode(code).0.to_vec()),
632+
// Token::Bytes([].to_vec()),
633+
// ];
634+
// let calldata = CREATE_FUNCTION_SIGNATURE
635+
// .iter()
636+
// .cloned()
637+
// .chain(encode(&params))
638+
// .collect();
639+
640+
// let mut signed = L2Tx::new_signed(
641+
// CONTRACT_DEPLOYER_ADDRESS,
642+
// calldata,
643+
// Nonce(0),
644+
// Fee {
645+
// gas_limit: U256::from(30000000u32),
646+
// max_fee_per_gas: U256::from(250_000_000),
647+
// max_priority_fee_per_gas: U256::from(0),
648+
// gas_per_pubdata_limit: U256::from(get_max_gas_per_pubdata_byte(
649+
// ProtocolVersionId::latest().into(),
650+
// )),
651+
// },
652+
// U256::zero(),
653+
// L2ChainId::from(270),
654+
// &PRIVATE_KEY,
655+
// vec![code.to_vec()], // maybe not needed?
656+
// Default::default(),
657+
// )
658+
// .expect("should create a signed execute transaction");
659+
660+
// signed.set_input(H256::random().as_bytes().to_vec(), H256::random());
661+
662+
// signed.into()
663+
// }
664+
665+
// #[test]
666+
// fn test_vm() {
667+
// let path = PathBuf::from("./src/versions/era_vm/test_contract/storage");
668+
// let test_contract = std::fs::read(&path).expect("failed to read file");
669+
// let code = cut_to_allowed_bytecode_size(&test_contract).unwrap();
670+
// let tx = get_deploy_tx(code);
671+
// let timestamp = unix_timestamp_ms();
672+
// let mut vm = Vm::new(
673+
// crate::interface::L1BatchEnv {
674+
// previous_batch_hash: None,
675+
// number: L1BatchNumber(1),
676+
// timestamp,
677+
// fee_input: BatchFeeInput::l1_pegged(
678+
// 50_000_000_000, // 50 gwei
679+
// 250_000_000, // 0.25 gwei
680+
// ),
681+
// fee_account: Address::random(),
682+
// enforced_base_fee: None,
683+
// first_l2_block: L2BlockEnv {
684+
// number: 1,
685+
// timestamp,
686+
// prev_block_hash: L2BlockHasher::legacy_hash(L2BlockNumber(0)),
687+
// max_virtual_blocks_to_create: 100,
688+
// },
689+
// },
690+
// crate::interface::SystemEnv {
691+
// zk_porter_available: false,
692+
// version: ProtocolVersionId::latest(),
693+
// base_system_smart_contracts: SYSTEM_CONTRACTS.clone(),
694+
// bootloader_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
695+
// execution_mode: TxExecutionMode::VerifyExecute,
696+
// default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
697+
// chain_id: L2ChainId::from(270),
698+
// },
699+
// Rc::new(RefCell::new(StorageView::new(&*STORAGE))),
700+
// );
701+
// vm.push_transaction(tx);
702+
// let a = vm.execute(VmExecutionMode::OneTx);
703+
// println!("{:?}", a.result);
704+
// }
705+
// }

0 commit comments

Comments
 (0)