Skip to content

Commit cf3d4fa

Browse files
committed
Add stf-runner-demo binary
1 parent 857ba8c commit cf3d4fa

File tree

6 files changed

+1698
-0
lines changed

6 files changed

+1698
-0
lines changed

bin/stf-runner-demo/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "stf-runner-demo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "stf-runner-demo"
8+
path = "main.rs"
9+
10+
[dependencies]
11+
stf-runner = { path = "../../crates/services/stf-runner" }
12+
jsonrpsee = { workspace = true, features = ["server", "macros"] }
13+
tokio = { workspace = true, features = ["full"] }
14+
serde = { workspace = true, features = ["derive"] }
15+
serde_json = { workspace = true }
16+
rand = { workspace = true }
17+
anyhow = { workspace = true }
18+
tracing = { workspace = true }
19+
tracing-subscriber = { workspace = true }
20+
hex = { workspace = true }
21+
22+
strata-primitives = { workspace = true }
23+
strata-asm-common = { workspace = true }
24+
strata-asm-logs = { workspace = true }
25+
strata-chainexec = { workspace = true }
26+
strata-chaintsn = { workspace = true }
27+
strata-l1-txfmt = { workspace = true }
28+
bitcoin = { workspace = true }

bin/stf-runner-demo/README.md

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
# STF Runner Demo
2+
3+
An interactive CLI demonstration of the State Transition Function (STF) for the Orchestration Layer (OL) blockchain. This demo showcases block processing, account management, L1->L2 deposits, withdrawals, and state transitions in real-time.
4+
5+
## Overview
6+
7+
The STF Runner Demo provides a hands-on way to understand how the Orchestration Layer processes blocks and manages account states. It demonstrates:
8+
9+
- Block Processing: Genesis, valid blocks, and invalid block rejection
10+
- L1->L2 Bridge: Deposit functionality via ASM logs
11+
- Account Management: Balance tracking and state updates
12+
- Withdrawals: Balance deduction through SNARK account updates
13+
- State Inspection: Real-time STF state and account balance viewing
14+
15+
## Features
16+
17+
- Automatic Genesis: Genesis block processed on startup
18+
- Deposit System: L1 deposits increase account balances
19+
- Withdrawal System: Deducts balances via output transfers
20+
- State Tracking: View slots, epochs, accounts root, and state root
21+
- Block Inspector: Debug view of any processed block
22+
- Validation: Demonstrates invalid block rejection
23+
- Block Listing: Compact view of all processed blocks
24+
25+
## Getting Started
26+
27+
### Prerequisites
28+
29+
- Rust (latest stable)
30+
- Cargo
31+
32+
### Running the Demo
33+
34+
From the project root:
35+
36+
```bash
37+
cargo run --bin stf-runner-demo
38+
```
39+
40+
Or from the demo directory:
41+
42+
```bash
43+
cd bin/stf-runner-demo
44+
cargo run
45+
```
46+
47+
## Usage
48+
49+
Upon startup, the demo initializes:
50+
51+
```
52+
STF Runner Demo - Interactive CLI
53+
==================================
54+
55+
Initializing demo accounts...
56+
Account 0: 0000000000000000000000000000000000000000000000000000000000000000 balance = 0 sats
57+
Account 1: 0101010101010101010101010101010101010101010101010101010101010101 balance = 0 sats
58+
59+
Creating bridge account for withdrawals...
60+
Bridge Account: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff for withdrawals
61+
62+
Processing genesis block...
63+
Genesis block processed successfully!
64+
State root: f98ac7fb8baa471f
65+
66+
Current STF State:
67+
Slot: 0
68+
Epoch: 0
69+
Accounts Root: bcadcb..ac221a
70+
State Root: f98ac7..aa471f
71+
```
72+
73+
### Available Commands
74+
75+
| Command | Alias | Description | Example |
76+
|---------|-------|-------------|---------|
77+
| help | h | Show available commands | help |
78+
| state | s | Display current STF state | state |
79+
| accounts | a | Show account balances | accounts |
80+
| blocks | l | List all processed blocks | blocks |
81+
| debug <slot> | | Show detailed block info | debug 1 |
82+
| deposit <amount> | | Process L1 deposit | deposit 5000 |
83+
| withdraw <amount> | | Process withdrawal | withdraw 1000 |
84+
| block | b | Process empty test block | block |
85+
| invalid | i | Process invalid block (fails) | invalid |
86+
| quit | q | Exit the demo | quit |
87+
88+
## Example Workflow
89+
90+
### 1. Check Initial State
91+
92+
```
93+
stf> state
94+
Current STF State:
95+
Slot: 0
96+
Epoch: 0
97+
Accounts Root: bcadcb..ac221a
98+
State Root: f98ac7..aa471f
99+
100+
stf> accounts
101+
Account States:
102+
Account 0: 0000000000...000 balance = 0 sats
103+
Account 1: 0101010101...101 balance = 0 sats
104+
```
105+
106+
### 2. Make a Deposit
107+
108+
```
109+
stf> deposit 5000
110+
Processing L1 deposit of 5000 sats to account 0...
111+
SUCCESS: Deposit block processed!
112+
New state root: 698480..8cf426
113+
Account 0 balance updated - use 'accounts' to see changes
114+
```
115+
116+
### 3. Check Updated State
117+
118+
```
119+
stf> state
120+
Current STF State:
121+
Slot: 1
122+
Epoch: 1
123+
Accounts Root: 98e418..517a77
124+
State Root: 698480..8cf426
125+
126+
stf> accounts
127+
Account States:
128+
Account 0: 0000000000...000 balance = 5000 sats
129+
Account 1: 0101010101...101 balance = 0 sats
130+
```
131+
132+
### 4. Make a Withdrawal
133+
134+
```
135+
stf> withdraw 1000
136+
Processing withdrawal of 1000 sats from account 0...
137+
SUCCESS: Withdrawal block processed!
138+
New state root: d480b7..764585
139+
Account 0 balance updated - use 'accounts' to see changes
140+
```
141+
142+
### 5. Verify Balance Deduction
143+
144+
```
145+
stf> accounts
146+
Account States:
147+
Account 0: 0000000000...000 balance = 4000 sats
148+
Account 1: 0101010101...101 balance = 0 sats
149+
```
150+
151+
### 6. View Block History
152+
153+
```
154+
stf> blocks
155+
Processed Blocks:
156+
Slot Block ID State Root
157+
---- -------- ----------
158+
0 853c66fe..9273f72a f98ac7fb..8baa471f
159+
1 646499ba..7501aa45 69848057..448cf426
160+
2 6f05d17e..3b9e1587 d480b737..9f764585
161+
```
162+
163+
### 7. Debug a Specific Block
164+
165+
```
166+
stf> debug 1
167+
Debug Info for Block at Slot 1:
168+
OLBlock {
169+
signed_header: SignedOLBlockHeader {
170+
header: OLBlockHeader {
171+
timestamp: 1000005000,
172+
slot: 1,
173+
epoch: 0,
174+
parent_blockid: 853c66fe..9273f72a,
175+
body_root: 0101010101..01010101,
176+
state_root: 0000000000..00000000,
177+
},
178+
signature: 0303030303..03030303,
179+
},
180+
body: OLBlockBody {
181+
logs: [],
182+
txs: None,
183+
l1update: Some(
184+
L1Update {
185+
inner_state_root: 0000000000..00000000,
186+
new_l1_height: 1000,
187+
manifests: [
188+
AsmManifest {
189+
l1_block_id: aaaaaaaaaa..aaaaaaaa,
190+
logs: [
191+
AsmLogEntry { ... }
192+
],
193+
},
194+
],
195+
},
196+
),
197+
},
198+
}
199+
```
200+
201+
### 8. Test Invalid Block
202+
203+
```
204+
stf> invalid
205+
Processing invalid block (should fail)...
206+
EXPECTED: Invalid block rejected: invalid block header: Invalid slot sequence
207+
```
208+
209+
## Architecture
210+
211+
### Account System
212+
213+
- Account 0: 0000000000...000 - Primary demo account
214+
- Account 1: 0101010101...101 - Secondary demo account
215+
- Bridge Account: ffffffff...fff - Withdrawal destination
216+
217+
### Block Types
218+
219+
1. Genesis Block (Slot 0): Establishes initial state
220+
2. Deposit Blocks: Contain L1Update with ASM logs for deposits
221+
3. Withdrawal Blocks: Contain transactions with OutputTransfer
222+
4. Test Blocks: Empty blocks for slot progression
223+
5. Invalid Blocks: Demonstrate validation (slot regression)
224+
225+
### State Tracking
226+
227+
- Slot: Increments with each processed block
228+
- Epoch: Increments when L1Update is processed
229+
- Accounts Root: Hash of all account states
230+
- State Root: Computed root of current STF state
231+
232+
## Implementation Details
233+
234+
### Deposit Flow
235+
236+
1. DepositLog created with amount and target account
237+
2. Converted to AsmLogEntry
238+
3. Packaged in AsmManifest and L1Update
239+
4. Processed by STF, crediting account balance
240+
5. Epoch incremented
241+
242+
### Withdrawal Flow
243+
244+
1. OutputTransfer created with destination and amount
245+
2. Packaged in SnarkAccountUpdate transaction
246+
3. STF processes transaction, debiting source account
247+
4. Balance saved back to ledger
248+
249+
### State Root Computation
250+
251+
- Block headers contain dummy state roots during creation
252+
- STF computes actual state root during processing
253+
- Demo tracks computed roots for accurate display
254+
255+
## Code Structure
256+
257+
```
258+
bin/stf-runner-demo/
259+
├── main.rs # Main CLI application
260+
├── Cargo.toml # Dependencies and binary config
261+
└── README.md # This file
262+
```
263+
264+
### Key Functions
265+
266+
- main(): CLI loop and initialization
267+
- create_*_block(): Block generation functions
268+
- show_*(): Display functions for state/accounts/blocks
269+
- SimpleStateAccessor: StateAccessor trait implementation
270+
271+
## Troubleshooting
272+
273+
### Common Issues
274+
275+
**Q: Why does my withdrawal not reduce the balance?**
276+
A: Ensure you have sufficient balance. Check with 'accounts' command first.
277+
278+
**Q: Why do some blocks get rejected?**
279+
A: The 'invalid' command intentionally creates blocks with slot regression to demonstrate validation.
280+
281+
**Q: What's the difference between state root in blocks vs STF state?**
282+
A: Block headers contain dummy state roots. The STF computes real state roots during processing.
283+
284+
## Development
285+
286+
### Adding New Commands
287+
288+
1. Add command parsing in main() match statement
289+
2. Create corresponding block generation function
290+
3. Update show_help() with new command
291+
292+
### Modifying Account Behavior
293+
294+
- Edit account initialization in main()
295+
- Modify transaction generation in create_*_block()
296+
- Update display logic in show_accounts()
297+
298+
## Dependencies
299+
300+
- stf-runner: Core STF implementation
301+
- strata-primitives: Basic types and utilities
302+
- strata-asm-logs: ASM log structures
303+
- hex: Hexadecimal encoding for display
304+
- anyhow: Error handling

0 commit comments

Comments
 (0)