Skip to content

Commit

Permalink
caching system : re-calculated domain separator by comparing the curr…
Browse files Browse the repository at this point in the history
…ent chain id and earlier chain id
  • Loading branch information
bidyut-arianelabs committed Sep 22, 2022
1 parent b8e56ce commit 29af526
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions contracts/domain/BosonConstants.sol
Expand Up @@ -159,6 +159,8 @@ string constant FEE_PERCENTAGE_INVALID = "Percentage representation must be less
string constant VALUE_ZERO_NOT_ALLOWED = "Value must be greater than 0";

// EIP712Lib
string constant PROTOCOL_NAME = "BosonProtocolDiamond";
string constant PROTOCOL_VERSION = "V1";
bytes32 constant EIP712_DOMAIN_TYPEHASH = keccak256(
bytes("EIP712Domain(string name,string version,address verifyingContract,bytes32 salt)")
);
Expand Down
4 changes: 3 additions & 1 deletion contracts/protocol/facets/ConfigHandlerFacet.sol
Expand Up @@ -66,7 +66,9 @@ contract ConfigHandlerFacet is IBosonConfigHandler, ProtocolBase {

// Initialize protocol meta-transaction config params
ProtocolLib.ProtocolMetaTxInfo storage pmti = protocolMetaTxInfo();
pmti.domainSeparator = EIP712Lib.domainSeparator("BosonProtocolDiamond", "V1");
pmti.domainSeparator = EIP712Lib.domainSeparator(PROTOCOL_NAME, PROTOCOL_VERSION);
pmti.cachedChainId = block.chainid;
pmti.cachedThis = address(this);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions contracts/protocol/libs/EIP712Lib.sol
Expand Up @@ -63,12 +63,19 @@ library EIP712Lib {
}

/**
* @notice Gets the domain separator from storage.
* @notice Gets the domain separator from storage if matches with the chain id and diamond address, else, build new domain separator.
*
* @return the domain separator from storage
* @return the domain separator
*/
function getDomainSeparator() private view returns (bytes32) {
return ProtocolLib.protocolMetaTxInfo().domainSeparator;
address cachedThis = ProtocolLib.protocolMetaTxInfo().cachedThis;
uint256 cachedChainId = ProtocolLib.protocolMetaTxInfo().cachedChainId;

if (address(this) == cachedThis && block.chainid == cachedChainId) {
return ProtocolLib.protocolMetaTxInfo().domainSeparator;
} else {
return domainSeparator(PROTOCOL_NAME, PROTOCOL_VERSION);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions contracts/protocol/libs/ProtocolLib.sol
Expand Up @@ -200,6 +200,10 @@ library ProtocolLib {
bool isMetaTransaction;
// The domain Separator of the protocol
bytes32 domainSeparator;
// The cached chain id
uint256 cachedChainId;
// The cached address of the diamond
address cachedThis;
// nonce => existance of nonce in the mapping
mapping(uint256 => bool) usedNonce;
// map function name to input type
Expand Down

0 comments on commit 29af526

Please sign in to comment.