LordXido commited on
Commit
01cf25d
·
verified ·
1 Parent(s): 2a4c759

Update src/vm.rs

Browse files
Files changed (1) hide show
  1. src/vm.rs +57 -12
src/vm.rs CHANGED
@@ -1,10 +1,19 @@
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,19 +23,35 @@ 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
- // FIX: Copy instruction into owned buffer
 
 
 
 
 
 
 
 
23
  let instr: [u8; 8] = self.program[self.pc..self.pc + 8]
24
  .try_into()
25
- .map_err(|_| "Invalid instruction length")?;
26
 
27
  let opcode = Opcode::from_byte(instr[0])
28
  .ok_or("Invalid opcode")?;
29
 
 
 
 
 
 
 
30
  match opcode {
31
  Opcode::INVCHK => self.invchk()?,
32
  Opcode::TADD => self.trust_add(&instr)?,
@@ -40,12 +65,15 @@ impl BCNVM {
40
  Opcode::CREW => self.work_credit()?,
41
  Opcode::IBND => self.identity_bind()?,
42
  Opcode::ILOCK => self.identity_lock()?,
 
 
43
  Opcode::HALT => break,
44
  Opcode::NOOP => {},
45
  _ => return Err("Unimplemented opcode".into()),
46
  }
47
 
48
  self.pc += 8;
 
49
  }
50
  Ok(())
51
  }
@@ -61,26 +89,22 @@ impl BCNVM {
61
  }
62
 
63
  fn trust_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
64
- let imm = instr[5] as i64;
65
- self.state.trust += imm;
66
  Ok(())
67
  }
68
 
69
  fn trust_sub(&mut self, instr: &[u8; 8]) -> Result<(), String> {
70
- let imm = instr[5] as i64;
71
- self.state.trust -= imm;
72
  Ok(())
73
  }
74
 
75
  fn flow_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
76
- let imm = instr[5] as i64;
77
- self.state.flow_total += imm;
78
  Ok(())
79
  }
80
 
81
  fn flow_vel(&mut self) -> Result<(), String> {
82
- self.state.flow_velocity =
83
- self.state.flow_total as f64 / 86400.0;
84
  Ok(())
85
  }
86
 
@@ -107,10 +131,8 @@ impl BCNVM {
107
 
108
  fn credit_return(&mut self, instr: &[u8; 8]) -> Result<(), String> {
109
  let imm = instr[5] as i64;
110
-
111
  self.state.credit_balance -= imm;
112
  self.state.trust += imm / 10;
113
-
114
  Ok(())
115
  }
116
 
@@ -138,4 +160,27 @@ impl BCNVM {
138
  self.state.identity_locked = true;
139
  Ok(())
140
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  }
 
1
  use crate::{opcode::Opcode, registers::Registers, state::VMState};
2
 
3
+ #[derive(Debug)]
4
+ pub struct TraceEvent {
5
+ pub pc: usize,
6
+ pub opcode: u8,
7
+ pub state_snapshot: VMState,
8
+ }
9
+
10
  pub struct BCNVM {
11
  pub regs: Registers,
12
  pub state: VMState,
13
  pub pc: usize,
14
  pub program: Vec<u8>,
15
+ pub gas: u64,
16
+ pub trace: Vec<TraceEvent>,
17
  }
18
 
19
  impl BCNVM {
 
23
  state: VMState::new(),
24
  pc: 0,
25
  program,
26
+ gas: 10_000,
27
+ trace: Vec::new(),
28
  }
29
  }
30
 
31
  pub fn run(&mut self) -> Result<(), String> {
32
  while self.pc < self.program.len() {
33
+ if self.gas == 0 {
34
+ return Err("Out of gas".into());
35
+ }
36
+
37
+ if self.pc + 8 > self.program.len() {
38
+ return Err("Instruction out of bounds".into());
39
+ }
40
+
41
+ // ✅ Borrow-checker safe decode
42
  let instr: [u8; 8] = self.program[self.pc..self.pc + 8]
43
  .try_into()
44
+ .map_err(|_| "Invalid instruction")?;
45
 
46
  let opcode = Opcode::from_byte(instr[0])
47
  .ok_or("Invalid opcode")?;
48
 
49
+ self.trace.push(TraceEvent {
50
+ pc: self.pc,
51
+ opcode: instr[0],
52
+ state_snapshot: self.state.clone(),
53
+ });
54
+
55
  match opcode {
56
  Opcode::INVCHK => self.invchk()?,
57
  Opcode::TADD => self.trust_add(&instr)?,
 
65
  Opcode::CREW => self.work_credit()?,
66
  Opcode::IBND => self.identity_bind()?,
67
  Opcode::ILOCK => self.identity_lock()?,
68
+ Opcode::VCAST => self.vote_cast(&instr)?,
69
+ Opcode::VWEI => self.vote_weight()?,
70
  Opcode::HALT => break,
71
  Opcode::NOOP => {},
72
  _ => return Err("Unimplemented opcode".into()),
73
  }
74
 
75
  self.pc += 8;
76
+ self.gas -= 1;
77
  }
78
  Ok(())
79
  }
 
89
  }
90
 
91
  fn trust_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
92
+ self.state.trust += instr[5] as i64;
 
93
  Ok(())
94
  }
95
 
96
  fn trust_sub(&mut self, instr: &[u8; 8]) -> Result<(), String> {
97
+ self.state.trust -= instr[5] as i64;
 
98
  Ok(())
99
  }
100
 
101
  fn flow_add(&mut self, instr: &[u8; 8]) -> Result<(), String> {
102
+ self.state.flow_total += instr[5] as i64;
 
103
  Ok(())
104
  }
105
 
106
  fn flow_vel(&mut self) -> Result<(), String> {
107
+ self.state.flow_velocity = self.state.flow_total as f64 / 86400.0;
 
108
  Ok(())
109
  }
110
 
 
131
 
132
  fn credit_return(&mut self, instr: &[u8; 8]) -> Result<(), String> {
133
  let imm = instr[5] as i64;
 
134
  self.state.credit_balance -= imm;
135
  self.state.trust += imm / 10;
 
136
  Ok(())
137
  }
138
 
 
160
  self.state.identity_locked = true;
161
  Ok(())
162
  }
163
+
164
+ // 🔥 Governance extension
165
+ fn vote_weight(&mut self) -> Result<(), String> {
166
+ let weight =
167
+ 0.5 * self.state.trust as f64 +
168
+ 0.3 * self.state.flow_velocity +
169
+ 0.2 * self.state.productivity as f64;
170
+
171
+ self.regs.set(12, weight as i64);
172
+ Ok(())
173
+ }
174
+
175
+ fn vote_cast(&mut self, instr: &[u8; 8]) -> Result<(), String> {
176
+ let proposal_id = instr[5];
177
+ let weight = self.regs.get(12);
178
+
179
+ println!(
180
+ "Vote cast → Proposal {} | Weight {}",
181
+ proposal_id, weight
182
+ );
183
+
184
+ Ok(())
185
+ }
186
  }