labcoat-test 0.1.0

Host-side Rust test harness for Labcoat Alkanes WASM contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Native host-side integration testing for Labcoat contracts.
//!
//! `labcoat test` compiles WASIp1 WebAssembly modules and points this harness
//! at the resulting artifact directory through `LABCOAT_TEST_ARTIFACT_DIR`.

use serde::Deserialize;
use std::path::{Path, PathBuf};
use wasmtime::{Caller, Engine, Extern, Linker, Module, Store};
use wasmtime_wasi::preview1::{self, WasiP1Ctx};
use wasmtime_wasi::WasiCtxBuilder;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value {
    U128(u128),
    String(String),
}

impl From<u128> for Value {
    fn from(value: u128) -> Self {
        Self::U128(value)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Self::String(value.to_owned())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AlkaneTransfer {
    pub block: u128,
    pub tx: u128,
    pub value: u128,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ChainContext {
    pub myself: (u128, u128),
    pub caller: (u128, u128),
    pub vout: u128,
    pub incoming: Vec<AlkaneTransfer>,
}

impl ChainContext {
    pub fn deterministic() -> Self {
        Self {
            myself: (1, 1),
            caller: (2, 2),
            vout: 0,
            incoming: Vec::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallResult {
    pub transfers: Vec<AlkaneTransfer>,
    pub data: Vec<u8>,
}

impl CallResult {
    pub fn data_text(&self) -> String {
        let data = self.data.strip_prefix(&[0, 0, 0, 0]).unwrap_or(&self.data);
        String::from_utf8_lossy(data)
            .trim_end_matches('\0')
            .to_owned()
    }
}

#[derive(Debug, thiserror::Error)]
pub enum HarnessError {
    #[error("contract artifact not found: {0}")]
    ArtifactMissing(PathBuf),
    #[error("method `{0}` is not present in the contract ABI")]
    UnknownMethod(String),
    #[error("contract trapped or reverted: {0}")]
    Revert(String),
    #[error("invalid contract response: {0}")]
    InvalidResponse(String),
    #[error(transparent)]
    Runtime(#[from] anyhow::Error),
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

#[derive(Debug, Deserialize)]
struct ContractAbi {
    methods: Vec<AbiMethod>,
}

#[derive(Debug, Deserialize)]
struct AbiMethod {
    name: String,
    opcode: u128,
}

struct HostState {
    wasi: WasiP1Ctx,
    context: Vec<u8>,
}

pub struct ContractHarness {
    engine: Engine,
    module: Module,
    abi: ContractAbi,
    context: ChainContext,
}

impl ContractHarness {
    pub fn for_contract(name: &str) -> Result<Self, HarnessError> {
        let root = std::env::var_os("LABCOAT_TEST_ARTIFACT_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|| PathBuf::from(".labcoat/test-artifacts"));
        Self::from_files(
            root.join(format!("{}.wasm", name)),
            root.join(format!("{}.abi.json", name)),
        )
    }

    pub fn from_files(
        wasm_path: impl AsRef<Path>,
        abi_path: impl AsRef<Path>,
    ) -> Result<Self, HarnessError> {
        let wasm_path = wasm_path.as_ref();
        if !wasm_path.exists() {
            return Err(HarnessError::ArtifactMissing(wasm_path.to_path_buf()));
        }
        let abi_path = abi_path.as_ref();
        if !abi_path.exists() {
            return Err(HarnessError::ArtifactMissing(abi_path.to_path_buf()));
        }
        let engine = Engine::default();
        let module = Module::from_file(&engine, wasm_path).map_err(HarnessError::Runtime)?;
        let abi = serde_json::from_slice(&std::fs::read(abi_path)?)?;
        Ok(Self {
            engine,
            module,
            abi,
            context: ChainContext::deterministic(),
        })
    }

    pub fn context(&self) -> &ChainContext {
        &self.context
    }

    pub fn context_mut(&mut self) -> &mut ChainContext {
        &mut self.context
    }

    pub fn set_context(&mut self, context: ChainContext) -> &mut Self {
        self.context = context;
        self
    }

    pub fn call_method(
        &mut self,
        method: &str,
        args: &[Value],
    ) -> Result<CallResult, HarnessError> {
        let opcode = self
            .abi
            .methods
            .iter()
            .find(|entry| entry.name == method)
            .map(|entry| entry.opcode)
            .ok_or_else(|| HarnessError::UnknownMethod(method.to_owned()))?;
        self.call_opcode(opcode, args)
    }

    pub fn call_opcode(
        &mut self,
        opcode: u128,
        args: &[Value],
    ) -> Result<CallResult, HarnessError> {
        let context = serialize_context(&self.context, opcode, args)?;
        let mut linker: Linker<HostState> = Linker::new(&self.engine);
        preview1::add_to_linker_sync(&mut linker, |state| &mut state.wasi)
            .map_err(HarnessError::Runtime)?;
        add_alkanes_imports(&mut linker)?;

        let state = HostState {
            wasi: WasiCtxBuilder::new().build_p1(),
            context,
        };
        let mut store = Store::new(&self.engine, state);
        let instance = linker
            .instantiate(&mut store, &self.module)
            .map_err(HarnessError::Runtime)?;
        if let Ok(initialize) = instance.get_typed_func::<(), ()>(&mut store, "_initialize") {
            initialize
                .call(&mut store, ())
                .map_err(|e| HarnessError::Revert(format!("{e:#}")))?;
        }
        let execute = instance
            .get_typed_func::<(), i32>(&mut store, "__execute")
            .map_err(HarnessError::Runtime)?;
        let pointer = execute
            .call(&mut store, ())
            .map_err(|e| HarnessError::Revert(format!("{e:#}")))?;
        let memory = instance
            .get_memory(&mut store, "memory")
            .ok_or_else(|| HarnessError::InvalidResponse("missing memory export".into()))?;
        decode_response(memory.data(&store), pointer)
    }
}

fn add_alkanes_imports(linker: &mut Linker<HostState>) -> Result<(), HarnessError> {
    linker
        .func_wrap(
            "env",
            "println",
            |mut caller: Caller<'_, HostState>, pointer: i32, length: i32| {
                let memory = caller
                    .get_export("memory")
                    .and_then(Extern::into_memory)
                    .ok_or_else(|| anyhow::anyhow!("missing memory export"))?;
                let data = memory
                    .data(&caller)
                    .get(pointer as usize..pointer as usize + length as usize)
                    .ok_or_else(|| anyhow::anyhow!("println outside memory"))?;
                eprintln!("{}", String::from_utf8_lossy(data));
                Ok(())
            },
        )
        .map_err(HarnessError::Runtime)?;
    linker
        .func_wrap(
            "env",
            "abort",
            |_message: i32, _file: i32, line: i32, column: i32| -> anyhow::Result<()> {
                anyhow::bail!("WASM abort at {line}:{column}")
            },
        )
        .map_err(HarnessError::Runtime)?;
    linker
        .func_wrap(
            "env",
            "__request_context",
            |caller: Caller<'_, HostState>| caller.data().context.len() as i32,
        )
        .map_err(HarnessError::Runtime)?;
    linker
        .func_wrap(
            "env",
            "__load_context",
            |mut caller: Caller<'_, HostState>, pointer: i32| -> anyhow::Result<i32> {
                let context = caller.data().context.clone();
                let memory = caller
                    .get_export("memory")
                    .and_then(Extern::into_memory)
                    .ok_or_else(|| anyhow::anyhow!("missing memory export"))?;
                memory.write(&mut caller, pointer as usize, &context)?;
                // The contract ABI declares an i32 result. The legacy JS host
                // returned `undefined`, which WebAssembly coerced to zero.
                Ok(0)
            },
        )
        .map_err(HarnessError::Runtime)?;
    Ok(())
}

fn serialize_context(
    context: &ChainContext,
    opcode: u128,
    args: &[Value],
) -> Result<Vec<u8>, HarnessError> {
    let mut output = Vec::new();
    for value in [
        context.myself.0,
        context.myself.1,
        context.caller.0,
        context.caller.1,
        context.vout,
        context.incoming.len() as u128,
    ] {
        output.extend_from_slice(&value.to_le_bytes());
    }
    for incoming in &context.incoming {
        output.extend_from_slice(&incoming.block.to_le_bytes());
        output.extend_from_slice(&incoming.tx.to_le_bytes());
        output.extend_from_slice(&incoming.value.to_le_bytes());
    }
    output.extend_from_slice(&opcode.to_le_bytes());
    for arg in args {
        match arg {
            Value::U128(value) => output.extend_from_slice(&value.to_le_bytes()),
            Value::String(value) => {
                let bytes = value.as_bytes();
                if bytes.is_empty() {
                    return Err(HarnessError::InvalidResponse(
                        "string arguments must not be empty".into(),
                    ));
                }
                for chunk in bytes.chunks(16) {
                    let mut word = [0u8; 16];
                    word[..chunk.len()].copy_from_slice(chunk);
                    output.extend_from_slice(&word);
                }
            }
        }
    }
    Ok(output)
}

fn decode_response(memory: &[u8], pointer: i32) -> Result<CallResult, HarnessError> {
    let pointer = usize::try_from(pointer)
        .map_err(|_| HarnessError::InvalidResponse("negative response pointer".into()))?;
    let length_offset = pointer
        .checked_sub(4)
        .ok_or_else(|| HarnessError::InvalidResponse("response pointer is below 4".into()))?;
    let length_bytes: [u8; 4] = memory
        .get(length_offset..pointer)
        .ok_or_else(|| HarnessError::InvalidResponse("missing response length".into()))?
        .try_into()
        .unwrap();
    let length = u32::from_le_bytes(length_bytes) as usize;
    let response = memory
        .get(pointer..pointer + length)
        .ok_or_else(|| HarnessError::InvalidResponse("response exceeds memory".into()))?;
    let (count, mut offset) = read_u128(response, 0)?;
    let mut transfers = Vec::new();
    for _ in 0..count {
        let (block, next) = read_u128(response, offset)?;
        let (tx, next2) = read_u128(response, next)?;
        let (value, next3) = read_u128(response, next2)?;
        offset = next3;
        transfers.push(AlkaneTransfer { block, tx, value });
    }
    Ok(CallResult {
        transfers,
        data: response[offset..].to_vec(),
    })
}

fn read_u128(bytes: &[u8], offset: usize) -> Result<(u128, usize), HarnessError> {
    let end = offset + 16;
    let word: [u8; 16] = bytes
        .get(offset..end)
        .ok_or_else(|| HarnessError::InvalidResponse("truncated u128 word".into()))?
        .try_into()
        .unwrap();
    Ok((u128::from_le_bytes(word), end))
}

pub fn assert_revert<T: std::fmt::Debug>(result: Result<T, HarnessError>, message: &str) {
    let error = result.expect_err("expected contract call to revert");
    assert!(
        error.to_string().contains(message),
        "expected revert containing `{message}`, got `{error}`"
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn context_encoding_includes_transfers_opcode_and_arguments() {
        let context = ChainContext {
            myself: (1, 2),
            caller: (3, 4),
            vout: 5,
            incoming: vec![AlkaneTransfer {
                block: 6,
                tx: 7,
                value: 8,
            }],
        };
        let encoded =
            serialize_context(&context, 9, &[Value::U128(10), Value::String("AB".into())]).unwrap();
        let words: Vec<u128> = encoded
            .chunks_exact(16)
            .map(|word| u128::from_le_bytes(word.try_into().unwrap()))
            .collect();
        assert_eq!(words, vec![1, 2, 3, 4, 5, 1, 6, 7, 8, 9, 10, 0x4241]);
    }

    #[test]
    fn response_decoding_returns_transfers_and_text() {
        let mut response = Vec::new();
        response.extend_from_slice(&1u128.to_le_bytes());
        response.extend_from_slice(&2u128.to_le_bytes());
        response.extend_from_slice(&3u128.to_le_bytes());
        response.extend_from_slice(&4u128.to_le_bytes());
        response.extend_from_slice(b"hello");
        let mut memory = (response.len() as u32).to_le_bytes().to_vec();
        memory.extend_from_slice(&response);
        let result = decode_response(&memory, 4).unwrap();
        assert_eq!(result.transfers.len(), 1);
        assert_eq!(result.transfers[0].value, 4);
        assert_eq!(result.data_text(), "hello");
    }

    #[test]
    fn harness_looks_up_methods_and_reports_traps() {
        let root = std::env::temp_dir().join(format!(
            "labcoat-harness-{}-{}",
            std::process::id(),
            std::thread::current().name().unwrap_or("test")
        ));
        std::fs::create_dir_all(&root).unwrap();
        let wasm = wat::parse_str(
            r#"(module
                (memory (export "memory") 1)
                (data (i32.const 0)
                    "\12\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\68\69")
                (func (export "__execute") (result i32) i32.const 4)
            )"#,
        )
        .unwrap();
        let wasm_path = root.join("Example.wasm");
        let abi_path = root.join("Example.abi.json");
        std::fs::write(&wasm_path, wasm).unwrap();
        std::fs::write(
            &abi_path,
            r#"{"contract":"Example","methods":[{"name":"greet","opcode":1,"params":[],"returns":"String"}]}"#,
        )
        .unwrap();

        let mut harness = ContractHarness::from_files(&wasm_path, &abi_path).unwrap();
        let result = harness.call_method("greet", &[]).unwrap();
        assert_eq!(result.data_text(), "hi");
        assert_eq!(harness.call_opcode(1, &[]).unwrap().data_text(), "hi");
        assert!(matches!(
            harness.call_method("Missing", &[]),
            Err(HarnessError::UnknownMethod(_))
        ));

        let trap = wat::parse_str(
            r#"(module
                (memory (export "memory") 1)
                (func (export "__execute") (result i32) unreachable)
            )"#,
        )
        .unwrap();
        std::fs::write(&wasm_path, trap).unwrap();
        let mut harness = ContractHarness::from_files(&wasm_path, &abi_path).unwrap();
        assert_revert(harness.call_method("greet", &[]), "contract trapped");

        let expected_revert = wat::parse_str(
            r#"(module
                (import "env" "abort" (func $abort (param i32 i32 i32 i32)))
                (memory (export "memory") 1)
                (func (export "__execute") (result i32)
                    i32.const 0
                    i32.const 0
                    i32.const 12
                    i32.const 34
                    call $abort
                    i32.const 0)
            )"#,
        )
        .unwrap();
        std::fs::write(&wasm_path, expected_revert).unwrap();
        let mut harness = ContractHarness::from_files(&wasm_path, &abi_path).unwrap();
        assert_revert(harness.call_method("greet", &[]), "WASM abort at 12:34");
        std::fs::remove_dir_all(root).ok();
    }
}