-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add LOAD_INPUT_BY_FIELD syscall
- Loading branch information
Showing
5 changed files
with
315 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use ckb_core::transaction::CellInput; | ||
use ckb_protocol::{OutPoint as FbsOutPoint, Script as FbsScript}; | ||
use ckb_vm::{CoreMachine, Error as VMError, Memory, Register, Syscalls, A0, A3, A4, A5, A7}; | ||
use flatbuffers::FlatBufferBuilder; | ||
use syscalls::utils::store_data; | ||
use syscalls::{InputField, Source, ITEM_MISSING, LOAD_INPUT_BY_FIELD_SYSCALL_NUMBER, SUCCESS}; | ||
|
||
#[derive(Debug)] | ||
pub struct LoadInputByField<'a> { | ||
inputs: &'a [&'a CellInput], | ||
current: Option<&'a CellInput>, | ||
} | ||
|
||
impl<'a> LoadInputByField<'a> { | ||
pub fn new( | ||
inputs: &'a [&'a CellInput], | ||
current: Option<&'a CellInput>, | ||
) -> LoadInputByField<'a> { | ||
LoadInputByField { inputs, current } | ||
} | ||
|
||
fn fetch_input(&self, source: Source, index: usize) -> Option<&CellInput> { | ||
match source { | ||
Source::Input => self.inputs.get(index).cloned(), | ||
Source::Output => None, | ||
Source::Current => self.current, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadInputByField<'a> { | ||
fn initialize(&mut self, _machine: &mut CoreMachine<R, M>) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut CoreMachine<R, M>) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != LOAD_INPUT_BY_FIELD_SYSCALL_NUMBER { | ||
return Ok(false); | ||
} | ||
|
||
let index = machine.registers()[A3].to_usize(); | ||
let source = Source::parse_from_u64(machine.registers()[A4].to_u64())?; | ||
let field = InputField::parse_from_u64(machine.registers()[A5].to_u64())?; | ||
|
||
let input = self.fetch_input(source, index); | ||
if input.is_none() { | ||
machine.registers_mut()[A0] = R::from_u8(ITEM_MISSING); | ||
return Ok(true); | ||
} | ||
let input = input.unwrap(); | ||
|
||
match field { | ||
InputField::Unlock => { | ||
let mut builder = FlatBufferBuilder::new(); | ||
let offset = FbsScript::build(&mut builder, &input.unlock); | ||
builder.finish(offset, None); | ||
let data = builder.finished_data(); | ||
store_data(machine, data)?; | ||
} | ||
InputField::OutPoint => { | ||
let mut builder = FlatBufferBuilder::new(); | ||
let offset = FbsOutPoint::build(&mut builder, &input.previous_output); | ||
builder.finish(offset, None); | ||
let data = builder.finished_data(); | ||
store_data(machine, data)?; | ||
} | ||
}; | ||
machine.registers_mut()[A0] = R::from_u8(SUCCESS); | ||
Ok(true) | ||
} | ||
} |
Oops, something went wrong.