hedera 0.43.0

The SDK for interacting with Hedera Hashgraph.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use std::collections::HashMap;

use hedera::{
    AccountCreateTransaction,
    AccountDeleteTransaction,
    AccountId,
    Client,
    Hbar,
    NodeAddressBookQuery,
    NodeUpdateTransaction,
    PrivateKey,
    ServiceEndpoint,
    Status,
};

/// Helper function to setup client with local DAB tests configuration
fn setup_dab_tests_client() -> Client {
    let mut network = HashMap::new();
    network.insert("127.0.0.1:50211".to_string(), AccountId::new(0, 0, 3));
    network.insert("127.0.0.1:51211".to_string(), AccountId::new(0, 0, 4));

    let client = Client::for_network(network).unwrap();
    client.set_mirror_network(vec!["127.0.0.1:5600".to_string()]);

    // Set the operator to be account 0.0.2
    let operator_account_id = AccountId::new(0, 0, 2);
    let operator_key = PrivateKey::from_str_der(
        "302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137",
    )
    .unwrap();

    client.set_operator(operator_account_id, operator_key);
    client
}

#[tokio::test]
async fn can_execute_node_update_transaction() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    let response = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .description("testUpdated")
        .decline_reward(true)
        .grpc_proxy_endpoint(ServiceEndpoint {
            ip_address_v4: None,
            port: 123456,
            domain_name: "testWebUpdatedsdfsdfsdfsdf.com".to_owned(),
        })
        .execute(&client)
        .await?;

    let receipt = response.get_receipt(&client).await?;
    assert_eq!(receipt.status, Status::Success);

    Ok(())
}

#[tokio::test]
async fn can_delete_grpc_web_proxy_endpoint() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    let response = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .delete_grpc_proxy_endpoint()
        .execute(&client)
        .await?;

    let receipt = response.get_receipt(&client).await?;
    assert_eq!(receipt.status, Status::Success);

    Ok(())
}

#[tokio::test]
async fn can_change_node_account_id_and_revert_back() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Change node account ID from 0.0.8 to 0.0.2
    let response1 = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .account_id(AccountId::new(0, 0, 2))
        .execute(&client)
        .await?;

    let receipt1 = response1.get_receipt(&client).await?;
    assert_eq!(receipt1.status, Status::Success);

    // Revert the ID back to 0.0.8
    let response2 = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .account_id(AccountId::new(0, 0, 3))
        .execute(&client)
        .await?;

    let receipt2 = response2.get_receipt(&client).await?;
    assert_eq!(receipt2.status, Status::Success);

    Ok(())
}

#[tokio::test]
async fn fails_with_invalid_signature_when_updating_without_admin_key() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Create a new account to be the operator
    let new_operator_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_operator_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .initial_balance(Hbar::new(2))
        .execute(&client)
        .await?;

    let create_receipt = create_resp.get_receipt(&client).await?;
    let new_operator = create_receipt.account_id.unwrap();

    // Set the new account as operator
    client.set_operator(new_operator, new_operator_key);

    // Try to update node account ID without admin key signature
    let res = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .description("testUpdated")
        .account_id(AccountId::new(0, 0, 50))
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert_matches::assert_matches!(
        res,
        Err(hedera::Error::ReceiptStatus { status: Status::InvalidSignature, .. })
    );

    Ok(())
}

#[tokio::test]
async fn can_change_node_account_id_to_the_same_account() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    let response = NodeUpdateTransaction::new()
        .node_id(1)
        .node_account_ids(vec![AccountId::new(0, 0, 3)])
        .account_id(AccountId::new(0, 0, 4))
        .execute(&client)
        .await?;

    let receipt = response.get_receipt(&client).await?;
    assert_eq!(receipt.status, Status::Success);

    Ok(())
}

#[tokio::test]
async fn fails_when_changing_to_non_existent_account_id() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    let res = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .description("testUpdated")
        .account_id(AccountId::new(0, 0, 999999999))
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert_matches::assert_matches!(
        res,
        Err(hedera::Error::ReceiptStatus { status: Status::InvalidSignature, .. })
    );

    Ok(())
}

#[tokio::test]
async fn fails_when_changing_node_account_id_without_account_key() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Create a new account
    let new_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .initial_balance(Hbar::new(2))
        .execute(&client)
        .await?;

    let create_receipt = create_resp.get_receipt(&client).await?;
    let new_node_account_id = create_receipt.account_id.unwrap();

    // Try to set node account ID to new account without signing with new account's key
    let res = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .description("testUpdated")
        .account_id(new_node_account_id)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert_matches::assert_matches!(
        res,
        Err(hedera::Error::ReceiptStatus { status: Status::InvalidSignature, .. })
    );

    Ok(())
}

#[tokio::test]
async fn fails_when_changing_to_deleted_account_id() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Create a new account
    let new_account_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_account_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .execute(&client)
        .await?;

    let create_receipt = create_resp.get_receipt(&client).await?;
    let new_account = create_receipt.account_id.unwrap();

    // Delete the account
    let delete_resp = AccountDeleteTransaction::new()
        .account_id(new_account)
        .transfer_account_id(client.get_operator_account_id().unwrap())
        .freeze_with(&client)?
        .sign(new_account_key.clone())
        .execute(&client)
        .await?;

    delete_resp.get_receipt(&client).await?;

    // Try to set node account ID to deleted account
    let res = NodeUpdateTransaction::new()
        .node_id(0)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .description("testUpdated")
        .account_id(new_account)
        .freeze_with(&client)?
        .sign(new_account_key)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    assert_matches::assert_matches!(
        res,
        Err(hedera::Error::ReceiptStatus { status: Status::AccountDeleted, .. })
    );

    Ok(())
}

#[tokio::test]
async fn fails_when_new_node_account_has_zero_balance() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Create a new account with zero balance
    let new_account_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_account_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .execute(&client)
        .await?;

    let create_receipt = create_resp.get_receipt(&client).await?;
    let new_account = create_receipt.account_id.unwrap();

    // Try to set node account ID to account with zero balance
    let res = NodeUpdateTransaction::new()
        .node_id(0)
        .description("testUpdated")
        .account_id(new_account)
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .freeze_with(&client)?
        .sign(new_account_key)
        .execute(&client)
        .await?
        .get_receipt(&client)
        .await;

    // Should fail with status code 526 (NODE_ACCOUNT_HAS_ZERO_BALANCE)
    assert_matches::assert_matches!(
        res,
        Err(hedera::Error::ReceiptStatus { status: Status::NodeAccountHasZeroBalance, .. })
    );

    Ok(())
}

#[tokio::test]
async fn updates_addressbook_and_retries_after_node_account_id_change() -> anyhow::Result<()> {
    let client = setup_dab_tests_client();

    // Create the account that will be the new node account ID
    let new_account_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_account_key.public_key())
        .initial_balance(Hbar::new(1))
        .execute(&client)
        .await?;

    let create_receipt = create_resp.get_receipt(&client).await?;
    let new_node_account_id = create_receipt.account_id.unwrap();

    // Update node account ID (0.0.4 -> new_node_account_id)
    let update_resp = NodeUpdateTransaction::new()
        .node_id(1)
        .account_id(new_node_account_id)
        .node_account_ids(vec![AccountId::new(0, 0, 3)])
        .freeze_with(&client)?
        .sign(new_account_key)
        .execute(&client)
        .await?;

    update_resp.get_receipt(&client).await?;

    // Wait for mirror node to import data
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

    let another_new_key = PrivateKey::generate_ed25519();

    // Submit to the updated node - should trigger addressbook refresh
    // We force this request to go to the node that changed (0.0.4) to ensure we hit the "InvalidNodeAccount" error
    // which triggers the address book update.
    // We set max attempts to 1 because we expect this specific request to fail (it's hitting the old node account ID),
    // but we just want the side effect of updating the address book.
    client.set_max_attempts(1);

    let _ = AccountCreateTransaction::new()
        .set_key_without_alias(another_new_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4)])
        .execute(&client)
        .await;

    // Reset max attempts for subsequent requests
    client.set_max_attempts(10);

    // Verify address book has been updated
    let network = client.network();
    let has_new_node_account =
        network.values().any(|account_id| *account_id == new_node_account_id);

    println!("network: {:?}", network);
    assert!(has_new_node_account, "Address book should contain the new node account ID");

    // Check if the new node account has the specific address and apply workaround if needed
    let network = client.network();
    let node_address = network
        .iter()
        .find(|(_, account_id)| **account_id == new_node_account_id)
        .map(|(address, _)| address);

    if let Some(address) = node_address {
        assert_eq!(
            address,
            "network-node2-svc.solo.svc.cluster.local:50211",
            "Expected node with account {} to have address network-node2-svc.solo.svc.cluster.local:50211",
            new_node_account_id
        );

        // Apply workaround: change port from 50211 to 51211
        let mut updated_network = HashMap::new();
        for (addr, acc_id) in network.iter() {
            if *acc_id == new_node_account_id {
                updated_network
                    .insert("network-node2-svc.solo.svc.cluster.local:51211".to_string(), *acc_id);
            } else {
                updated_network.insert(addr.clone(), *acc_id);
            }
        }
        client.set_network(updated_network)?;
    }

    // This transaction should succeed with the new node account ID
    let final_resp = AccountCreateTransaction::new()
        .set_key_without_alias(another_new_key.public_key())
        .node_account_ids(vec![new_node_account_id])
        .execute(&client)
        .await?;

    let final_receipt = final_resp.get_receipt(&client).await?;
    assert_eq!(final_receipt.status, Status::Success);

    // Revert the node account ID
    let revert_resp = NodeUpdateTransaction::new()
        .node_id(1)
        .node_account_ids(vec![new_node_account_id])
        .account_id(AccountId::new(0, 0, 4))
        .execute(&client)
        .await?;

    revert_resp.get_receipt(&client).await?;

    Ok(())
}

#[tokio::test]
async fn handles_node_account_id_change_without_mirror_node_setup() -> anyhow::Result<()> {
    // Create a client without mirror network
    let network_client = setup_dab_tests_client();

    // Remove mirror network
    network_client.set_mirror_network(vec![]);

    // Create the account that will be the new node account ID
    let new_account_key = PrivateKey::generate_ed25519();
    let create_resp = AccountCreateTransaction::new()
        .set_key_without_alias(new_account_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 3), AccountId::new(0, 0, 4)])
        .initial_balance(Hbar::new(1))
        .execute(&network_client)
        .await?;

    let create_receipt = create_resp.get_receipt(&network_client).await?;
    let new_node_account_id = create_receipt.account_id.unwrap();

    // Update node account ID
    let update_resp = NodeUpdateTransaction::new()
        .node_id(1)
        .account_id(new_node_account_id)
        .node_account_ids(vec![AccountId::new(0, 0, 3)])
        .freeze_with(&network_client)?
        .sign(new_account_key)
        .execute(&network_client)
        .await?;

    update_resp.get_receipt(&network_client).await?;

    // Wait for changes to propagate
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

    let another_new_key = PrivateKey::generate_ed25519();

    // Submit transaction - should retry since no mirror node to update addressbook
    let test_resp = AccountCreateTransaction::new()
        .set_key_without_alias(another_new_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4), AccountId::new(0, 0, 3)])
        .execute(&network_client)
        .await?;

    let test_receipt = test_resp.get_receipt(&network_client).await?;
    assert_eq!(test_receipt.status, Status::Success);

    // Verify address book has NOT been updated (no mirror node)
    let network = network_client.network();
    let node1 = network.iter().find(|(_, account_id)| **account_id == AccountId::new(0, 0, 3));
    let node2 = network.iter().find(|(_, account_id)| **account_id == AccountId::new(0, 0, 4));

    assert!(node1.is_some(), "Node 0.0.3 should still be in the network state");
    assert!(node2.is_some(), "Node 0.0.4 should still be in the network state");

    // This transaction should succeed with retries
    let final_resp = AccountCreateTransaction::new()
        .set_key_without_alias(another_new_key.public_key())
        .node_account_ids(vec![AccountId::new(0, 0, 4), AccountId::new(0, 0, 3)])
        .execute(&network_client)
        .await?;

    let final_receipt = final_resp.get_receipt(&network_client).await?;
    assert_eq!(final_receipt.status, Status::Success);

    // Revert the node account ID
    let revert_resp = NodeUpdateTransaction::new()
        .node_id(1)
        .node_account_ids(vec![AccountId::new(0, 0, 3)])
        .account_id(AccountId::new(0, 0, 4))
        .execute(&network_client)
        .await?;

    revert_resp.get_receipt(&network_client).await?;
    // Wait for changes to propagate
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

    Ok(())
}