Skip to content

Commit

Permalink
Support user_data option in transfer cli (#2369)
Browse files Browse the repository at this point in the history
* feat: add "user_data" arg in "transfer" cli

* doc: rebuild the CLI.md

* fix: update the "UserData::from_option_string" impl

* fix: correct the "UserData::from_option_string"

* doc: add more documentation to "user_data" option

* fix: handle the error case of parsing UserData from string

* Update linera-service/src/linera/main.rs

Co-authored-by: Andreas Fackler <afck@users.noreply.github.com>
Signed-off-by: duguorong009 <80258679+duguorong009@users.noreply.github.com>

---------

Signed-off-by: duguorong009 <80258679+duguorong009@users.noreply.github.com>
Co-authored-by: Andreas Fackler <afck@users.noreply.github.com>
  • Loading branch information
duguorong009 and afck authored Aug 12, 2024
1 parent fde6dc4 commit 560cbc3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ A Byzantine-fault tolerant sidechain with low-latency finality and high throughp

Transfer funds

**Usage:** `linera transfer --from <SENDER> --to <RECIPIENT> <AMOUNT>`
**Usage:** `linera transfer [OPTIONS] --from <SENDER> --to <RECIPIENT> <AMOUNT>`

###### **Arguments:**

Expand All @@ -150,6 +150,7 @@ Transfer funds

* `--from <SENDER>` — Sending chain ID (must be one of our chains)
* `--to <RECIPIENT>` — Recipient account
* `--data <USER_DATA>` — User Data (should be a string whose bytes are at most 32 in length)



Expand Down
4 changes: 4 additions & 0 deletions linera-client/src/client_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ pub enum ClientCommand {

/// Amount to transfer
amount: Amount,

/// User Data (should be a string whose bytes are at most 32 in length)
#[arg(long = "data")]
user_data: Option<String>,
},

/// Open (i.e. activate) a new chain deriving the UID from an existing one.
Expand Down
28 changes: 28 additions & 0 deletions linera-execution/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ impl Recipient {
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Hash, Default, Debug, Serialize, Deserialize)]
pub struct UserData(pub Option<[u8; 32]>);

impl UserData {
pub fn from_option_string(opt_str: Option<String>) -> Result<Self, usize> {
// Convert the Option<String> to Option<[u8; 32]>
let option_array = match opt_str {
Some(s) => {
// Convert the String to a Vec<u8>
let vec = s.into_bytes();
if vec.len() <= 32 {
// Create an array from the Vec<u8>
let mut array = [b' '; 32];

// Copy bytes from the vector into the array
let len = vec.len().min(32);
array[..len].copy_from_slice(&vec[..len]);

Some(array)
} else {
return Err(vec.len());
}
}
None => None,
};

// Return the UserData with the converted Option<[u8; 32]>
Ok(UserData(option_array))
}
}

#[derive(Error, Debug)]
pub enum SystemExecutionError {
#[error(transparent)]
Expand Down
12 changes: 10 additions & 2 deletions linera-service/src/linera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ impl Runnable for Job {
sender,
recipient,
amount,
user_data,
} => {
let data = match UserData::from_option_string(user_data) {
Ok(data) => data,
Err(bytes_len) => bail!(
"Failed to parse user data ({} bytes) exceeding max length (32 bytes)",
bytes_len
),
};
let chain_client = context.make_chain_client(sender.chain_id);
info!(
"Starting transfer of {} native tokens from {} to {}",
Expand All @@ -125,10 +133,10 @@ impl Runnable for Job {
let certificate = context
.apply_client_command(&chain_client, |chain_client| {
let chain_client = chain_client.clone();
let user_data = data.clone();
async move {
let data = UserData::default();
chain_client
.transfer_to_account(sender.owner, amount, recipient, data)
.transfer_to_account(sender.owner, amount, recipient, user_data)
.await
}
})
Expand Down

0 comments on commit 560cbc3

Please sign in to comment.