Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 language/move-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ petgraph = "0.5.1"
walkdir = "2.3.1"
tempfile = "3.2.0"
once_cell = "1.7.2"
num-bigint = "0.4.0"

move-binary-format = { path = "../move-binary-format" }
move-core-types = { path = "../move-core/types" }
Expand Down
54 changes: 36 additions & 18 deletions language/move-compiler/src/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
use move_core_types::account_address::AccountAddress;
use move_ir_types::location::*;
use move_symbol_pool::Symbol;
use num_bigint::BigUint;
use petgraph::{algo::astar as petgraph_astar, graphmap::DiGraphMap};
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -61,6 +62,25 @@ pub fn parse_u128(s: &str) -> Result<(u128, NumberFormat), ParseIntError> {
Ok((u128::from_str_radix(txt, base as u32)?, base))
}

// Parse an address from a decimal or hex encoding
pub fn parse_address(s: &str) -> Option<([u8; AccountAddress::LENGTH], NumberFormat)> {
let (txt, base) = determine_num_text_and_base(s);
let parsed = BigUint::parse_bytes(
txt.as_bytes(),
match base {
NumberFormat::Hex => 16,
NumberFormat::Decimal => 10,
},
)?;
let bytes = parsed.to_bytes_be();
if bytes.len() > AccountAddress::LENGTH {
return None;
}
let mut result = [0u8; AccountAddress::LENGTH];
result[(AccountAddress::LENGTH - bytes.len())..].clone_from_slice(&bytes);
Some((result, base))
}

//**************************************************************************************************
// Address
//**************************************************************************************************
Expand All @@ -78,9 +98,7 @@ pub struct NumericalAddress {
impl NumericalAddress {
// bytes used for errors when an address is not known but is needed
pub const DEFAULT_ERROR_ADDRESS: Self = NumericalAddress {
bytes: AccountAddress::new([
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8,
]),
bytes: AccountAddress::ONE,
format: NumberFormat::Hex,
};

Expand All @@ -100,22 +118,22 @@ impl NumericalAddress {
}

pub fn parse_str(s: &str) -> Result<NumericalAddress, String> {
let (n, format) = match parse_u128(s) {
Ok(res) => res,
Err(_) => {
// TODO the kind of error is in an unstable nightly API
// But currently the only way this should fail is if the number is too long
return Err(
match parse_address(s) {
Some((n, format)) => Ok(NumericalAddress {
bytes: AccountAddress::new(n),
format,
}),
None =>
// TODO the kind of error is in an unstable nightly API
// But currently the only way this should fail is if the number is too long
{
Err(format!(
"Invalid address literal. The numeric value is too large. The maximum size is \
16 bytes"
.to_owned(),
);
{} bytes",
AccountAddress::LENGTH
))
}
};
Ok(NumericalAddress {
bytes: AccountAddress::new(n.to_be_bytes()),
format,
})
}
}
}

Expand All @@ -129,7 +147,7 @@ impl fmt::Display for NumericalAddress {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.format {
NumberFormat::Decimal => {
let n = u128::from_be_bytes(self.bytes.into_bytes());
let n = BigUint::from_bytes_be(self.bytes.as_ref());
write!(f, "{}", n)
}
NumberFormat::Hex => write!(f, "{:#X}", self),
Expand Down
1 change: 1 addition & 0 deletions language/move-core/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ regex = "1.4.3"
serde_json = "1.0.64"

[features]
address20 = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this add the feature all the time? I'm confused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it just defines the feature. To enable it address20 needs to be adde to default (or explicitly specified when adding the move-core-types dependency)

default = []
fuzzing = ["proptest", "proptest-derive"]
12 changes: 11 additions & 1 deletion language/move-core/types/src/account_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ impl AccountAddress {
}

/// The number of bytes in an address.
pub const LENGTH: usize = 16;
/// Default to 16 bytes, can be set to 20 bytes with address20 feature.
pub const LENGTH: usize = if cfg!(feature = "address20") { 20 } else { 16 };

/// Hex address: 0x0
pub const ZERO: Self = Self([0u8; Self::LENGTH]);

/// Hex address: 0x1
pub const ONE: Self = Self::get_hex_address_one();

const fn get_hex_address_one() -> Self {
let mut addr = [0u8; AccountAddress::LENGTH];
addr[AccountAddress::LENGTH - 1] = 1u8;
Self(addr)
}

pub fn random() -> Self {
let mut rng = OsRng;
let buf: [u8; Self::LENGTH] = rng.gen();
Expand Down
5 changes: 2 additions & 3 deletions language/move-core/types/src/language_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use std::fmt::{Display, Formatter};
pub const CODE_TAG: u8 = 0;
pub const RESOURCE_TAG: u8 = 1;

pub const CORE_CODE_ADDRESS: AccountAddress = AccountAddress::new([
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8,
]);
/// Hex address: 0x1
pub const CORE_CODE_ADDRESS: AccountAddress = AccountAddress::ONE;

#[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)]
pub enum TypeTag {
Expand Down
3 changes: 1 addition & 2 deletions language/move-prover/interpreter/src/concrete/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub type EvalResult<T> = ::std::result::Result<T, BigInt>;
// Constants
//**************************************************************************************************

const DIEM_CORE_ADDR: AccountAddress =
AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
const DIEM_CORE_ADDR: AccountAddress = AccountAddress::ONE;

//**************************************************************************************************
// Evaluation context
Expand Down
4 changes: 2 additions & 2 deletions language/move-prover/interpreter/src/concrete/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use bytecode_interpreter_crypto::{
use move_binary_format::errors::Location;
use move_core_types::{
account_address::AccountAddress,
language_storage::CORE_CODE_ADDRESS,
vm_status::{sub_status, StatusCode},
};
use move_model::{
Expand Down Expand Up @@ -53,8 +54,7 @@ pub type ExecResult<T> = ::std::result::Result<T, AbortInfo>;
// Constants
//**************************************************************************************************

const DIEM_CORE_ADDR: AccountAddress =
AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
const DIEM_CORE_ADDR: AccountAddress = CORE_CODE_ADDRESS;

// TODO(mengxu): these constants are defined in values_impl.rs which are currently not exposed.
const INDEX_OUT_OF_BOUNDS: u64 = sub_status::NFE_VECTOR_ERROR_BASE + 1;
Expand Down
9 changes: 8 additions & 1 deletion language/move-stdlib/src/natives/unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ use std::collections::VecDeque;

use move_core_types::account_address::AccountAddress;

fn to_le_bytes(i: u64) -> [u8; AccountAddress::LENGTH] {
let bytes = i.to_le_bytes();
let mut result = [0u8; AccountAddress::LENGTH];
result[..bytes.len()].clone_from_slice(bytes.as_ref());
result
}

pub fn native_create_signers_for_testing(
_context: &mut NativeContext,
ty_args: Vec<Type>,
Expand All @@ -22,7 +29,7 @@ pub fn native_create_signers_for_testing(

let num_signers = pop_arg!(args, u64);
let signers = Value::vector_for_testing_only(
(0..num_signers).map(|i| Value::signer(AccountAddress::new((i as u128).to_le_bytes()))),
(0..num_signers).map(|i| Value::signer(AccountAddress::new(to_le_bytes(i)))),
);

Ok(NativeResult::ok(ONE_GAS_UNIT, smallvec![signers]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
Command `sandbox publish --override-ordering A --override-ordering B -v`:
error[E02001]: duplicate declaration, item, or annotation
┌─ ./sources/CyclicFriendsPart2.move:1:13
1 │ module 0x3::A {
│ ^ Duplicate definition for module '0x3::A'
┌─ ./sources/CyclicFriendsPart1.move:1:13
1 │ module 0x3::A {
│ - Module previously defined here

error[E02004]: invalid 'module' declaration
┌─ ./sources/CyclicFriendsPart2.move:8:24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module 0x3::A {
module 0x3::D {
public fun foo() {}
public fun bar() {}
}
Expand Down