-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Refactoring System Hooks to communicate with System Contracts #414
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
base: dev
Are you sure you want to change the base?
Conversation
| #[test] | ||
| fn test_withdrawal() { | ||
| let mut chain = Chain::empty(None); | ||
| let bytecode_vec: Vec<u8> = hex::decode("60806040526004361015610013575b610129565b61001e60003561002d565b6362f84b240361000e576100f3565b60e01c90565b60405190565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b909182601f830112156100915781359167ffffffffffffffff831161008c57602001926001830284011161008757565b610052565b61004d565b610048565b906020828203126100c857600082013567ffffffffffffffff81116100c3576100bf9201610057565b9091565b610043565b61003e565b90565b6100d9906100cd565b9052565b91906100f1906000602085019401906100d0565b565b346101245761012061010f610109366004610096565b90610404565b610117610033565b918291826100dd565b0390f35b610039565b600080fd5b600090565b90565b60018060a01b031690565b90565b61015861015361015d92610133565b610141565b610136565b90565b61016b617000610144565b90565b90565b61018561018061018a9261016e565b610141565b610136565b90565b634e487b7160e01b600052601160045260246000fd5b6101af6101b591610136565b91610136565b019060018060a01b0382116101c657565b61018d565b6101df6101da6101e492610136565b610141565b610136565b90565b6101f0906101cb565b90565b610216610211610201610160565b61020b6001610171565b906101a3565b6101e7565b90565b905090565b90826000939282370152565b90918261023a8161024193610219565b809361021e565b0190565b90916102509261022a565b90565b601f801991011690565b634e487b7160e01b600052604160045260246000fd5b9061027d90610253565b810190811067ffffffffffffffff82111761029757604052565b61025d565b906102af6102a8610033565b9283610273565b565b67ffffffffffffffff81116102cf576102cb602091610253565b0190565b61025d565b906102e66102e1836102b1565b61029c565b918252565b606090565b3d60001461030d576103013d6102d4565b903d6000602084013e5b565b6103156102eb565b9061030b565b151590565b5190565b90565b90565b61033e61033961034392610327565b610141565b610324565b90565b60000190565b60200190565b61035c90516100cd565b90565b1b90565b61037d61037861037283610320565b9261034c565b610352565b906020811061038b575b5090565b61039e906000199060200360080261035f565b1638610387565b6103ae906101e7565b90565b6103ba906100cd565b90565b60209181520190565b91906103e0816103d9816103e5956103bd565b809561021e565b610253565b0190565b909161040192602083019260008185039101526103c6565b90565b919061040e61012e565b5060008061041a6101f3565b85828591610432610429610033565b93849283610245565b03925af16104486104416102f0565b911561031b565b80156104cb575b6104ae5761045c90610363565b923384919261049461048e7f3a36e47291f4201faf137fab081d92295bce2d53be2c6ca68ba82c7faa9ce241936103a5565b936103b1565b936104a96104a0610033565b928392836103e9565b0390a3565b600063f801b06960e01b8152806104c760048201610346565b0390fd5b506104d581610320565b6104e86104e2602061032a565b91610324565b141561044f56fea2646970667358221220eaa72a072e95715690c88c25da2fd94b2b0a7a610c93721e3eb39b3c9804086464736f6c634300081c0033").expect("valid hex"); |
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.
Would be nice to add the source code as a comment (if it's not too big)
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.
Shall I just link PR for contracts?
…tly for incorrect callee / caller
| } | ||
| 4 => { | ||
| // contract_deployer: setBytecodeDetailsEVM(address,bytes32,uint32,bytes32) | ||
| // contract_deployer: setBytecodeDetailsEVM(address,bytes32,uint32,bytes32,uint32) |
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.
nit: please update comment
|
|
||
| [dependencies] | ||
| rig = { path = "../../rig", features = ["for_tests"] } | ||
| system_hooks = { path = "../../../system_hooks" } |
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.
looks like it's not used
| }; | ||
|
|
||
| // RUNTIME bytecode | ||
| pub static L1_MESSENGER_BYTECODE: Lazy<Vec<u8>> = Lazy::new(|| { |
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.
nit: you can do it without lazy, just define a const string, and then hex::decode as needed, there is an example in utils.rs. Though it's a styling preference, so if you prefer the current approach, we can keep it
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.
But then if we'll need to re-use in multiple places, hex::decode will be called every time, I think it looks neater with Lazy
|
|
||
| [dependencies] | ||
| rig = { path = "../../rig", features = ["for_tests"] } | ||
| system_hooks = { path = "../../../system_hooks" } |
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.
not used as well?
system_hooks/src/l1_messenger.rs
Outdated
| } = request; | ||
|
|
||
| debug_assert_eq!(callee, L1_MESSENGER_ADDRESS); | ||
| if caller != L1_MESSENGER_ADDRESS || callee != L1_MESSENGER_ADDRESS_HOOK { |
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.
for callee check it makes sense to keep debug assert IMHO
system_hooks/src/l1_messenger.rs
Outdated
| "L1 messenger failure: sendToL1 called with invalid calldata", | ||
| )); | ||
| } | ||
| let length: usize = U256::from_be_slice(&calldata[offset..offset + 32]) |
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.
it's not safe, we need to check that calldata length >= offset + 32
system_hooks/src/l1_messenger.rs
Outdated
| if calldata.len() < end { | ||
| return Ok(Err("L1 messenger failure: truncated bytes payload")); | ||
| } | ||
| let message = &calldata[start..end]; |
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.
same here, actually you can check code before your change in this place, there is similar logic to parse abi encoded bytes with needed checks. I understand that in practice it's not critical as this hook can be called only by our contract which always encodes data correctly, but better to make it error resistant for future
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.
Btw, not sure what are requirements, but you can also consider simplified ABI here, let's say that first 20/32 bytes is the sender, and rest of calldata is the message(&calldata[32..]), something like encodePacked
| resources.charge(&S::Resources::from_ergs(l1_message_cost_ergs))?; | ||
| // emit L1 message | ||
| let message_hash = system.io.emit_l1_message( | ||
| // We already charged gas for it |
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.
nit: IMHO it's nice to keep this comment
| } = request; | ||
|
|
||
| debug_assert_eq!(callee, CONTRACT_DEPLOYER_ADDRESS); | ||
| if caller != CONTRACT_DEPLOYER_ADDRESS || callee != SET_BYTECODE_ON_ADDRESS_HOOK { |
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.
Also better to check callee with debug assert
| )); | ||
| } | ||
|
|
||
| let address = B160::try_from_be_slice(&calldata[12..32]).ok_or(SystemError::LeafDefect( |
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.
nit: when parsing abi encoded addresses it makes sense to check that first 12 bytes are zeroes just in case, or add a explicit comment
Benchmark report
|
What ❔
PR strips down the messenger system hook, so all of the checks can be delegated to the respective system contract and the hook only accepts the raw message and emits events.
Why ❔
Simplification of protocol integration
Is this a breaking change?
Checklist