Skip to content

Commit 908258d

Browse files
added network property to AleoNetworkManager. changed use of Object::entries in wasm/src/programs/execution.rs to Object::keys (#1027)
1 parent d5c5eda commit 908258d

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

sdk/src/network-client.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ interface AleoNetworkClientOptions {
3333
* const publicNetworkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined, account);
3434
*/
3535
class AleoNetworkClient {
36-
host: string;
37-
headers: { [key: string]: string };
38-
account: Account | undefined;
39-
40-
constructor(host: string, options?: AleoNetworkClientOptions | undefined) {
41-
this.host = host + "/%%NETWORK%%";
36+
host: string;
37+
headers: { [key: string]: string };
38+
account: Account | undefined;
39+
readonly network: string;
40+
41+
constructor(host: string, options?: AleoNetworkClientOptions) {
42+
this.host = host + "/%%NETWORK%%";
43+
this.network = "%%NETWORK%%";
4244

4345
if (options && options.headers) {
4446
this.headers = options.headers;

sdk/src/program-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Account } from "./account";
2-
import { AleoNetworkClient, ProgramImports } from "./network-client";
2+
import { AleoNetworkClient, AleoNetworkClientOptions, ProgramImports } from "./network-client";
33
import { ImportedPrograms, ImportedVerifyingKeys } from "./models/imports";
44
import { RecordProvider, RecordSearchParams } from "./record-provider";
55

sdk/tests/network-client.test.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,11 @@ async function expectThrows(f: () => Promise<any>): Promise<void> {
3535

3636
describe("NodeConnection", () => {
3737
let connection: AleoNetworkClient;
38-
let network: string;
3938
let windowFetchSpy: sinon.SinonSpy;
4039

4140
beforeEach(() => {
42-
connection = new AleoNetworkClient(
43-
"https://api.explorer.provable.com/v1",
44-
);
45-
if (
46-
connection.host === "https://api.explorer.provable.com/v1/testnet"
47-
) {
48-
network = "testnet";
49-
} else {
50-
network = "mainnet";
51-
}
52-
windowFetchSpy = sinon.spy(globalThis, "fetch");
41+
connection = new AleoNetworkClient("https://api.explorer.provable.com/v1");
42+
windowFetchSpy = sinon.spy(globalThis, 'fetch');
5343
});
5444

5545
afterEach(() => {
@@ -381,9 +371,9 @@ describe("NodeConnection", () => {
381371
});
382372
});
383373

384-
describe("Test API methods that return wasm objects", () => {
385-
it("Plaintext returned from the API should have expected properties", async () => {
386-
if (network === "testnet") {
374+
describe('Test API methods that return wasm objects', () => {
375+
it('Plaintext returned from the API should have expected properties', async () => {
376+
if (connection.network === "testnet") {
387377
// Check a struct variant of a plaintext object.
388378
let plaintext = await connection.getProgramMappingPlaintext(
389379
"credits.aleo",
@@ -414,10 +404,8 @@ describe("NodeConnection", () => {
414404

415405
it("should have correct data within the wasm object and summary object for an execution transaction", async () => {
416406
// Get the first transaction at block 24700 on testnet.
417-
if (network === "testnet") {
418-
const transaction = await connection.getTransactionObject(
419-
"at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd",
420-
);
407+
if (connection.network === "testnet") {
408+
const transaction = await connection.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
421409
const transition = <Transition>transaction.transitions()[0];
422410
const summary = <TransactionObject>transaction.summary(true);
423411

@@ -527,10 +515,8 @@ describe("NodeConnection", () => {
527515

528516
it("should have correct data within the wasm object and summary object for a deployment transaction", async () => {
529517
// Get the deployment transaction for token_registry.aleo
530-
if (network === "mainnet") {
531-
const transaction = await connection.getTransactionObject(
532-
"at15mwg0jyhvpjjrfxwrlwzn8puusnmy7r3xzvpjht4e5gzgnp68q9qd0qqec",
533-
);
518+
if (connection.network === "mainnet") {
519+
const transaction = await connection.getTransactionObject("at15mwg0jyhvpjjrfxwrlwzn8puusnmy7r3xzvpjht4e5gzgnp68q9qd0qqec");
534520
const summary = <TransactionObject>transaction.summary(true);
535521
const deployment = <DeploymentObject>summary.deployment;
536522

@@ -558,8 +544,8 @@ describe("NodeConnection", () => {
558544
}
559545
});
560546

561-
it("Should give the correct JSON response when requesting multiple transactions", async () => {
562-
if (network === "testnet") {
547+
it('Should give the correct JSON response when requesting multiple transactions', async () => {
548+
if (connection.network === "testnet") {
563549
const transactions = await connection.getTransactions(27400);
564550
expect(transactions.length).equal(4);
565551
expect(transactions[0].status).equal("accepted");

sdk/tests/program-manager.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { expect } from "chai";
1414
describe('Program Manager', () => {
1515
const programManager = new ProgramManager("https://api.explorer.provable.com/v1");
1616
programManager.setAccount(new Account({privateKey: statePathRecordOwnerPrivateKey}));
17-
const network = (<AleoNetworkClient>programManager.networkClient).host === "https://api.explorer.provable.com/v1/testnet" ? "testnet" : "mainnet";
17+
const network = programManager.networkClient.network;
1818

1919
describe('Instantiate with AleoNetworkClientOptions', () => {
2020
it('should have the specified headers when instantiated', async () => {

wasm/src/programs/execution.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@
1616

1717
pub use super::*;
1818

19-
use crate::{log, native::ProgramIDNative, types::native::{
20-
CurrentNetwork, ExecutionNative, IdentifierNative, ProcessNative, ProgramID, ProgramNative, VerifyingKeyNative
21-
}, Transition};
19+
use crate::{
20+
Transition,
21+
log,
22+
native::ProgramIDNative,
23+
types::native::{
24+
CurrentNetwork,
25+
ExecutionNative,
26+
IdentifierNative,
27+
ProcessNative,
28+
ProgramID,
29+
ProgramNative,
30+
VerifyingKeyNative,
31+
},
32+
};
2233
use snarkvm_algorithms::snark::varuna::VarunaVersion;
2334

2435
use js_sys::{Array, Object, Reflect};
@@ -123,14 +134,14 @@ pub fn verify_function_execution(
123134
// Secondly, get the verifying keys and insert them into the process object.
124135
if let Some(imported_verifying_keys) = imported_verifying_keys {
125136
// Go through the imports and get the program IDs.
126-
let program_ids = Object::entries(&imported_verifying_keys)
137+
let program_ids = Object::keys(&imported_verifying_keys)
127138
.iter()
128-
.filter_map(|entries| Array::try_from(entries).unwrap().get(0).as_string()) // Safe unwraps because `entries` returns arrays with string keys.
129139
.map(|entry| {
140+
let entry = entry.as_string().unwrap(); // Safe unwraps because `keys` returns array of string keys.
130141
ProgramIDNative::from_str(&entry)
131142
.map_err(|_| format!("Program ID not found in imports provided: {entry}"))
132143
})
133-
.collect::<Result<Vec<_>,_>>()?;
144+
.collect::<Result<Vec<_>, _>>()?;
134145

135146
// Go through the imports and insert the verifying keys for each function.
136147
for imported_program_id in &program_ids {
@@ -141,8 +152,8 @@ pub fn verify_function_execution(
141152
)
142153
.map_err(|_| format!("Verifying key not found for imported program {}", imported_program_id))?;
143154
// Get the verifying key for each function.
144-
for i in 0..vk_list.length() {
145-
let vk = Array::try_from(vk_list.get(i)).map_err(|_| format!("Verifying key and function not found for {}, for each function provide an array of the form ['function_name', 'vk']", imported_program_id))?;
155+
for vk in vk_list.iter() {
156+
let vk = Array::try_from(vk).map_err(|_| format!("Verifying key and function not found for {}, for each function provide an array of the form ['function_name', 'vk']", imported_program_id))?;
146157
{
147158
// Insert the verifying key into the temporary process.
148159
let imported_function = IdentifierNative::from_str(

0 commit comments

Comments
 (0)