Skip to content

Commit

Permalink
Updated Conditions Args to SExp, Added Opcodes, Added more Interfaces…
Browse files Browse the repository at this point in the history
… to SExp and Auto add Derivation Idexes that get loaded to store
  • Loading branch information
DaOneLuna committed Nov 6, 2024
1 parent 8823b5b commit 348628b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cli/src/wallets/memory_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl WalletStore for MemoryWalletStore {
{
None => Err(Error::new(
ErrorKind::NotFound,
format!("Failed to find public_key: {public_key})"),
format!("Failed to find secret_key for pub_key: {public_key})"),
)),
Some(v) => {
let secret_key = SecretKey::from_bytes(v.value().as_ref()).map_err(|e| {
Expand Down
1 change: 1 addition & 0 deletions cli/src/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub trait WalletStore {
hardened: bool,
) -> Result<DerivationRecord, Error> {
let wallet_sk = self.wallet_sk(index, hardened)?;
let _ = self.secret_key_store().save_secret_key(&wallet_sk);
let pubkey = Bytes48::from(wallet_sk.sk_to_pk().to_bytes());
let puzzle_hash = puzzle_hash_for_pk(&pubkey)?;
self.add_puzzle_hash_and_keys(puzzle_hash, (Bytes32::from(wallet_sk), pubkey))
Expand Down
2 changes: 2 additions & 0 deletions core/src/blockchain/condition_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum ConditionOpcode {
AssertPuzzleAnnouncement = 63,
AssertConcurrentSpend = 64,
AssertConcurrentPuzzle = 65,
SendMessage = 66,
ReceiveMessage = 67,
AssertMyCoinId = 70,
AssertMyParentId = 71,
AssertMyPuzzlehash = 72,
Expand Down
20 changes: 14 additions & 6 deletions core/src/blockchain/condition_with_args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::blockchain::condition_opcode::ConditionOpcode;
use crate::clvm::sexp::{IntoSExp, SExp, NULL};
use crate::clvm::sexp::{AtomBuf, IntoSExp, SExp, NULL};
use dg_xch_macros::ChiaSerial;
use serde::{Deserialize, Serialize};

Expand All @@ -11,13 +11,21 @@ pub struct ConditionWithArgs {
impl IntoSExp for &ConditionWithArgs {
fn to_sexp(self) -> SExp {
let mut vars = vec![];
for var in &self.vars {
vars.push(var.as_slice().to_sexp());
for var in self.vars.iter().rev() {
vars.push(SExp::Atom(AtomBuf::from(var)));
}
let mut args_pair = NULL.clone();
let mut args_pair = None;
for var in vars.into_iter().rev() {
args_pair = args_pair.cons(var)
match args_pair {
None => args_pair = Some(var),
Some(pair) => args_pair = Some(pair.cons(var)),
}
}
self.opcode.to_sexp().cons(args_pair)
self.opcode.to_sexp().cons(args_pair.unwrap_or_else(|| NULL.clone()))
}
}
impl IntoSExp for ConditionWithArgs {
fn to_sexp(self) -> SExp {
(&self).to_sexp()
}
}
21 changes: 16 additions & 5 deletions core/src/clvm/condition_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::clvm::sexp::{IntoSExp, SExp};
use num_traits::ToPrimitive;
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use log::info;

pub type ConditionsDict<S> = HashMap<ConditionOpcode, Vec<ConditionWithArgs>, S>;

Expand Down Expand Up @@ -85,7 +86,9 @@ pub fn conditions_dict_for_solution<S: std::hash::BuildHasher + Default>(
) -> Result<(ConditionsDict<S>, u64), Error> {
match conditions_for_solution(puzzle_reveal, solution, max_cost) {
Ok((result, cost)) => Ok((conditions_by_opcode(result), cost)),
Err(error) => Err(error),
Err(error) => {
Err(error)
},
}
}

Expand All @@ -95,10 +98,18 @@ pub fn conditions_for_solution(
max_cost: u64,
) -> Result<(Vec<ConditionWithArgs>, u64), Error> {
match puzzle_reveal.run_with_cost(max_cost, &solution.to_program()) {
Ok((cost, r)) => match parse_sexp_to_conditions(&r.to_sexp()) {
Ok(conditions) => Ok((conditions, cost)),
Err(error) => Err(error),
Ok((cost, r)) => {
match parse_sexp_to_conditions(&r.to_sexp()) {
Ok(conditions) => Ok((conditions, cost)),
Err(error) => {
info!("{error:?}");
Err(error)
},
}
},
Err(error) => {
info!("{error:?}");
Err(error)
},
Err(error) => Err(error),
}
}
44 changes: 19 additions & 25 deletions core/src/clvm/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::blockchain::sized_bytes::{
use crate::clvm::assemble::is_hex;
use crate::clvm::assemble::keywords::KEYWORD_FROM_ATOM;
use crate::clvm::program::Program;
use dg_xch_serialize::ChiaProtocolVersion;
use dg_xch_serialize::ChiaSerialize;
use hex::encode;
use num_bigint::BigInt;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -424,31 +422,18 @@ pub trait TryIntoSExp {
}

impl IntoSExp for Vec<SExp> {
fn to_sexp(mut self) -> SExp {
if self.is_empty() {
return SExp::Pair(PairBuf::from((&*NULL, &*NULL)));
}
if self.len() == 1 {
return SExp::Pair(PairBuf::from((&self[0], &*NULL)));
}
if self.len() == 2 {
let last = self.pop().expect("Len Was Checked to Be 2");
let first = self.pop().expect("Len Was Checked to Be 2");
return SExp::Pair(PairBuf::from((first, last)));
}
let mut prog = None;
while let Some(next) = self.pop() {
match prog {
None => {
prog = Some(next);
}
Some(existing) => {
let new = next.cons(existing);
prog = Some(new);
fn to_sexp(self) -> SExp {
if let Some(sexp) = self.first().cloned() {
let mut end = NULL.clone();
if self.len() > 1 {
for other in self[1..].iter().rev() {
end = other.clone().cons(end);
}
}
sexp.cons(end)
} else {
NULL.clone()
}
prog.expect("Expected Program, Lengths Checked Above")
}
}

Expand All @@ -471,6 +456,15 @@ impl<T: IntoSExp> IntoSExp for Vec<T> {
}
}

impl<T: IntoSExp> IntoSExp for Option<T> {
fn to_sexp(self) -> SExp {
match self {
None => NULL.clone(),
Some(s) => s.to_sexp()
}
}
}

impl<T: IntoSExp> IntoSExp for (T, T) {
fn to_sexp(self) -> SExp {
self.0.to_sexp().cons(self.1.to_sexp())
Expand Down Expand Up @@ -509,7 +503,7 @@ impl IntoSExp for &Program {

impl IntoSExp for ConditionOpcode {
fn to_sexp(self) -> SExp {
SExp::Atom(AtomBuf::new(self.to_bytes(ChiaProtocolVersion::default())))
SExp::Atom(AtomBuf::new(vec![self as u8]))
}
}

Expand Down

0 comments on commit 348628b

Please sign in to comment.