Skip to content

Commit

Permalink
[types] add ChainId placeholder field in user txn
Browse files Browse the repository at this point in the history
  • Loading branch information
Christina Lee committed Jul 8, 2020
1 parent 5710293 commit 01bd851
Show file tree
Hide file tree
Showing 33 changed files with 191 additions and 34 deletions.
11 changes: 11 additions & 0 deletions client/libra-dev/src/transaction.rs
Expand Up @@ -20,6 +20,7 @@ use libra_types::{
account_config::{
from_currency_code_string, lbr_type_tag, type_tag_for_currency_code, LBR_NAME,
},
chain_id::ChainId,
transaction::{
authenticator::AuthenticationKey, helpers::TransactionSigner, RawTransaction, Script,
SignedTransaction, TransactionArgument, TransactionPayload,
Expand All @@ -40,6 +41,7 @@ pub unsafe extern "C" fn libra_SignedTransactionBytes_from(
gas_unit_price: u64,
gas_identifier: *const i8,
expiration_time_secs: u64,
chain_id: u64,
script_bytes: *const u8,
script_len: usize,
ptr_buf: *mut *mut u8,
Expand Down Expand Up @@ -90,6 +92,7 @@ pub unsafe extern "C" fn libra_SignedTransactionBytes_from(
.to_string_lossy()
.into_owned(),
expiration_time,
ChainId::new(chain_id),
);

let keypair = KeyPair::from(private_key);
Expand Down Expand Up @@ -338,6 +341,7 @@ pub unsafe extern "C" fn libra_RawTransactionBytes_from(
sender: *const u8,
receiver: *const u8,
sequence: u64,
chain_id: u64,
num_coins: u64,
max_gas_amount: u64,
gas_unit_price: u64,
Expand Down Expand Up @@ -405,6 +409,7 @@ pub unsafe extern "C" fn libra_RawTransactionBytes_from(
gas_unit_price,
LBR_NAME.to_owned(),
expiration_time,
ChainId::new(chain_id),
);

let raw_txn_bytes = match to_bytes(&raw_txn) {
Expand Down Expand Up @@ -649,6 +654,7 @@ mod test {
use libra_crypto::{PrivateKey, SigningKey, Uniform};
use libra_types::{
account_config::COIN1_NAME,
chain_id::MOCK_CHAIN_ID,
transaction::{SignedTransaction, TransactionArgument},
};
use move_core_types::language_storage::TypeTag;
Expand Down Expand Up @@ -709,6 +715,7 @@ mod test {
gas_unit_price,
coin_ident.as_ptr(),
expiration_time_secs,
MOCK_CHAIN_ID,
script_bytes.as_ptr(),
script_len,
buf_ptr,
Expand Down Expand Up @@ -758,6 +765,7 @@ mod test {
gas_unit_price,
coin_idnet.as_ptr(),
expiration_time_secs,
MOCK_CHAIN_ID,
script_bytes.as_ptr(),
script_len,
buf_ptr2,
Expand Down Expand Up @@ -806,6 +814,7 @@ mod test {
gas_unit_price,
coin_ident_2.as_ptr(),
expiration_time_secs,
MOCK_CHAIN_ID,
script_bytes.as_ptr(),
script_len,
buf_ptr,
Expand Down Expand Up @@ -1046,6 +1055,7 @@ mod test {
sender_address.as_ref().as_ptr(),
receiver_address.as_ref().as_ptr(),
sequence,
MOCK_CHAIN_ID,
amount,
max_gas_amount,
gas_unit_price,
Expand Down Expand Up @@ -1140,6 +1150,7 @@ mod test {
gas_unit_price,
LBR_NAME.to_owned(),
Duration::from_secs(expiration_time_secs),
ChainId::mock(),
),
public_key.clone(),
signature.clone(),
Expand Down
10 changes: 8 additions & 2 deletions client/swiss-knife/src/main.rs
Expand Up @@ -6,8 +6,11 @@ use libra_crypto::{
test_utils::KeyPair,
Signature, SigningKey, Uniform, ValidCryptoMaterialStringExt,
};
use libra_types::transaction::{
authenticator::AuthenticationKey, RawTransaction, SignedTransaction, TransactionPayload,
use libra_types::{
chain_id::ChainId,
transaction::{
authenticator::AuthenticationKey, RawTransaction, SignedTransaction, TransactionPayload,
},
};
use rand::{prelude::StdRng, SeedableRng};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -114,6 +117,8 @@ struct TxnParams {
pub sender_address: String,
// Sequence number of this transaction corresponding to sender's account.
pub sequence_number: u64,
// Chain ID of the network this transaction is intended for
pub chain_id: u64,
// Maximal total gas specified by wallet to spend for this transaction.
pub max_gas_amount: u64,
// Maximal price can be paid per gas.
Expand Down Expand Up @@ -190,6 +195,7 @@ fn generate_raw_txn(g: GenerateRawTxnRequest) -> GenerateRawTxnResponse {
g.txn_params.gas_unit_price,
g.txn_params.gas_currency_code,
std::time::Duration::new(g.txn_params.expiration_timestamp, 0),
ChainId::new(g.txn_params.chain_id),
);
GenerateRawTxnResponse {
raw_txn: hex::encode(
Expand Down
4 changes: 4 additions & 0 deletions config/management/src/validator_config.rs
Expand Up @@ -17,6 +17,7 @@ use libra_secure_storage::{CryptoStorage, KVStorage, Storage, Value};
use libra_secure_time::{RealTimeService, TimeService};
use libra_types::{
account_address::{self, AccountAddress},
chain_id::ChainId,
transaction::{RawTransaction, SignedTransaction, Transaction},
};
use std::{convert::TryFrom, time::Duration};
Expand All @@ -33,6 +34,8 @@ pub struct ValidatorConfig {
fullnode_address: NetworkAddress,
#[structopt(flatten)]
backends: SecureBackends,
#[structopt(long)]
chain_id: ChainId,
}

impl ValidatorConfig {
Expand Down Expand Up @@ -90,6 +93,7 @@ impl ValidatorConfig {
constants::GAS_UNIT_PRICE,
constants::GAS_CURRENCY_CODE.to_owned(),
Duration::from_secs(expiration_time),
self.chain_id,
);
let signature = local_storage
.sign(OPERATOR_KEY, &raw_transaction)
Expand Down
16 changes: 16 additions & 0 deletions config/src/config/key_manager_config.rs
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::config::{Error, LoggerConfig, PersistableConfig, SecureBackend};
use libra_types::chain_id::ChainId;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

Expand All @@ -21,6 +22,7 @@ pub struct KeyManagerConfig {
pub secure_backend: SecureBackend,
pub sleep_period_secs: u64,
pub txn_expiration_secs: u64,
pub chain_id: ChainId,
}

impl Default for KeyManagerConfig {
Expand All @@ -32,6 +34,7 @@ impl Default for KeyManagerConfig {
secure_backend: SecureBackend::InMemoryStorage,
sleep_period_secs: DEFAULT_SLEEP_PERIOD_SECS,
txn_expiration_secs: DEFAULT_TXN_EXPIRATION_SECS,
chain_id: ChainId::mainnet(),
}
}
}
Expand All @@ -53,4 +56,17 @@ impl KeyManagerConfig {
backend.set_data_dir(data_dir);
}
}

#[cfg(any(test, feature = "fuzzing"))]
pub fn test() -> Self {
Self {
json_rpc_endpoint: DEFAULT_JSON_RPC_ENDPOINT.into(),
logger: LoggerConfig::default(),
rotation_period_secs: DEFAULT_ROTATION_PERIOD_SECS,
secure_backend: SecureBackend::InMemoryStorage,
sleep_period_secs: DEFAULT_SLEEP_PERIOD_SECS,
txn_expiration_secs: DEFAULT_TXN_EXPIRATION_SECS,
chain_id: ChainId::mock(),
}
}
}
2 changes: 2 additions & 0 deletions execution/executor-benchmark/src/lib.rs
Expand Up @@ -16,6 +16,7 @@ use libra_types::{
coin1_tag, libra_root_address, testnet_dd_account_address, AccountResource, COIN1_NAME,
},
block_info::BlockInfo,
chain_id::ChainId,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
transaction::{
authenticator::AuthenticationKey, RawTransaction, Script, SignedTransaction, Transaction,
Expand Down Expand Up @@ -374,6 +375,7 @@ fn create_transaction(
0, /* gas_unit_price */
COIN1_NAME.to_owned(), /* gas_currency_code */
expiration_time,
ChainId::mock(),
);

let signature = private_key.sign(&raw_txn);
Expand Down
7 changes: 5 additions & 2 deletions execution/executor/src/mock_vm/mod.rs
Expand Up @@ -9,7 +9,8 @@ use libra_state_view::StateView;
use libra_types::{
access_path::AccessPath,
account_address::AccountAddress,
account_config::{libra_root_address, validator_set_address, LBR_NAME},
account_config::{association_address, libra_root_address, validator_set_address, LBR_NAME},
chain_id::ChainId,
contract_event::ContractEvent,
event::EventKey,
on_chain_config::{
Expand Down Expand Up @@ -315,6 +316,7 @@ fn encode_transaction(sender: AccountAddress, program: Script) -> Transaction {
0,
LBR_NAME.to_owned(),
std::time::Duration::from_secs(0),
ChainId::mock(),
);

let privkey = Ed25519PrivateKey::generate_for_testing();
Expand All @@ -327,7 +329,8 @@ fn encode_transaction(sender: AccountAddress, program: Script) -> Transaction {
}

pub fn encode_reconfiguration_transaction(sender: AccountAddress) -> Transaction {
let raw_transaction = RawTransaction::new_write_set(sender, 0, WriteSet::default());
let raw_transaction =
RawTransaction::new_write_set(sender, 0, WriteSet::default(), ChainId::mock());

let privkey = Ed25519PrivateKey::generate_for_testing();
Transaction::UserTransaction(
Expand Down
7 changes: 6 additions & 1 deletion language/e2e-tests/src/account.rs
Expand Up @@ -14,6 +14,7 @@ use libra_types::{
BalanceResource, KeyRotationCapabilityResource, ReceivedPaymentEvent, RoleId,
SentPaymentEvent, WithdrawCapabilityResource, COIN1_NAME, COIN2_NAME, LBR_NAME,
},
chain_id::ChainId,
event::EventHandle,
transaction::{
authenticator::AuthenticationKey, ChangeSet, Module, RawTransaction, Script,
Expand Down Expand Up @@ -212,7 +213,7 @@ impl Account {
) -> RawTransaction {
match payload {
TransactionPayload::WriteSet(writeset) => {
RawTransaction::new_change_set(address, sequence_number, writeset)
RawTransaction::new_change_set(address, sequence_number, writeset, ChainId::mock())
}
TransactionPayload::Module(module) => RawTransaction::new_module(
address,
Expand All @@ -222,6 +223,7 @@ impl Account {
gas_unit_price,
gas_currency_code,
Duration::from_secs(DEFAULT_EXPIRATION_TIME),
ChainId::mock(),
),
TransactionPayload::Script(script) => RawTransaction::new_script(
address,
Expand All @@ -231,6 +233,7 @@ impl Account {
gas_unit_price,
gas_currency_code,
Duration::from_secs(DEFAULT_EXPIRATION_TIME),
ChainId::mock(),
),
}
}
Expand Down Expand Up @@ -376,6 +379,7 @@ impl Account {
gas_currency_code,
// TTL is 86400s. Initial time was set to 0.
Duration::from_secs(DEFAULT_EXPIRATION_TIME),
ChainId::mock(),
)
}

Expand Down Expand Up @@ -465,6 +469,7 @@ impl TransactionBuilder {
.unwrap_or_else(|| LBR_NAME.to_owned()),
self.ttl
.unwrap_or_else(|| Duration::from_secs(DEFAULT_EXPIRATION_TIME)),
ChainId::mock(),
)
.sign(&self.sender.privkey, self.sender.pubkey)
.unwrap()
Expand Down
3 changes: 3 additions & 0 deletions language/functional-tests/src/evaluator.rs
Expand Up @@ -17,6 +17,7 @@ use libra_types::{
account_config,
account_config::LBR_NAME,
block_metadata::BlockMetadata,
chain_id::ChainId,
on_chain_config::VMPublishingOption,
transaction::{
Module as TransactionModule, RawTransaction, Script as TransactionScript,
Expand Down Expand Up @@ -340,6 +341,7 @@ fn make_script_transaction(
params.gas_unit_price,
params.gas_currency_code,
params.expiration_time,
ChainId::mock(),
)
.sign(params.privkey, params.pubkey.clone())?
.into_inner())
Expand All @@ -364,6 +366,7 @@ fn make_module_transaction(
params.gas_unit_price,
params.gas_currency_code,
params.expiration_time,
ChainId::mock(),
)
.sign(params.privkey, params.pubkey.clone())?
.into_inner())
Expand Down
2 changes: 1 addition & 1 deletion libra-node/src/main_node.rs
Expand Up @@ -126,7 +126,7 @@ pub fn setup_environment(node_config: &mut NodeConfig) -> LibraHandle {
for (role, network_config) in network_configs {
// Perform common instantiation steps
let (runtime, mut network_builder) =
NetworkBuilder::create(&node_config.base.chain_id, role, network_config);
NetworkBuilder::create(node_config.base.chain_id, role, network_config);
let network_id = network_config.network_id.clone();

// Create the endpoints to connect the Network to StateSynchronizer.
Expand Down
2 changes: 2 additions & 0 deletions mempool/src/tests/common.rs
Expand Up @@ -8,6 +8,7 @@ use libra_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform};
use libra_types::{
account_address::AccountAddress,
account_config::LBR_NAME,
chain_id::ChainId,
mempool_status::MempoolStatusCode,
transaction::{RawTransaction, Script, SignedTransaction},
};
Expand Down Expand Up @@ -83,6 +84,7 @@ impl TestTransaction {
self.gas_price,
LBR_NAME.to_owned(),
exp_time,
ChainId::mock(),
);
let mut seed: [u8; 32] = [0u8; 32];
seed[..4].copy_from_slice(&[1, 2, 3, 4]);
Expand Down
4 changes: 2 additions & 2 deletions network/builder/src/builder.rs
Expand Up @@ -123,7 +123,7 @@ impl NetworkBuilder {
}

pub fn create(
chain_id: &ChainId,
chain_id: ChainId,
role: RoleType,
config: &mut NetworkConfig,
) -> (Runtime, NetworkBuilder) {
Expand All @@ -146,7 +146,7 @@ impl NetworkBuilder {

let mut network_builder = NetworkBuilder::new(
runtime.handle().clone(),
chain_id.clone(),
chain_id,
config.network_id.clone(),
role,
peer_id,
Expand Down
2 changes: 1 addition & 1 deletion network/builder/src/dummy.rs
Expand Up @@ -139,7 +139,7 @@ pub fn setup_network() -> DummyNetwork {
// Set up the listener network
let mut network_builder = NetworkBuilder::new(
runtime.handle().clone(),
chain_id.clone(),
chain_id,
network_id.clone(),
RoleType::Validator,
listener_peer_id,
Expand Down
2 changes: 1 addition & 1 deletion network/src/peer_manager/builder.rs
Expand Up @@ -279,7 +279,7 @@ impl PeerManagerBuilder {
.expect("PeerManager can only be built once");

let protos = transport_context.supported_protocols();
let chain_id = transport_context.chain_id.clone();
let chain_id = transport_context.chain_id;
let network_id = self.network_context.network_id().clone();
let peer_id = self.network_context.peer_id();

Expand Down
5 changes: 3 additions & 2 deletions network/src/protocols/identity.rs
Expand Up @@ -49,7 +49,8 @@ mod tests {
ProtocolId,
};
use futures::{executor::block_on, future::join};
use libra_config::{chain_id::ChainId, network_id::NetworkId};
use libra_config::network_id::NetworkId;
use libra_types::chain_id::ChainId;
use memsocket::MemorySocket;

fn build_test_connection() -> (MemorySocket, MemorySocket) {
Expand All @@ -59,7 +60,7 @@ mod tests {
#[test]
fn simple_handshake() {
let network_id = NetworkId::Validator;
let chain_id = ChainId::default();
let chain_id = ChainId::mock();
let (mut outbound, mut inbound) = build_test_connection();

// Create client and server handshake messages.
Expand Down

0 comments on commit 01bd851

Please sign in to comment.