Skip to content

Commit 476ba48

Browse files
authored
Merge pull request #482 from hashgraph/sr/prng-transaction
2 parents bd0e8be + d3fcbe7 commit 476ba48

File tree

14 files changed

+509
-172
lines changed

14 files changed

+509
-172
lines changed

sdk/rust/examples/prng.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* ‌
3+
* Hedera Rust SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
use clap::Parser;
22+
use hedera::{AccountId, Client, PrivateKey, PrngTransaction};
23+
24+
#[derive(Parser, Debug)]
25+
struct Args {
26+
#[clap(long, env)]
27+
operator_account_id: AccountId,
28+
29+
#[clap(long, env)]
30+
operator_key: PrivateKey,
31+
32+
#[clap(long, env, default_value = "testnet")]
33+
hedera_network: String,
34+
}
35+
36+
#[tokio::main]
37+
async fn main() -> anyhow::Result<()> {
38+
let _ = dotenvy::dotenv();
39+
let args = Args::parse();
40+
41+
let client = Client::for_name(&args.hedera_network)?;
42+
43+
client.set_operator(args.operator_account_id, args.operator_key);
44+
45+
let record = PrngTransaction::new()
46+
.range(100)
47+
.execute(&client)
48+
.await?
49+
.get_record(&client)
50+
.await?;
51+
52+
println!("generated random number = {:?}", record.prng_number);
53+
54+
Ok(())
55+
}

sdk/rust/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ mod node_address;
143143
mod node_address_book;
144144
mod node_address_book_query;
145145
mod ping_query;
146+
mod prng_transaction;
146147
mod query;
147148
mod retry;
148149
mod schedule;
@@ -259,6 +260,7 @@ pub use node_address::NodeAddress;
259260
pub use node_address_book::NodeAddressBook;
260261
pub use node_address_book_query::NodeAddressBookQuery;
261262
pub(crate) use node_address_book_query::NodeAddressBookQueryData;
263+
pub use prng_transaction::PrngTransaction;
262264
pub(crate) use protobuf::{
263265
FromProtobuf,
264266
ToProtobuf,

sdk/rust/src/prng_transaction.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* ‌
3+
* Hedera Rust SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
use hedera_proto::services;
22+
use hedera_proto::services::util_service_client::UtilServiceClient;
23+
24+
use crate::entity_id::ValidateChecksums;
25+
use crate::protobuf::{
26+
FromProtobuf,
27+
ToProtobuf,
28+
};
29+
use crate::transaction::{
30+
AnyTransactionData,
31+
ChunkInfo,
32+
ToSchedulableTransactionDataProtobuf,
33+
ToTransactionDataProtobuf,
34+
TransactionData,
35+
TransactionExecute,
36+
};
37+
use crate::Transaction;
38+
39+
/// Random Number Generator Transaction.
40+
pub type PrngTransaction = Transaction<PrngTransactionData>;
41+
42+
#[derive(Debug, Clone, Default)]
43+
#[cfg_attr(test, derive(Eq, PartialEq))]
44+
pub struct PrngTransactionData {
45+
range: Option<u32>,
46+
}
47+
48+
impl PrngTransaction {
49+
/// Returns the upper-bound for the random number.
50+
pub fn get_range(&self) -> Option<u32> {
51+
self.data().range
52+
}
53+
54+
/// Sets the upper-bound for the random number.
55+
///
56+
/// If the value is zero, instead of returning a 32-bit number, a 384-bit number will be returned.
57+
pub fn range(&mut self, range: u32) -> &mut Self {
58+
self.data_mut().range = Some(range);
59+
60+
return self;
61+
}
62+
}
63+
64+
impl FromProtobuf<services::UtilPrngTransactionBody> for PrngTransactionData {
65+
fn from_protobuf(pb: services::UtilPrngTransactionBody) -> crate::Result<Self> {
66+
Ok(Self { range: (pb.range != 0).then_some(pb.range as u32) })
67+
}
68+
}
69+
70+
impl ToProtobuf for PrngTransactionData {
71+
type Protobuf = services::UtilPrngTransactionBody;
72+
fn to_protobuf(&self) -> Self::Protobuf {
73+
services::UtilPrngTransactionBody { range: self.range.unwrap_or_default() as i32 }
74+
}
75+
}
76+
77+
impl TransactionData for PrngTransactionData {}
78+
79+
impl From<PrngTransactionData> for AnyTransactionData {
80+
fn from(value: PrngTransactionData) -> Self {
81+
Self::Prng(value)
82+
}
83+
}
84+
85+
impl ValidateChecksums for PrngTransactionData {
86+
fn validate_checksums(&self, _ledger_id: &crate::LedgerId) -> crate::Result<()> {
87+
Ok(())
88+
}
89+
}
90+
91+
impl ToSchedulableTransactionDataProtobuf for PrngTransactionData {
92+
fn to_schedulable_transaction_data_protobuf(
93+
&self,
94+
) -> services::schedulable_transaction_body::Data {
95+
services::schedulable_transaction_body::Data::UtilPrng(self.to_protobuf())
96+
}
97+
}
98+
99+
impl ToTransactionDataProtobuf for PrngTransactionData {
100+
fn to_transaction_data_protobuf(
101+
&self,
102+
chunk_info: &ChunkInfo,
103+
) -> services::transaction_body::Data {
104+
let _ = chunk_info.assert_single_transaction();
105+
106+
services::transaction_body::Data::UtilPrng(self.to_protobuf())
107+
}
108+
}
109+
110+
impl TransactionExecute for PrngTransactionData {
111+
fn execute(
112+
&self,
113+
channel: tonic::transport::Channel,
114+
request: services::Transaction,
115+
) -> crate::BoxGrpcFuture<'_, services::TransactionResponse> {
116+
Box::pin(async { UtilServiceClient::new(channel).prng(request).await })
117+
}
118+
}

sdk/rust/src/schedule/schedulable_transaction_body.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod data {
2828
FileDeleteTransactionData as FileDelete,
2929
FileUpdateTransactionData as FileUpdate,
3030
};
31+
pub(super) use crate::prng_transaction::PrngTransactionData as Prng;
3132
pub(super) use crate::schedule::ScheduleDeleteTransactionData as ScheduleDelete;
3233
pub(super) use crate::system::{
3334
FreezeTransactionData as Freeze,
@@ -115,6 +116,7 @@ pub(super) enum AnySchedulableTransactionData {
115116
FileCreate(data::FileCreate),
116117
FileUpdate(data::FileUpdate),
117118
FileDelete(data::FileDelete),
119+
Prng(data::Prng),
118120
TokenAssociate(data::TokenAssociate),
119121
TokenBurn(data::TokenBurn),
120122
TokenCreate(data::TokenCreate),
@@ -184,6 +186,7 @@ impl AnySchedulableTransactionData {
184186
AnySchedulableTransactionData::SystemUndelete(it) => it.default_max_transaction_fee(),
185187
AnySchedulableTransactionData::Freeze(it) => it.default_max_transaction_fee(),
186188
AnySchedulableTransactionData::ScheduleDelete(it) => it.default_max_transaction_fee(),
189+
AnySchedulableTransactionData::Prng(it) => it.default_max_transaction_fee(),
187190
}
188191
}
189192
}
@@ -277,7 +280,7 @@ impl FromProtobuf<services::schedulable_transaction_body::Data> for AnySchedulab
277280
Data::ScheduleDelete(it) => {
278281
Ok(Self::ScheduleDelete(data::ScheduleDelete::from_protobuf(it)?))
279282
}
280-
Data::UtilPrng(_) => unimplemented!("Prng transaction not currently implemented"),
283+
Data::UtilPrng(it) => Ok(Self::Prng(data::Prng::from_protobuf(it)?)),
281284
}
282285
}
283286
}
@@ -399,6 +402,9 @@ impl ToSchedulableTransactionDataProtobuf for AnySchedulableTransactionData {
399402
AnySchedulableTransactionData::ScheduleDelete(it) => {
400403
it.to_schedulable_transaction_data_protobuf()
401404
}
405+
AnySchedulableTransactionData::Prng(it) => {
406+
it.to_schedulable_transaction_data_protobuf()
407+
}
402408
}
403409
}
404410
}
@@ -447,6 +453,8 @@ impl TryFrom<AnyTransactionData> for AnySchedulableTransactionData {
447453
AnyTransactionData::SystemUndelete(it) => Ok(Self::SystemUndelete(it)),
448454
AnyTransactionData::Freeze(it) => Ok(Self::Freeze(it)),
449455
AnyTransactionData::ScheduleDelete(it) => Ok(Self::ScheduleDelete(it)),
456+
AnyTransactionData::Prng(it) => Ok(Self::Prng(it)),
457+
450458
// fixme: basic-parse isn't suitable for this.
451459
AnyTransactionData::ScheduleCreate(_) => {
452460
Err(crate::Error::basic_parse("Cannot schedule `ScheduleCreateTransaction`"))
@@ -507,6 +515,7 @@ impl From<AnySchedulableTransactionData> for AnyTransactionData {
507515
AnySchedulableTransactionData::SystemUndelete(it) => Self::SystemUndelete(it),
508516
AnySchedulableTransactionData::Freeze(it) => Self::Freeze(it),
509517
AnySchedulableTransactionData::ScheduleDelete(it) => Self::ScheduleDelete(it),
518+
AnySchedulableTransactionData::Prng(it) => Self::Prng(it),
510519
}
511520
}
512521
}

0 commit comments

Comments
 (0)