Skip to content

Commit

Permalink
Decouple SuiAddress and PublicKeyBytes (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored Feb 18, 2022
1 parent 7cca4d2 commit 1b66bf5
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 159 deletions.
11 changes: 6 additions & 5 deletions sui/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ impl ClientServerBenchmark {
let mut rnd = <StdRng as rand::SeedableRng>::seed_from_u64(0);
for _ in 0..self.num_accounts {
let keypair = get_key_pair();
let address = keypair.0.into();
let object_id: ObjectID = ObjectID::random();
let object = if self.use_move {
Object::with_id_owner_gas_coin_object_for_testing(
ObjectID::random(),
SequenceNumber::new(),
keypair.0,
address,
rnd.gen::<u64>(),
)
} else {
Object::with_id_owner_for_testing(object_id, keypair.0)
Object::with_id_owner_for_testing(object_id, address)
};

assert!(object.version() == SequenceNumber::from(0));
Expand All @@ -188,7 +189,7 @@ impl ClientServerBenchmark {
state.insert_object(object).await;

let gas_object_id = ObjectID::random();
let gas_object = Object::with_id_owner_for_testing(gas_object_id, keypair.0);
let gas_object = Object::with_id_owner_for_testing(gas_object_id, address);
assert!(gas_object.version() == SequenceNumber::from(0));
let gas_object_ref = gas_object.to_object_reference();
state.init_order_lock(gas_object_ref).await;
Expand All @@ -201,7 +202,7 @@ impl ClientServerBenchmark {
info!("Preparing transactions.");
// Make one transaction per account (transfer order + confirmation).
let mut orders: Vec<Bytes> = Vec::new();
let mut next_recipient = get_key_pair().0;
let mut next_recipient: SuiAddress = get_key_pair().0.into();
for ((account_addr, object, secret), gas_obj) in account_objects.iter().zip(gas_objects) {
let object_ref = object.to_object_reference();
let gas_object_ref = gas_obj.to_object_reference();
Expand Down Expand Up @@ -237,7 +238,7 @@ impl ClientServerBenchmark {
};

// Set the next recipient to current
next_recipient = *account_addr;
next_recipient = account_addr.into();

// Serialize order
let serialized_order = serialize_order(&order);
Expand Down
4 changes: 1 addition & 3 deletions sui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ pub struct AccountInfo {
#[derive(Serialize, Deserialize)]
pub struct AuthorityInfo {
#[serde(serialize_with = "bytes_as_hex", deserialize_with = "bytes_from_hex")]
pub address: SuiAddress,
pub name: AuthorityName,
pub host: String,
pub base_port: u16,
}

#[derive(Serialize, Deserialize)]
pub struct AuthorityPrivateInfo {
#[serde(serialize_with = "bytes_as_hex", deserialize_with = "bytes_from_hex")]
pub address: SuiAddress,
pub key_pair: KeyPair,
pub host: String,
pub port: u16,
Expand Down
16 changes: 8 additions & 8 deletions sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn start_network(config: &NetworkConfig) -> Result<(), anyhow::Error> {
config
.authorities
.iter()
.map(|info| (info.address, DEFAULT_WEIGHT))
.map(|info| (info.key_pair.public(), DEFAULT_WEIGHT))
.collect(),
);

Expand Down Expand Up @@ -91,20 +91,19 @@ async fn genesis(config: &mut NetworkConfig) -> Result<(), anyhow::Error> {
info!("Creating new authorities...");
let authorities_db_path = working_dir.join("authorities_db");
for _ in 0..4 {
let (address, key_pair) = get_key_pair();
let (pub_key, key_pair) = get_key_pair();
let info = AuthorityPrivateInfo {
address,
key_pair,
host: "127.0.0.1".to_string(),
port: port_allocator.next_port().expect("No free ports"),
db_path: authorities_db_path.join(encode_bytes_hex(&address)),
db_path: authorities_db_path.join(encode_bytes_hex(&pub_key)),
};
authority_info.push(AuthorityInfo {
address,
name: pub_key,
host: info.host.clone(),
base_port: info.port,
});
authorities.insert(info.address, 1);
authorities.insert(pub_key, 1);
config.authorities.push(info);
}

Expand All @@ -115,7 +114,8 @@ async fn genesis(config: &mut NetworkConfig) -> Result<(), anyhow::Error> {

info!("Creating test objects...");
for _ in 0..5 {
let (address, key_pair) = get_key_pair();
let (pub_key, key_pair) = get_key_pair();
let address = pub_key.into();
new_addresses.push(AccountInfo { address, key_pair });
for _ in 0..5 {
let new_object = Object::with_id_owner_gas_coin_object_for_testing(
Expand Down Expand Up @@ -162,7 +162,7 @@ async fn make_server(

let state = AuthorityState::new_with_genesis_modules(
committee.clone(),
authority.address,
authority.key_pair.public(),
Box::pin(authority.key_pair.copy()),
store,
)
Expand Down
7 changes: 5 additions & 2 deletions sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ async fn test_addresses_command() -> Result<(), anyhow::Error> {
// Add 3 accounts
for _ in 0..3 {
wallet_config.accounts.push({
let (address, key_pair) = get_key_pair();
AccountInfo { address, key_pair }
let (pub_key, key_pair) = get_key_pair();
AccountInfo {
address: pub_key.into(),
key_pair,
}
});
}
let mut context = WalletContext::new(wallet_config)?;
Expand Down
26 changes: 13 additions & 13 deletions sui/src/wallet_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use sui_core::authority_client::AuthorityClient;
use sui_core::client::{Client, ClientAddressManager, ClientState};
use sui_network::network::NetworkClient;
use sui_types::base_types::{
decode_bytes_hex, encode_bytes_hex, get_key_pair, AuthorityName, ObjectID, PublicKeyBytes,
SuiAddress,
decode_bytes_hex, encode_bytes_hex, get_key_pair, AuthorityName, ObjectID, SuiAddress,
};
use sui_types::committee::Committee;
use sui_types::messages::ExecutionStatus;
Expand Down Expand Up @@ -37,7 +36,7 @@ pub enum WalletCommands {
Object {
/// Owner address
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
owner: PublicKeyBytes,
owner: SuiAddress,

/// Object ID of the object to fetch
#[structopt(long)]
Expand All @@ -53,7 +52,7 @@ pub enum WalletCommands {
Publish {
/// Sender address
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
sender: PublicKeyBytes,
sender: SuiAddress,

/// Path to directory containing a Move package
#[structopt(long)]
Expand All @@ -73,7 +72,7 @@ pub enum WalletCommands {
Call {
/// Sender address
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
sender: PublicKeyBytes,
sender: SuiAddress,
/// Object ID of the package, which contains the module
#[structopt(long)]
package: ObjectID,
Expand Down Expand Up @@ -108,11 +107,11 @@ pub enum WalletCommands {
Transfer {
/// Sender address
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
from: PublicKeyBytes,
from: SuiAddress,

/// Recipient address
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
to: PublicKeyBytes,
to: SuiAddress,

/// Object to transfer, in 20 bytes Hex string
#[structopt(long)]
Expand All @@ -126,7 +125,7 @@ pub enum WalletCommands {
#[structopt(name = "sync")]
SyncClientState {
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
address: PublicKeyBytes,
address: SuiAddress,
},

/// Obtain the Account Addresses managed by the wallet.
Expand All @@ -142,7 +141,7 @@ pub enum WalletCommands {
Objects {
/// Address of the account
#[structopt(long, parse(try_from_str = decode_bytes_hex))]
address: PublicKeyBytes,
address: SuiAddress,
},
}

Expand Down Expand Up @@ -271,7 +270,8 @@ impl WalletCommands {
client_state.sync_client_state().await?;
}
WalletCommands::NewAddress => {
let (address, key) = get_key_pair();
let (pub_key, key) = get_key_pair();
let address = pub_key.into();
context.config.accounts.push(AccountInfo {
address,
key_pair: key,
Expand Down Expand Up @@ -322,12 +322,12 @@ impl WalletContext {
.config
.authorities
.iter()
.map(|authority| (authority.address, 1))
.map(|authority| (authority.name, 1))
.collect();
let committee = Committee::new(voting_rights);
let authority_clients = self.make_authority_clients();
self.address_manager
.get_or_create_state_mut(*owner, kp, committee, authority_clients)
.get_or_create_state_mut(kp.public(), kp, committee, authority_clients)
}

fn make_authority_clients(&self) -> BTreeMap<AuthorityName, AuthorityClient> {
Expand All @@ -340,7 +340,7 @@ impl WalletContext {
self.config.send_timeout,
self.config.recv_timeout,
));
authority_clients.insert(authority.address, client);
authority_clients.insert(authority.name, client);
}
authority_clients
}
Expand Down
6 changes: 3 additions & 3 deletions sui_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl AuthorityState {
// Check the object owner is either the transaction sender, or
// another mutable object in the input.
fp_ensure!(
order.sender() == &object.owner
order.sender_address() == object.owner
|| mutable_object_addresses.contains(&object.owner),
SuiError::IncorrectSigner
);
Expand Down Expand Up @@ -258,7 +258,7 @@ impl AuthorityState {
.collect();

// Insert into the certificates map
let mut tx_ctx = TxContext::new(order.sender(), transaction_digest);
let mut tx_ctx = TxContext::new(&order.sender_address(), transaction_digest);

let gas_object_id = order.gas_payment_object_ref().0;
let (mut temporary_store, status) = self.execute_order(order, inputs, &mut tx_ctx)?;
Expand Down Expand Up @@ -291,7 +291,7 @@ impl AuthorityState {
// unwraps here are safe because we built `inputs`
let mut gas_object = inputs.pop().unwrap();

let sender = *order.sender();
let sender = order.sender_address();
let status = match order.data.kind {
OrderKind::Transfer(t) => AuthorityState::transfer(
&mut temporary_store,
Expand Down
29 changes: 19 additions & 10 deletions sui_core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ impl<A> ClientAddressManager<A> {
/// Get (if exists) or create a new managed address state
pub fn get_or_create_state_mut(
&mut self,
address: SuiAddress,
pub_key: PublicKeyBytes,
secret: StableSyncSigner,
committee: Committee,
authority_clients: BTreeMap<AuthorityName, A>,
) -> Result<&mut ClientState<A>, SuiError> {
let address = pub_key.into();
if let std::collections::btree_map::Entry::Vacant(e) = self.address_states.entry(address) {
// Load the records if available
let single_store = if self.store.is_managed_address(address)? {
Expand All @@ -67,7 +68,7 @@ impl<A> ClientAddressManager<A> {
self.store.manage_new_address(address)
}?;
e.insert(ClientState::new_for_manager(
address,
pub_key,
secret,
committee,
authority_clients,
Expand All @@ -79,14 +80,16 @@ impl<A> ClientAddressManager<A> {
}

/// Get all the states
pub fn get_managed_address_states(&self) -> &BTreeMap<PublicKeyBytes, ClientState<A>> {
pub fn get_managed_address_states(&self) -> &BTreeMap<SuiAddress, ClientState<A>> {
&self.address_states
}
}

pub struct ClientState<AuthorityAPI> {
/// Our Sui address.
address: SuiAddress,
// TODO: We will need to embed pub_key into secret.
pub_key: PublicKeyBytes,
/// Our signature key.
secret: StableSyncSigner,
/// Authority entry point.
Expand Down Expand Up @@ -151,28 +154,30 @@ impl<A> ClientState<A> {
#[cfg(test)]
pub fn new(
path: PathBuf,
address: SuiAddress,
pub_key: PublicKeyBytes,
secret: StableSyncSigner,
committee: Committee,
authority_clients: BTreeMap<AuthorityName, A>,
) -> Result<Self, SuiError> {
Ok(ClientState {
address,
address: pub_key.into(),
pub_key,
secret,
authorities: AuthorityAggregator::new(committee, authority_clients),
store: client_store::ClientSingleAddressStore::new(path),
})
}

pub fn new_for_manager(
address: SuiAddress,
pub_key: PublicKeyBytes,
secret: StableSyncSigner,
committee: Committee,
authority_clients: BTreeMap<AuthorityName, A>,
store: client_store::ClientSingleAddressStore,
) -> Result<Self, SuiError> {
Ok(ClientState {
address,
address: pub_key.into(),
pub_key,
secret,
authorities: AuthorityAggregator::new(committee, authority_clients),
store,
Expand All @@ -183,6 +188,10 @@ impl<A> ClientState<A> {
self.address
}

pub fn pub_key(&self) -> PublicKeyBytes {
self.pub_key
}

pub fn next_sequence_number(&self, object_id: &ObjectID) -> Result<SequenceNumber, SuiError> {
if self.store.object_sequence_numbers.contains_key(object_id)? {
Ok(self
Expand Down Expand Up @@ -529,7 +538,7 @@ where
let order = Order::new_transfer(
recipient,
object_ref,
self.address,
self.pub_key,
gas_payment,
&*self.secret,
);
Expand Down Expand Up @@ -614,7 +623,7 @@ where
gas_budget: u64,
) -> Result<(CertifiedOrder, OrderEffects), anyhow::Error> {
let move_call_order = Order::new_move_call(
self.address,
self.pub_key,
package_object_ref,
module,
function,
Expand All @@ -637,7 +646,7 @@ where
// Try to compile the package at the given path
let compiled_modules = build_move_package_to_bytes(Path::new(&package_source_files_path))?;
let move_publish_order = Order::new_module(
self.address,
self.pub_key,
gas_object_ref,
compiled_modules,
gas_budget,
Expand Down
4 changes: 2 additions & 2 deletions sui_core/src/safe_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use sui_types::{
pub struct SafeClient<C> {
authority_client: C,
committee: Committee,
address: SuiAddress,
address: PublicKeyBytes,
}

impl<C> SafeClient<C> {
pub fn new(authority_client: C, committee: Committee, address: SuiAddress) -> Self {
pub fn new(authority_client: C, committee: Committee, address: PublicKeyBytes) -> Self {
Self {
authority_client,
committee,
Expand Down
Loading

0 comments on commit 1b66bf5

Please sign in to comment.