-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore transient slot values after calling script (#217)
We now cache the old transient slot values (e.g. for `activeNonce`, `callback`, etc) in order to restore them after calling the Quark script. This also adds a new test to test batched submission for a contract with callbacks to check its behavior. --------- Co-authored-by: kevincheng96 <[email protected]>
- Loading branch information
1 parent
b00f3f6
commit 95da9ec
Showing
6 changed files
with
187 additions
and
20 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
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,44 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.27; | ||
|
||
import "quark-core/src/QuarkScript.sol"; | ||
import "quark-core/src/QuarkWallet.sol"; | ||
|
||
contract BatchSend { | ||
function submitTwo( | ||
QuarkWallet wallet1, | ||
QuarkWallet.QuarkOperation memory op1, | ||
uint8 v1, | ||
bytes32 r1, | ||
bytes32 s1, | ||
QuarkWallet wallet2, | ||
QuarkWallet.QuarkOperation memory op2, | ||
uint8 v2, | ||
bytes32 r2, | ||
bytes32 s2 | ||
) public returns (uint256) { | ||
wallet1.executeQuarkOperation(op1, v1, r1, s1); | ||
wallet2.executeQuarkOperation(op2, v2, r2, s2); | ||
return IncrementByCallback(address(wallet1)).number(); | ||
} | ||
} | ||
|
||
contract IncrementByCallback is QuarkScript { | ||
uint256 public number; | ||
|
||
function run() public { | ||
allowCallback(); | ||
IncrementByCallback(address(this)).increment(); | ||
IncrementByCallback(address(this)).increment(); | ||
} | ||
|
||
function increment() external { | ||
number++; | ||
} | ||
} | ||
|
||
contract CallIncrement is QuarkScript { | ||
function run(address wallet) public { | ||
IncrementByCallback(wallet).increment(); | ||
} | ||
} |
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,96 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
pragma solidity 0.8.27; | ||
|
||
import "forge-std/Test.sol"; | ||
import "forge-std/console.sol"; | ||
|
||
import {YulHelper} from "test/lib/YulHelper.sol"; | ||
import {SignatureHelper} from "test/lib/SignatureHelper.sol"; | ||
import {QuarkOperationHelper, ScriptType} from "test/lib/QuarkOperationHelper.sol"; | ||
|
||
import {CodeJar} from "codejar/src/CodeJar.sol"; | ||
|
||
import {QuarkScript} from "quark-core/src/QuarkScript.sol"; | ||
import {QuarkNonceManager} from "quark-core/src/QuarkNonceManager.sol"; | ||
import {QuarkWallet, QuarkWalletMetadata} from "quark-core/src/QuarkWallet.sol"; | ||
import {QuarkWalletStandalone} from "quark-core/src/QuarkWalletStandalone.sol"; | ||
import {IHasSignerExecutor} from "quark-core/src/interfaces/IHasSignerExecutor.sol"; | ||
|
||
import {QuarkMinimalProxy} from "quark-proxy/src/QuarkMinimalProxy.sol"; | ||
|
||
import {BatchSend} from "test/lib/BatchCallback.sol"; | ||
|
||
contract BatchCallbackTest is Test { | ||
enum ExecutionType { | ||
Signature, | ||
Direct | ||
} | ||
|
||
CodeJar public codeJar; | ||
QuarkNonceManager public nonceManager; | ||
QuarkWallet public walletImplementation; | ||
|
||
uint256 alicePrivateKey = 0x8675309; | ||
address aliceAccount = vm.addr(alicePrivateKey); | ||
QuarkWallet aliceWallet; // see constructor() | ||
|
||
uint256 bobPrivateKey = 0x8675309; | ||
address bobAccount = vm.addr(bobPrivateKey); | ||
QuarkWallet bobWallet; // see constructor() | ||
|
||
bytes32 constant EXHAUSTED_TOKEN = bytes32(type(uint256).max); | ||
|
||
// wallet proxy instantiation helper | ||
function newWallet(address signer, address executor) internal returns (QuarkWallet) { | ||
return QuarkWallet(payable(new QuarkMinimalProxy(address(walletImplementation), signer, executor))); | ||
} | ||
|
||
constructor() { | ||
codeJar = new CodeJar(); | ||
console.log("CodeJar deployed to: %s", address(codeJar)); | ||
|
||
nonceManager = new QuarkNonceManager(); | ||
console.log("QuarkNonceManager deployed to: %s", address(nonceManager)); | ||
|
||
walletImplementation = new QuarkWallet(codeJar, nonceManager); | ||
console.log("QuarkWallet implementation: %s", address(walletImplementation)); | ||
|
||
aliceWallet = newWallet(aliceAccount, address(0)); | ||
console.log("Alice signer: %s", aliceAccount); | ||
console.log("Alice wallet at: %s", address(aliceWallet)); | ||
|
||
bobWallet = newWallet(bobAccount, address(0)); | ||
console.log("Bob signer: %s", bobAccount); | ||
console.log("Bob wallet at: %s", address(bobWallet)); | ||
} | ||
|
||
/** | ||
* get active nonce, submission token, replay count *************************** | ||
* | ||
* single | ||
*/ | ||
function testBatchCallWithCallback() public { | ||
// gas: do not meter set-up | ||
vm.pauseGasMetering(); | ||
BatchSend batchSend = new BatchSend(); | ||
bytes memory incrementByCallbackScript = new YulHelper().getCode("BatchCallback.sol/IncrementByCallback.json"); | ||
QuarkWallet.QuarkOperation memory op1 = new QuarkOperationHelper().newBasicOpWithCalldata( | ||
aliceWallet, incrementByCallbackScript, abi.encodeWithSignature("run()"), ScriptType.ScriptSource | ||
); | ||
(uint8 v1, bytes32 r1, bytes32 s1) = new SignatureHelper().signOp(alicePrivateKey, aliceWallet, op1); | ||
|
||
bytes memory callIncrementScript = new YulHelper().getCode("BatchCallback.sol/CallIncrement.json"); | ||
QuarkWallet.QuarkOperation memory op2 = new QuarkOperationHelper().newBasicOpWithCalldata( | ||
bobWallet, | ||
callIncrementScript, | ||
abi.encodeWithSignature("run(address)", address(aliceWallet)), | ||
ScriptType.ScriptSource | ||
); | ||
(uint8 v2, bytes32 r2, bytes32 s2) = new SignatureHelper().signOp(bobPrivateKey, bobWallet, op2); | ||
|
||
// gas: meter execute | ||
vm.resumeGasMetering(); | ||
vm.expectRevert(abi.encodeWithSelector(QuarkWallet.NoActiveCallback.selector)); | ||
batchSend.submitTwo(aliceWallet, op1, v1, r1, s1, bobWallet, op2, v2, r2, s2); | ||
} | ||
} |
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