|
1 | | -# PVQ |
| 1 | +# PVQ: PolkaVM Query System for Polkadot |
2 | 2 |
|
3 | | -PolkaVM Query for Polkadot |
| 3 | +<!-- generated by polka.codes --> |
4 | 4 |
|
5 | | -## Getting Started |
| 5 | +[](LICENSE) |
| 6 | +[](https://github.com/open-web3-stack/PVQ/actions) |
| 7 | + |
| 8 | +A powerful and secure query system for Polkadot parachains that enables developers to write expressive, sandboxed guest programs using PolkaVM. PVQ provides a safe and efficient alternative to custom RPC endpoints for runtime data queries. |
| 9 | + |
| 10 | +## β¨ Features |
| 11 | + |
| 12 | +- **π Secure Execution**: Sandboxed PolkaVM environment for safe query execution |
| 13 | +- **π§© Modular Extensions**: Extensible system for exposing runtime functionalities |
| 14 | +- **β‘ High Performance**: Efficient RISC-V execution with minimal overhead |
| 15 | +- **π οΈ Developer Friendly**: Rust-first development experience with procedural macros |
| 16 | +- **π Runtime Integration**: Seamless integration with Substrate runtimes |
| 17 | +- **π Rich Querying**: Support for complex queries involving multiple runtime components |
| 18 | + |
| 19 | +## ποΈ Architecture |
| 20 | + |
| 21 | +The PVQ system consists of several interconnected components: |
| 22 | + |
| 23 | +``` |
| 24 | +βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ |
| 25 | +β PVQ Program βββββΆβ PVQ Executor βββββΆβ Substrate β |
| 26 | +β (Guest Code) β β (Host Side) β β Runtime β |
| 27 | +βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ |
| 28 | + β β β |
| 29 | + β βββββββββββββββββββ β |
| 30 | + βββββββββββββββΆβ PVQ Extensions ββββββββββββββββ |
| 31 | + β (Modules) β |
| 32 | + βββββββββββββββββββ |
| 33 | +``` |
| 34 | + |
| 35 | +### Core Components |
| 36 | + |
| 37 | +| Component | Description | |
| 38 | +|-----------|-------------| |
| 39 | +| **[PVQ Program](pvq-program/)** | Guest programs written in Rust that compile to RISC-V | |
| 40 | +| **[PVQ Executor](pvq-executor/)** | Host-side component managing PolkaVM instances and runtime interaction | |
| 41 | +| **[PVQ Extensions](pvq-extension/)** | Modular system exposing runtime functionalities to guest programs | |
| 42 | +| **[PVQ Runtime API](pvq-runtime-api/)** | Substrate runtime API for external query submission | |
| 43 | +| **[PVQ Primitives](pvq-primitives/)** | Common types and utilities shared across components | |
| 44 | + |
| 45 | +### Available Extensions |
| 46 | + |
| 47 | +- **[Core Extension](pvq-extension-core/)**: Fundamental functionalities and extension discovery |
| 48 | +- **[Fungibles Extension](pvq-extension-fungibles/)**: Asset querying, balances, and metadata |
| 49 | +- **[Swap Extension](pvq-extension-swap/)**: DEX interactions, liquidity pools, and price quotes |
| 50 | + |
| 51 | +## π Getting Started |
6 | 52 |
|
7 | 53 | ### Prerequisites |
8 | 54 |
|
9 | | -- Pull vendored `polkavm`: `git submodule update --init --recursive`. |
10 | | -- Install `polkatool` (for relinking the standard RV32E ELF to a PolkaVM blob) and `chain-spec-builder` (for building chainspec from a wasm): `make tools` |
| 55 | +Ensure you have the following installed: |
| 56 | + |
| 57 | +- **Rust** (latest stable version) |
| 58 | +- **Git** with submodule support |
| 59 | + |
| 60 | +### Installation |
| 61 | + |
| 62 | +1. **Clone the repository with submodules:** |
| 63 | + ```bash |
| 64 | + git clone --recursive https://github.com/open-web3-stack/PVQ.git |
| 65 | + cd PVQ |
| 66 | + ``` |
| 67 | + |
| 68 | +2. **Install required tools:** |
| 69 | + ```bash |
| 70 | + make tools |
| 71 | + ``` |
| 72 | + This installs `polkatool` for ELF to PolkaVM blob conversion and `chain-spec-builder`. |
| 73 | + |
| 74 | +3. **Build the project:** |
| 75 | + ```bash |
| 76 | + cargo build --release |
| 77 | + ``` |
| 78 | + |
| 79 | +### Quick Start |
| 80 | + |
| 81 | +#### Running Example Programs |
| 82 | + |
| 83 | +1. **Build guest programs:** |
| 84 | + ```bash |
| 85 | + make guests |
| 86 | + ``` |
| 87 | + |
| 88 | +2. **Run a test program:** |
| 89 | + ```bash |
| 90 | + cargo run -p pvq-test-runner -- --program output/guest-sum-balance |
| 91 | + ``` |
| 92 | + |
| 93 | +#### Available Example Programs |
| 94 | + |
| 95 | +| Program | Description | |
| 96 | +|---------|-------------| |
| 97 | +| `guest-sum-balance` | Sum balances of multiple accounts | |
| 98 | +| `guest-total-supply` | Get total supply of an asset | |
| 99 | +| `guest-sum-balance-percent` | Calculate percentage of total supply for account balances | |
| 100 | +| `guest-swap-info` | Query DEX/swap information | |
| 101 | + |
| 102 | +### Runtime Integration |
| 103 | + |
| 104 | +#### Testing with PoC Runtime |
| 105 | + |
| 106 | +1. **Start local test chain:** |
| 107 | + ```bash |
| 108 | + make run |
| 109 | + ``` |
| 110 | + |
| 111 | +2. **Build and test programs:** |
| 112 | + ```bash |
| 113 | + make guests |
| 114 | + cargo run -p pvq-test-runner -- --program output/guest-total-supply |
| 115 | + ``` |
| 116 | + |
| 117 | +3. **Use with Polkadot JS UI:** |
| 118 | + - Copy the hex-encoded `args` from test runner logs |
| 119 | + - Upload program and arguments through the PJS UI |
| 120 | + |
| 121 | +## π Documentation |
| 122 | + |
| 123 | +### Writing Your First PVQ Program |
| 124 | + |
| 125 | +```rust |
| 126 | +use pvq_program::program; |
| 127 | +use pvq_extension_fungibles::ExtensionFungibles; |
| 128 | + |
| 129 | +#[program] |
| 130 | +fn query_balance(account: [u8; 32], asset_id: u32) -> u128 { |
| 131 | + ExtensionFungibles::balance(asset_id, &account) |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Creating Custom Extensions |
| 136 | + |
| 137 | +```rust |
| 138 | +use pvq_extension::extension_decl; |
| 139 | + |
| 140 | +#[extension_decl] |
| 141 | +pub trait MyCustomExtension { |
| 142 | + fn my_query() -> String; |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### Component Documentation |
| 147 | + |
| 148 | +- π **[Development Guide](docs/development.md)** - Comprehensive development setup |
| 149 | +- π§ **[Extension Development](docs/extensions.md)** - Creating custom extensions |
| 150 | +- ποΈ **[Architecture Deep Dive](docs/architecture.md)** - System design and internals |
| 151 | +- π **[API Reference](docs/api.md)** - Complete API documentation |
| 152 | + |
| 153 | +## π οΈ Development |
| 154 | + |
| 155 | +### Project Structure |
| 156 | + |
| 157 | +``` |
| 158 | +PVQ/ |
| 159 | +βββ poc/runtime/ # PoC Substrate runtime |
| 160 | +βββ pvq-program/ # Program macro and utilities |
| 161 | +βββ pvq-executor/ # Core execution engine |
| 162 | +βββ pvq-extension*/ # Extension system and implementations |
| 163 | +βββ pvq-runtime-api/ # Runtime API definition |
| 164 | +βββ pvq-test-runner/ # Testing utilities |
| 165 | +βββ guest-examples/ # Example programs |
| 166 | +βββ vendor/polkavm/ # Vendored PolkaVM dependency |
| 167 | +``` |
| 168 | + |
| 169 | +### Building from Source |
| 170 | + |
| 171 | +```bash |
| 172 | +# Development build |
| 173 | +cargo build |
| 174 | + |
| 175 | +# Release build with optimizations |
| 176 | +cargo build --release |
| 177 | + |
| 178 | +# Build guest examples |
| 179 | +make guests |
| 180 | + |
| 181 | +# Run all tests |
| 182 | +cargo test --all |
| 183 | + |
| 184 | +# Run clippy lints |
| 185 | +cargo clippy --all --all-targets |
| 186 | +``` |
| 187 | + |
| 188 | +### Testing |
| 189 | + |
| 190 | +```bash |
| 191 | +# Run unit tests |
| 192 | +cargo test |
| 193 | + |
| 194 | +# Run integration tests |
| 195 | +cargo test --test integration |
| 196 | + |
| 197 | +# Test specific component |
| 198 | +cargo test -p pvq-executor |
| 199 | + |
| 200 | +# Run example programs |
| 201 | +make test-guests |
| 202 | +``` |
| 203 | + |
| 204 | +## π€ Contributing |
| 205 | + |
| 206 | +We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details. |
| 207 | + |
| 208 | +### Development Workflow |
| 209 | + |
| 210 | +1. Fork the repository |
| 211 | +2. Create a feature branch (`git checkout -b feature/amazing-feature`) |
| 212 | +3. Make your changes and add tests |
| 213 | +4. Ensure all tests pass (`cargo test --all`) |
| 214 | +5. Run formatting (`cargo fmt`) and linting (`cargo clippy`) |
| 215 | +6. Commit your changes (`git commit -am 'Add amazing feature'`) |
| 216 | +7. Push to the branch (`git push origin feature/amazing-feature`) |
| 217 | +8. Open a Pull Request |
| 218 | + |
| 219 | +### Code Style |
| 220 | + |
| 221 | +- Follow Rust standard formatting (`cargo fmt`) |
| 222 | +- Ensure clippy passes (`cargo clippy`) |
| 223 | +- Add comprehensive tests for new features |
| 224 | +- Document public APIs with rustdoc comments |
| 225 | + |
| 226 | +## π License |
11 | 227 |
|
12 | | -### Run Examples |
| 228 | +This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. |
13 | 229 |
|
14 | | -`guest-examples` contains several guest programs to test the PVQ. |
| 230 | +## π Acknowledgments |
15 | 231 |
|
16 | | -1. Build guest program: `make guests` |
17 | | -2. Run test runner: `cargo run -p pvq-test-runner -- --program output/<guest-program>` |
| 232 | +- [PolkaVM](https://github.com/koute/polkavm) - The underlying virtual machine |
| 233 | +- [Substrate](https://substrate.io/) - The blockchain development framework |
| 234 | +- [Polkadot](https://polkadot.network/) - The multi-chain protocol |
18 | 235 |
|
19 | | -Available PoC guest programs: |
| 236 | +## π Links |
20 | 237 |
|
21 | | -- `guest-sum-balance`: sum the balances of multiple accounts |
22 | | -- `guest-total-supply`: get the total supply of an asset |
23 | | -- `guest-sum-balance-percent`: sum the balances of multiple accounts and calculate the percentage of the total supply |
| 238 | +- π **Homepage**: [https://acala.network](https://acala.network) |
| 239 | +- π **Documentation**: [https://docs.pvq.dev](https://docs.pvq.dev) |
| 240 | +- π¬ **Discord**: [Join our community](https://discord.gg/acala) |
| 241 | +- π¦ **Twitter**: [@AcalaNetwork](https://twitter.com/AcalaNetwork) |
24 | 242 |
|
25 | | -### RuntimeAPI PoC |
| 243 | +--- |
26 | 244 |
|
27 | | -1. Use chopsticks to start a local chain with the RuntimeAPI enabled: `make run` |
28 | | -2. Build guest programs: `make guests` |
29 | | -3. Run test runner to display hex-encoded `args` in tracing logs: `cargo run -p pvq-test-runner -- --program output/<guest-program>` |
30 | | -4. Upload `program` and `args` in PJS UI. |
| 245 | +**Built with β€οΈ by the Acala team** |
0 commit comments