Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum Error {
/// Entry error
#[error(transparent)]
Entry(#[from] EntryError),
/// Key error
/// Key error
#[error(transparent)]
Key(#[from] KeyError),
/// Kvp error
Expand Down Expand Up @@ -83,7 +83,7 @@ pub enum EntryError {
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum KeyError {
/// Empty key string
/// Empty key string
#[error("the key string is empty")]
EmptyKey,
/// Missing root key separator
Expand Down
82 changes: 55 additions & 27 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a> Iterator for EntryIter<'a> {
self.current += 1;
Some(e)
}
None => None
None => None,
}
}
}
Expand All @@ -189,6 +189,7 @@ struct VerifyIter<'a> {
entries: Vec<&'a Entry>,
seqno: usize,
prev_seqno: usize,
prev_cid: Cid,
kvp: Kvp<'a>,
lock_scripts: Vec<Script>,
error: Option<Error>,
Expand All @@ -211,7 +212,7 @@ impl<'a> Iterator for VerifyIter<'a> {
let mut pstack = Stk::default();
let mut rstack = Stk::default();

// check the seqno meet the criteria
// check the seqno meets the criteria
if self.seqno > 0 && self.seqno != self.prev_seqno + 1 {
// set our index out of range
self.seqno = self.entries.len();
Expand All @@ -220,13 +221,22 @@ impl<'a> Iterator for VerifyIter<'a> {
return Some(Err(self.error.clone().unwrap()));
}

// check if the cid meets the criteria
if entry.prev() != self.prev_cid {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::BrokenPrevLink.into());
return Some(Err(self.error.clone().unwrap()));
}

// 'unlock:
let mut result = {
// run the unlock script using the entry as the kvp to get the
// stack in the vm::Context set up.
let unlock_ctx = vm::Context {
current: entry, // limit the available data to just the entry
proposed: entry, // limit the available data to just the entry
current: entry, // limit the available data to just the entry
proposed: entry, // limit the available data to just the entry
pstack: &mut pstack,
rstack: &mut rstack,
check_count: 0,
Expand All @@ -243,11 +253,13 @@ impl<'a> Iterator for VerifyIter<'a> {
let mut instance = match vm::Builder::new()
.with_context(unlock_ctx)
.with_bytes(entry.unlock.clone())
.try_build() {
.try_build()
{
Ok(i) => i,
Err(e) => {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::Wacc(e).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand All @@ -258,6 +270,7 @@ impl<'a> Iterator for VerifyIter<'a> {
if let Some(e) = instance.run("for_great_justice").err() {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::Wacc(e).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand All @@ -277,10 +290,14 @@ impl<'a> Iterator for VerifyIter<'a> {
if !result {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(
LogError::VerifyFailed(
format!("unlock script failed\nvalues:\n{:?}\nreturn:\n{:?}",
rstack, pstack)).into());
LogError::VerifyFailed(format!(
"unlock script failed\nvalues:\n{:?}\nreturn:\n{:?}",
rstack, pstack
))
.into(),
);
return Some(Err(self.error.clone().unwrap()));
}

Expand All @@ -301,6 +318,7 @@ impl<'a> Iterator for VerifyIter<'a> {
if let Some(e) = self.kvp.apply_entry_ops(entry).err() {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::UpdateKvpFailed(e.to_string()).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand All @@ -315,6 +333,7 @@ impl<'a> Iterator for VerifyIter<'a> {
Err(e) => {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(e);
return Some(Err(self.error.clone().unwrap()));
}
Expand Down Expand Up @@ -347,11 +366,13 @@ impl<'a> Iterator for VerifyIter<'a> {
let mut instance = match vm::Builder::new()
.with_context(lock_ctx)
.with_bytes(lock.clone())
.try_build() {
.try_build()
{
Ok(i) => i,
Err(e) => {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::Wacc(e).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand All @@ -362,6 +383,7 @@ impl<'a> Iterator for VerifyIter<'a> {
if let Some(e) = instance.run("move_every_zig").err() {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::Wacc(e).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand Down Expand Up @@ -390,6 +412,7 @@ impl<'a> Iterator for VerifyIter<'a> {
if let Some(e) = self.kvp.apply_entry_ops(entry).err() {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(LogError::UpdateKvpFailed(e.to_string()).into());
return Some(Err(self.error.clone().unwrap()));
}
Expand All @@ -399,13 +422,19 @@ impl<'a> Iterator for VerifyIter<'a> {
// update the seqno
self.prev_seqno = self.seqno;
self.seqno += 1;
// update the cid
self.prev_cid = entry.cid();
} else {
// set our index out of range
self.seqno = self.entries.len();
// set the error state
self.error = Some(
LogError::VerifyFailed(
format!("unlock script failed\nvalues:\n{:?}\nreturn:\n{:?}",
rstack, pstack)).into());
LogError::VerifyFailed(format!(
"unlock script failed\nvalues:\n{:?}\nreturn:\n{:?}",
rstack, pstack
))
.into(),
);
return Some(Err(self.error.clone().unwrap()));
}

Expand Down Expand Up @@ -435,9 +464,10 @@ impl Log {
entries,
seqno: 0,
prev_seqno: 0,
prev_cid: Cid::null(),
kvp: Kvp::default(),
lock_scripts: vec![self.first_lock.clone()],
error: None
error: None,
}
}

Expand Down Expand Up @@ -735,11 +765,11 @@ mod tests {
let e1 = entry::Builder::default()
.with_vlad(&vlad)
.with_seqno(0)
.add_lock(&lock) // "/" -> lock.wast
.add_lock(&lock) // "/" -> lock.wast
.with_unlock(&unlock)
.add_op(&ephemeral_op) // "/ephemeral"
.add_op(&pubkey1_op) // "/pubkey"
.add_op(&preimage1_op) // "/preimage"
.add_op(&ephemeral_op) // "/ephemeral"
.add_op(&pubkey1_op) // "/pubkey"
.add_op(&preimage1_op) // "/preimage"
.try_build(|e| {
let ev: Vec<u8> = e.clone().into();
let sv = ephemeral.sign_view().unwrap();
Expand All @@ -752,11 +782,11 @@ mod tests {
let e2 = entry::Builder::default()
.with_vlad(&vlad)
.with_seqno(1)
.add_lock(&lock) // "/" -> lock.wast
.add_lock(&lock) // "/" -> lock.wast
.with_unlock(&unlock)
.with_prev(&e1.cid())
.add_op(&Op::Delete("/ephemeral".try_into().unwrap())) // "/ephemeral"
.add_op(&pubkey2_op) // "/pubkey"
.add_op(&Op::Delete("/ephemeral".try_into().unwrap())) // "/ephemeral"
.add_op(&pubkey2_op) // "/pubkey"
.try_build(|e| {
let ev: Vec<u8> = e.clone().into();
let sv = key1.sign_view().unwrap();
Expand All @@ -769,7 +799,7 @@ mod tests {
let e3 = entry::Builder::default()
.with_vlad(&vlad)
.with_seqno(2)
.add_lock(&lock) // "/" -> lock.wast
.add_lock(&lock) // "/" -> lock.wast
.with_unlock(&unlock)
.with_prev(&e2.cid())
.try_build(|e| {
Expand All @@ -784,14 +814,12 @@ mod tests {
let e4 = entry::Builder::default()
.with_vlad(&vlad)
.with_seqno(3)
.add_lock(&lock) // "/" -> lock.wast
.add_lock(&lock) // "/" -> lock.wast
.with_unlock(&unlock)
.with_prev(&e3.cid())
.add_op(&pubkey3_op) // "/pubkey"
.add_op(&preimage2_op) // "/preimage"
.try_build(|_| {
Ok(b"for great justice".to_vec())
})
.add_op(&pubkey3_op) // "/pubkey"
.add_op(&preimage2_op) // "/preimage"
.try_build(|_| Ok(b"for great justice".to_vec()))
.unwrap();
//println!("{:?}", e4);

Expand Down
Loading