Skip to content

Commit 61ffff1

Browse files
authored
Merge pull request #36 from HorizenOfficial/as/wasm_common
As/wasm common
2 parents c674235 + b265e82 commit 61ffff1

16 files changed

Lines changed: 614 additions & 777 deletions

File tree

runtime/wasm-go/README.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# WASM Go Module: Payment App
22

3-
This repository contains the Go implementation of the **Payment App** WASM module, an example application for handling deposits, transfers, and withdrawals for the Horizen PES project.
3+
This module contains the Go implementation of the **Payment App** WASM module — a privacy-preserving payment application for deposits, transfers, and withdrawals, built on the Horizen PES (Privacy Preserving Execution System) framework.
44

5-
**Note:** The WebAssembly (WASM) runtime itself is implemented and maintained in the `horizen-pes` repository. This module depends on that runtime for building and executing tests.
5+
The PES framework (`horizen-pes`) is **application-agnostic**: it provides a generic execution pipeline (EVM blockchain → Manager → Executor → WASM Runtime) that processes requests without ever parsing application payloads. This module is a specific application that plugs into that framework — the only layer that knows about payment logic. Any WASM module implementing the expected exports can replace it.
6+
7+
**Note:** The WebAssembly (WASM) runtime itself (Wasmtime) is implemented and maintained in the `horizen-pes` repository. This module depends on that runtime for building and executing tests.
68

79
## Prerequisites
810

@@ -30,13 +32,19 @@ tinygo version
3032

3133
## Dependencies
3234

33-
TODO: This will change when the github public repo will be available.
35+
This module depends on two external packages:
36+
37+
- **`horizen-pes`** — The application-agnostic PES framework. Provides the generic WASM runtime (Wasmtime), common types (`common.Request`, `common.Event`, `common.Withdrawal`), and the `Runtime` interface. Used in tests to run the compiled WASM module.
38+
- **`horizen-cce-common-go/wasm`** — Shared WASM guest-side types and utilities. Provides `types.Uint256`, `types.Address`, `types.PlainEvent`, result types (`LoadModuleResult`, `DepositResult`, `ProcessResult`, `DeanonymizationResult`), memory allocator (`utils.Allocate`/`Deallocate`), logging, and pointer conversions. These are the shared data structures that the wallet also imports to ensure identical serialization.
3439

35-
This module depends on the `horizen-pes` repository.
36-
Since it is a private repo, you need to set Go to access Github private repos:
40+
Both dependencies use `replace` directives in `go.mod`. For local development, uncomment the local path replaces pointing to sibling directories.
3741

42+
Since these are private repos, you need to configure Go for private module access:
43+
44+
```bash
3845
go env -w GOPRIVATE=github.com/HorizenOfficial/*
3946
git config --global url."git@github.com:".insteadOf "https://github.com/"
47+
```
4048

4149

4250
## Building
@@ -54,9 +62,41 @@ tinygo build -o build/payment_app.wasm -target wasi main.go
5462

5563
This will create the `build/payment_app.wasm` file.
5664

65+
## Module Structure
66+
67+
```
68+
runtime/wasm-go/
69+
├── main.go # WASM export functions (bridge between runtime and app logic)
70+
├── app/
71+
│ ├── app.go # Application logic (LoadModule, DepositFunds, ProcessRequest, GenerateDeanonymizationReport)
72+
│ └── types.go # App-specific types (PayloadInstructions, TransferInstruction, WithdrawInstruction, AccountState)
73+
├── wasmtime_runtime_test.go # Unit/integration tests against WASM runtime
74+
├── integration_test.go # Integration tests for compiled WASM binary
75+
├── system_tests/ # E2E system tests (full PES stack simulation)
76+
├── build/ # Dev WASM binary output
77+
├── production_build/ # Production WASM binary output
78+
└── Makefile
79+
```
80+
81+
### WASM Exports
82+
83+
The module exports these functions for the generic PES runtime to call:
84+
85+
| Export | Purpose |
86+
|---|---|
87+
| `load_module(appId)` | Initialize application state |
88+
| `deposit(appId, sender, value, state)` | Credit sender account |
89+
| `process_request(appId, sender, payload, state)` | Handle transfers and withdrawals |
90+
| `generate_deanonymization_report(payload, state)` | Generate compliance reports |
91+
| `get_memory_stats()` | Return WASM memory allocation statistics |
92+
93+
### Shared Data Structures
94+
95+
The wallet (`wallet/`) constructs `PayloadInstructions` (defined in `app/types.go`) and encrypts them before submitting to the blockchain. This module receives and decrypts those instructions inside the TEE. Both sides import `types.Address` and `types.Uint256` from `horizen-cce-common-go/wasm/types` to ensure identical serialization.
96+
5797
## Development Workflow
5898

59-
1. **Modify WASM Module**: The core application logic is in `main.go` and `app/app.go`. Utility functions are located in `utils/`.
99+
1. **Modify WASM Module**: The core application logic is in `app/app.go`. The WASM export bridge is in `main.go`. App-specific types are in `app/types.go`. Shared guest-side types and utilities come from `horizen-cce-common-go/wasm`.
60100
2. **Rebuild Module**: After making changes, rebuild the WASM module using `make build` or the `tinygo` command directly.
61101
3. **Update Tests**: Add or update corresponding tests in `wasmtime_runtime_test.go` or `integration_test.go` to reflect your changes.
62102
4. **Verify Changes**: Run the test suite to ensure everything is working correctly:
@@ -69,9 +109,14 @@ This will create the `build/payment_app.wasm` file.
69109
To run the tests, use the standard `go test` command:
70110

71111
```bash
72-
go test ./...
112+
# Fast suite (skips Wasmtime-dependent tests)
113+
CI_FLAG=true go test -v ./...
114+
115+
# Full suite (includes all Wasmtime integration tests)
116+
go test -v ./...
73117
```
74118

119+
Use `CI_FLAG=true` to skip tests that require the Wasmtime runtime or external dependencies.
75120

76121
### Test Files Overview
77122

@@ -91,6 +136,9 @@ This project contains three distinct types of tests, each with a different focus
91136
* **Type**: End-to-End (E2E) System Test
92137
* **Scope**: Covers the entire application stack, including simulated components like an "Executor," a "Manager," a database, and a blockchain.
93138
* **Purpose**: To validate that all components of the system work together correctly in a production-like environment. It tests the full user flow, including cryptographic operations, request submission, and state verification across the entire distributed system.
139+
* **Key tests**:
140+
- `TestPaymentAppFullFlow`: Deploys the app, registers user and auditor keys, deposits funds, withdraws funds, and generates a deanonymization report. Validates deposit and withdrawal event fields, verifies on-chain withdrawal recording, checks update payload signatures, and verifies the deanonymization report (framework envelope, base64-encoded report data, and expected user balance after all operations).
141+
* **Note**: Skipped when `CI_FLAG=true` due to long execution time.
94142
95143
## Resources
96144

0 commit comments

Comments
 (0)