casper_rust_wasm_sdk/sdk/binary_port/
mod.rs

1use crate::{types::sdk_error::SdkError, SDK};
2use casper_binary_port::{
3    ConsensusStatus, ConsensusValidatorChanges, GlobalStateQueryResult, LastProgress, NetworkName,
4    NodeStatus, ReactorStateName, RecordId, RewardResponse, SpeculativeExecutionResult,
5    TransactionWithExecutionInfo, Uptime,
6};
7use casper_binary_port_access::{
8    available_block_range, block_header_by_hash, block_header_by_height, block_synchronizer_status,
9    block_with_signatures_by_hash, block_with_signatures_by_height, chainspec_raw_bytes,
10    consensus_status, consensus_validator_changes, delegator_reward_by_block_hash,
11    delegator_reward_by_block_height, delegator_reward_by_era, global_state_item,
12    global_state_item_by_block_hash, global_state_item_by_block_height,
13    global_state_item_by_state_root_hash, last_progress, latest_block_header,
14    latest_block_with_signatures, latest_switch_block_header, network_name, next_upgrade,
15    node_status, peers, protocol_version, reactor_state, read_record, transaction_by_hash,
16    try_accept_transaction, try_speculative_execution, uptime, validator_reward_by_block_hash,
17    validator_reward_by_block_height, validator_reward_by_era,
18};
19use casper_types::{
20    AvailableBlockRange, BlockHash, BlockHeader, BlockSynchronizerStatus, BlockWithSignatures,
21    ChainspecRawBytes, Digest, EraId, Key, NextUpgrade, Peers, ProtocolVersion, PublicKey,
22    Transaction, TransactionHash,
23};
24
25pub mod wasm32;
26
27impl SDK {
28    /// Returns the latest switch block header.
29    pub async fn get_binary_latest_switch_block_header(
30        &self,
31        node_address: Option<String>,
32    ) -> Result<Option<BlockHeader>, SdkError> {
33        let node_address = self.get_node_address(node_address);
34        match latest_switch_block_header(&node_address).await {
35            Ok(block_header) => Ok(block_header),
36            Err(err) => Err(SdkError::CustomError {
37                context: "Failed to retrieve latest switch block header",
38                error: err.to_string(),
39            }),
40        }
41    }
42
43    /// Returns the latest block header.
44    pub async fn get_binary_latest_block_header(
45        &self,
46        node_address: Option<String>,
47    ) -> Result<Option<BlockHeader>, SdkError> {
48        let node_address = self.get_node_address(node_address);
49        match latest_block_header(&node_address).await {
50            Ok(block_header) => Ok(block_header),
51            Err(err) => Err(SdkError::CustomError {
52                context: "Failed to retrieve latest block header",
53                error: err.to_string(),
54            }),
55        }
56    }
57
58    /// Returns the block header at the given height.
59    pub async fn get_binary_block_header_by_height(
60        &self,
61        node_address: Option<String>,
62        height: u64,
63    ) -> Result<Option<BlockHeader>, SdkError> {
64        let node_address = self.get_node_address(node_address);
65        match block_header_by_height(&node_address, height).await {
66            Ok(block_header) => Ok(block_header),
67            Err(err) => Err(SdkError::CustomError {
68                context: "Failed to retrieve block header by height",
69                error: err.to_string(),
70            }),
71        }
72    }
73
74    /// Returns the block header with the given hash.
75    pub async fn get_binary_block_header_by_hash(
76        &self,
77        node_address: Option<String>,
78        block_hash: BlockHash,
79    ) -> Result<Option<BlockHeader>, SdkError> {
80        let node_address = self.get_node_address(node_address);
81        match block_header_by_hash(&node_address, block_hash).await {
82            Ok(block_header) => Ok(block_header),
83            Err(err) => Err(SdkError::CustomError {
84                context: "Failed to retrieve block header by block_hash",
85                error: err.to_string(),
86            }),
87        }
88    }
89
90    /// Returns the latest signed block along with signatures.
91    pub async fn get_binary_latest_block_with_signatures(
92        &self,
93        node_address: Option<String>,
94    ) -> Result<Option<BlockWithSignatures>, SdkError> {
95        let node_address = self.get_node_address(node_address);
96        match latest_block_with_signatures(&node_address).await {
97            Ok(signed_block) => Ok(signed_block),
98            Err(err) => Err(SdkError::CustomError {
99                context: "Failed to retrieve latest signed block",
100                error: err.to_string(),
101            }),
102        }
103    }
104
105    /// Returns the signed block at the given height.
106    pub async fn get_binary_block_with_signatures_by_height(
107        &self,
108        node_address: Option<String>,
109        height: u64,
110    ) -> Result<Option<BlockWithSignatures>, SdkError> {
111        let node_address = self.get_node_address(node_address);
112        match block_with_signatures_by_height(&node_address, height).await {
113            Ok(signed_block) => Ok(signed_block),
114            Err(err) => Err(SdkError::CustomError {
115                context: "Failed to retrieve signed block by height",
116                error: err.to_string(),
117            }),
118        }
119    }
120
121    /// Returns the signed block with the given hash.
122    pub async fn get_binary_block_with_signatures_by_hash(
123        &self,
124        node_address: Option<String>,
125        block_hash: BlockHash,
126    ) -> Result<Option<BlockWithSignatures>, SdkError> {
127        let node_address = self.get_node_address(node_address);
128        match block_with_signatures_by_hash(&node_address, block_hash).await {
129            Ok(signed_block) => Ok(signed_block),
130            Err(err) => Err(SdkError::CustomError {
131                context: "Failed to retrieve signed block by block_hash",
132                error: err.to_string(),
133            }),
134        }
135    }
136
137    /// Returns the transaction by its hash.
138    pub async fn get_binary_transaction_by_hash(
139        &self,
140        node_address: Option<String>,
141        hash: TransactionHash,
142        with_finalized_approvals: bool,
143    ) -> Result<Option<TransactionWithExecutionInfo>, SdkError> {
144        let node_address = self.get_node_address(node_address);
145        match transaction_by_hash(&node_address, hash, with_finalized_approvals).await {
146            Ok(transaction) => Ok(transaction),
147            Err(err) => Err(SdkError::CustomError {
148                context: "Failed to retrieve transaction by hash",
149                error: err.to_string(),
150            }),
151        }
152    }
153
154    /// Returns the peer list.
155    pub async fn get_binary_peers(&self, node_address: Option<String>) -> Result<Peers, SdkError> {
156        let node_address = self.get_node_address(node_address);
157        match peers(&node_address).await {
158            Ok(peers) => Ok(peers),
159            Err(err) => Err(SdkError::CustomError {
160                context: "Failed to retrieve peer list",
161                error: err.to_string(),
162            }),
163        }
164    }
165
166    /// Returns the node uptime.
167    pub async fn get_binary_uptime(
168        &self,
169        node_address: Option<String>,
170    ) -> Result<Uptime, SdkError> {
171        let node_address = self.get_node_address(node_address);
172        match uptime(&node_address).await {
173            Ok(uptime) => Ok(uptime),
174            Err(err) => Err(SdkError::CustomError {
175                context: "Failed to retrieve node uptime",
176                error: err.to_string(),
177            }),
178        }
179    }
180
181    /// Returns the last progress recorded by the node.
182    pub async fn get_binary_last_progress(
183        &self,
184        node_address: Option<String>,
185    ) -> Result<LastProgress, SdkError> {
186        let node_address = self.get_node_address(node_address);
187        match last_progress(&node_address).await {
188            Ok(progress) => Ok(progress),
189            Err(err) => Err(SdkError::CustomError {
190                context: "Failed to retrieve last progress",
191                error: err.to_string(),
192            }),
193        }
194    }
195
196    /// Returns the current reactor state.
197    pub async fn get_binary_reactor_state(
198        &self,
199        node_address: Option<String>,
200    ) -> Result<ReactorStateName, SdkError> {
201        let node_address = self.get_node_address(node_address);
202        match reactor_state(&node_address).await {
203            Ok(state) => Ok(state),
204            Err(err) => Err(SdkError::CustomError {
205                context: "Failed to retrieve reactor state",
206                error: err.to_string(),
207            }),
208        }
209    }
210
211    /// Returns the network name.
212    pub async fn get_binary_network_name(
213        &self,
214        node_address: Option<String>,
215    ) -> Result<NetworkName, SdkError> {
216        let node_address = self.get_node_address(node_address);
217        match network_name(&node_address).await {
218            Ok(network_name) => Ok(network_name),
219            Err(err) => Err(SdkError::CustomError {
220                context: "Failed to retrieve network name",
221                error: err.to_string(),
222            }),
223        }
224    }
225
226    /// Returns the last consensus validator changes.
227    pub async fn get_binary_consensus_validator_changes(
228        &self,
229        node_address: Option<String>,
230    ) -> Result<ConsensusValidatorChanges, SdkError> {
231        let node_address = self.get_node_address(node_address);
232        match consensus_validator_changes(&node_address).await {
233            Ok(changes) => Ok(changes),
234            Err(err) => Err(SdkError::CustomError {
235                context: "Failed to retrieve consensus validator changes",
236                error: err.to_string(),
237            }),
238        }
239    }
240
241    /// Returns the block synchronizer status.
242    pub async fn get_binary_block_synchronizer_status(
243        &self,
244        node_address: Option<String>,
245    ) -> Result<BlockSynchronizerStatus, SdkError> {
246        let node_address = self.get_node_address(node_address);
247        match block_synchronizer_status(&node_address).await {
248            Ok(status) => Ok(status),
249            Err(err) => Err(SdkError::CustomError {
250                context: "Failed to retrieve block synchronizer status",
251                error: err.to_string(),
252            }),
253        }
254    }
255
256    /// Returns the available block range.
257    pub async fn get_binary_available_block_range(
258        &self,
259        node_address: Option<String>,
260    ) -> Result<AvailableBlockRange, SdkError> {
261        let node_address = self.get_node_address(node_address);
262        match available_block_range(&node_address).await {
263            Ok(block_range) => Ok(block_range),
264            Err(err) => Err(SdkError::CustomError {
265                context: "Failed to retrieve available block range",
266                error: err.to_string(),
267            }),
268        }
269    }
270
271    /// Returns information about the next upgrade point.
272    pub async fn get_binary_next_upgrade(
273        &self,
274        node_address: Option<String>,
275    ) -> Result<Option<NextUpgrade>, SdkError> {
276        let node_address = self.get_node_address(node_address);
277        match next_upgrade(&node_address).await {
278            Ok(upgrade) => Ok(upgrade),
279            Err(err) => Err(SdkError::CustomError {
280                context: "Failed to retrieve next upgrade",
281                error: err.to_string(),
282            }),
283        }
284    }
285
286    /// Returns the consensus status.
287    pub async fn get_binary_consensus_status(
288        &self,
289        node_address: Option<String>,
290    ) -> Result<ConsensusStatus, SdkError> {
291        let node_address = self.get_node_address(node_address);
292        match consensus_status(&node_address).await {
293            Ok(status) => Ok(status),
294            Err(err) => Err(SdkError::CustomError {
295                context: "Failed to retrieve consensus status",
296                error: err.to_string(),
297            }),
298        }
299    }
300
301    /// Returns the raw chainspec bytes and additional configuration.
302    pub async fn get_binary_chainspec_raw_bytes(
303        &self,
304        node_address: Option<String>,
305    ) -> Result<ChainspecRawBytes, SdkError> {
306        let node_address = self.get_node_address(node_address);
307        match chainspec_raw_bytes(&node_address).await {
308            Ok(bytes) => Ok(bytes),
309            Err(err) => Err(SdkError::CustomError {
310                context: "Failed to retrieve chainspec raw bytes",
311                error: err.to_string(),
312            }),
313        }
314    }
315
316    /// Retrieves the node status.
317    pub async fn get_binary_node_status(
318        &self,
319        node_address: Option<String>,
320    ) -> Result<NodeStatus, SdkError> {
321        // Get the node address, falling back to self's default if not provided
322        let node_address = self.get_node_address(node_address);
323
324        match node_status(&node_address).await {
325            Ok(status) => Ok(status),
326            Err(err) => Err(SdkError::CustomError {
327                context: "Failed to retrieve node status",
328                error: err.to_string(),
329            }),
330        }
331    }
332
333    /// Retrieves the reward for the given validator at the given era.
334    pub async fn get_binary_validator_reward_by_era(
335        &self,
336        node_address: Option<String>,
337        validator_key: PublicKey,
338        era: EraId,
339    ) -> Result<Option<RewardResponse>, SdkError> {
340        let node_address = self.get_node_address(node_address);
341
342        match validator_reward_by_era(&node_address, validator_key, era).await {
343            Ok(reward) => Ok(reward),
344            Err(err) => Err(SdkError::CustomError {
345                context: "Failed to retrieve validator reward by era",
346                error: err.to_string(),
347            }),
348        }
349    }
350
351    /// Retrieves the reward for the given validator at the era containing the block at given height.
352    pub async fn get_binary_validator_reward_by_block_height(
353        &self,
354        node_address: Option<String>,
355        validator_key: PublicKey,
356        block_height: u64,
357    ) -> Result<Option<RewardResponse>, SdkError> {
358        let node_address = self.get_node_address(node_address);
359
360        match validator_reward_by_block_height(&node_address, validator_key, block_height).await {
361            Ok(reward) => Ok(reward),
362            Err(err) => Err(SdkError::CustomError {
363                context: "Failed to retrieve validator reward by block height",
364                error: err.to_string(),
365            }),
366        }
367    }
368
369    /// Retrieves the reward for the given validator at the era containing the block with given hash.
370    pub async fn get_binary_validator_reward_by_block_hash(
371        &self,
372        node_address: Option<String>,
373        validator_key: PublicKey,
374        block_hash: BlockHash,
375    ) -> Result<Option<RewardResponse>, SdkError> {
376        let node_address = self.get_node_address(node_address);
377
378        match validator_reward_by_block_hash(&node_address, validator_key, block_hash).await {
379            Ok(reward) => Ok(reward),
380            Err(err) => Err(SdkError::CustomError {
381                context: "Failed to retrieve validator reward by block hash",
382                error: err.to_string(),
383            }),
384        }
385    }
386
387    /// Retrieves the reward for the given delegator at the given era.
388    pub async fn get_binary_delegator_reward_by_era(
389        &self,
390        node_address: Option<String>,
391        validator_key: PublicKey,
392        delegator_key: PublicKey,
393        era: EraId,
394    ) -> Result<Option<RewardResponse>, SdkError> {
395        let node_address = self.get_node_address(node_address);
396
397        match delegator_reward_by_era(&node_address, validator_key, delegator_key, era).await {
398            Ok(reward) => Ok(reward),
399            Err(err) => Err(SdkError::CustomError {
400                context: "Failed to retrieve delegator reward by era",
401                error: err.to_string(),
402            }),
403        }
404    }
405
406    /// Retrieves the reward for the given delegator at the era containing the block at given height.
407    pub async fn get_binary_delegator_reward_by_block_height(
408        &self,
409        node_address: Option<String>,
410        validator_key: PublicKey,
411        delegator_key: PublicKey,
412        block_height: u64,
413    ) -> Result<Option<RewardResponse>, SdkError> {
414        let node_address = self.get_node_address(node_address);
415
416        match delegator_reward_by_block_height(
417            &node_address,
418            validator_key,
419            delegator_key,
420            block_height,
421        )
422        .await
423        {
424            Ok(reward) => Ok(reward),
425            Err(err) => Err(SdkError::CustomError {
426                context: "Failed to retrieve delegator reward by block height",
427                error: err.to_string(),
428            }),
429        }
430    }
431
432    /// Retrieves the reward for the given delegator at the era containing the block with given hash.
433    pub async fn get_binary_delegator_reward_by_block_hash(
434        &self,
435        node_address: Option<String>,
436        validator_key: PublicKey,
437        delegator_key: PublicKey,
438        block_hash: BlockHash,
439    ) -> Result<Option<RewardResponse>, SdkError> {
440        let node_address = self.get_node_address(node_address);
441
442        match delegator_reward_by_block_hash(
443            &node_address,
444            validator_key,
445            delegator_key,
446            block_hash,
447        )
448        .await
449        {
450            Ok(reward) => Ok(reward),
451            Err(err) => Err(SdkError::CustomError {
452                context: "Failed to retrieve delegator reward by block hash",
453                error: err.to_string(),
454            }),
455        }
456    }
457
458    /// Reads a record from the node.
459    pub async fn get_binary_read_record(
460        &self,
461        node_address: Option<String>,
462        record_id: RecordId,
463        key: &[u8],
464    ) -> Result<Vec<u8>, SdkError> {
465        // Check if the key is empty and return an error if it is
466        if key.is_empty() {
467            return Err(SdkError::CustomError {
468                context: "Failed to read record",
469                error: "Key cannot be an empty array".to_string(),
470            });
471        }
472
473        let node_address = self.get_node_address(node_address);
474
475        match read_record(&node_address, record_id, key).await {
476            Ok(record) => Ok(record),
477            Err(err) => Err(SdkError::CustomError {
478                context: "Failed to read record",
479                error: err.to_string(),
480            }),
481        }
482    }
483
484    /// Retrieves an item from the global state using the most recent state root hash.
485    pub async fn get_binary_global_state_item(
486        &self,
487        node_address: Option<String>,
488        key: Key,
489        path: Vec<String>,
490    ) -> Result<Option<GlobalStateQueryResult>, SdkError> {
491        let node_address = self.get_node_address(node_address);
492        match global_state_item(&node_address, key, path).await {
493            Ok(item) => Ok(item),
494            Err(err) => Err(SdkError::CustomError {
495                context: "Failed to retrieve global state item",
496                error: err.to_string(),
497            }),
498        }
499    }
500
501    /// Retrieves an item from the global state using a specific state root hash.
502    pub async fn get_binary_global_state_item_by_state_root_hash(
503        &self,
504        node_address: Option<String>,
505        state_root_hash: Digest,
506        key: Key,
507        path: Vec<String>,
508    ) -> Result<Option<GlobalStateQueryResult>, SdkError> {
509        let node_address = self.get_node_address(node_address);
510        match global_state_item_by_state_root_hash(&node_address, state_root_hash, key, path).await
511        {
512            Ok(item) => Ok(item),
513            Err(err) => Err(SdkError::CustomError {
514                context: "Failed to retrieve global state item by state root hash",
515                error: err.to_string(),
516            }),
517        }
518    }
519
520    /// Retrieves an item from the global state using a block hash.
521    pub async fn get_binary_global_state_item_by_block_hash(
522        &self,
523        node_address: Option<String>,
524        block_hash: BlockHash,
525        key: Key,
526        path: Vec<String>,
527    ) -> Result<Option<GlobalStateQueryResult>, SdkError> {
528        let node_address = self.get_node_address(node_address);
529        match global_state_item_by_block_hash(&node_address, block_hash, key, path).await {
530            Ok(item) => Ok(item),
531            Err(err) => Err(SdkError::CustomError {
532                context: "Failed to retrieve global state item by block hash",
533                error: err.to_string(),
534            }),
535        }
536    }
537
538    /// Retrieves an item from the global state using a block height.
539    pub async fn get_binary_global_state_item_by_block_height(
540        &self,
541        node_address: Option<String>,
542        block_height: u64,
543        key: Key,
544        path: Vec<String>,
545    ) -> Result<Option<GlobalStateQueryResult>, SdkError> {
546        let node_address = self.get_node_address(node_address);
547        match global_state_item_by_block_height(&node_address, block_height, key, path).await {
548            Ok(item) => Ok(item),
549            Err(err) => Err(SdkError::CustomError {
550                context: "Failed to retrieve global state item by block height",
551                error: err.to_string(),
552            }),
553        }
554    }
555
556    /// Attempts to send a transaction to the node for inclusion.
557    pub async fn get_binary_try_accept_transaction(
558        &self,
559        node_address: Option<String>,
560        transaction: Transaction,
561    ) -> Result<(), SdkError> {
562        let node_address = self.get_node_address(node_address);
563        match try_accept_transaction(&node_address, transaction).await {
564            Ok(_) => Ok(()),
565            Err(err) => Err(SdkError::CustomError {
566                context: "Failed to accept transaction",
567                error: err.to_string(),
568            }),
569        }
570    }
571
572    /// Attempts to send a transaction to the node for speculative execution.
573    pub async fn get_binary_try_speculative_execution(
574        &self,
575        node_address: Option<String>,
576        transaction: Transaction,
577    ) -> Result<SpeculativeExecutionResult, SdkError> {
578        let node_address = self.get_node_address(node_address);
579        match try_speculative_execution(&node_address, transaction).await {
580            Ok(result) => Ok(result),
581            Err(err) => Err(SdkError::CustomError {
582                context: "Failed to perform speculative execution",
583                error: err.to_string(),
584            }),
585        }
586    }
587
588    /// Retrieves the protocol version from the node.
589    pub async fn get_binary_protocol_version(
590        &self,
591        node_address: Option<String>,
592    ) -> Result<ProtocolVersion, SdkError> {
593        let node_address = self.get_node_address(node_address);
594        match protocol_version(&node_address).await {
595            Ok(version) => Ok(version),
596            Err(err) => Err(SdkError::CustomError {
597                context: "Failed to retrieve protocol version",
598                error: err.to_string(),
599            }),
600        }
601    }
602}
603
604#[cfg(test)]
605mod tests {
606    use crate::{
607        helpers::{public_key_from_secret_key, secret_key_from_pem},
608        types::transaction_params::transaction_str_params::TransactionStrParams,
609    };
610
611    use super::*;
612    use casper_types::bytesrepr::ToBytes;
613    use sdk_tests::{
614        config::TRANSFER_AMOUNT,
615        tests::helpers::{get_network_constants, get_user_secret_key},
616    };
617
618    #[tokio::test]
619    async fn test_get_binary_latest_switch_block_header_success() {
620        let sdk = SDK::new(None, None, None);
621        let (_, _, _, node_address, _) = get_network_constants();
622
623        let result = sdk
624            .get_binary_latest_switch_block_header(Some(node_address))
625            .await;
626
627        let block_header = result.unwrap();
628        assert!(block_header.is_some());
629        let block_header = block_header.unwrap();
630
631        assert!(!block_header.body_hash().to_string().is_empty());
632        assert!(block_header.height() > 0);
633    }
634
635    #[tokio::test]
636    async fn test_get_binary_latest_block_header_success() {
637        let sdk = SDK::new(None, None, None);
638        let (_, _, _, node_address, _) = get_network_constants();
639
640        let result = sdk.get_binary_latest_block_header(Some(node_address)).await;
641
642        let block_header = result.unwrap();
643        assert!(block_header.is_some());
644        let block_header = block_header.unwrap();
645
646        assert!(!block_header.body_hash().to_string().is_empty());
647        assert!(block_header.height() > 0);
648    }
649
650    #[tokio::test]
651    async fn test_get_binary_block_header_by_height_success() {
652        let sdk = SDK::new(None, None, None);
653        let (_, _, _, node_address, _) = get_network_constants();
654        let block_height = 1; // Sample block height
655
656        let result = sdk
657            .get_binary_block_header_by_height(Some(node_address), block_height)
658            .await;
659
660        let block_header = result.unwrap();
661        assert!(block_header.is_some());
662        let block_header = block_header.unwrap();
663
664        assert!(!block_header.body_hash().to_string().is_empty());
665        assert_eq!(block_header.height(), block_height);
666    }
667
668    #[tokio::test]
669    async fn test_get_binary_block_header_by_hash_success() {
670        let sdk = SDK::new(None, None, None);
671        let (_, _, _, node_address, _) = get_network_constants();
672        let block_height = 1; // Sample block height
673
674        let result = sdk
675            .get_binary_block_header_by_height(Some(node_address.clone()), block_height)
676            .await;
677
678        let block_header = result.unwrap();
679        assert!(block_header.is_some());
680        let block_header = block_header.unwrap();
681        let block_hash = block_header.block_hash();
682
683        assert!(!block_hash.to_string().is_empty());
684
685        let result = sdk
686            .get_binary_block_header_by_hash(Some(node_address), block_hash)
687            .await;
688
689        let block_header = result.unwrap();
690        assert!(block_header.is_some());
691        let block_header = block_header.unwrap();
692
693        assert_eq!(
694            block_header.block_hash().to_hex_string(),
695            block_hash.to_hex_string()
696        );
697    }
698
699    #[tokio::test]
700    async fn test_get_binary_latest_block_with_signatures_success() {
701        let sdk = SDK::new(None, None, None);
702        let (_, _, _, node_address, _) = get_network_constants();
703
704        let result = sdk
705            .get_binary_latest_block_with_signatures(Some(node_address))
706            .await;
707        let signed_block = result.unwrap();
708        assert!(signed_block.is_some());
709        let signed_block = signed_block.unwrap();
710        assert!(!signed_block.block().hash().to_string().is_empty());
711        assert!(signed_block.block().height() > 0);
712    }
713
714    #[tokio::test]
715    async fn test_get_binary_block_with_signatures_by_height_success() {
716        let sdk = SDK::new(None, None, None);
717        let (_, _, _, node_address, _) = get_network_constants();
718        let block_height = 1;
719
720        let result = sdk
721            .get_binary_block_with_signatures_by_height(Some(node_address), block_height)
722            .await;
723        let signed_block = result.unwrap();
724        assert!(signed_block.is_some());
725        let signed_block = signed_block.unwrap();
726        assert_eq!(signed_block.block().height(), block_height);
727        assert!(!signed_block.block().hash().to_string().is_empty());
728    }
729
730    #[tokio::test]
731    async fn test_get_binary_block_with_signatures_by_hash_success() {
732        let sdk = SDK::new(None, None, None);
733        let (_, _, _, node_address, _) = get_network_constants();
734        let block_height = 1;
735
736        let result = sdk
737            .get_binary_block_with_signatures_by_height(Some(node_address.clone()), block_height)
738            .await;
739        let signed_block = result.unwrap();
740        assert!(signed_block.is_some());
741        let signed_block = signed_block.unwrap();
742        assert_eq!(signed_block.block().height(), block_height);
743        let block_hash = signed_block.block().hash();
744        assert!(!block_hash.to_string().is_empty());
745
746        let result = sdk
747            .get_binary_block_with_signatures_by_hash(Some(node_address), *block_hash)
748            .await;
749
750        let signed_block = result.unwrap();
751        assert!(signed_block.is_some());
752        let signed_block = signed_block.unwrap();
753        assert_eq!(
754            signed_block.block().hash().to_hex_string(),
755            block_hash.to_hex_string()
756        );
757    }
758
759    #[tokio::test]
760    async fn test_get_binary_transaction_by_hash_success() {
761        let sdk = SDK::new(None, None, None);
762        let (rpc_address, _, _, node_address, chain_name) = get_network_constants();
763
764        let secret_key = get_user_secret_key(None).unwrap();
765        let initiator_addr = public_key_from_secret_key(&secret_key).unwrap();
766
767        let transaction_params = TransactionStrParams::default();
768        transaction_params.set_secret_key(&secret_key);
769        transaction_params.set_chain_name(&chain_name);
770        transaction_params.set_payment_amount(TRANSFER_AMOUNT);
771
772        let transfer = sdk
773            .transfer_transaction(
774                None,
775                &initiator_addr, // self transfer
776                TRANSFER_AMOUNT,
777                transaction_params,
778                None,
779                None,
780                Some(rpc_address.clone()),
781            )
782            .await
783            .unwrap();
784        let transaction_hash = transfer.result.transaction_hash;
785        assert!(!transaction_hash.to_string().is_empty());
786
787        let result = sdk
788            .get_binary_transaction_by_hash(Some(node_address), transaction_hash, false)
789            .await;
790
791        let transaction = result.unwrap();
792        assert!(transaction.is_some());
793        let transaction = transaction.unwrap();
794        assert_eq!(
795            transaction.into_inner().0.hash().to_hex_string(),
796            transaction_hash.to_hex_string()
797        );
798    }
799
800    #[tokio::test]
801    async fn test_get_binary_peers_success() {
802        let sdk = SDK::new(None, None, None);
803        let (_, _, _, node_address, _) = get_network_constants();
804
805        let result = sdk.get_binary_peers(Some(node_address)).await;
806        let peers = result.unwrap();
807        assert!(!peers.into_inner().is_empty());
808    }
809
810    #[tokio::test]
811    async fn test_get_binary_uptime_success() {
812        let sdk = SDK::new(None, None, None);
813        let (_, _, _, node_address, _) = get_network_constants();
814
815        let result = sdk.get_binary_uptime(Some(node_address)).await;
816        let uptime = result.unwrap();
817        assert!(!uptime.into_inner().to_string().is_empty());
818    }
819
820    #[tokio::test]
821    async fn test_get_binary_last_progress_success() {
822        let sdk = SDK::new(None, None, None);
823        let (_, _, _, node_address, _) = get_network_constants();
824
825        let result = sdk.get_binary_last_progress(Some(node_address)).await;
826        let progress = result.unwrap();
827        assert!(!progress.into_inner().to_string().is_empty());
828    }
829
830    #[tokio::test]
831    async fn test_get_binary_reactor_state_success() {
832        let sdk = SDK::new(None, None, None);
833        let (_, _, _, node_address, _) = get_network_constants();
834
835        let result = sdk.get_binary_reactor_state(Some(node_address)).await;
836        let state = result.unwrap();
837        assert!(!state.into_inner().to_string().is_empty());
838    }
839
840    #[tokio::test]
841    async fn test_get_binary_network_name_success() {
842        let sdk = SDK::new(None, None, None);
843        let (_, _, _, node_address, _) = get_network_constants();
844
845        let result = sdk.get_binary_network_name(Some(node_address)).await;
846        let network_name = result.unwrap();
847        assert!(!network_name.into_inner().to_string().is_empty());
848    }
849
850    #[tokio::test]
851    async fn test_get_binary_consensus_validator_changes_success() {
852        let sdk = SDK::new(None, None, None);
853        let (_, _, _, node_address, _) = get_network_constants();
854
855        let result = sdk
856            .get_binary_consensus_validator_changes(Some(node_address))
857            .await;
858        let changes = result.unwrap();
859        // Todo check empty
860        assert!(changes.into_inner().is_empty());
861    }
862
863    #[tokio::test]
864    async fn test_get_binary_block_synchronizer_status_success() {
865        let sdk = SDK::new(None, None, None);
866        let (_, _, _, node_address, _) = get_network_constants();
867
868        let result = sdk
869            .get_binary_block_synchronizer_status(Some(node_address))
870            .await;
871        let status = result.unwrap();
872        assert!(!status.to_bytes().unwrap().is_empty());
873    }
874
875    #[tokio::test]
876    async fn test_get_binary_available_block_range_success() {
877        let sdk = SDK::new(None, None, None);
878        let (_, _, _, node_address, _) = get_network_constants();
879
880        let result = sdk
881            .get_binary_available_block_range(Some(node_address))
882            .await;
883        let block_range = result.unwrap();
884        assert!(block_range.low() <= block_range.high());
885    }
886
887    #[tokio::test]
888    async fn test_get_binary_next_upgrade_success() {
889        let sdk = SDK::new(None, None, None);
890        let (_, _, _, node_address, _) = get_network_constants();
891
892        let result = sdk.get_binary_next_upgrade(Some(node_address)).await;
893        let upgrade = result.unwrap();
894        // TODO check
895        assert!(upgrade.is_none());
896    }
897
898    #[tokio::test]
899    async fn test_get_binary_consensus_status_success() {
900        let sdk = SDK::new(None, None, None);
901        let (_, _, _, node_address, _) = get_network_constants();
902
903        let result = sdk.get_binary_consensus_status(Some(node_address)).await;
904        let status = result.unwrap();
905        assert!(!status.validator_public_key().to_string().is_empty());
906        assert!(status.round_length().is_some())
907    }
908
909    #[tokio::test]
910    async fn test_get_binary_chainspec_raw_bytes_success() {
911        let sdk = SDK::new(None, None, None);
912        let (_, _, _, node_address, _) = get_network_constants();
913
914        let result = sdk.get_binary_chainspec_raw_bytes(Some(node_address)).await;
915        let chainspec = result.unwrap();
916        assert!(!chainspec.chainspec_bytes().is_empty());
917    }
918
919    #[tokio::test]
920    async fn test_get_binary_node_status_success() {
921        let sdk = SDK::new(None, None, None);
922        let (_, _, _, node_address, _) = get_network_constants();
923
924        let get_binary_node_status = sdk.get_binary_node_status(Some(node_address)).await;
925
926        let get_binary_node_status = get_binary_node_status.unwrap();
927        assert!(!get_binary_node_status
928            .protocol_version
929            .to_string()
930            .is_empty());
931        assert!(!get_binary_node_status.chainspec_name.to_string().is_empty());
932    }
933
934    #[tokio::test]
935    async fn test_get_binary_validator_reward_by_era_success() {
936        let sdk = SDK::new(None, None, None);
937        let (_, _, _, node_address, _) = get_network_constants();
938        // TODO Get validator key
939        let secret_key = get_user_secret_key(None).unwrap();
940        let secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
941        let validator_key = PublicKey::from(&secret_key_from_pem);
942        let era = EraId::new(1);
943
944        let result = sdk
945            .get_binary_validator_reward_by_era(Some(node_address), validator_key, era)
946            .await;
947
948        // TODO
949        let reward = result.unwrap();
950        assert!(reward.is_none());
951    }
952
953    #[tokio::test]
954    async fn test_get_binary_validator_reward_by_block_height_success() {
955        let sdk = SDK::new(None, None, None);
956        let (_, _, _, node_address, _) = get_network_constants();
957        // TODO Get validator key
958        let secret_key = get_user_secret_key(None).unwrap();
959        let secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
960        let validator_key = PublicKey::from(&secret_key_from_pem);
961        let block_height = 1;
962
963        let result = sdk
964            .get_binary_validator_reward_by_block_height(
965                Some(node_address),
966                validator_key,
967                block_height,
968            )
969            .await;
970
971        // TODO
972        let reward = result.unwrap();
973        assert!(reward.is_none());
974    }
975
976    #[tokio::test]
977    async fn test_get_binary_validator_reward_by_block_hash_success() {
978        let sdk = SDK::new(None, None, None);
979        let (_, _, _, node_address, _) = get_network_constants();
980        // TODO Get validator key
981        let secret_key = get_user_secret_key(None).unwrap();
982        let secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
983        let validator_key = PublicKey::from(&secret_key_from_pem);
984
985        let block_height = 1;
986
987        let result = sdk
988            .get_binary_block_with_signatures_by_height(Some(node_address.clone()), block_height)
989            .await;
990        let signed_block = result.unwrap();
991        assert!(signed_block.is_some());
992        let signed_block = signed_block.unwrap();
993        assert_eq!(signed_block.block().height(), block_height);
994        let block_hash = signed_block.block().hash();
995        assert!(!block_hash.to_string().is_empty());
996
997        let result = sdk
998            .get_binary_validator_reward_by_block_hash(
999                Some(node_address),
1000                validator_key,
1001                *block_hash,
1002            )
1003            .await;
1004
1005        // TODO
1006        let reward = result.unwrap();
1007        assert!(reward.is_none());
1008    }
1009
1010    #[tokio::test]
1011    async fn test_get_binary_delegator_reward_by_era_success() {
1012        let sdk = SDK::new(None, None, None);
1013        let (_, _, _, node_address, _) = get_network_constants();
1014        // TODO Get delegator key
1015        let secret_key = get_user_secret_key(None).unwrap();
1016        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1017        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1018
1019        // TODO Get Delegator key
1020        let secret_key = get_user_secret_key(Some("user-2")).unwrap();
1021        let delegator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1022        let delegator_key = PublicKey::from(&delegator_secret_key_from_pem);
1023
1024        let era = EraId::new(1);
1025
1026        let result = sdk
1027            .get_binary_delegator_reward_by_era(
1028                Some(node_address),
1029                validator_key,
1030                delegator_key,
1031                era,
1032            )
1033            .await;
1034
1035        // TODO
1036        let reward = result.unwrap();
1037        assert!(reward.is_none());
1038    }
1039
1040    #[tokio::test]
1041    async fn test_get_binary_delegator_reward_by_block_height_success() {
1042        let sdk = SDK::new(None, None, None);
1043        let (_, _, _, node_address, _) = get_network_constants();
1044        // TODO Get delegator key
1045        let secret_key = get_user_secret_key(None).unwrap();
1046        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1047        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1048
1049        // TODO Get Delegator key
1050        let secret_key = get_user_secret_key(Some("user-2")).unwrap();
1051        let delegator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1052        let delegator_key = PublicKey::from(&delegator_secret_key_from_pem);
1053        let block_height = 1;
1054
1055        let result = sdk
1056            .get_binary_delegator_reward_by_block_height(
1057                Some(node_address),
1058                validator_key,
1059                delegator_key,
1060                block_height,
1061            )
1062            .await;
1063
1064        // TODO
1065        let reward = result.unwrap();
1066        assert!(reward.is_none());
1067    }
1068
1069    #[tokio::test]
1070    async fn test_get_binary_delegator_reward_by_block_hash_success() {
1071        let sdk = SDK::new(None, None, None);
1072        let (_, _, _, node_address, _) = get_network_constants();
1073        // TODO Get delegator key
1074        let secret_key = get_user_secret_key(None).unwrap();
1075        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1076        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1077
1078        // TODO Get Delegator key
1079        let secret_key = get_user_secret_key(Some("user-2")).unwrap();
1080        let delegator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1081        let delegator_key = PublicKey::from(&delegator_secret_key_from_pem);
1082
1083        let block_height = 1;
1084
1085        let result = sdk
1086            .get_binary_block_with_signatures_by_height(Some(node_address.clone()), block_height)
1087            .await;
1088        let signed_block = result.unwrap();
1089        assert!(signed_block.is_some());
1090        let signed_block = signed_block.unwrap();
1091        assert_eq!(signed_block.block().height(), block_height);
1092        let block_hash = signed_block.block().hash();
1093        assert!(!block_hash.to_string().is_empty());
1094
1095        let result = sdk
1096            .get_binary_delegator_reward_by_block_hash(
1097                Some(node_address),
1098                validator_key,
1099                delegator_key,
1100                *block_hash,
1101            )
1102            .await;
1103
1104        // TODO
1105        let reward = result.unwrap();
1106        assert!(reward.is_none());
1107    }
1108
1109    #[tokio::test]
1110    async fn test_get_binary_read_record_success() {
1111        let sdk = SDK::new(None, None, None);
1112        let (_, _, _, node_address, _) = get_network_constants();
1113        let record_id = RecordId::BlockHeader;
1114        let key = b"record_key";
1115
1116        let result = sdk
1117            .get_binary_read_record(Some(node_address), record_id, key)
1118            .await;
1119
1120        // TODO
1121        let reward = result.unwrap();
1122        assert!(reward.is_empty());
1123    }
1124
1125    #[tokio::test]
1126    async fn test_get_binary_global_state_item_success() {
1127        let sdk = SDK::new(None, None, None);
1128        let (_, _, _, node_address, _) = get_network_constants();
1129
1130        let secret_key = get_user_secret_key(None).unwrap();
1131        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1132        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1133
1134        let key = Key::Account(validator_key.to_account_hash());
1135
1136        let path = vec![];
1137
1138        let result = sdk
1139            .get_binary_global_state_item(Some(node_address), key, path)
1140            .await;
1141        let item = result.unwrap();
1142        assert!(item.is_some());
1143    }
1144
1145    #[tokio::test]
1146    async fn test_get_binary_global_state_item_by_state_root_hash_success() {
1147        let sdk = SDK::new(None, None, None);
1148        let (_, _, _, node_address, _) = get_network_constants();
1149
1150        let secret_key = get_user_secret_key(None).unwrap();
1151        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1152        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1153
1154        let key = Key::Account(validator_key.to_account_hash());
1155        let path = vec![];
1156        let height = 1;
1157
1158        let result = sdk
1159            .get_binary_block_header_by_height(Some(node_address.clone()), height)
1160            .await;
1161
1162        let block_header = result.unwrap();
1163        assert!(block_header.is_some());
1164        let block_header = block_header.unwrap();
1165        let state_root_hash = block_header.state_root_hash();
1166
1167        assert!(!state_root_hash.to_string().is_empty());
1168
1169        let result = sdk
1170            .get_binary_global_state_item_by_state_root_hash(
1171                Some(node_address),
1172                *state_root_hash,
1173                key,
1174                path,
1175            )
1176            .await;
1177        let item = result.unwrap();
1178        assert!(item.is_some());
1179    }
1180
1181    #[tokio::test]
1182    async fn test_get_binary_global_state_item_by_block_hash_success() {
1183        let sdk = SDK::new(None, None, None);
1184        let (_, _, _, node_address, _) = get_network_constants();
1185        let secret_key = get_user_secret_key(None).unwrap();
1186        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1187        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1188
1189        let key = Key::Account(validator_key.to_account_hash());
1190        let path = vec![];
1191
1192        let block_height = 1;
1193
1194        let result = sdk
1195            .get_binary_block_header_by_height(Some(node_address.clone()), block_height)
1196            .await;
1197
1198        let block_header = result.unwrap();
1199        assert!(block_header.is_some());
1200        let block_header = block_header.unwrap();
1201        let block_hash = block_header.block_hash();
1202
1203        assert!(!block_hash.to_string().is_empty());
1204
1205        let result = sdk
1206            .get_binary_global_state_item_by_block_hash(Some(node_address), block_hash, key, path)
1207            .await;
1208        let item = result.unwrap();
1209        assert!(item.is_some());
1210    }
1211
1212    #[tokio::test]
1213    async fn test_get_binary_global_state_item_by_block_height_success() {
1214        let sdk = SDK::new(None, None, None);
1215        let (_, _, _, node_address, _) = get_network_constants();
1216        let secret_key = get_user_secret_key(None).unwrap();
1217        let validator_secret_key_from_pem = secret_key_from_pem(&secret_key).unwrap();
1218        let validator_key = PublicKey::from(&validator_secret_key_from_pem);
1219
1220        let key = Key::Account(validator_key.to_account_hash());
1221        let path = vec![];
1222
1223        let block_height = 1;
1224
1225        let result = sdk
1226            .get_binary_global_state_item_by_block_height(
1227                Some(node_address),
1228                block_height,
1229                key,
1230                path,
1231            )
1232            .await;
1233        let item = result.unwrap();
1234        assert!(item.is_some());
1235    }
1236
1237    #[tokio::test]
1238    async fn test_get_binary_try_accept_transaction_success() {
1239        let sdk = SDK::new(None, None, None);
1240        let (_, _, _, node_address, chain_name) = get_network_constants();
1241
1242        let secret_key = get_user_secret_key(None).unwrap();
1243        let initiator_addr = public_key_from_secret_key(&secret_key).unwrap();
1244
1245        let transaction_params = TransactionStrParams::default();
1246        transaction_params.set_secret_key(&secret_key);
1247        transaction_params.set_chain_name(&chain_name);
1248        transaction_params.set_payment_amount(TRANSFER_AMOUNT);
1249
1250        let make_transfer_transaction = sdk
1251            .make_transfer_transaction(
1252                None,
1253                &initiator_addr, // self transfer
1254                TRANSFER_AMOUNT,
1255                transaction_params,
1256                None,
1257            )
1258            .unwrap();
1259        assert!(!make_transfer_transaction.hash().to_string().is_empty());
1260
1261        let result = sdk
1262            .get_binary_try_accept_transaction(Some(node_address), make_transfer_transaction.into())
1263            .await;
1264
1265        assert!(result.is_ok());
1266    }
1267
1268    #[tokio::test]
1269    #[ignore]
1270    async fn _test_get_binary_try_speculative_execution_success() {
1271        let sdk = SDK::new(None, None, None);
1272        let (_, _, _, node_address, chain_name) = get_network_constants();
1273
1274        let secret_key = get_user_secret_key(None).unwrap();
1275        let initiator_addr = public_key_from_secret_key(&secret_key).unwrap();
1276
1277        let transaction_params = TransactionStrParams::default();
1278        transaction_params.set_secret_key(&secret_key);
1279        transaction_params.set_chain_name(&chain_name);
1280        transaction_params.set_payment_amount(TRANSFER_AMOUNT);
1281
1282        let make_transfer_transaction = sdk
1283            .make_transfer_transaction(
1284                None,
1285                &initiator_addr, // self transfer
1286                TRANSFER_AMOUNT,
1287                transaction_params,
1288                None,
1289            )
1290            .unwrap();
1291        assert!(!make_transfer_transaction.hash().to_string().is_empty());
1292
1293        let result = sdk
1294            .get_binary_try_speculative_execution(
1295                Some(node_address),
1296                make_transfer_transaction.into(),
1297            )
1298            .await;
1299
1300        // TODO check transaction V1 in speculative exec
1301        assert!(result.is_ok());
1302    }
1303
1304    #[tokio::test]
1305    async fn test_get_binary_protocol_version_success() {
1306        let sdk = SDK::new(None, None, None);
1307        let (_, _, _, node_address, _) = get_network_constants();
1308
1309        let result = sdk.get_binary_protocol_version(Some(node_address)).await;
1310        let protocol_version = result.unwrap();
1311        assert!(!protocol_version.value().to_string().is_empty());
1312    }
1313}