Spaces:
Sleeping
Sleeping
Update src/vm.rs
Browse files
src/vm.rs
CHANGED
|
@@ -1,22 +1,10 @@
|
|
| 1 |
-
use crate::mesh::MeshNetwork;
|
| 2 |
use crate::{opcode::Opcode, registers::Registers, state::VMState};
|
| 3 |
|
| 4 |
-
#[derive(Debug)]
|
| 5 |
-
pub struct TraceEvent {
|
| 6 |
-
pub pc: usize,
|
| 7 |
-
pub opcode: u8,
|
| 8 |
-
pub state_snapshot: VMState,
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
pub struct BCNVM {
|
| 12 |
pub regs: Registers,
|
| 13 |
pub state: VMState,
|
| 14 |
pub pc: usize,
|
| 15 |
pub program: Vec<u8>,
|
| 16 |
-
pub gas: u64,
|
| 17 |
-
pub trace: Vec<TraceEvent>,
|
| 18 |
-
pub mesh: MeshNetwork, // 🔥 NEW
|
| 19 |
-
}
|
| 20 |
}
|
| 21 |
|
| 22 |
impl BCNVM {
|
|
@@ -26,25 +14,15 @@ impl BCNVM {
|
|
| 26 |
state: VMState::new(),
|
| 27 |
pc: 0,
|
| 28 |
program,
|
| 29 |
-
gas: 10_000,
|
| 30 |
-
trace: Vec::new(),
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
-
pub fn
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
pc: 0,
|
| 39 |
-
program,
|
| 40 |
-
gas: 10_000,
|
| 41 |
-
trace: Vec::new(),
|
| 42 |
-
mesh: MeshNetwork::new(node_id),
|
| 43 |
-
}
|
| 44 |
-
}
|
| 45 |
}
|
| 46 |
|
| 47 |
-
// ✅ Borrow-checker safe decode
|
| 48 |
let instr: [u8; 8] = self.program[self.pc..self.pc + 8]
|
| 49 |
.try_into()
|
| 50 |
.map_err(|_| "Invalid instruction")?;
|
|
@@ -52,35 +30,16 @@ impl BCNVM {
|
|
| 52 |
let opcode = Opcode::from_byte(instr[0])
|
| 53 |
.ok_or("Invalid opcode")?;
|
| 54 |
|
| 55 |
-
self.trace.push(TraceEvent {
|
| 56 |
-
pc: self.pc,
|
| 57 |
-
opcode: instr[0],
|
| 58 |
-
state_snapshot: self.state.clone(),
|
| 59 |
-
});
|
| 60 |
-
|
| 61 |
match opcode {
|
| 62 |
Opcode::INVCHK => self.invchk()?,
|
| 63 |
-
Opcode::
|
| 64 |
-
Opcode::
|
| 65 |
-
|
| 66 |
-
Opcode::FVEL => self.flow_vel()?,
|
| 67 |
-
Opcode::CISS => self.credit_issue(&instr)?,
|
| 68 |
-
Opcode::CRET => self.credit_return(&instr)?,
|
| 69 |
-
Opcode::CCAP => self.credit_cap()?,
|
| 70 |
-
Opcode::CDEF => self.credit_default()?,
|
| 71 |
-
Opcode::CREW => self.work_credit()?,
|
| 72 |
-
Opcode::IBND => self.identity_bind()?,
|
| 73 |
-
Opcode::ILOCK => self.identity_lock()?,
|
| 74 |
-
Opcode::VCAST => self.vote_cast(&instr)?,
|
| 75 |
-
Opcode::VWEI => self.vote_weight()?,
|
| 76 |
-
Opcode::HALT => break,
|
| 77 |
-
Opcode::NOOP => {},
|
| 78 |
-
_ => return Err("Unimplemented opcode".into()),
|
| 79 |
}
|
| 80 |
|
| 81 |
self.pc += 8;
|
| 82 |
-
self.gas -= 1;
|
| 83 |
}
|
|
|
|
| 84 |
Ok(())
|
| 85 |
}
|
| 86 |
|
|
@@ -88,106 +47,6 @@ impl BCNVM {
|
|
| 88 |
if self.state.trust < 0 {
|
| 89 |
return Err("Trust underflow".into());
|
| 90 |
}
|
| 91 |
-
if self.state.credit_balance < 0 {
|
| 92 |
-
return Err("Negative credit balance".into());
|
| 93 |
-
}
|
| 94 |
-
Ok(())
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
fn trust_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 98 |
-
self.state.trust += instr[5] as i64;
|
| 99 |
-
Ok(())
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
fn trust_sub(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 103 |
-
self.state.trust -= instr[5] as i64;
|
| 104 |
-
Ok(())
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
fn flow_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 108 |
-
self.state.flow_total += instr[5] as i64;
|
| 109 |
-
Ok(())
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
fn flow_vel(&mut self) -> Result<(), String> {
|
| 113 |
-
self.state.flow_velocity = self.state.flow_total as f64 / 86400.0;
|
| 114 |
-
Ok(())
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
fn credit_cap(&mut self) -> Result<(), String> {
|
| 118 |
-
self.state.credit_cap =
|
| 119 |
-
(self.state.trust + self.state.productivity) / 2;
|
| 120 |
-
Ok(())
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
fn credit_issue(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 124 |
-
let imm = instr[5] as i64;
|
| 125 |
-
|
| 126 |
-
if self.state.credit_balance > 0 {
|
| 127 |
-
return Err("No rollovers allowed".into());
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
if imm > self.state.credit_cap {
|
| 131 |
-
return Err("Exceeds credit cap".into());
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
self.state.credit_balance += imm;
|
| 135 |
-
Ok(())
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
fn credit_return(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 139 |
-
let imm = instr[5] as i64;
|
| 140 |
-
self.state.credit_balance -= imm;
|
| 141 |
-
self.state.trust += imm / 10;
|
| 142 |
Ok(())
|
| 143 |
}
|
| 144 |
-
|
| 145 |
-
fn credit_default(&mut self) -> Result<(), String> {
|
| 146 |
-
self.state.default_risk += 0.1;
|
| 147 |
-
self.state.trust -= 10;
|
| 148 |
-
self.state.credit_balance = 0;
|
| 149 |
-
Ok(())
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
fn work_credit(&mut self) -> Result<(), String> {
|
| 153 |
-
self.state.credit_balance -= 10;
|
| 154 |
-
self.state.trust += 5;
|
| 155 |
-
Ok(())
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
fn identity_bind(&mut self) -> Result<(), String> {
|
| 159 |
-
if self.state.identity_locked {
|
| 160 |
-
return Err("Identity already locked".into());
|
| 161 |
-
}
|
| 162 |
-
Ok(())
|
| 163 |
-
}
|
| 164 |
-
|
| 165 |
-
fn identity_lock(&mut self) -> Result<(), String> {
|
| 166 |
-
self.state.identity_locked = true;
|
| 167 |
-
Ok(())
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
// 🔥 Governance extension
|
| 171 |
-
fn vote_weight(&mut self) -> Result<(), String> {
|
| 172 |
-
let weight =
|
| 173 |
-
0.5 * self.state.trust as f64 +
|
| 174 |
-
0.3 * self.state.flow_velocity +
|
| 175 |
-
0.2 * self.state.productivity as f64;
|
| 176 |
-
|
| 177 |
-
self.regs.set(12, weight as i64);
|
| 178 |
-
Ok(())
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
fn vote_cast(&mut self, instr: &[u8; 8]) -> Result<(), String> {
|
| 182 |
-
let proposal_id = instr[5];
|
| 183 |
-
let weight = self.regs.get(12);
|
| 184 |
-
|
| 185 |
-
println!(
|
| 186 |
-
"Vote cast → Proposal {} | Weight {}",
|
| 187 |
-
proposal_id, weight
|
| 188 |
-
);
|
| 189 |
-
|
| 190 |
-
Ok(())
|
| 191 |
-
}
|
| 192 |
-
}
|
| 193 |
-
self.mesh.reconcile(&mut self.state);
|
|
|
|
|
|
|
| 1 |
use crate::{opcode::Opcode, registers::Registers, state::VMState};
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
pub struct BCNVM {
|
| 4 |
pub regs: Registers,
|
| 5 |
pub state: VMState,
|
| 6 |
pub pc: usize,
|
| 7 |
pub program: Vec<u8>,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
}
|
| 9 |
|
| 10 |
impl BCNVM {
|
|
|
|
| 14 |
state: VMState::new(),
|
| 15 |
pc: 0,
|
| 16 |
program,
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
+
pub fn run(&mut self) -> Result<(), String> {
|
| 21 |
+
while self.pc < self.program.len() {
|
| 22 |
+
if self.pc + 8 > self.program.len() {
|
| 23 |
+
return Err("Instruction out of bounds".into());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
|
|
|
| 26 |
let instr: [u8; 8] = self.program[self.pc..self.pc + 8]
|
| 27 |
.try_into()
|
| 28 |
.map_err(|_| "Invalid instruction")?;
|
|
|
|
| 30 |
let opcode = Opcode::from_byte(instr[0])
|
| 31 |
.ok_or("Invalid opcode")?;
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
match opcode {
|
| 34 |
Opcode::INVCHK => self.invchk()?,
|
| 35 |
+
Opcode::HALT => break,
|
| 36 |
+
Opcode::NOOP => {}
|
| 37 |
+
_ => {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
self.pc += 8;
|
|
|
|
| 41 |
}
|
| 42 |
+
|
| 43 |
Ok(())
|
| 44 |
}
|
| 45 |
|
|
|
|
| 47 |
if self.state.trust < 0 {
|
| 48 |
return Err("Trust underflow".into());
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
Ok(())
|
| 51 |
}
|
| 52 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|