Skip to content

Commit

Permalink
Unify the size of SuiAddress and ObjectID (also move AccountAddress) (M…
Browse files Browse the repository at this point in the history
…ystenLabs#540)

* Unify SuiAddress and ObjectID (move AccountAddress)

* feat: Switch SuiAddress hashing to sha2-256 truncated to 20 initial bytes

This changes from Addresses on 32 bytes.

* fix: remove `impl From<Vec<[u8]>> for SuiAddress`

There is no situation where the `TryFrom<&[u8]>` isn't more flexible and preferred.

* fix: update the admin address in the Hero test script

Co-authored-by: François Garillot <[email protected]>
  • Loading branch information
lxfind and huitseeker authored Feb 24, 2022
1 parent 871500f commit dba291b
Show file tree
Hide file tree
Showing 28 changed files with 184 additions and 291 deletions.
18 changes: 9 additions & 9 deletions sui_core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ async fn test_handle_move_transaction() {

// Check that gas is properly deducted.
// If the number changes, we want to verify that the change is intended.
let gas_cost = 62;
let gas_cost = 53;
let gas_payment_object = authority_state
.get_object(&gas_payment_object_id)
.await
Expand Down Expand Up @@ -622,7 +622,7 @@ async fn test_handle_move_transaction_insufficient_budget() {
Vec::new(),
vec![
16u64.to_le_bytes().to_vec(),
bcs::to_bytes(&sender.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(sender)).unwrap(),
],
9,
&sender_key,
Expand Down Expand Up @@ -1346,7 +1346,7 @@ async fn test_hero() {
let modules = sui_framework::build_move_package(&hero_path, build_config, false).unwrap();

// 2. Create an admin account, and a player account.
// Using a hard-coded key to match the address in the Move code.
// Using a hard-coded key to match the Admin address in the Move code.
// This needs to be hard-coded because the module needs to know the admin's address
// in advance.
let (admin, admin_key) = get_key_pair_from_bytes(&[
Expand Down Expand Up @@ -1430,7 +1430,7 @@ async fn test_hero() {
ident_str!("transfer").to_owned(),
vec![],
vec![cap.unwrap().0],
vec![bcs::to_bytes(&player).unwrap()],
vec![bcs::to_bytes(&AccountAddress::from(player)).unwrap()],
)
.await
.unwrap();
Expand Down Expand Up @@ -1497,9 +1497,9 @@ async fn test_hero() {

// 7. Give them a boar!
let pure_args = vec![
bcs::to_bytes(&10_u64).unwrap(), // hp
bcs::to_bytes(&10_u64).unwrap(), // strength
bcs::to_bytes(&player.to_vec()).unwrap(), // recipient
bcs::to_bytes(&10_u64).unwrap(), // hp
bcs::to_bytes(&10_u64).unwrap(), // strength
bcs::to_bytes(&AccountAddress::from(player)).unwrap(), // recipient
];
let effects = call_move(
&authority,
Expand Down Expand Up @@ -1631,7 +1631,7 @@ async fn test_object_owning_another_object() {
"transfer",
vec![],
vec![obj2],
vec![bcs::to_bytes(&sender2.to_vec()).unwrap()],
vec![bcs::to_bytes(&AccountAddress::from(sender2)).unwrap()],
)
.await
.unwrap();
Expand Down Expand Up @@ -1887,7 +1887,7 @@ async fn create_move_object(
vec![],
vec![
16u64.to_le_bytes().to_vec(),
bcs::to_bytes(&sender.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(*sender)).unwrap(),
],
)
.await
Expand Down
20 changes: 10 additions & 10 deletions sui_core/src/unit_tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::client::client_store::ClientSingleAddressStore;
use crate::client::{Client, ClientState};
use async_trait::async_trait;
use futures::lock::Mutex;
use move_core_types::{ident_str, identifier::Identifier};
use move_core_types::{account_address::AccountAddress, ident_str, identifier::Identifier};
use std::{
collections::{BTreeMap, HashMap},
convert::TryInto,
Expand Down Expand Up @@ -160,7 +160,7 @@ fn transaction_create(

let pure_arguments = vec![
value.to_le_bytes().to_vec(),
bcs::to_bytes(&dest.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(dest)).unwrap(),
];

Transaction::new_move_call(
Expand All @@ -186,7 +186,7 @@ fn transaction_transfer(
framework_obj_ref: ObjectRef,
gas_object_ref: ObjectRef,
) -> Transaction {
let pure_args = vec![bcs::to_bytes(&dest.to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(dest)).unwrap()];

Transaction::new_move_call(
src,
Expand Down Expand Up @@ -804,7 +804,7 @@ async fn test_move_calls_object_create() {
// When creating an ObjectBasics object, we provide the value (u64) and address which will own the object
let pure_args = vec![
object_value.to_le_bytes().to_vec(),
bcs::to_bytes(&client1.address().to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(client1.address())).unwrap(),
];
let call_response = client1
.move_call(
Expand Down Expand Up @@ -865,7 +865,7 @@ async fn test_move_calls_object_transfer() {
// When creating an ObjectBasics object, we provide the value (u64) and address which will own the object
let pure_args = vec![
object_value.to_le_bytes().to_vec(),
bcs::to_bytes(&client1.address().to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(client1.address())).unwrap(),
];
let call_response = client1
.move_call(
Expand All @@ -888,7 +888,7 @@ async fn test_move_calls_object_transfer() {
let (new_obj_ref, _) = transaction_effects.created[0];
gas_object_ref = client_object(&mut client1, gas_object_ref.0).await.0;

let pure_args = vec![bcs::to_bytes(&client2.address().to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(client2.address())).unwrap()];
let call_response = client1
.move_call(
framework_obj_ref,
Expand Down Expand Up @@ -953,7 +953,7 @@ async fn test_move_calls_object_transfer_and_freeze() {
// When creating an ObjectBasics object, we provide the value (u64) and address which will own the object
let pure_args = vec![
object_value.to_le_bytes().to_vec(),
bcs::to_bytes(&client1.address().to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(client1.address())).unwrap(),
];
let call_response = client1
.move_call(
Expand All @@ -975,7 +975,7 @@ async fn test_move_calls_object_transfer_and_freeze() {
let new_obj_ref = client_object(&mut client1, new_obj_ref.0).await.0;
gas_object_ref = client_object(&mut client1, gas_object_ref.0).await.0;

let pure_args = vec![bcs::to_bytes(&client2.address().to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(client2.address())).unwrap()];
let call_response = client1
.move_call(
framework_obj_ref,
Expand Down Expand Up @@ -1040,7 +1040,7 @@ async fn test_move_calls_object_delete() {
// When creating an ObjectBasics object, we provide the value (u64) and address which will own the object
let pure_args = vec![
object_value.to_le_bytes().to_vec(),
bcs::to_bytes(&client1.address().to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(client1.address())).unwrap(),
];
let call_response = client1
.move_call(
Expand Down Expand Up @@ -2340,7 +2340,7 @@ async fn test_address_manager() {
let gas_ref_1 = get_latest_ref(sample_auth, gas_object1).await;
let pure_args = vec![
bcs::to_bytes(&100u64).unwrap(),
bcs::to_bytes(&client1.address().to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(client1.address())).unwrap(),
];
let call_response = client1
.move_call(
Expand Down
24 changes: 12 additions & 12 deletions sui_programmability/adapter/src/unit_tests/adapter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn test_object_basics() {
// ObjectBasics::create expects integer value and recipient address
let pure_args = vec![
10u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr1.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr1)).unwrap(),
];
call(
&mut storage,
Expand All @@ -247,7 +247,7 @@ fn test_object_basics() {
assert_eq!(obj1.version(), obj1_seq);

// 2. Transfer obj1 to addr2
let pure_args = vec![bcs::to_bytes(&addr2.to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(addr2)).unwrap()];
call(
&mut storage,
&native_functions,
Expand Down Expand Up @@ -284,7 +284,7 @@ fn test_object_basics() {
// 3. Create another object obj2 owned by addr2, use it to update addr1
let pure_args = vec![
20u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr2.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr2)).unwrap(),
];
call(
&mut storage,
Expand Down Expand Up @@ -385,7 +385,7 @@ fn test_wrap_unwrap() {
// 1. Create obj1 owned by addr
let pure_args = vec![
10u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr)).unwrap(),
];
call(
&mut storage,
Expand Down Expand Up @@ -489,23 +489,23 @@ fn test_move_call_insufficient_gas() {
let addr1 = get_key_pair().0;
let pure_args = vec![
10u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr1.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr1)).unwrap(),
];
let response = call(
&mut storage,
&native_functions,
"ObjectBasics",
"create",
gas_object,
20, // This budget is not enough to execute all bytecode.
15, // This budget is not enough to execute all bytecode.
Vec::new(),
Vec::new(),
pure_args.clone(),
);
let err = response.unwrap().unwrap_err();
assert!(err.1.to_string().contains("VMError with status OUT_OF_GAS"));
// Provided gas_budget will be deducted as gas.
assert_eq!(err.0, 20);
assert_eq!(err.0, 15);

// Trying again with a different gas budget.
let gas_object = storage.read_object(&gas_object_id).unwrap();
Expand Down Expand Up @@ -586,7 +586,7 @@ fn test_transfer_and_freeze() {
// ObjectBasics::create expects integer value and recipient address
let pure_args = vec![
10u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr1.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr1)).unwrap(),
];
call(
&mut storage,
Expand All @@ -608,7 +608,7 @@ fn test_transfer_and_freeze() {
assert!(!obj1.is_read_only());

// 2. Call transfer_and_freeze.
let pure_args = vec![bcs::to_bytes(&addr2.to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(addr2)).unwrap()];
call(
&mut storage,
&native_functions,
Expand All @@ -629,7 +629,7 @@ fn test_transfer_and_freeze() {
assert!(obj1.owner == addr2);

// 3. Call transfer again and it should fail.
let pure_args = vec![bcs::to_bytes(&addr1.to_vec()).unwrap()];
let pure_args = vec![bcs::to_bytes(&AccountAddress::from(addr1)).unwrap()];
let result = call(
&mut storage,
&native_functions,
Expand Down Expand Up @@ -934,7 +934,7 @@ fn test_coin_transfer() {
vec![to_transfer],
vec![
10u64.to_le_bytes().to_vec(),
bcs::to_bytes(&addr1.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr1)).unwrap(),
],
)
.unwrap()
Expand Down Expand Up @@ -1015,7 +1015,7 @@ fn test_simple_call() {
let addr = base_types::get_new_address();
let pure_args = vec![
obj_val.to_le_bytes().to_vec(),
bcs::to_bytes(&addr.to_vec()).unwrap(),
bcs::to_bytes(&AccountAddress::from(addr)).unwrap(),
];

let response = call(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Test::M1 {
use FastX::Address;
use FastX::ID::VersionedID;
use FastX::TxContext::{Self, TxContext};
use FastX::Transfer;
Expand All @@ -14,10 +13,10 @@ module Test::M1 {
value1
}

public fun create(value: u64, recipient: vector<u8>, ctx: &mut TxContext) {
public fun create(value: u64, recipient: address, ctx: &mut TxContext) {
Transfer::transfer(
Object { id: TxContext::new_id(ctx), value },
Address::new(recipient)
recipient
)
}
}
7 changes: 3 additions & 4 deletions sui_programmability/examples/sources/CombinableObjects.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/// new objects
module Examples::CombinableObjects {
use Examples::TrustedCoin::EXAMPLE;
use FastX::Address::{Self, Address};
use FastX::Coin::{Self, Coin};
use FastX::ID::{Self, VersionedID};
use FastX::Transfer;
Expand All @@ -21,7 +20,7 @@ module Examples::CombinableObjects {
}

/// Address selling ham, bread, etc
const GROCERY: vector<u8> = b"";
const GROCERY: address = @0x0;
/// Price for ham
const HAM_PRICE: u64 = 10;
/// Price for bread
Expand Down Expand Up @@ -55,7 +54,7 @@ module Examples::CombinableObjects {
Sandwich { id: TxContext::new_id(ctx) }
}

fun admin(): Address {
Address::new(GROCERY)
fun admin(): address {
GROCERY
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// An example of a custom object with comments explaining the relevant bits
module Examples::CustomObjectTemplate {
use FastX::Address::{Self, Address};
use FastX::ID::VersionedID;
use FastX::Transfer;
use FastX::TxContext::{Self, TxContext};
Expand Down Expand Up @@ -44,7 +43,7 @@ module Examples::CustomObjectTemplate {

/// Example of transferring an object to a a new owner. A struct can only
/// be transferred by the module that declares it.
public fun transfer(o: Object, recipient: Address) {
public fun transfer(o: Object, recipient: address) {
assert!(some_conditional_logic(), 0);
Transfer::transfer(o, recipient)
}
Expand Down Expand Up @@ -89,13 +88,13 @@ module Examples::CustomObjectTemplate {
to_consume: Object,
// ... end objects, begin primitive type inputs
int_input: u64,
bytes_input: vector<u8>,
recipient: address,
// end primitive types. last arg must be TxContext
ctx: &mut TxContext,
) {
let v = read_field(to_read);
write_field(to_write, v + int_input);
transfer(to_consume, Address::new(bytes_input));
transfer(to_consume, recipient);
// demonstrate creating a new object for the sender
let sender = TxContext::get_signer_address(ctx);
Transfer::transfer(create(ctx), sender)
Expand Down
5 changes: 2 additions & 3 deletions sui_programmability/examples/sources/EconMod.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
module Examples::EconMod {
use Examples::HeroMod::{Self, SeaMonster, RUM};
use Examples::Hero::Hero;
use FastX::Address::Address;
use FastX::Coin::{Self, Coin};
use FastX::ID::{Self, VersionedID};
use FastX::Transfer;
Expand All @@ -20,7 +19,7 @@ module Examples::EconMod {
/// Monster to be slay by the owner of this object
monster: SeaMonster,
/// Identity of the user that originally owned the monster
monster_owner: Address,
monster_owner: address,
/// Number of tokens that will go to the helper. The owner will get
/// the `monster` reward - `helper_reward` tokens
helper_reward: u64,
Expand All @@ -35,7 +34,7 @@ module Examples::EconMod {
public fun create(
monster: SeaMonster,
helper_reward: u64,
helper: Address,
helper: address,
ctx: &mut TxContext,
) {
// make sure the advertised reward is not too large + that the owner
Expand Down
Loading

0 comments on commit dba291b

Please sign in to comment.