Skip to content

Commit

Permalink
feat: strips out bitcoin query methods from management canister IDL (#…
Browse files Browse the repository at this point in the history
…893)

* feat: strips out bitcoin query methods from management canister IDL

* adds wasm_memory_limit to canister settings

* removes bitcoin query e2e test
  • Loading branch information
krpeacock authored Jun 12, 2024
1 parent e78676f commit 796fcef
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 317 deletions.
76 changes: 70 additions & 6 deletions bin/update-management-idl.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { $, cd, fetch } from 'zx';
import path from 'path';

fetch(
'https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did',
).then(async res => {
const main = async () => {
const res = await fetch(
'https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did',
);
res.text().then(async text => {
const root = path.resolve(__dirname, '..');

const candid = text;
// TODO: remove this function once the bitcoin queries are removed from the candid spec
const candid = stripBitcoinQueries(text);

await cd(`${root}/packages/agent/src/canisters`);

await $`echo ${candid} > management.did`;

// Format the candid file
await $`npx prettier --write --plugin=prettier-plugin-motoko **/*.did`;

// Generate the idl and interface files
let ts = (await $`didc bind management.did -t ts`).toString();
let js = (await $`didc bind management.did -t js`).toString();

Expand All @@ -27,7 +34,6 @@ fetch(
`;

// replace first line of service

ts = ts.replace(`export interface _SERVICE {`, `export default interface _SERVICE {`);
js = js.replace(`export const idlFactory = ({ IDL }) => {`, `export default ({ IDL }) => {`);

Expand All @@ -37,13 +43,71 @@ fetch(
ts = prefix + ts;
js = prefix + js;

// write the files
await $`echo ${js} > management_idl.ts`;
await $`echo ${ts} > management_service.ts`;

await cd(`${root}`);

// Format the generated files
await $`npm run prettier:format`;

console.log('Done!');
});
});
};

function stripBitcoinQueries(text: string): string {
// get_utxos_query
let newText = text.replace(
`type bitcoin_get_utxos_query_args = record {
address : bitcoin_address;
network : bitcoin_network;
filter : opt variant {
min_confirmations : nat32;
page : blob;
};
};`,
'',
);
newText = newText.replace(
`
type bitcoin_get_utxos_query_result = record {
utxos : vec utxo;
tip_block_hash : block_hash;
tip_height : nat32;
next_page : opt blob;
};`,
'',
);

newText = newText.replace(
`bitcoin_get_utxos_query : (bitcoin_get_utxos_query_args) -> (bitcoin_get_utxos_query_result) query;`,
'',
);

// bitcoin_get_balance_query
newText = newText.replace(
`bitcoin_get_balance_query : (bitcoin_get_balance_query_args) -> (bitcoin_get_balance_query_result) query;`,
'',
);

newText = newText.replace(
`
type bitcoin_get_balance_query_args = record {
address : bitcoin_address;
network : bitcoin_network;
min_confirmations : opt nat32;
};`,
'',
);

newText = newText.replace(
`
type bitcoin_get_balance_query_result = satoshi;`,
'',
);

return newText;
}

main();
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Changes default stored key for `auth-client` to use ECDSAKey* Also updates the

### Changed

* feat: strips out bitcoin query methods from management canister IDL
* Adds retry logic to `HttpAgent`. By default, retries three times before throwing an error, to offer a more cohesive workflow
* Improves and truncates error messages in Candid
* fixes flaky tests for syncTime
Expand Down
26 changes: 1 addition & 25 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Actor,
AnonymousIdentity,
HttpAgent,
Identity,
CanisterStatus,
getManagementCanister,
} from '@dfinity/agent';
import { Actor, AnonymousIdentity, HttpAgent, Identity, CanisterStatus } from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Principal } from '@dfinity/principal';
Expand Down Expand Up @@ -168,20 +161,3 @@ describe('controllers', () => {
});
});

describe('bitcoin query', async () => {
it('should return the balance of a bitcoin address', async () => {
// TODO - verify node signature for bitcoin once supported
const agent = await makeAgent({ host: 'https://icp-api.io', verifyQuerySignatures: false });
const management = getManagementCanister({
agent,
});

const result = await management.bitcoin_get_balance_query({
address: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
network: { mainnet: null },
min_confirmations: [6],
});
console.log(`balance for address: ${result}`);
expect(result).toBeGreaterThan(0n);
});
});
14 changes: 7 additions & 7 deletions e2e/node/canisters/declarations/counter/counter.did
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
service : {
greet: (text) -> (text);
inc: () -> ();
inc_read: () -> (nat);
queryGreet: (text) -> (text) query;
read: () -> (nat) query;
write: (nat) -> ();
}
greet : (text) -> (text);
inc : () -> ();
inc_read : () -> (nat);
queryGreet : (text) -> (text) query;
read : () -> (nat) query;
write : (nat) -> ();
};
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"os": "^0.1.2",
"path": "^0.12.7",
"prettier": "^2.8.8",
"prettier-plugin-motoko": "^0.8.4",
"pretty-quick": "^3.1.3",
"release-it": "^16.2.1",
"size-limit": "^11.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ export class Actor {
memory_allocation: settings.memory_allocation ? [settings.memory_allocation] : [],
reserved_cycles_limit: [],
log_visibility: [],
wasm_memory_limit: [],
},
];
}
Expand Down
1 change: 0 additions & 1 deletion packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { AnonymousIdentity, SignIdentity, toHex } from '../..';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { AgentError } from '../../errors';
import { AgentHTTPResponseError } from './errors';
import { ExponentialBackoff } from '../../polling/backoff';
const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window.fetch = global.fetch;
(global as any).window = window;
Expand Down
Loading

0 comments on commit 796fcef

Please sign in to comment.