-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement OP_WITHIN opcode #28
Changes from 4 commits
5cb66db
20f2e13
62f8808
5c5995e
235d58e
f4bde7c
0db9527
51ca4fc
0996527
b1ac568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ pub mod Opcode { | |
pub const OP_0: u8 = 0; | ||
pub const OP_1: u8 = 81; | ||
pub const OP_ADD: u8 = 147; | ||
pub const OP_GREATERTHAN: u8 = 160; | ||
pub const OP_WITHIN: u8 = 165; | ||
|
||
use shinigami::engine::Engine; | ||
use shinigami::stack::ScriptStackTrait; | ||
|
@@ -155,7 +157,25 @@ pub mod Opcode { | |
145 => not_implemented(ref engine), | ||
146 => not_implemented(ref engine), | ||
147 => opcode_add(ref engine), | ||
_ => not_implemented(ref engine) | ||
148 => not_implemented(ref engine), | ||
149 => not_implemented(ref engine), | ||
150 => not_implemented(ref engine), | ||
151 => not_implemented(ref engine), | ||
152 => not_implemented(ref engine), | ||
153 => not_implemented(ref engine), | ||
154 => not_implemented(ref engine), | ||
155 => not_implemented(ref engine), | ||
156 => not_implemented(ref engine), | ||
157 => not_implemented(ref engine), | ||
158 => not_implemented(ref engine), | ||
159 => not_implemented(ref engine), | ||
160 => opcode_greaterthan(ref engine), | ||
161 => not_implemented(ref engine), | ||
162 => not_implemented(ref engine), | ||
163 => not_implemented(ref engine), | ||
164 => not_implemented(ref engine), | ||
165 => opcode_within(ref engine), | ||
_ => not_implemented(ref engine), | ||
} | ||
} | ||
|
||
|
@@ -174,6 +194,27 @@ pub mod Opcode { | |
engine.dstack.push_int(a + b); | ||
} | ||
|
||
fn opcode_greaterthan(ref engine: Engine) { | ||
let a = engine.dstack.pop_int(); | ||
let b = engine.dstack.pop_int(); | ||
engine.dstack.push_int(if a > b { | ||
0 | ||
} else { | ||
1 | ||
}); | ||
} | ||
|
||
fn opcode_within(ref engine: Engine) { | ||
let a = engine.dstack.pop_int(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be prefered to make the variable naming here more explicit, like min_value, max_value, ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will rename the variables now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi I just pushed an update to rename the variables to be more explicit |
||
let b = engine.dstack.pop_int(); | ||
let c = engine.dstack.pop_int(); | ||
engine.dstack.push_int(if a >= b && a < c { | ||
0 | ||
} else { | ||
1 | ||
}); | ||
} | ||
|
||
fn not_implemented(ref engine: Engine) { | ||
panic!("Opcode not implemented"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refer to this implementation to see the proper ordering of variables on the stack : https://github.com/btcsuite/btcd/blob/b161cd6a199b4e35acec66afc5aad221f05fe1e3/txscript/opcode.go#L1844
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, opcode_within is number 165 in the order, please could you tell me what you mean? I have pushed an update where I renamed and reordered the variables though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we have to make sure the values poped off the stack are in the same order as the bitcoin spec, so here
max_value
should be popped first, thenmin_value
, thenselected_value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @b-j-roberts I have rearranged the variables based on the order max before min before value