From 0d1b657d36810f3ac8b139b70a2ef1f653d9847f Mon Sep 17 00:00:00 2001 From: leekt Date: Tue, 4 Jun 2024 03:46:25 +0900 Subject: [PATCH 1/3] feat: increase nonce on installModule when validator has been installed previously --- src/Kernel.sol | 12 ++++++-- src/sdk/KernelTestBase.sol | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/Kernel.sol b/src/Kernel.sol index d1f3784..5695a45 100644 --- a/src/Kernel.sol +++ b/src/Kernel.sol @@ -340,6 +340,12 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager if (moduleType == MODULE_TYPE_VALIDATOR) { ValidationStorage storage vs = _validationStorage(); ValidationId vId = ValidatorLib.validatorToIdentifier(IValidator(module)); + if (vs.validationConfig[vId].nonce == vs.currentNonce) { + // only increase currentNonce when vId's currentNonce is same + unchecked { + vs.currentNonce++; + } + } ValidationConfig memory config = ValidationConfig({nonce: vs.currentNonce, hook: IHook(address(bytes20(initData[0:20])))}); bytes calldata validatorData; @@ -354,8 +360,10 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager selectorData.length := calldataload(sub(selectorData.offset, 32)) } _installValidation(vId, config, validatorData, hookData); - // NOTE: we don't allow configure on selector data on v3.1, but using bytes instead of bytes4 for selector data to make sure we are future proof - _setSelector(vId, bytes4(selectorData[0:4]), true); + if(selectorData.length == 4) { + // NOTE: we don't allow configure on selector data on v3.1, but using bytes instead of bytes4 for selector data to make sure we are future proof + _setSelector(vId, bytes4(selectorData[0:4]), true); + } } else if (moduleType == MODULE_TYPE_EXECUTOR) { bytes calldata executorData; bytes calldata hookData; diff --git a/src/sdk/KernelTestBase.sol b/src/sdk/KernelTestBase.sol index 2edca6e..53f115d 100644 --- a/src/sdk/KernelTestBase.sol +++ b/src/sdk/KernelTestBase.sol @@ -654,6 +654,67 @@ abstract contract KernelTestBase is Test { WithHook } + function _installValidator(IValidator validator) internal { + vm.deal(address(kernel), 1e18); + PackedUserOperation[] memory ops = new PackedUserOperation[](1); + ops[0] = _prepareUserOp( + VALIDATION_TYPE_ROOT, + false, + false, + abi.encodeWithSelector( + kernel.installModule.selector, + 1, + address(validator), + abi.encodePacked( + address(0), // Hook + abi.encode( + hex"", // validator data + hex"", // hook data + hex"" // selector data + ) + ) + ), + true, + true + ); + entrypoint.handleOps(ops, payable(address(0xdeadbeef))); + } + + function _uninstallValidator(IValidator validator) internal { + vm.deal(address(kernel), 1e18); + PackedUserOperation[] memory ops = new PackedUserOperation[](1); + ops[0] = _prepareUserOp( + VALIDATION_TYPE_ROOT, + false, + false, + abi.encodeWithSelector( + kernel.uninstallModule.selector, + 1, + address(validator), + hex"" + ), + true, + true + ); + entrypoint.handleOps(ops, payable(address(0xdeadbeef))); + } + + function testValidatorInstall() external whenInitialized { + MockValidator mv = new MockValidator(); + _installValidator(mv); + ValidationManager.ValidationConfig memory config = kernel.validationConfig(ValidatorLib.validatorToIdentifier(mv)); + assertEq(config.nonce, 1); + assertEq(address(config.hook), address(1)); + _uninstallValidator(mv); + config = kernel.validationConfig(ValidatorLib.validatorToIdentifier(mv)); + assertEq(config.nonce, 1); + assertEq(address(config.hook), address(0)); + _installValidator(mv); + config = kernel.validationConfig(ValidatorLib.validatorToIdentifier(mv)); + assertEq(config.nonce, 2); + assertEq(address(config.hook), address(1)); + } + function _installAction(HookInfo withHook) internal { vm.deal(address(kernel), 1e18); MockAction mockAction = new MockAction(); From 4e7ee0e454fc03043e94f0709ef1b56b46785e30 Mon Sep 17 00:00:00 2001 From: leekt Date: Tue, 4 Jun 2024 03:58:17 +0900 Subject: [PATCH 2/3] rc3 --- src/Kernel.sol | 9 ++++++++- src/sdk/KernelTestBase.sol | 31 ++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Kernel.sol b/src/Kernel.sol index 5695a45..ee18482 100644 --- a/src/Kernel.sol +++ b/src/Kernel.sol @@ -54,6 +54,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager error InvalidModuleType(); error InvalidCaller(); error InvalidSelector(); + error InitConfigError(uint256 idx); event Received(address sender, uint256 amount); event Upgraded(address indexed implementation); @@ -92,7 +93,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager } } - function initialize(ValidationId _rootValidator, IHook hook, bytes calldata validatorData, bytes calldata hookData) + function initialize(ValidationId _rootValidator, IHook hook, bytes calldata validatorData, bytes calldata hookData, bytes[] calldata initConfig) external { ValidationStorage storage vs = _validationStorage(); @@ -108,6 +109,12 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager ValidationConfig memory config = ValidationConfig({nonce: uint32(1), hook: hook}); vs.currentNonce = 1; _installValidation(_rootValidator, config, validatorData, hookData); + for(uint256 i = 0; i Date: Tue, 4 Jun 2024 03:58:25 +0900 Subject: [PATCH 3/3] fmt --- src/Kernel.sol | 18 +++++++++++------- src/sdk/KernelTestBase.sol | 29 ++++++++--------------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/Kernel.sol b/src/Kernel.sol index ee18482..a3a4e4b 100644 --- a/src/Kernel.sol +++ b/src/Kernel.sol @@ -93,9 +93,13 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager } } - function initialize(ValidationId _rootValidator, IHook hook, bytes calldata validatorData, bytes calldata hookData, bytes[] calldata initConfig) - external - { + function initialize( + ValidationId _rootValidator, + IHook hook, + bytes calldata validatorData, + bytes calldata hookData, + bytes[] calldata initConfig + ) external { ValidationStorage storage vs = _validationStorage(); require(ValidationId.unwrap(vs.rootValidator) == bytes21(0), "already initialized"); if (ValidationId.unwrap(_rootValidator) == bytes21(0)) { @@ -109,9 +113,9 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager ValidationConfig memory config = ValidationConfig({nonce: uint32(1), hook: hook}); vs.currentNonce = 1; _installValidation(_rootValidator, config, validatorData, hookData); - for(uint256 i = 0; i