Skip to content

Commit

Permalink
feat(namada): decode more parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jun 25, 2024
1 parent 78df816 commit d22e824
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/namada/NamadaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,19 @@ export async function fetchProtocolParameters (
) {
const keys = connection.decode.storage_keys();
const [
maxBlockDuration, maxGasForBlock, feeUnshieldingGasLimit, gasCostTable,
maxBlockDuration,
maxGasForBlock,
feeUnshieldingGasLimit,
gasCostTable,
] = await Promise.all([
connection.fetchStorageValueImpl(keys.maxBlockDuration),
connection.fetchStorageValueImpl(keys.maxGasForBlock),
connection.fetchStorageValueImpl(keys.feeUnshieldingGasLimit),
connection.fetchStorageValueImpl(keys.gasCostTable),
connection.fetchStorageValueImpl(keys.maxBlockDuration)
.then(x=>connection.decode.u64(x)),
connection.fetchStorageValueImpl(keys.maxGasForBlock)
.then(x=>connection.decode.u64(x)),
connection.fetchStorageValueImpl(keys.feeUnshieldingGasLimit)
.then(x=>connection.decode.u64(x)),
connection.fetchStorageValueImpl(keys.gasCostTable)
.then(x=>connection.decode.gas_cost_table(x)),
])
return {
maxBlockDuration,
Expand Down
2 changes: 2 additions & 0 deletions packages/namada/NamadaDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export async function initDecoder (decoder: string|URL|Uint8Array): Promise<void
export { Decode }

export interface NamadaDecoder {
u64 (_: Uint8Array): bigint
address_to_amount (_: Uint8Array): Record<string, bigint>
addresses (_: Uint8Array): string[]
address (_: Uint8Array): string
epoch_duration (_: Uint8Array): { minNumOfBlocks: number, minDuration: number }
gas_cost_table (_: Uint8Array): Record<string, string>
gov_parameters (_: Uint8Array): Partial<Gov.Parameters>
gov_proposal (_: Uint8Array): Partial<Gov.Proposal>
gov_votes (_: Uint8Array): Partial<Gov.Vote>[]
Expand Down
12 changes: 12 additions & 0 deletions packages/namada/pkg/fadroma_namada.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
export class Decode {
free(): void;
/**
* @param {Uint8Array} source
* @returns {bigint}
*/
static u64(source: Uint8Array): bigint;
/**
* @returns {object}
*/
static storage_keys(): object;
Expand All @@ -16,6 +21,11 @@ export class Decode {
/**
* @param {Uint8Array} source
* @returns {object}
*/
static gas_cost_table(source: Uint8Array): object;
/**
* @param {Uint8Array} source
* @returns {object}
*/
static tx(source: Uint8Array): object;
/**
Expand Down Expand Up @@ -96,8 +106,10 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_decode_free: (a: number) => void;
readonly decode_u64: (a: number, b: number) => void;
readonly decode_storage_keys: (a: number) => void;
readonly decode_epoch_duration: (a: number, b: number) => void;
readonly decode_gas_cost_table: (a: number, b: number) => void;
readonly decode_tx: (a: number, b: number) => void;
readonly decode_address: (a: number, b: number) => void;
readonly decode_addresses: (a: number, b: number) => void;
Expand Down
38 changes: 38 additions & 0 deletions packages/namada/pkg/fadroma_namada.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ export class Decode {
wasm.__wbg_decode_free(ptr);
}
/**
* @param {Uint8Array} source
* @returns {bigint}
*/
static u64(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.decode_u64(retptr, addHeapObject(source));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {object}
*/
static storage_keys() {
Expand Down Expand Up @@ -213,6 +232,25 @@ export class Decode {
* @param {Uint8Array} source
* @returns {object}
*/
static gas_cost_table(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.decode_gas_cost_table(retptr, addHeapObject(source));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Uint8Array} source
* @returns {object}
*/
static tx(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand Down
2 changes: 2 additions & 0 deletions packages/namada/pkg/fadroma_namada_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_decode_free(a: number): void;
export function decode_u64(a: number, b: number): void;
export function decode_storage_keys(a: number): void;
export function decode_epoch_duration(a: number, b: number): void;
export function decode_gas_cost_table(a: number, b: number): void;
export function decode_tx(a: number, b: number): void;
export function decode_address(a: number, b: number): void;
export function decode_addresses(a: number, b: number): void;
Expand Down
20 changes: 20 additions & 0 deletions packages/namada/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ pub struct Decode;
#[wasm_bindgen]
impl Decode {

#[wasm_bindgen]
pub fn u64 (source: Uint8Array) -> Result<BigInt, Error> {
let value = u64::try_from_slice(&to_bytes(&source))
.map_err(|e|Error::new(&format!("{e}")))?;
Ok(BigInt::from(value))
}

#[wasm_bindgen]
pub fn storage_keys () -> Result<Object, Error> {
Ok(to_object! {
"epochDuration" =
get_epoch_duration_storage_key().to_string(),
"maxBlockDuration" =
get_max_expected_time_per_block_key().to_string(),
"vpAllowlist" =
get_tx_allowlist_storage_key().to_string(),
"maxGasForBlock" =
get_max_block_gas_key().to_string(),
"feeUnshieldingGasLimit" =
Expand All @@ -34,6 +43,17 @@ impl Decode {
})
}

#[wasm_bindgen]
pub fn gas_cost_table (source: Uint8Array) -> Result<Object, Error> {
let data = BTreeMap::<namada::core::address::Address, namada::token::Amount>
::try_from_slice(&to_bytes(&source)).map_err(|e|Error::new(&format!("{e}")))?;
let result = Object::new();
for (key, val) in data.iter() {
Reflect::set(&result, &format!("{key}").into(), &format!("{val}").into())?;
}
Ok(result)
}

#[wasm_bindgen]
pub fn tx (source: Uint8Array) -> Result<Object, Error> {
let tx = Tx::try_from_slice(&to_bytes(&source)).map_err(|e|Error::new(&format!("{e}")))?;
Expand Down
1 change: 1 addition & 0 deletions packages/namada/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(crate) use namada::{
get_max_block_gas_key,
get_fee_unshielding_gas_limit_key,
get_gas_cost_key,
get_tx_allowlist_storage_key,
},
},
storage::KeySeg,
Expand Down

0 comments on commit d22e824

Please sign in to comment.