Skip to content

Commit

Permalink
switch to bcs bytes for txn args in script function
Browse files Browse the repository at this point in the history
  • Loading branch information
guangyuz committed Mar 25, 2021
1 parent 15d9a13 commit 9c6e96a
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 154 deletions.
12 changes: 6 additions & 6 deletions chain/tests/test_epoch_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ fn build_cast_vote_txn(
Identifier::new("cast_vote").unwrap(),
vec![stc_type_tag(), action_type_tag],
vec![
TransactionArgument::Address(*alice.address()),
TransactionArgument::U64(proposer_id),
TransactionArgument::Bool(true),
TransactionArgument::U128(voting_power / 2),
bcs_ext::to_bytes(alice.address()).unwrap(),
bcs_ext::to_bytes(&proposer_id).unwrap(),
bcs_ext::to_bytes(&true).unwrap(),
bcs_ext::to_bytes(&(voting_power / 2)).unwrap(),
],
);
alice.sign_txn(build_transaction(
Expand All @@ -150,8 +150,8 @@ fn build_queue_txn(
Identifier::new("queue_proposal_action").unwrap(),
vec![stc_type_tag(), action_type_tag],
vec![
TransactionArgument::Address(*alice.address()),
TransactionArgument::U64(0),
bcs_ext::to_bytes(alice.address()).unwrap(),
bcs_ext::to_bytes(&0).unwrap(),
],
);
alice.sign_txn(build_transaction(
Expand Down
3 changes: 2 additions & 1 deletion cmd/starcoin/src/account/execute_script_function_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use starcoin_types::transaction::{
};
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::transaction::ScriptFunction;
use starcoin_vm_types::transaction_argument::convert_txn_args;
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
use structopt::StructOpt;

Expand Down Expand Up @@ -109,7 +110,7 @@ impl CommandAction for ExecuteScriptFunctionCmd {
script_function.module,
script_function.function,
type_tags,
args,
convert_txn_args(&args),
),
opt.max_gas_amount,
opt.gas_price,
Expand Down
5 changes: 3 additions & 2 deletions cmd/starcoin/src/dev/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use starcoin_types::transaction::{
};
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
use starcoin_vm_types::transaction_argument::convert_txn_args;
use std::path::PathBuf;
use stdlib::restore_stdlib_in_dir;
use structopt::StructOpt;
Expand Down Expand Up @@ -168,7 +169,7 @@ impl CommandAction for ExecuteCommand {
// package deploy
(Some((bytecode, false)), function_id) => {
let module_init_script_function = function_id
.map(|id| ScriptFunction::new(id.module, id.function, type_tags, args));
.map(|id| ScriptFunction::new(id.module, id.function, type_tags, convert_txn_args(&args)));
let package =
Package::new(vec![Module::new(bytecode)], module_init_script_function)?;
TransactionPayload::Package(package)
Expand All @@ -184,7 +185,7 @@ impl CommandAction for ExecuteCommand {
// script function
(None, Some(function_id)) => {
let script_function =
ScriptFunction::new(function_id.module, function_id.function, type_tags, args);
ScriptFunction::new(function_id.module, function_id.function, type_tags, convert_txn_args(&args));
TransactionPayload::ScriptFunction(script_function)
}
(None, None) => {
Expand Down
5 changes: 3 additions & 2 deletions cmd/starcoin/src/dev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use starcoin_vm_types::language_storage::ModuleId;
use starcoin_vm_types::transaction::{
RawUserTransaction, SignedUserTransaction, TransactionPayload,
};
use starcoin_vm_types::transaction_argument::convert_txn_args;
use starcoin_vm_types::{
account_config::{association_address, genesis_address, AccountResource},
transaction::Package,
Expand Down Expand Up @@ -233,7 +234,7 @@ fn test_upgrade_module() {
),
Identifier::new("cast_vote").unwrap(),
type_tags,
args,
convert_txn_args(&args),
);
let vote_raw_txn = RawUserTransaction::new_script_function(
default_account.address,
Expand Down Expand Up @@ -402,7 +403,7 @@ fn test_only_new_module() {
),
Identifier::new("update_module_upgrade_strategy").unwrap(),
Vec::new(),
args,
convert_txn_args(&args),
);
let only_new_module_strategy_raw_txn = RawUserTransaction::new_script_function(
default_account.address,
Expand Down
3 changes: 1 addition & 2 deletions etc/starcoin_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ ScriptFunction:
SEQ:
TYPENAME: TypeTag
- args:
SEQ:
TYPENAME: TransactionArgument
SEQ: BYTES
ScriptFunctionABI:
STRUCT:
- name: STR
Expand Down
18 changes: 9 additions & 9 deletions executor/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,10 @@ pub fn peer_to_peer_txn(
expiration_timestamp_secs: u64,
chain_id: ChainId,
) -> SignedUserTransaction {
let mut args: Vec<TransactionArgument> = Vec::new();
args.push(TransactionArgument::Address(*receiver.address()));
args.push(TransactionArgument::U8Vector(receiver.auth_key().to_vec()));
args.push(TransactionArgument::U128(transfer_amount));
let mut args: Vec<Vec<u8>> = Vec::new();
args.push(bcs_ext::to_bytes(receiver.address()).unwrap());
args.push(bcs_ext::to_bytes(&receiver.auth_key().to_vec()).unwrap());
args.push(bcs_ext::to_bytes(&transfer_amount).unwrap());

// get a SignedTransaction
sender.create_signed_txn_with_args(
Expand Down Expand Up @@ -687,12 +687,12 @@ pub fn create_account_txn_sent_as_association(
expiration_timstamp_secs: u64,
net: &ChainNetwork,
) -> SignedUserTransaction {
let mut args: Vec<TransactionArgument> = Vec::new();
args.push(TransactionArgument::Address(*new_account.address()));
args.push(TransactionArgument::U8Vector(
let mut args: Vec<Vec<u8>> = Vec::new();
args.push(bcs_ext::to_bytes(new_account.address()).unwrap());
args.push(bcs_ext::to_bytes(&
new_account.auth_key().to_vec(),
));
args.push(TransactionArgument::U128(initial_amount));
).unwrap());
args.push(bcs_ext::to_bytes(&initial_amount).unwrap());

create_signed_txn_with_association_account(
TransactionPayload::ScriptFunction(ScriptFunction::new(
Expand Down
6 changes: 3 additions & 3 deletions executor/src/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ fn test_gen_accounts() -> Result<()> {
Identifier::new("peer_to_peer_batch").unwrap(),
vec![stc_type_tag()],
vec![
TransactionArgument::U8Vector(address_vec),
TransactionArgument::U8Vector(auth_key_vec),
TransactionArgument::U128(1),
bcs_ext::to_bytes(&address_vec).unwrap(),
bcs_ext::to_bytes(&auth_key_vec).unwrap(),
bcs_ext::to_bytes(&1).unwrap(),
],
);
association_execute(
Expand Down
12 changes: 6 additions & 6 deletions executor/src/stdlib_test/module_upgrade_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ fn test_dao_upgrade_module() -> Result<()> {
Identifier::new("propose_module_upgrade").unwrap(),
vec![stc_type_tag()],
vec![
TransactionArgument::Address(genesis_address()),
TransactionArgument::U8Vector(package_hash.to_vec()),
TransactionArgument::U64(1),
TransactionArgument::U64(0),
bcs_ext::to_bytes(&genesis_address()).unwrap(),
bcs_ext::to_bytes(&package_hash.to_vec()).unwrap(),
bcs_ext::to_bytes(&1).unwrap(),
bcs_ext::to_bytes(&0).unwrap(),
],
);
let execute_script_function = ScriptFunction::new(
Expand All @@ -51,8 +51,8 @@ fn test_dao_upgrade_module() -> Result<()> {
Identifier::new("submit_module_upgrade_plan").unwrap(),
vec![stc_type_tag()],
vec![
TransactionArgument::Address(*alice.address()),
TransactionArgument::U64(0),
bcs_ext::to_bytes(alice.address()).unwrap(),
bcs_ext::to_bytes(&0).unwrap(),
],
);
let chain_state = dao_vote_test(
Expand Down
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified genesis/generated/proxima/genesis
Binary file not shown.
87 changes: 84 additions & 3 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ use starcoin_vm_types::transaction::{
Script, SignedUserTransaction, Transaction, TransactionInfo, TransactionOutput,
TransactionPayload, TransactionStatus,
};
use starcoin_vm_types::transaction_argument::convert_txn_args;
use starcoin_vm_types::vm_status::{DiscardedVMStatus, KeptVMStatus};
use starcoin_vm_types::write_set::WriteOp;
use std::collections::BTreeMap;
use std::convert::{TryFrom, TryInto};
use std::str::FromStr;
use hex::FromHex;

pub type ByteCode = Vec<u8>;

Expand Down Expand Up @@ -179,6 +181,8 @@ pub struct ScriptData {
pub type_args: Vec<TypeTagView>,
#[serde(default)]
pub args: Vec<TransactionArgumentView>,
#[serde(default)]
pub arguments_bcs: Vec<BytesView>,
}

impl ScriptData {
Expand All @@ -201,11 +205,12 @@ impl ScriptData {
fn into_data(self) -> Result<Script, ScriptFunction> {
let ty_args: Vec<_> = self.type_args.into_iter().map(|s| s.0).collect();
let args: Vec<_> = self.args.into_iter().map(|s| s.0).collect();
let arguments_bcs: Vec<_> = self.arguments_bcs.into_iter().map(|s| s.0.to_vec()).collect();

match self.code.0 {
ByteCodeOrScriptFunction::ByteCode(code) => Ok(Script::new(code, ty_args, args)),
ByteCodeOrScriptFunction::ScriptFunction(FunctionId { module, function }) => {
Err(ScriptFunction::new(module, function, ty_args, args))
Err(ScriptFunction::new(module, function, ty_args, arguments_bcs))
}
}
}
Expand All @@ -229,6 +234,7 @@ impl From<Script> for ScriptData {
.into_iter()
.map(TransactionArgumentView::from)
.collect(),
arguments_bcs: vec![],
}
}
}
Expand All @@ -240,11 +246,12 @@ impl From<ScriptFunction> for ScriptData {
function: s.function().to_owned(),
})),
type_args: s.ty_args().iter().cloned().map(TypeTagView::from).collect(),
args: s
args: vec![],
arguments_bcs: s
.args()
.iter()
.cloned()
.map(TransactionArgumentView::from)
.map(BytesView::from)
.collect(),
}
}
Expand Down Expand Up @@ -1140,6 +1147,80 @@ macro_rules! impl_str_view_for {
impl_str_view_for! {u64 i64 u128 i128}
impl_str_view_for! {ByteCodeOrScriptFunction}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BytesView(Box<[u8]>);

impl BytesView {
pub fn new<T: Into<Box<[u8]>>>(bytes: T) -> Self {
Self(bytes.into())
}

pub fn into_inner(self) -> Box<[u8]> {
self.0
}

pub fn inner(&self) -> &[u8] {
&self.0
}
}

impl std::ops::Deref for BytesView {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::convert::AsRef<[u8]> for BytesView {
fn as_ref(&self) -> &[u8] {
self.inner()
}
}

impl std::fmt::Display for BytesView {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for byte in self.inner() {
write!(f, "{:02x}", byte)?;
}

Ok(())
}
}

impl From<&[u8]> for BytesView {
fn from(bytes: &[u8]) -> Self {
Self(bytes.into())
}
}

impl From<Vec<u8>> for BytesView {
fn from(bytes: Vec<u8>) -> Self {
Self(bytes.into_boxed_slice())
}
}

impl<'de> Deserialize<'de> for BytesView {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
<Vec<u8>>::from_hex(s)
.map_err(D::Error::custom)
.map(Into::into)
}
}

impl Serialize for BytesView {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
hex::encode(self).serialize(serializer)
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ContractCall {
pub function_id: FunctionIdView,
Expand Down
Loading

0 comments on commit 9c6e96a

Please sign in to comment.