-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Mode to disable backward jumps on Neo AppEngine 3 #837
Changes from all commits
5cdd28e
0aafb26
668c160
c75b4e9
e0ad7ef
bb1731d
75cf115
8ee60a2
39215b6
8688f38
e896027
b1cc6a7
b72e8f9
3381b5e
97330bd
d1c03c0
13ba5b0
93f4734
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 |
---|---|---|
|
@@ -1069,5 +1069,134 @@ public void ToJson() | |
jObj["script"].AsString().Should().Be("4220202020202020202020202020202020202020202020202020202020202020"); | ||
jObj["sys_fee"].AsString().Should().Be("4200000000"); | ||
} | ||
|
||
[TestMethod] | ||
public void Infinite_Loop_Not_Allowed_On_Application() | ||
{ | ||
byte[] script; | ||
using (ScriptBuilder sb = new ScriptBuilder()) | ||
{ | ||
sb.Emit(OpCode.JMP, new byte[] { 0, 0, 0, 0 }); | ||
script = sb.ToArray(); | ||
} | ||
|
||
long netfee = 100000000; // network fee | ||
|
||
// Check Application | ||
long appGas = 0; | ||
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, null, null, netfee, false)) | ||
{ | ||
engine.LoadScript(script); | ||
Assert.AreEqual(VMState.FAULT, engine.Execute()); | ||
Assert.AreEqual(0, engine.ResultStack.Count); | ||
appGas += engine.GasConsumed; | ||
} | ||
|
||
// many gas are spent before fault | ||
Assert.AreEqual(appGas, 100000040); | ||
} | ||
|
||
[TestMethod] | ||
public void Infinite_Loop_Not_Allowed_On_Verification() | ||
{ | ||
byte[] script; | ||
using (ScriptBuilder sb = new ScriptBuilder()) | ||
{ | ||
sb.Emit(OpCode.JMP, new byte[] { 0, 0, 0, 0 }); | ||
script = sb.ToArray(); | ||
} | ||
|
||
long netfee = 100000000; // network fee | ||
|
||
// Check Verification | ||
long verificationGas = 0; | ||
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, null, null, netfee, false)) | ||
{ | ||
engine.LoadScript(script); | ||
Assert.AreEqual(VMState.FAULT, engine.Execute()); | ||
Assert.AreEqual(0, engine.ResultStack.Count); | ||
verificationGas += engine.GasConsumed; | ||
} | ||
|
||
// no gas is spent (backwards jump is not allowed) | ||
Assert.AreEqual(verificationGas, 0); | ||
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. @erikzhang @shargon this basic test is already giving evidence that, for Verification, zero gas is spent (no backward jumps allowed), but on Application, it goes until funds are exhausted (and fails. this wouldn't consume the funds on verification, and this could be repeated again and again). 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. How can we prevent the issue with CALLS? 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. On CALLS, we have two options... count StepInto(), or just count it on PreExecute(). Must see what's better. |
||
} | ||
|
||
[TestMethod] | ||
public void Recursion_Not_Allowed_On_Application() | ||
{ | ||
// example for recursion (no local variables or parameters) | ||
/* | ||
00 11: PUSH0 #An empty array of bytes is pushed onto the stack | ||
c5 12: NEWARRAY # | ||
6b 13: TOALTSTACK # Puts the input onto the top of the alt stack. Removes it from the main stack. | ||
61 14: NOP # Does nothing. | ||
65 15: CALL fcff # -4 | ||
61 18: NOP # Does nothing. | ||
6c 19: FROMALTSTACK # Puts the input onto the top of the main stack. Removes it from the alt stack. | ||
75 20: DROP # Removes the top stack item. | ||
66 21: RET # | ||
*/ | ||
|
||
byte[] script; | ||
using (ScriptBuilder sb = new ScriptBuilder()) | ||
{ | ||
sb.Emit(OpCode.CALL, new byte[] { 0, 0, 0, 0 }); | ||
script = sb.ToArray(); | ||
} | ||
|
||
long netfee = 100000000; // network fee | ||
|
||
// Check Application | ||
long appGas = 0; | ||
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, null, null, netfee, false)) | ||
{ | ||
engine.LoadScript(script); | ||
Assert.AreEqual(VMState.FAULT, engine.Execute()); | ||
Assert.AreEqual(0, engine.ResultStack.Count); | ||
appGas += engine.GasConsumed; | ||
} | ||
// many gas are spent before fault | ||
Assert.AreEqual(appGas, 22528000); | ||
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. Now it's passing much overlimit... million gas-satoshi extra, we should fix that. 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. |
||
} | ||
|
||
[TestMethod] | ||
public void Recursion_Not_Allowed_On_Verification() | ||
{ | ||
// example for recursion (no local variables or parameters) | ||
/* | ||
00 11: PUSH0 #An empty array of bytes is pushed onto the stack | ||
c5 12: NEWARRAY # | ||
6b 13: TOALTSTACK # Puts the input onto the top of the alt stack. Removes it from the main stack. | ||
61 14: NOP # Does nothing. | ||
65 15: CALL fcff # -4 | ||
61 18: NOP # Does nothing. | ||
6c 19: FROMALTSTACK # Puts the input onto the top of the main stack. Removes it from the alt stack. | ||
75 20: DROP # Removes the top stack item. | ||
66 21: RET # | ||
*/ | ||
|
||
byte[] script; | ||
using (ScriptBuilder sb = new ScriptBuilder()) | ||
{ | ||
sb.Emit(OpCode.CALL, new byte[] { 0, 0, 0, 0 }); | ||
script = sb.ToArray(); | ||
} | ||
|
||
long netfee = 100000000; // network fee | ||
|
||
// Check Verification | ||
long verificationGas = 0; | ||
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, null, null, netfee, false)) | ||
shargon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
engine.LoadScript(script); | ||
Assert.AreEqual(VMState.FAULT, engine.Execute()); | ||
Assert.AreEqual(0, engine.ResultStack.Count); | ||
verificationGas += engine.GasConsumed; | ||
} | ||
// no gas should be spent (recursion is not allowed) | ||
// TODO: fix recursion | ||
Assert.AreEqual(verificationGas, 22528000); | ||
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. This should be zero (or near zero), as long as we block recursive behavior. Perhaps stack context count is enough... not sure. |
||
} | ||
} | ||
} |
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.
We are consuming more gas than limit... 40 units more here, it shouldn't I guess.
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.
the method first increase the gas, so is normal to overpass this limit i think
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.
This is passing 40, its reasonable... the other is passing 1 million.
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.
We should break before adding to this variable, I guess.