Skip to content

Commit cf0ef65

Browse files
committed
fix: inspectability
1 parent 44af4fc commit cf0ef65

File tree

11 files changed

+81
-36
lines changed

11 files changed

+81
-36
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::Path;
66
fn main() {
77
// config parameters
88
let dwb_capacity = env::var("DWB_CAPACITY").unwrap_or_else(|_| "64".to_string());
9-
let btbe_capacity = env::var("BTBE_CAPACITY").unwrap_or_else(|_| "128".to_string());
9+
let btbe_capacity = env::var("BTBE_CAPACITY").unwrap_or_else(|_| "64".to_string());
1010

1111
// path to destination config.rs file
1212
let out_dir = env::var("OUT_DIR").expect("Missing OUT_DIR");

src/contract.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S
313313
};
314314

315315
let padded_result = pad_handle_result(response, RESPONSE_BLOCK_SIZE);
316-
msg.evaporate_to_target(api)?;
316+
let evaporated = msg.evaporate_to_target(api)?;
317+
317318
padded_result
318319
}
319320

src/dwb.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use crate::state::{safe_add, safe_add_u64};
1111
use crate::transaction_history::{Tx, TRANSACTIONS};
1212
#[cfg(feature = "gas_tracking")]
1313
use crate::gas_tracker::GasTracker;
14+
#[cfg(feature = "gas_tracking")]
15+
use cosmwasm_std::{Binary, to_binary};
16+
#[cfg(feature = "gas_tracking")]
17+
use crate::msg::QueryAnswer;
1418

1519
include!(concat!(env!("OUT_DIR"), "/config.rs"));
1620

src/msg.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,11 @@ pub enum ExecuteAnswer {
368368
}
369369

370370
pub trait Evaporator {
371-
fn evaporate_to_target(&self, api: &dyn Api) -> StdResult<()>;
371+
fn evaporate_to_target(&self, api: &dyn Api) -> StdResult<u64>;
372372
}
373373

374374
impl Evaporator for ExecuteMsg {
375-
fn evaporate_to_target(&self, api: &dyn Api) -> StdResult<()> {
375+
fn evaporate_to_target(&self, api: &dyn Api) -> StdResult<u64> {
376376
match self {
377377
ExecuteMsg::Redeem { gas_target, .. }
378378
| ExecuteMsg::Deposit { gas_target, .. }
@@ -407,10 +407,11 @@ impl Evaporator for ExecuteMsg {
407407
if gas_used < gas_target.u64() {
408408
let evaporate_amount = gas_target.u64() - gas_used;
409409
api.gas_evaporate(evaporate_amount as u32)?;
410+
return Ok(evaporate_amount)
410411
}
411-
Ok(())
412+
Ok(0)
412413
}
413-
None => Ok(()),
414+
None => Ok(0),
414415
},
415416
}
416417
}

tests/dwb/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SECRET_LCD=http://localhost:1317
2+
SECRET_RPC=http://localhost:26657
3+
SECRET_CHAIN=secretdev-1
4+
ENABLE_EVAPORATION_TESTS=1

tests/dwb/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# DWB Integration Test Suite
2+
3+
## Requirements
4+
The test suite is run using [bun](https://bun.sh/).
5+
6+
## Setup
7+
From this directory:
8+
```sh
9+
bun install
10+
cp .env.example .env
11+
```
12+
13+
Edit the `.env` file (or leave as is) to configure the network to either your localsecret or pulsar-3.
14+
15+
## Run
16+
```sh
17+
bun run test ## compiles the contract for integration tests and runs the main test suite
18+
```
19+
20+
21+
## Debugging
22+
23+
In case there is a silent failure, it may help to run the suite using node.js instead of bun. You can compile it and run it and debug it interactively with the following commands:
24+
```sh
25+
bun run build && node --env-file=.env --inspect-brk dist/main.js
26+
```
27+
28+
The console output should look something like this:
29+
![Integration test preview](https://github.com/user-attachments/assets/be2fedda-550c-45e6-aee4-5af45a84d5b8)

tests/dwb/src/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type {TrustedContextUrl} from '@solar-republic/types';
33
import {base64_to_bytes} from '@blake.regalia/belt';
44
import {Wallet} from '@solar-republic/neutrino';
55

6-
export const P_LOCALSECRET_LCD = (process.env['SECRET_LCD'] || 'http://localhost:1317') as TrustedContextUrl;
7-
export const P_LOCALSECRET_RPC = (process.env['SECRET_RPC'] || 'http://localhost:26656') as TrustedContextUrl;
6+
export const P_SECRET_LCD = (process.env['SECRET_LCD'] || 'http://localhost:1317') as TrustedContextUrl;
7+
export const P_SECRET_RPC = (process.env['SECRET_RPC'] || 'http://localhost:26656') as TrustedContextUrl;
8+
export const SI_SECRET_CHAIN = (process.env['SECRET_CHAIN'] || 'secretdev-1') as TrustedContextUrl;
89
export const B_TEST_EVAPORATION = !!parseInt(process.env['ENABLE_EVAPORATION_TESTS'] || '0');
910

1011
export const X_GAS_PRICE = 0.1;
@@ -15,7 +16,7 @@ export const [k_wallet_a, k_wallet_b, k_wallet_c, k_wallet_d] = await Promise.al
1516
'buqil+tLeeW7VLuugvOdTmkP3+tUwlCoScPZxeteBPE=',
1617
'UFrCdmofR9iChp6Eg7kE5O3wT+jsOXwJPWwB6kSeuhE=',
1718
'MM/1ZSbT5RF1BnaY6ui/i7yEN0mukGzvXUv+jOyjD0E=',
18-
].map(sb64_sk => Wallet(base64_to_bytes(sb64_sk), 'secretdev-1', P_LOCALSECRET_LCD, P_LOCALSECRET_RPC, 'secret')));
19+
].map(sb64_sk => Wallet(base64_to_bytes(sb64_sk), SI_SECRET_CHAIN, P_SECRET_LCD, P_SECRET_RPC, 'secret')));
1920

2021
export const H_ADDRS = {
2122
[k_wallet_a.addr]: 'Alice',

tests/dwb/src/contract.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {destructSecretRegistrationKey} from '@solar-republic/cosmos-grpc/secret/
1515
import {querySecretRegistrationTxKey} from '@solar-republic/cosmos-grpc/secret/registration/v1beta1/query';
1616
import {SecretWasm, TendermintEventFilter, TendermintWs, broadcast_result, create_and_sign_tx_direct, exec_fees} from '@solar-republic/neutrino';
1717

18-
import {X_GAS_PRICE, P_LOCALSECRET_LCD, P_LOCALSECRET_RPC} from './constants';
18+
import {X_GAS_PRICE, P_SECRET_LCD, P_SECRET_RPC} from './constants';
1919

20-
const k_tef = await TendermintEventFilter(P_LOCALSECRET_RPC);
20+
const k_tef = await TendermintEventFilter(P_SECRET_RPC);
2121

2222
export async function exec(k_wallet: Wallet, atu8_msg: EncodedGoogleProtobufAny, xg_gas_limit: bigint): Promise<TxResultTuple> {
2323
const [atu8_raw, atu8_signdoc, si_txn] = await create_and_sign_tx_direct(
@@ -43,11 +43,13 @@ export async function upload_code(k_wallet: Wallet, atu8_wasm: Uint8Array): Prom
4343
const sb16_hash = cast<CwHexLower>(bytes_to_hex(atu8_hash));
4444

4545
// fetch all uploaded codes
46-
const [,, g_codes] = await querySecretComputeCodes(P_LOCALSECRET_LCD);
46+
const [,, g_codes] = await querySecretComputeCodes(P_SECRET_LCD);
4747

4848
// already uploaded
4949
const g_existing = g_codes?.code_infos?.find(g => g.code_hash! === sb16_hash);
5050
if(g_existing) {
51+
console.info(`Found code ID ${g_existing.code_id} already uploaded to network`);
52+
5153
return g_existing.code_id as WeakUintStr;
5254
}
5355

@@ -66,10 +68,10 @@ export async function upload_code(k_wallet: Wallet, atu8_wasm: Uint8Array): Prom
6668
}
6769

6870
export async function instantiate_contract(k_wallet: Wallet, sg_code_id: WeakUintStr, h_init_msg: JsonObject): Promise<WeakSecretAccAddr> {
69-
const [,, g_reg] = await querySecretRegistrationTxKey(P_LOCALSECRET_LCD);
71+
const [,, g_reg] = await querySecretRegistrationTxKey(P_SECRET_LCD);
7072
const [atu8_cons_pk] = destructSecretRegistrationKey(g_reg!);
7173
const k_wasm = SecretWasm(atu8_cons_pk!);
72-
const [,, g_hash] = await querySecretComputeCodeHashByCodeId(P_LOCALSECRET_LCD, sg_code_id);
74+
const [,, g_hash] = await querySecretComputeCodeHashByCodeId(P_SECRET_LCD, sg_code_id);
7375

7476
// @ts-expect-error imported types versioning
7577
const atu8_body = await k_wasm.encodeMsg(g_hash!.code_hash, h_init_msg);

tests/dwb/src/gas-checker.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ export class GasChecker {
2323

2424
// each group
2525
for(const [si_group, a_logs_local] of entries(h_local)) {
26-
// logs emitted from this transfer group
27-
let c_logs = 0;
28-
2926
// find group in baseline
3027
const a_logs_baseline = _h_baseline[si_group];
3128

@@ -50,12 +47,13 @@ export class GasChecker {
5047

5148
// comment only
5249
if('#' === si_group[0]) {
53-
console.log([
54-
' '.repeat(8)+si_group.slice(0, 20).padEnd(20, ' '),
55-
' '.repeat(3),
56-
SX_ANSI_CYAN+s_comment_local+SX_ANSI_RESET,
57-
].join(' │ '));
58-
c_logs += 1;
50+
if(s_comment_local.trim()) {
51+
console.log([
52+
' '.repeat(8)+si_group.slice(0, 20).padEnd(20, ' '),
53+
' '.repeat(3),
54+
SX_ANSI_CYAN+s_comment_local+SX_ANSI_RESET,
55+
].join(' │ '));
56+
}
5957
}
6058
// non-zero delta
6159
else if(xg_delta || '@' === s_comment_local[0]) {
@@ -64,7 +62,6 @@ export class GasChecker {
6462
delta_color(xg_delta, 3),
6563
('@' === s_comment_local[0]? SX_ANSI_MAGENTA: '')+s_comment_local+SX_ANSI_RESET,
6664
].join(' │ '));
67-
c_logs += 1;
6865
}
6966
}
7067
}

tests/dwb/src/main.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {encodeGoogleProtobufAny} from '@solar-republic/cosmos-grpc/google/protob
1212
import {SecretApp, SecretContract, Wallet, broadcast_result, create_and_sign_tx_direct, random_32, type TxMeta, type WeakSecretAccAddr} from '@solar-republic/neutrino';
1313
import {BigNumber} from 'bignumber.js';
1414

15-
import {B_TEST_EVAPORATION, N_DECIMALS, P_LOCALSECRET_LCD, P_LOCALSECRET_RPC, X_GAS_PRICE, k_wallet_a, k_wallet_b, k_wallet_c, k_wallet_d} from './constants';
15+
import {B_TEST_EVAPORATION, N_DECIMALS, P_SECRET_LCD, P_SECRET_RPC, SI_SECRET_CHAIN, X_GAS_PRICE, k_wallet_a, k_wallet_b, k_wallet_c, k_wallet_d} from './constants';
1616
import {upload_code, instantiate_contract} from './contract';
1717
import {DwbValidator} from './dwb';
1818
import {GasChecker} from './gas-checker';
@@ -22,6 +22,8 @@ const S_CONTRACT_LABEL = 'snip2x-test_'+bytes_to_base64(crypto.getRandomValues(b
2222

2323
const atu8_wasm = readFileSync('../../contract.wasm');
2424

25+
console.log(k_wallet_a.addr);
26+
2527
console.debug(`Uploading code...`);
2628
const sg_code_id = await upload_code(k_wallet_a, atu8_wasm);
2729

@@ -58,7 +60,7 @@ const k_contract = await SecretContract<SecretContractInterface<{
5860
gas_target?: WeakUint128Str;
5961
}];
6062
};
61-
}>>(P_LOCALSECRET_LCD, sa_snip);
63+
}>>(P_SECRET_LCD, sa_snip);
6264

6365
const k_app_a = SecretApp(k_wallet_a, k_contract, X_GAS_PRICE);
6466
const k_app_b = SecretApp(k_wallet_b, k_contract, X_GAS_PRICE);
@@ -103,25 +105,29 @@ async function transfer_chain(sx_chain: string) {
103105

104106
// evaporation
105107
if(B_TEST_EVAPORATION) {
106-
const xg_gas_wanted = 250_000n;
107-
const xg_gas_target = xg_gas_wanted - 100n;
108+
const xg_post_evaporate_buffer = 50_000n;
109+
const xg_gas_wanted = 100_000n;
110+
const xg_gas_target = xg_gas_wanted - xg_post_evaporate_buffer;
108111

109112
const [g_exec, xc_code, sx_res, g_meta, h_events, si_txn] = await k_app_a.exec('transfer', {
110113
amount: `${500000n}` as CwUint128,
111114
recipient: k_wallet_b.addr,
112115
gas_target: `${xg_gas_target}`,
113116
}, xg_gas_wanted);
114117

118+
console.log({g_meta});
119+
115120
if(xc_code) {
116121
throw Error(`Failed evaporation test: ${sx_res}`);
117122
}
118123

119124
const xg_gas_used = BigInt(g_meta?.gas_used || '0');
120-
if(bigint_abs(xg_gas_wanted, xg_gas_used) > 1000n) {
125+
if(xg_gas_used < xg_gas_target) {
126+
throw Error(`Expected gas used to be greater than ${xg_gas_target} but only used ${xg_gas_used}`);
127+
}
128+
else if(bigint_abs(xg_gas_wanted, xg_gas_used) > xg_post_evaporate_buffer) {
121129
throw Error(`Expected gas used to be ${xg_gas_wanted} but found ${xg_gas_used}`);
122130
}
123-
124-
console.log(g_meta);
125131
}
126132

127133
{
@@ -161,7 +167,7 @@ if(B_TEST_EVAPORATION) {
161167
for(let i_sim=0; i_sim<N_SIMULATIONS; i_sim++) {
162168
const si_receiver = i_sim+'';
163169

164-
const k_wallet = await Wallet(await sha256(text_to_bytes(si_receiver)), 'secretdev-1', P_LOCALSECRET_LCD, P_LOCALSECRET_RPC, 'secret');
170+
const k_wallet = await Wallet(await sha256(text_to_bytes(si_receiver)), SI_SECRET_CHAIN, P_SECRET_LCD, P_SECRET_RPC, 'secret');
165171

166172
const k_app_sim = SecretApp(k_wallet, k_contract, X_GAS_PRICE);
167173

@@ -223,8 +229,8 @@ if(B_TEST_EVAPORATION) {
223229
const si_owner = i_sim+'';
224230
const si_recipient = (i_sim - 1)+'';
225231

226-
const k_wallet_owner = await Wallet(await sha256(text_to_bytes(si_owner)), 'secretdev-1', P_LOCALSECRET_LCD, P_LOCALSECRET_RPC, 'secret');
227-
const k_wallet_recipient = await Wallet(await sha256(text_to_bytes(si_recipient)), 'secretdev-1', P_LOCALSECRET_LCD, P_LOCALSECRET_RPC, 'secret');
232+
const k_wallet_owner = await Wallet(await sha256(text_to_bytes(si_owner)), SI_SECRET_CHAIN, P_SECRET_LCD, P_SECRET_RPC, 'secret');
233+
const k_wallet_recipient = await Wallet(await sha256(text_to_bytes(si_recipient)), SI_SECRET_CHAIN, P_SECRET_LCD, P_SECRET_RPC, 'secret');
228234

229235
const k_app_owner = SecretApp(k_wallet_owner, k_contract, X_GAS_PRICE);
230236
const k_app_recipient = SecretApp(k_wallet_recipient, k_contract, X_GAS_PRICE);

0 commit comments

Comments
 (0)