Skip to content

Commit 98a4711

Browse files
committed
[Rest API] support for signed int
1 parent a461876 commit 98a4711

File tree

21 files changed

+624
-143
lines changed

21 files changed

+624
-143
lines changed

api/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod objects;
1717
mod option_test;
1818
mod resource_groups;
1919
mod secp256k1_ecdsa;
20+
mod signed_int_test;
2021
mod simulation_test;
2122
mod state_test;
2223
mod string_resource_test;

api/src/tests/signed_int_test.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright © Aptos Foundation
2+
// Parts of the project are originally copyright © Meta Platforms, Inc.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
use super::new_test_context_with_orderless_flags;
6+
use aptos_api_test_context::{current_function_name, TestContext};
7+
use rstest::rstest;
8+
use serde_json::json;
9+
use std::path::PathBuf;
10+
11+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
12+
#[rstest(
13+
use_txn_payload_v2_format,
14+
use_orderless_transactions,
15+
case(false, false),
16+
case(true, false),
17+
case(true, true)
18+
)]
19+
async fn test_signed_ints(use_txn_payload_v2_format: bool, use_orderless_transactions: bool) {
20+
let mut context = new_test_context_with_orderless_flags(
21+
current_function_name!(),
22+
use_txn_payload_v2_format,
23+
use_orderless_transactions,
24+
);
25+
let mut account = context.create_account().await;
26+
let account_addr = account.address();
27+
28+
// Publish packages
29+
let named_addresses = vec![("account".to_string(), account_addr)];
30+
let txn = futures::executor::block_on(async move {
31+
let path = PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
32+
.join("../aptos-move/move-examples/signed_int/calculator");
33+
TestContext::build_package_with_latest_language(path, named_addresses)
34+
});
35+
context.publish_package(&mut account, txn).await;
36+
37+
let state_resource = format!("{}::{}::{}", account_addr, "calculator", "State");
38+
let state = &context
39+
.gen_resource(&account_addr, &state_resource)
40+
.await
41+
.unwrap()["data"];
42+
43+
println!("[Result] state: {:?}", state);
44+
45+
context
46+
.api_execute_entry_function(
47+
&mut account,
48+
&format!("0x{}::calculator::number", account_addr.to_hex()),
49+
json!([]),
50+
json!(["0"]),
51+
)
52+
.await;
53+
54+
let state = &context
55+
.gen_resource(&account_addr, &state_resource)
56+
.await
57+
.unwrap()["data"];
58+
59+
println!("[Result] state: {:?}", state);
60+
61+
context
62+
.api_execute_entry_function(
63+
&mut account,
64+
&format!("0x{}::calculator::add", account_addr.to_hex()),
65+
json!([]),
66+
json!(["2"]),
67+
)
68+
.await;
69+
70+
let state = &context
71+
.gen_resource(&account_addr, &state_resource)
72+
.await
73+
.unwrap()["data"];
74+
75+
println!("[Result] state: {:?}", state);
76+
77+
context
78+
.api_execute_entry_function(
79+
&mut account,
80+
&format!("0x{}::calculator::sub", account_addr.to_hex()),
81+
json!([]),
82+
json!(["-2"]),
83+
)
84+
.await;
85+
86+
let state = &context
87+
.gen_resource(&account_addr, &state_resource)
88+
.await
89+
.unwrap()["data"];
90+
91+
println!("[Result] state: {:?}", state);
92+
93+
context
94+
.api_execute_entry_function(
95+
&mut account,
96+
&format!("0x{}::calculator::mul", account_addr.to_hex()),
97+
json!([]),
98+
json!(["2"]),
99+
)
100+
.await;
101+
102+
let state = &context
103+
.gen_resource(&account_addr, &state_resource)
104+
.await
105+
.unwrap()["data"];
106+
107+
println!("[Result] state: {:?}", state);
108+
109+
context
110+
.api_execute_entry_function(
111+
&mut account,
112+
&format!("0x{}::calculator::div", account_addr.to_hex()),
113+
json!([]),
114+
json!(["3"]),
115+
)
116+
.await;
117+
118+
let state = &context
119+
.gen_resource(&account_addr, &state_resource)
120+
.await
121+
.unwrap()["data"];
122+
123+
println!("[Result] state: {:?}", state);
124+
}

api/types/src/bytecode.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ pub trait Bytecode {
8585
SignatureToken::U64 => MoveType::U64,
8686
SignatureToken::U128 => MoveType::U128,
8787
SignatureToken::U256 => MoveType::U256,
88+
SignatureToken::I8 => MoveType::I8,
89+
SignatureToken::I16 => MoveType::I16,
90+
SignatureToken::I32 => MoveType::I32,
91+
SignatureToken::I64 => MoveType::I64,
92+
SignatureToken::I128 => MoveType::I128,
93+
SignatureToken::I256 => MoveType::I256,
8894
SignatureToken::Address => MoveType::Address,
8995
SignatureToken::Signer => MoveType::Signer,
9096
SignatureToken::Vector(t) => MoveType::Vector {
@@ -115,15 +121,6 @@ pub trait Bytecode {
115121
abilities: *abilities,
116122
}
117123
},
118-
SignatureToken::I8
119-
| SignatureToken::I16
120-
| SignatureToken::I32
121-
| SignatureToken::I64
122-
| SignatureToken::I128
123-
| SignatureToken::I256 => {
124-
// TODO(#17645): signed integers
125-
MoveType::Unparsable("signed integer NYI".to_string())
126-
},
127124
}
128125
}
129126

api/types/src/convert.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ impl<'a, S: StateView> MoveConverter<'a, S> {
916916
MoveTypeLayout::U64 => serde_json::from_value::<crate::U64>(val)?.into(),
917917
MoveTypeLayout::U128 => serde_json::from_value::<crate::U128>(val)?.into(),
918918
MoveTypeLayout::U256 => serde_json::from_value::<crate::U256>(val)?.into(),
919+
MoveTypeLayout::I8 => I8(serde_json::from_value::<i8>(val)?),
920+
MoveTypeLayout::I16 => I16(serde_json::from_value::<i16>(val)?),
921+
MoveTypeLayout::I32 => I32(serde_json::from_value::<i32>(val)?),
922+
MoveTypeLayout::I64 => serde_json::from_value::<crate::I64>(val)?.into(),
923+
MoveTypeLayout::I128 => serde_json::from_value::<crate::I128>(val)?.into(),
924+
MoveTypeLayout::I256 => serde_json::from_value::<crate::I256>(val)?.into(),
919925
MoveTypeLayout::Address => serde_json::from_value::<crate::Address>(val)?.into(),
920926
MoveTypeLayout::Vector(item_layout) => {
921927
self.try_into_vm_value_vector(item_layout.as_ref(), val)?
@@ -935,15 +941,6 @@ impl<'a, S: StateView> MoveConverter<'a, S> {
935941
MoveTypeLayout::Signer | MoveTypeLayout::Native(..) => {
936942
bail!("unexpected move type {:?} for value {:?}", layout, val)
937943
},
938-
MoveTypeLayout::I8
939-
| MoveTypeLayout::I16
940-
| MoveTypeLayout::I32
941-
| MoveTypeLayout::I64
942-
| MoveTypeLayout::I128
943-
| MoveTypeLayout::I256 => {
944-
// TODO(#17645): signed integers
945-
bail!("unimplemented support for signed integers")
946-
},
947944
})
948945
}
949946

api/types/src/derives.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use crate::{
2222
move_types::{MoveAbility, MoveStructValue},
2323
Address, AssetType, EntryFunctionId, HashValue, HexEncodedBytes, IdentifierWrapper,
24-
MoveModuleId, MoveStructTag, MoveType, StateKeyWrapper, U128, U256, U64,
24+
MoveModuleId, MoveStructTag, MoveType, StateKeyWrapper, I128, I256, I64, U128, U256, U64,
2525
};
2626
use aptos_openapi::{impl_poem_parameter, impl_poem_type};
2727
use indoc::indoc;
@@ -309,6 +309,51 @@ impl_poem_type!(
309309
)
310310
);
311311

312+
impl_poem_type!(
313+
I64,
314+
"string",
315+
(
316+
example = Some(serde_json::Value::String("-32425224034".to_string())),
317+
format = Some("int64"),
318+
description = Some(indoc! {"
319+
A string containing a 64-bit signed integer.
320+
321+
We represent i64 values as a string to ensure compatibility with languages such
322+
as JavaScript that do not parse i64s in JSON natively.
323+
"})
324+
)
325+
);
326+
327+
impl_poem_type!(
328+
I128,
329+
"string",
330+
(
331+
example = Some(serde_json::Value::String("-32425224034".to_string())),
332+
format = Some("int128"),
333+
description = Some(indoc! {"
334+
A string containing a 128-bit signed integer.
335+
336+
We represent i128 values as a string to ensure compatibility with languages such
337+
as JavaScript that do not parse i128s in JSON natively.
338+
"})
339+
)
340+
);
341+
342+
impl_poem_type!(
343+
I256,
344+
"string",
345+
(
346+
example = Some(serde_json::Value::String("-32425224034".to_string())),
347+
format = Some("int256"),
348+
description = Some(indoc! {"
349+
A string containing a 256-bit signed integer.
350+
351+
We represent i256 values as a string to ensure compatibility with languages such
352+
as JavaScript that do not parse i256s in JSON natively.
353+
"})
354+
)
355+
);
356+
312357
impl_poem_parameter!(
313358
Address,
314359
AssetType,
@@ -318,5 +363,7 @@ impl_poem_parameter!(
318363
MoveStructTag,
319364
StateKeyWrapper,
320365
U64,
321-
U128
366+
U128,
367+
I64,
368+
I128
322369
);

api/types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use move_types::{
3838
HexEncodedBytes, MoveAbility, MoveFunction, MoveFunctionGenericTypeParam,
3939
MoveFunctionVisibility, MoveModule, MoveModuleBytecode, MoveModuleId, MoveResource,
4040
MoveScriptBytecode, MoveStruct, MoveStructField, MoveStructTag, MoveType, MoveValue,
41-
ResourceGroup, MAX_RECURSIVE_TYPES_ALLOWED, U128, U256, U64,
41+
ResourceGroup, I128, I256, I64, MAX_RECURSIVE_TYPES_ALLOWED, U128, U256, U64,
4242
};
4343
use serde::{Deserialize, Deserializer};
4444
pub use state::RawStateValueRequest;

0 commit comments

Comments
 (0)