Skip to content

Commit

Permalink
[breaking][libra-framework] Refactor writeset prologue and epilogue
Browse files Browse the repository at this point in the history
Closes: #6042
  • Loading branch information
Runtian Zhou authored and bors-libra committed Sep 22, 2020
1 parent 9983c1a commit ce88eb4
Show file tree
Hide file tree
Showing 39 changed files with 385 additions and 581 deletions.
30 changes: 15 additions & 15 deletions json-rpc/tests/integration_test.rs
Expand Up @@ -39,35 +39,35 @@ fn create_test_cases() -> Vec<Test> {
resp.result.unwrap(),
json!([
{
"burn_events_key": "04000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "06000000000000000000000000000000000000000a550c18",
"burn_events_key": "05000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "07000000000000000000000000000000000000000a550c18",
"code": "Coin1",
"exchange_rate_update_events_key": "07000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "08000000000000000000000000000000000000000a550c18",
"fractional_part": 100,
"mint_events_key": "03000000000000000000000000000000000000000a550c18",
"preburn_events_key": "05000000000000000000000000000000000000000a550c18",
"mint_events_key": "04000000000000000000000000000000000000000a550c18",
"preburn_events_key": "06000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 0.5
},
{
"burn_events_key": "09000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "0b000000000000000000000000000000000000000a550c18",
"burn_events_key": "0a000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "0c000000000000000000000000000000000000000a550c18",
"code": "Coin2",
"exchange_rate_update_events_key": "0c000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "0d000000000000000000000000000000000000000a550c18",
"fractional_part": 100,
"mint_events_key": "08000000000000000000000000000000000000000a550c18",
"preburn_events_key": "0a000000000000000000000000000000000000000a550c18",
"mint_events_key": "09000000000000000000000000000000000000000a550c18",
"preburn_events_key": "0b000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 0.5
},
{
"burn_events_key": "0e000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "10000000000000000000000000000000000000000a550c18",
"burn_events_key": "0f000000000000000000000000000000000000000a550c18",
"cancel_burn_events_key": "11000000000000000000000000000000000000000a550c18",
"code": "LBR",
"exchange_rate_update_events_key": "11000000000000000000000000000000000000000a550c18",
"exchange_rate_update_events_key": "12000000000000000000000000000000000000000a550c18",
"fractional_part": 1000,
"mint_events_key": "0d000000000000000000000000000000000000000a550c18",
"preburn_events_key": "0f000000000000000000000000000000000000000a550c18",
"mint_events_key": "0e000000000000000000000000000000000000000a550c18",
"preburn_events_key": "10000000000000000000000000000000000000000a550c18",
"scaling_factor": 1000000,
"to_lbr_exchange_rate": 1.0
}
Expand Down
37 changes: 35 additions & 2 deletions language/e2e-testsuite/src/tests/write_set.rs
Expand Up @@ -10,13 +10,14 @@ use libra_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform};
use libra_types::{
access_path::AccessPath,
account_config::{lbr_type_tag, CORE_CODE_ADDRESS},
chain_id::{ChainId, NamedChain},
contract_event::ContractEvent,
on_chain_config::new_epoch_event_key,
transaction::{
authenticator::AuthenticationKey, ChangeSet, TransactionStatus, WriteSetPayload,
},
vm_status::{KeptVMStatus, StatusCode},
write_set::{WriteOp, WriteSetMut},
write_set::{WriteOp, WriteSet, WriteSetMut},
};
use move_core_types::{
identifier::Identifier,
Expand Down Expand Up @@ -204,7 +205,7 @@ fn bad_writesets() {
*genesis_account.address(),
StructTag {
address: CORE_CODE_ADDRESS,
module: Identifier::new("LibraWriteSetManager").unwrap(),
module: Identifier::new("LibraAccount").unwrap(),
name: Identifier::new("LibraWriteSetManager").unwrap(),
type_params: vec![],
},
Expand Down Expand Up @@ -250,6 +251,38 @@ fn bad_writesets() {
output.status(),
&TransactionStatus::Discard(StatusCode::INVALID_WRITE_SET)
);

// (5) The WriteSet has bad ChainId
let writeset_txn = genesis_account
.transaction()
.write_set(WriteSetPayload::Direct(ChangeSet::new(
WriteSet::default(),
vec![],
)))
.sequence_number(1)
.chain_id(ChainId::new(NamedChain::DEVNET.id()))
.sign();
let output = executor.execute_transaction(writeset_txn);
assert_eq!(
output.status(),
&TransactionStatus::Discard(StatusCode::REJECTED_WRITE_SET)
);

// (6) The WriteSet has expired
let writeset_txn = genesis_account
.transaction()
.write_set(WriteSetPayload::Direct(ChangeSet::new(
WriteSet::default(),
vec![],
)))
.sequence_number(1)
.ttl(0)
.sign();
let output = executor.execute_transaction(writeset_txn);
assert_eq!(
output.status(),
&TransactionStatus::Discard(StatusCode::REJECTED_WRITE_SET)
);
}

#[test]
Expand Down
Expand Up @@ -4,7 +4,7 @@
//! proposer: vivian
//! block-time: 1000000

// check: EventKey([20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: EventKey([21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: NewBlockEvent
// check: 1000000

Expand Down
16 changes: 6 additions & 10 deletions language/libra-vm/src/errors.rs
Expand Up @@ -19,11 +19,7 @@ pub const ETRANSACTION_EXPIRED: u64 = 1006; // transaction expiration time excee
pub const EBAD_CHAIN_ID: u64 = 1007; // chain_id in transaction doesn't match the one on-chain
pub const ESCRIPT_NOT_ALLOWED: u64 = 1008;
pub const EMODULE_NOT_ALLOWED: u64 = 1009;

// writeset prologue sequence nubmer is too new
pub const EWS_PROLOGUE_SEQUENCE_NUMBER_TOO_NEW: u64 = 1011;
// invalid sender (not libra root) for write set
pub const EINVALID_WRITESET_SENDER: u64 = 1033;
pub const EINVALID_WRITESET_SENDER: u64 = 1010; // invalid sender (not libra root) for write set

const INVALID_STATE: u8 = 1;
const INVALID_ARGUMENT: u8 = 7;
Expand Down Expand Up @@ -135,7 +131,7 @@ pub fn convert_write_set_prologue_error(status: VMStatus) -> VMStatus {
match status {
VMStatus::Executed => VMStatus::Executed,
VMStatus::MoveAbort(location, code)
if location != known_locations::write_set_manager_module_abort() =>
if location != known_locations::account_module_abort() =>
{
let (category, reason) = error_split(code);
error!(
Expand All @@ -149,10 +145,10 @@ pub fn convert_write_set_prologue_error(status: VMStatus) -> VMStatus {
VMStatus::MoveAbort(location, code) => match error_split(code) {
(INVALID_ARGUMENT, EINVALID_WRITESET_SENDER)
| (INVALID_ARGUMENT, ESEQUENCE_NUMBER_TOO_OLD)
| (INVALID_ARGUMENT, EWS_PROLOGUE_SEQUENCE_NUMBER_TOO_NEW)
| (INVALID_ARGUMENT, EBAD_ACCOUNT_AUTHENTICATION_KEY) => {
VMStatus::Error(StatusCode::REJECTED_WRITE_SET)
}
| (INVALID_ARGUMENT, ESEQUENCE_NUMBER_TOO_NEW)
| (INVALID_ARGUMENT, EBAD_ACCOUNT_AUTHENTICATION_KEY)
| (INVALID_ARGUMENT, ETRANSACTION_EXPIRED)
| (INVALID_ARGUMENT, EBAD_CHAIN_ID) => VMStatus::Error(StatusCode::REJECTED_WRITE_SET),
(category, reason) => {
error!(
"[libra_vm] Unexpected write set prologue Move abort: {:?}::{:?} \
Expand Down
16 changes: 0 additions & 16 deletions language/libra-vm/src/libra_transaction_executor.rs
Expand Up @@ -448,22 +448,6 @@ impl LibraVM {
return Ok((e, discard_error_output(StatusCode::REJECTED_WRITE_SET)));
};

// Bump the sequence number of sender.
let gas_schedule = zero_cost_schedule();
let mut cost_strategy = CostStrategy::system(&gas_schedule, GasUnits::new(0));

session.execute_function(
&account_config::ACCOUNT_MODULE,
&BUMP_SEQUENCE_NUMBER_NAME,
vec![],
vec![Value::transaction_argument_signer_reference(
txn_data.sender,
)],
txn_data.sender,
&mut cost_strategy,
expect_only_successful_execution(BUMP_SEQUENCE_NUMBER_NAME.as_str()),
)?;

let change_set = match txn.payload() {
TransactionPayload::WriteSet(writeset_payload) => {
match self.execute_writeset(remote_cache, writeset_payload, Some(txn_data.sender()))
Expand Down
10 changes: 8 additions & 2 deletions language/libra-vm/src/libra_vm.rs
Expand Up @@ -335,16 +335,21 @@ impl LibraVMImpl {
) -> Result<(), VMStatus> {
let txn_sequence_number = txn_data.sequence_number();
let txn_public_key = txn_data.authentication_key_preimage().to_vec();
let txn_expiration_timestamp_secs = txn_data.expiration_timestamp_secs();
let chain_id = txn_data.chain_id();

let gas_schedule = zero_cost_schedule();
let mut cost_strategy = CostStrategy::system(&gas_schedule, GasUnits::new(0));
session.execute_function(
&LIBRA_WRITESET_MANAGER_MODULE,
&account_config::ACCOUNT_MODULE,
&WRITESET_PROLOGUE_NAME,
vec![],
vec![
Value::transaction_argument_signer_reference(txn_data.sender),
Value::u64(txn_sequence_number),
Value::vector_u8(txn_public_key),
Value::u64(txn_expiration_timestamp_secs),
Value::u8(chain_id.id()),
],
txn_data.sender,
&mut cost_strategy,
Expand All @@ -365,12 +370,13 @@ impl LibraVMImpl {
let gas_schedule = zero_cost_schedule();
let mut cost_strategy = CostStrategy::system(&gas_schedule, GasUnits::new(0));
session.execute_function(
&LIBRA_WRITESET_MANAGER_MODULE,
&account_config::ACCOUNT_MODULE,
&WRITESET_EPILOGUE_NAME,
vec![],
vec![
Value::transaction_argument_signer_reference(txn_data.sender),
Value::vector_u8(change_set_bytes),
Value::u64(txn_data.sequence_number),
],
txn_data.sender,
&mut cost_strategy,
Expand Down
13 changes: 2 additions & 11 deletions language/libra-vm/src/system_module_names.rs
Expand Up @@ -8,13 +8,6 @@ use once_cell::sync::Lazy;

// Data to resolve basic account and transaction flow functions and structs
/// The ModuleId for the libra writeset manager module
pub static LIBRA_WRITESET_MANAGER_MODULE: Lazy<ModuleId> = Lazy::new(|| {
ModuleId::new(
account_config::CORE_CODE_ADDRESS,
Identifier::new("LibraWriteSetManager").unwrap(),
)
});

/// The ModuleId for the libra block module
pub static LIBRA_BLOCK_MODULE: Lazy<ModuleId> = Lazy::new(|| {
ModuleId::new(
Expand All @@ -29,12 +22,10 @@ pub static SCRIPT_PROLOGUE_NAME: Lazy<Identifier> =
pub static MODULE_PROLOGUE_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("module_prologue").unwrap());
pub static WRITESET_PROLOGUE_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("prologue").unwrap());
Lazy::new(|| Identifier::new("writeset_prologue").unwrap());
pub static WRITESET_EPILOGUE_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("epilogue").unwrap());
Lazy::new(|| Identifier::new("writeset_epilogue").unwrap());
pub static USER_EPILOGUE_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("epilogue").unwrap());
pub static BUMP_SEQUENCE_NUMBER_NAME: Lazy<Identifier> =
Lazy::new(|| Identifier::new("bump_sequence_number").unwrap());
pub static BLOCK_PROLOGUE: Lazy<Identifier> =
Lazy::new(|| Identifier::new("block_prologue").unwrap());
16 changes: 0 additions & 16 deletions language/move-core/types/src/vm_status.rs
Expand Up @@ -353,22 +353,6 @@ pub mod known_locations {
pub fn designated_dealer_module_abort() -> AbortLocation {
AbortLocation::Module(DESIGNATED_DEALER_MODULE.clone())
}

/// The name of the Write Set Manager module.
pub const WRITE_SET_MANAGER_MODULE_NAME: &str = "LibraWriteSetManager";
/// The Identifier for the Write Set Manager module.
pub static WRITE_SET_MANAGER_MODULE_IDENTIFIER: Lazy<Identifier> =
Lazy::new(|| Identifier::new(WRITE_SET_MANAGER_MODULE_NAME).unwrap());
/// The ModuleId for the Write Set Manager module.
pub static WRITE_SET_MANAGER_MODULE: Lazy<ModuleId> = Lazy::new(|| {
ModuleId::new(
CORE_CODE_ADDRESS,
WRITE_SET_MANAGER_MODULE_IDENTIFIER.clone(),
)
});
pub fn write_set_manager_module_abort() -> AbortLocation {
AbortLocation::Module(WRITE_SET_MANAGER_MODULE.clone())
}
}

macro_rules! derive_status_try_from_repr {
Expand Down
Expand Up @@ -4,7 +4,7 @@
//! proposer: vivian
//! block-time: 1000000

// check: EventKey([20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: EventKey([21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 85, 12, 24])
// check: NewBlockEvent
// check: 1000000

Expand Down

This file was deleted.

Binary file not shown.
Binary file removed language/stdlib/compiled/stdlib/032_LibraAccount.mv
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added language/stdlib/compiled/stdlib/037_Genesis.mv
Binary file not shown.
Binary file removed language/stdlib/compiled/stdlib/038_Genesis.mv
Binary file not shown.
2 changes: 0 additions & 2 deletions language/stdlib/modules/Genesis.move
Expand Up @@ -18,7 +18,6 @@ module Genesis {
use 0x1::LibraTimestamp;
use 0x1::LibraTransactionPublishingOption;
use 0x1::LibraVersion;
use 0x1::LibraWriteSetManager;
use 0x1::TransactionFee;
use 0x1::LibraVMConfig;

Expand Down Expand Up @@ -72,7 +71,6 @@ module Genesis {
lr_account,
);
LibraBlock::initialize_block_metadata(lr_account);
LibraWriteSetManager::initialize(lr_account);

let lr_rotate_key_cap = LibraAccount::extract_key_rotation_capability(lr_account);
LibraAccount::rotate_authentication_key(&lr_rotate_key_cap, lr_auth_key);
Expand Down

0 comments on commit ce88eb4

Please sign in to comment.