Skip to content

Commit

Permalink
fix(namada): update 0.40 global parameter list
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Jul 9, 2024
1 parent 521963d commit 3a4ff5d
Show file tree
Hide file tree
Showing 7 changed files with 7,996 additions and 6,063 deletions.
47 changes: 26 additions & 21 deletions packages/namada/NamadaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,30 @@ export async function fetchProtocolParameters (
connection: Pick<NamadaConnection, 'fetchStorageValueImpl'|'decode'>
) {
const keys = connection.decode.storage_keys();
const [
maxBlockDuration,
maxGasForBlock,
feeUnshieldingGasLimit,
gasCostTable,
] = await Promise.all([
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,
maxGasForBlock,
feeUnshieldingGasLimit,
gasCostTable,
}
const parameters: Record<string, unknown> = {}
await Promise.all(Object.entries(connection.decode.storage_keys())
.map(([name, key])=>connection.fetchStorageValueImpl(key).then(binary=>{
//console.log({name, key, binary})
if (binary.length === 0) {
return
}
switch (name) {
case 'gasCostTable':
return parameters[name]=connection.decode.gas_cost_table(binary)
case 'epochDuration':
return parameters[name]=connection.decode.epoch_duration(binary)
case 'maxTxBytes':
return parameters[name]=connection.decode.u32(binary)
case 'txAllowlist':
case 'vpAllowlist':
return parameters[name]=connection.decode.vec_string(binary)
case 'isNativeTokenTransferable':
return parameters[name]=!!binary[0]
case 'implicitVpCodeHash':
return parameters[name]=connection.decode.code_hash(binary)
default:
return parameters[name]=connection.decode.u64(binary)
}
})))
return parameters
}
21 changes: 16 additions & 5 deletions packages/namada/NamadaDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export async function initDecoder (decoder: string|URL|Uint8Array): Promise<void
export { Decode }

export interface NamadaDecoder {
u32 (_: Uint8Array): bigint
u64 (_: Uint8Array): bigint
vec_string (_: Uint8Array): string[]
code_hash (_: Uint8Array): string

address_to_amount (_: Uint8Array): Record<string, bigint>
addresses (_: Uint8Array): string[]
Expand Down Expand Up @@ -118,10 +121,18 @@ export interface NamadaDecoder {
pos_validator_state (_: Uint8Array): unknown

storage_keys (): {
epochDuration: string,
maxBlockDuration: string,
maxGasForBlock: string,
feeUnshieldingGasLimit: string,
gasCostTable: string
epochDuration: string
epochsPerYear: string
gasCostTable: string
gasScale: string
implicitVpCodeHash: string
maspEpochMultipler: string
maspFeePaymentGasLimi: string
maxBlockGas: string
maxProposalBytes: string
maxTxBytes: string
isNativeTokenTransferable: string
txAllowlist: string
vpAllowlist: string
}
}
18 changes: 18 additions & 0 deletions packages/namada/pkg/fadroma_namada.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ export class Decode {
/**
* @param {Uint8Array} source
* @returns {bigint}
*/
static u32(source: Uint8Array): bigint;
/**
* @param {Uint8Array} source
* @returns {bigint}
*/
static u64(source: Uint8Array): bigint;
/**
* @param {Uint8Array} source
* @returns {Array<any>}
*/
static vec_string(source: Uint8Array): Array<any>;
/**
* @param {Uint8Array} source
* @returns {string}
*/
static code_hash(source: Uint8Array): string;
/**
* @returns {object}
*/
static storage_keys(): object;
Expand Down Expand Up @@ -106,7 +121,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_u32: (a: number, b: number) => void;
readonly decode_u64: (a: number, b: number) => void;
readonly decode_vec_string: (a: number, b: number) => void;
readonly decode_code_hash: (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;
Expand Down
75 changes: 68 additions & 7 deletions packages/namada/pkg/fadroma_namada.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ export class Decode {
* @param {Uint8Array} source
* @returns {bigint}
*/
static u32(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.decode_u32(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 {bigint}
*/
static u64(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand All @@ -192,6 +211,44 @@ export class Decode {
}
}
/**
* @param {Uint8Array} source
* @returns {Array<any>}
*/
static vec_string(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.decode_vec_string(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 {string}
*/
static code_hash(source) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.decode_code_hash(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 @@ -584,26 +641,30 @@ function __wbg_get_imports() {
const ret = getObject(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_number_new = function(arg0) {
const ret = arg0;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_from_u64 = function(arg0) {
const ret = BigInt.asUintN(64, arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_from_i128 = function(arg0, arg1) {
const ret = arg0 << BigInt(64) | BigInt.asUintN(64, arg1);
imports.wbg.__wbindgen_bigint_from_i64 = function(arg0) {
const ret = arg0;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_from_i64 = function(arg0) {
imports.wbg.__wbindgen_number_new = function(arg0) {
const ret = arg0;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_from_i128 = function(arg0, arg1) {
const ret = arg0 << BigInt(64) | BigInt.asUintN(64, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbg_new_16b304a2cfa7ff4a = function() { return logError(function () {
const ret = new Array();
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_BigInt_42b692c18e1ac6d6 = function() { return logError(function (arg0) {
const ret = BigInt(getObject(arg0));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_new_72fb9a18b5ae2624 = function() { return logError(function () {
const ret = new Object();
return addHeapObject(ret);
Expand Down
3 changes: 3 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,7 +2,10 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_decode_free(a: number): void;
export function decode_u32(a: number, b: number): void;
export function decode_u64(a: number, b: number): void;
export function decode_vec_string(a: number, b: number): void;
export function decode_code_hash(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;
Expand Down
33 changes: 29 additions & 4 deletions packages/namada/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@ use crate::{*, to_js::*};
#[wasm_bindgen]
pub struct Decode;

fn decode <T: namada::core::borsh::BorshDeserialize> (source: &Uint8Array) -> Result<T, Error> {
T::try_from_slice(&to_bytes(&source)).map_err(|e|Error::new(&format!("{e}")))
}

#[wasm_bindgen]
impl Decode {

#[wasm_bindgen]
pub fn u32 (source: Uint8Array) -> Result<BigInt, Error> {
decode::<u32>(&source).map(BigInt::from)
}

#[wasm_bindgen]
pub fn u64 (source: Uint8Array) -> Result<BigInt, Error> {
let value = u64::try_from_slice(&to_bytes(&source))
decode::<u64>(&source).map(BigInt::from)
}

#[wasm_bindgen]
pub fn vec_string (source: Uint8Array) -> Result<Array, Error> {
let values = Vec::<String>::try_from_slice(&to_bytes(&source))
.map_err(|e|Error::new(&format!("{e}")))?;
Ok(BigInt::from(value))
let result = Array::new();
for value in values.into_iter() {
result.push(&value.into());
}
Ok(result)
}

#[wasm_bindgen]
pub fn code_hash (source: Uint8Array) -> Result<JsString, Error> {
decode::<namada::core::hash::Hash>(&source)
.map(hex::encode_upper)
.map(JsString::from)
}

#[wasm_bindgen]
Expand All @@ -24,7 +49,7 @@ impl Decode {
get_gas_cost_key().to_string(),
"gasScale" =
get_gas_scale_key().to_string(),
"implicitVp" =
"implicitVpCodeHash" =
get_implicit_vp_key().to_string(),
"maspEpochMultipler" =
get_masp_epoch_multiplier_key().to_string(),
Expand All @@ -36,7 +61,7 @@ impl Decode {
get_max_proposal_bytes_key().to_string(),
"maxTxBytes" =
get_max_tx_bytes_key().to_string(),
"nativeTokenTransferable" =
"isNativeTokenTransferable" =
get_native_token_transferable_key().to_string(),
"txAllowlist" =
get_tx_allowlist_storage_key().to_string(),
Expand Down
Loading

0 comments on commit 3a4ff5d

Please sign in to comment.