-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: optimize local calls #14
Open
fp-crypto
wants to merge
2
commits into
main
Choose a base branch
from
fp-optimize-internal-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ compiler: | |
version: 0.8.11 | ||
remappings: | ||
- "@openzeppelin=OpenZeppelin/[email protected]" | ||
|
||
viaIR: true | ||
optimizer: | ||
details: | ||
yul: true |
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 |
---|---|---|
|
@@ -29,6 +29,8 @@ abstract contract VM { | |
self = address(this); | ||
} | ||
|
||
function dispatch(bytes memory inputs) internal virtual returns (bool success, bytes memory ret) {} | ||
|
||
function _execute(bytes32[] calldata commands, bytes[] memory state) | ||
internal returns (bytes[] memory) | ||
{ | ||
|
@@ -61,14 +63,28 @@ abstract contract VM { | |
) | ||
); | ||
} else if (flags & FLAG_CT_MASK == FLAG_CT_CALL) { | ||
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. shouldn't |
||
(success, outdata) = address(uint160(uint256(command))).call( // target | ||
// inputs | ||
state.buildInputs( | ||
//selector | ||
bytes4(command), | ||
indices | ||
) | ||
); | ||
address _target = address(uint160(uint256(command))); | ||
bytes memory inputs = state.buildInputs( | ||
//selector | ||
bytes4(command), | ||
indices | ||
); | ||
success = false; | ||
|
||
if (_target == address(this)) { | ||
(success, outdata) = dispatch(inputs); | ||
} | ||
|
||
if (!success) { | ||
(success, outdata) = _target.call( // target | ||
// inputs | ||
state.buildInputs( | ||
//selector | ||
bytes4(command), | ||
indices | ||
) | ||
); | ||
} | ||
} else if (flags & FLAG_CT_MASK == FLAG_CT_STATICCALL) { | ||
(success, outdata) = address(uint160(uint256(command))).staticcall( // target | ||
// inputs | ||
|
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,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.11; | ||
|
||
import "../VM.sol"; | ||
|
||
contract TestableVMWithMath is VM { | ||
function execute(bytes32[] calldata commands, bytes[] memory state) | ||
public | ||
payable | ||
returns (bytes[] memory) | ||
{ | ||
return _execute(commands, state); | ||
} | ||
|
||
function sum(uint256 a, uint256 b) public pure returns (uint256) { | ||
return a + b; | ||
} | ||
|
||
|
||
function dispatch(bytes memory inputs) | ||
internal | ||
override | ||
returns (bool _success, bytes memory _ret) | ||
{ | ||
bytes4 _selector = bytes4(bytes32(inputs)); | ||
if (this.sum.selector == _selector) { | ||
uint256 a; | ||
uint256 b; | ||
assembly { | ||
a := mload(add(inputs, 36)) | ||
b := mload(add(inputs, 68)) | ||
} | ||
uint256 res = sum(a, b); | ||
_ret = new bytes(32); | ||
assembly { | ||
mstore(add(_ret, 32), res) | ||
} | ||
return (true, _ret); | ||
} | ||
} | ||
} |
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,20 @@ | ||
from brownie import Contract, accounts, Wei, chain, TestableVM | ||
from weiroll import WeirollContract, WeirollPlanner | ||
|
||
|
||
def test_vm_with_math(weiroll_vm_with_math): | ||
weiroll_vm = weiroll_vm_with_math | ||
whale = accounts.at("0x57757E3D981446D585Af0D9Ae4d7DF6D64647806", force=True) | ||
|
||
planner = WeirollPlanner(weiroll_vm) | ||
sum = planner.call(weiroll_vm, "sum", 1, 2) | ||
sum_2 = planner.call(weiroll_vm, "sum", sum, 3) | ||
sum_2 = planner.call(weiroll_vm, "sum", 3, sum_2) | ||
|
||
cmds, state = planner.plan() | ||
weiroll_tx = weiroll_vm.execute( | ||
cmds, state, {"from": whale, "gas_limit": 8_000_000, "gas_price": 0} | ||
) | ||
|
||
print(weiroll_tx.return_value) | ||
#assert False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
were these changes to the VM incorporated into ape-roll?