Skip to content

Commit

Permalink
[Global Constants] Created a global-constants crate in the configurat…
Browse files Browse the repository at this point in the history
…ion directory.

Closes: aptos-labs#3788
Approved by: davidiw
  • Loading branch information
JoshLind authored and bors-libra committed May 8, 2020
1 parent 9437320 commit 84e2450
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 84 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ members = [
"config",
"config/config-builder",
"config/generate-key",
"config/global-constants",
"config/management",
"consensus",
"consensus/consensus-types",
Expand Down
10 changes: 10 additions & 0 deletions config/global-constants/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "libra-global-constants"
version = "0.1.0"
authors = ["Libra Association <[email protected]>"]
description = "Libra's global constant crate: the source of truth for constant definitions that span multiple crates"
repository = "https://github.com/libra/libra"
homepage = "https://libra.org"
license = "Apache-2.0"
publish = false
edition = "2018"
23 changes: 23 additions & 0 deletions config/global-constants/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! The purpose of this crate is to offer a single source of truth for the definitions of shared
//! constants within the Libra codebase. This is useful because many different components within
//! Libra often require access to global constant definitions (e.g., Libra Safety Rules,
//! the Key Manager, and Secure Storage). To avoid duplicating these definitions across crates
//! (and better allow these constants to be updated in a single location), we define them here.
#![forbid(unsafe_code)]

/// Definitions of global cryptographic keys (e.g., as held in secure storage)
pub const ASSOCIATION_KEY: &str = "association";
pub const CONSENSUS_KEY: &str = "consensus";
pub const FULLNODE_NETWORK_KEY: &str = "fullnode_network";
pub const OPERATOR_KEY: &str = "operator";
pub const OWNER_KEY: &str = "owner";
pub const VALIDATOR_NETWORK_KEY: &str = "validator_network";

/// Definitions of global data items (e.g., as held in secure storage)
pub const EPOCH: &str = "epoch";
pub const LAST_VOTED_ROUND: &str = "last_voted_round";
pub const PREFERRED_ROUND: &str = "preferred_round";
pub const WAYPOINT: &str = "waypoint";
1 change: 1 addition & 0 deletions config/management/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ toml = { version = "0.5.3", default-features = false }

libra-config = { path = "..", version = "0.1.0" }
libra-crypto = { path = "../../crypto/crypto", version = "0.1.0" }
libra-global-constants = { path = "../../config/global-constants", version = "0.1.0"}
libra-network-address = { path = "../../network/network-address", version = "0.1.0" }
libra-secure-storage = { path = "../../secure/storage", version = "0.1.0" }
libra-secure-time = { path = "../../secure/time", version = "0.1.0" }
Expand Down
18 changes: 12 additions & 6 deletions config/management/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{constants, error::Error, layout::Layout, SingleBackend};
use crate::{
error::Error,
layout::Layout,
management_constants::{COMMON_NS, LAYOUT, VALIDATOR_CONFIG},
SingleBackend,
};
use libra_crypto::ed25519::Ed25519PublicKey;
use libra_global_constants::{ASSOCIATION_KEY, OPERATOR_KEY};
use libra_secure_storage::Storage;
use libra_types::transaction::{Transaction, TransactionPayload};
use std::{convert::TryInto, path::PathBuf};
Expand Down Expand Up @@ -43,7 +49,7 @@ impl Genesis {
let association: Box<dyn Storage> = association_config.try_into()?;

let association_key = association
.get(constants::ASSOCIATION_KEY)
.get(ASSOCIATION_KEY)
.map_err(|e| Error::RemoteStorageReadError(e.to_string()))?;
association_key
.value
Expand All @@ -56,11 +62,11 @@ impl Genesis {
let mut common_config = self.backend.backend.clone();
common_config
.parameters
.insert("namespace".into(), constants::COMMON_NS.into());
.insert("namespace".into(), COMMON_NS.into());
let common: Box<dyn Storage> = common_config.try_into()?;

let layout = common
.get(constants::LAYOUT)
.get(LAYOUT)
.map_err(|e| Error::RemoteStorageReadError(e.to_string()))?
.value
.string()
Expand All @@ -79,14 +85,14 @@ impl Genesis {
let validator: Box<dyn Storage> = validator_config.try_into()?;

let key = validator
.get(constants::OPERATOR_KEY)
.get(OPERATOR_KEY)
.map_err(|e| Error::RemoteStorageReadError(e.to_string()))?
.value
.ed25519_public_key()
.map_err(|e| Error::RemoteStorageReadError(e.to_string()))?;

let txn = validator
.get(constants::VALIDATOR_CONFIG)
.get(VALIDATOR_CONFIG)
.map_err(|e| Error::RemoteStorageReadError(e.to_string()))?
.value;
let txn = txn.transaction().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions config/management/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{constants, error::Error, SingleBackend};
use crate::{error::Error, management_constants::LAYOUT, SingleBackend};
use libra_secure_storage::{Storage, Value};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -62,7 +62,7 @@ impl SetLayout {

let value = Value::String(data);
remote
.create_with_default_policy(constants::LAYOUT, value)
.create_with_default_policy(LAYOUT, value)
.map_err(|e| Error::RemoteStorageWriteError(e.to_string()))?;

Ok(layout)
Expand Down
98 changes: 34 additions & 64 deletions config/management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@ mod validator_config;

use crate::{error::Error, layout::SetLayout, secure_backend::SecureBackend};
use libra_crypto::ed25519::Ed25519PublicKey;
use libra_global_constants::{
ASSOCIATION_KEY, CONSENSUS_KEY, EPOCH, FULLNODE_NETWORK_KEY, LAST_VOTED_ROUND, OPERATOR_KEY,
OWNER_KEY, PREFERRED_ROUND, VALIDATOR_NETWORK_KEY, WAYPOINT,
};
use libra_secure_storage::{Storage, Value};
use libra_types::{transaction::Transaction, waypoint::Waypoint};
use std::{convert::TryInto, fmt::Write, str::FromStr};
use structopt::StructOpt;

pub mod constants {
pub const ASSOCIATION_KEY: &str = "association";
pub mod management_constants {
pub const COMMON_NS: &str = "common";
pub const CONSENSUS_KEY: &str = "consensus";
pub const EPOCH: &str = "epoch";
pub const FULLNODE_NETWORK_KEY: &str = "fullnode_network";
pub const LAST_VOTED_ROUND: &str = "last_voted_round";
pub const LAYOUT: &str = "layout";
pub const OWNER_KEY: &str = "owner";
pub const OPERATOR_KEY: &str = "operator";
pub const PREFERRED_ROUND: &str = "preferred_round";
pub const VALIDATOR_CONFIG: &str = "validator_config";
pub const VALIDATOR_NETWORK_KEY: &str = "validator_network";
pub const WAYPOINT: &str = "waypoint";

pub const GAS_UNIT_PRICE: u64 = 0;
pub const MAX_GAS_AMOUNT: u64 = 1_000_000;
Expand Down Expand Up @@ -110,7 +104,7 @@ impl Command {

pub fn association_key(self) -> Result<Ed25519PublicKey, Error> {
if let Command::AssociationKey(secure_backends) = self {
Self::submit_key(constants::ASSOCIATION_KEY, secure_backends)
Self::submit_key(ASSOCIATION_KEY, secure_backends)
} else {
Err(Error::UnexpectedCommand(
CommandName::AssociationKey,
Expand All @@ -132,7 +126,7 @@ impl Command {

pub fn operator_key(self) -> Result<Ed25519PublicKey, Error> {
if let Command::OperatorKey(secure_backends) = self {
Self::submit_key(constants::OPERATOR_KEY, secure_backends)
Self::submit_key(OPERATOR_KEY, secure_backends)
} else {
Err(Error::UnexpectedCommand(
CommandName::OperatorKey,
Expand All @@ -143,7 +137,7 @@ impl Command {

pub fn owner_key(self) -> Result<Ed25519PublicKey, Error> {
if let Command::OwnerKey(secure_backends) = self {
Self::submit_key(constants::OWNER_KEY, secure_backends)
Self::submit_key(OWNER_KEY, secure_backends)
} else {
Err(Error::UnexpectedCommand(
CommandName::OwnerKey,
Expand Down Expand Up @@ -188,28 +182,20 @@ impl Command {
writeln!(buffer, "Keys").unwrap();
writeln!(buffer, "=================================================").unwrap();

Self::write_key(storage.as_ref(), &mut buffer, constants::CONSENSUS_KEY);
Self::write_key(
storage.as_ref(),
&mut buffer,
constants::FULLNODE_NETWORK_KEY,
);
Self::write_key(storage.as_ref(), &mut buffer, constants::OWNER_KEY);
Self::write_key(storage.as_ref(), &mut buffer, constants::OPERATOR_KEY);
Self::write_key(
storage.as_ref(),
&mut buffer,
constants::VALIDATOR_NETWORK_KEY,
);
Self::write_key(storage.as_ref(), &mut buffer, CONSENSUS_KEY);
Self::write_key(storage.as_ref(), &mut buffer, FULLNODE_NETWORK_KEY);
Self::write_key(storage.as_ref(), &mut buffer, OWNER_KEY);
Self::write_key(storage.as_ref(), &mut buffer, OPERATOR_KEY);
Self::write_key(storage.as_ref(), &mut buffer, VALIDATOR_NETWORK_KEY);

writeln!(buffer, "=================================================").unwrap();
writeln!(buffer, "Data").unwrap();
writeln!(buffer, "=================================================").unwrap();

Self::write_u64(storage.as_ref(), &mut buffer, constants::EPOCH);
Self::write_u64(storage.as_ref(), &mut buffer, constants::LAST_VOTED_ROUND);
Self::write_u64(storage.as_ref(), &mut buffer, constants::PREFERRED_ROUND);
Self::write_waypoint(storage.as_ref(), &mut buffer, constants::WAYPOINT);
Self::write_u64(storage.as_ref(), &mut buffer, EPOCH);
Self::write_u64(storage.as_ref(), &mut buffer, LAST_VOTED_ROUND);
Self::write_u64(storage.as_ref(), &mut buffer, PREFERRED_ROUND);
Self::write_waypoint(storage.as_ref(), &mut buffer, WAYPOINT);

writeln!(buffer, "=================================================").unwrap();

Expand Down Expand Up @@ -322,6 +308,7 @@ pub struct SingleBackend {
#[cfg(test)]
pub mod tests {
use super::*;
use crate::management_constants::{COMMON_NS, LAYOUT, VALIDATOR_CONFIG};
use libra_network_address::NetworkAddress;
use libra_secure_storage::{Policy, Value, VaultStorage};
use libra_types::account_address::AccountAddress;
Expand All @@ -347,7 +334,7 @@ pub mod tests {
// Step 1) Define and upload the layout specifying which identities have which roles. This
// is uplaoded to the common namespace.

let mut common = default_storage(constants::COMMON_NS.into());
let mut common = default_storage(COMMON_NS.into());
common.reset_and_clear().unwrap();

// Note: owners are irrelevant currently
Expand All @@ -364,7 +351,7 @@ pub mod tests {
.unwrap();
file.sync_all().unwrap();

set_layout(temppath.path().to_str().unwrap(), constants::COMMON_NS).unwrap();
set_layout(temppath.path().to_str().unwrap(), COMMON_NS).unwrap();

// Step 2) Upload the association key:

Expand Down Expand Up @@ -424,12 +411,7 @@ pub mod tests {
.unwrap();
file.sync_all().unwrap();
set_layout(temppath.path().to_str().unwrap(), namespace).unwrap();
let stored_layout = storage
.get(constants::LAYOUT)
.unwrap()
.value
.string()
.unwrap();
let stored_layout = storage.get(LAYOUT).unwrap().value.string().unwrap();
assert_eq!(layout_text, stored_layout);
}

Expand All @@ -453,7 +435,7 @@ pub mod tests {
)
.unwrap();

let remote_txn = remote.get(constants::VALIDATOR_CONFIG).unwrap().value;
let remote_txn = remote.get(VALIDATOR_CONFIG).unwrap().value;
let remote_txn = remote_txn.transaction().unwrap();

assert_eq!(local_txn, remote_txn);
Expand All @@ -479,13 +461,13 @@ pub mod tests {
#[test]
#[ignore]
fn test_owner_key() {
test_key(constants::OWNER_KEY, owner_key);
test_key(OWNER_KEY, owner_key);
}

#[test]
#[ignore]
fn test_operator_key() {
test_key(constants::OPERATOR_KEY, operator_key);
test_key(OPERATOR_KEY, operator_key);
}

fn test_key(key_name: &str, op: fn(&str, &str) -> Result<Ed25519PublicKey, Error>) {
Expand Down Expand Up @@ -526,34 +508,22 @@ pub mod tests {
let policy = Policy::public();
storage.reset_and_clear().unwrap();

storage
.create_key(constants::ASSOCIATION_KEY, &policy)
.unwrap();
storage
.create_key(constants::CONSENSUS_KEY, &policy)
.unwrap();
storage
.create_key(constants::FULLNODE_NETWORK_KEY, &policy)
.unwrap();
storage.create_key(constants::OWNER_KEY, &policy).unwrap();
storage
.create_key(constants::OPERATOR_KEY, &policy)
.unwrap();
storage
.create_key(constants::VALIDATOR_NETWORK_KEY, &policy)
.unwrap();
storage.create_key(ASSOCIATION_KEY, &policy).unwrap();
storage.create_key(CONSENSUS_KEY, &policy).unwrap();
storage.create_key(FULLNODE_NETWORK_KEY, &policy).unwrap();
storage.create_key(OWNER_KEY, &policy).unwrap();
storage.create_key(OPERATOR_KEY, &policy).unwrap();
storage.create_key(VALIDATOR_NETWORK_KEY, &policy).unwrap();

storage.create(EPOCH, Value::U64(0), &policy).unwrap();
storage
.create(constants::EPOCH, Value::U64(0), &policy)
.unwrap();
storage
.create(constants::LAST_VOTED_ROUND, Value::U64(0), &policy)
.create(LAST_VOTED_ROUND, Value::U64(0), &policy)
.unwrap();
storage
.create(constants::PREFERRED_ROUND, Value::U64(0), &policy)
.create(PREFERRED_ROUND, Value::U64(0), &policy)
.unwrap();
storage
.create(constants::WAYPOINT, Value::String("".into()), &policy)
.create(WAYPOINT, Value::String("".into()), &policy)
.unwrap();
}

Expand Down
Loading

0 comments on commit 84e2450

Please sign in to comment.