Skip to content

Commit

Permalink
Merge branch 'main' into sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed May 12, 2023
2 parents 2aec9e5 + 23b782f commit 8582df9
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 360 deletions.
68 changes: 36 additions & 32 deletions packages/evm/contracts/AllowanceTracker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,32 @@ abstract contract AllowanceTracker is Core {
*/
function _flushPrepare(Consumption[] memory consumptions) internal {
uint256 count = consumptions.length;
unchecked {
for (uint256 i; i < count; ++i) {
Consumption memory consumption = consumptions[i];

bytes32 key = consumption.allowanceKey;
uint128 consumed = consumption.consumed;
for (uint256 i; i < count; ) {
Consumption memory consumption = consumptions[i];

// Retrieve the allowance and calculate its current updated balance
// and next refill timestamp.
Allowance memory allowance = allowances[key];
(uint128 balance, uint64 refillTimestamp) = _accruedAllowance(
allowance,
block.timestamp
);
bytes32 key = consumption.allowanceKey;
uint128 consumed = consumption.consumed;

// Retrieve the allowance and calculate its current updated balance
// and next refill timestamp.
Allowance storage allowance = allowances[key];
(uint128 balance, uint64 refillTimestamp) = _accruedAllowance(
allowance,
block.timestamp
);

assert(balance == consumption.balance);
assert(consumed <= balance);
// Flush
allowance.balance = balance - consumed;
allowance.refillTimestamp = refillTimestamp;

assert(balance == consumption.balance);
assert(consumed <= balance);
// Flush
allowances[key].balance = balance - consumed;
allowances[key].refillTimestamp = refillTimestamp;
// Emit an event to signal the total consumed amount.
emit ConsumeAllowance(key, consumed, balance - consumed);

// Emit an event to signal the total consumed amount.
emit ConsumeAllowance(key, consumed, balance - consumed);
unchecked {
++i;
}
}
}
Expand All @@ -93,19 +96,20 @@ abstract contract AllowanceTracker is Core {
bool success
) internal {
uint256 count = consumptions.length;
unchecked {
for (uint256 i; i < count; ++i) {
Consumption memory consumption = consumptions[i];
bytes32 key = consumption.allowanceKey;
if (success) {
emit ConsumeAllowance(
key,
consumption.consumed,
consumption.balance - consumption.consumed
);
} else {
allowances[key].balance = consumption.balance;
}
for (uint256 i; i < count; ) {
Consumption memory consumption = consumptions[i];
bytes32 key = consumption.allowanceKey;
if (success) {
emit ConsumeAllowance(
key,
consumption.consumed,
consumption.balance - consumption.consumed
);
} else {
allowances[key].balance = consumption.balance;
}
unchecked {
++i;
}
}
}
Expand Down
65 changes: 39 additions & 26 deletions packages/evm/contracts/Consumptions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ library Consumptions {
Consumption[] memory consumptions
) internal pure returns (Consumption[] memory result) {
uint256 length = consumptions.length;
unchecked {
result = new Consumption[](length);
for (uint256 i; i < length; ++i) {
result[i].allowanceKey = consumptions[i].allowanceKey;
result[i].balance = consumptions[i].balance;
result[i].consumed = consumptions[i].consumed;

result = new Consumption[](length);
for (uint256 i; i < length; ) {
result[i].allowanceKey = consumptions[i].allowanceKey;
result[i].balance = consumptions[i].balance;
result[i].consumed = consumptions[i].consumed;

unchecked {
++i;
}
}
}
Expand All @@ -28,11 +31,14 @@ library Consumptions {
bytes32 key
) internal pure returns (uint256, bool) {
uint256 length = consumptions.length;
unchecked {
for (uint256 i; i < length; ++i) {
if (consumptions[i].allowanceKey == key) {
return (i, true);
}

for (uint256 i; i < length; ) {
if (consumptions[i].allowanceKey == key) {
return (i, true);
}

unchecked {
++i;
}
}

Expand All @@ -49,23 +55,30 @@ library Consumptions {
result = new Consumption[](c1.length + c2.length);

uint256 length = c1.length;
unchecked {
for (uint256 i; i < length; ++i) {
result[i].allowanceKey = c1[i].allowanceKey;
result[i].balance = c1[i].balance;
result[i].consumed = c1[i].consumed;

for (uint256 i; i < length; ) {
result[i].allowanceKey = c1[i].allowanceKey;
result[i].balance = c1[i].balance;
result[i].consumed = c1[i].consumed;

unchecked {
++i;
}
}

for (uint256 i; i < c2.length; ) {
(uint256 index, bool found) = find(c1, c2[i].allowanceKey);
if (found) {
result[index].consumed += c2[i].consumed;
} else {
result[length].allowanceKey = c2[i].allowanceKey;
result[length].balance = c2[i].balance;
result[length].consumed = c2[i].consumed;
length++;
}

for (uint256 i; i < c2.length; ++i) {
(uint256 index, bool found) = find(c1, c2[i].allowanceKey);
if (found) {
result[index].consumed += c2[i].consumed;
} else {
result[length].allowanceKey = c2[i].allowanceKey;
result[length].balance = c2[i].balance;
result[length].consumed = c2[i].consumed;
length++;
}
unchecked {
++i;
}
}

Expand Down
28 changes: 15 additions & 13 deletions packages/evm/contracts/Decoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,23 @@ library Decoder {
bool isInline;
if (template) isInline = Topology.isInline(node.children[0]);

unchecked {
uint256 offset;
for (uint256 i; i < length; ++i) {
if (!template) isInline = Topology.isInline(node.children[i]);
uint256 offset;
for (uint256 i; i < length; ) {
if (!template) isInline = Topology.isInline(node.children[i]);

_walk(
data,
_locationInBlock(data, location, offset, isInline),
node.children[template ? 0 : i],
result.children[i]
);
_walk(
data,
_locationInBlock(data, location, offset, isInline),
node.children[template ? 0 : i],
result.children[i]
);

uint256 childSize = result.children[i].size;
result.size += isInline ? childSize : childSize + 32;
offset += isInline ? childSize : 32;

uint256 childSize = result.children[i].size;
result.size += isInline ? childSize : childSize + 32;
offset += isInline ? childSize : 32;
unchecked {
++i;
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions packages/evm/contracts/Integrity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ library Integrity {

error UnsuitableCompValue(uint256 index);

error UnsupportedOperator(uint256 index);

error UnsuitableParent(uint256 index);

error UnsuitableChildCount(uint256 index);
Expand Down Expand Up @@ -131,19 +133,18 @@ library Integrity {
if (compValue.length != 32) {
revert UnsuitableCompValue(index);
}
} else {
require(
operator == Operator.EtherWithinAllowance ||
operator == Operator.CallWithinAllowance,
"Placeholder Operators are not supported"
);

} else if (
operator == Operator.EtherWithinAllowance ||
operator == Operator.CallWithinAllowance
) {
if (paramType != ParameterType.None) {
revert UnsuitableParameterType(index);
}
if (compValue.length != 32) {
revert UnsuitableCompValue(index);
}
} else {
revert UnsupportedOperator(index);
}
}

Expand Down Expand Up @@ -230,9 +231,10 @@ library Integrity {
for (uint256 i = 0; i < conditions.length; i++) {
ConditionFlat memory condition = conditions[i];
if (
(condition.operator >= Operator.And &&
((condition.operator >= Operator.And &&
condition.operator <= Operator.Nor) ||
condition.paramType == ParameterType.Array
condition.paramType == ParameterType.Array) &&
childrenBounds[i].length > 1
) {
compatiblechildTypeTree(conditions, i, childrenBounds);
}
Expand All @@ -254,8 +256,6 @@ library Integrity {
uint256 index,
Topology.Bounds[] memory childrenBounds
) private pure {
assert(childrenBounds[index].length > 0);

uint256 start = childrenBounds[index].start;
uint256 end = childrenBounds[index].end;

Expand Down
27 changes: 14 additions & 13 deletions packages/evm/contracts/PermissionBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ abstract contract PermissionBuilder is Core {
address targetAddress,
ExecutionOptions options
) external onlyOwner {
roles[roleKey].targets[targetAddress] = TargetAddress(
Clearance.Target,
options
);
roles[roleKey].targets[targetAddress] = TargetAddress({
clearance: Clearance.Target,
options: options
});
emit AllowTarget(roleKey, targetAddress, options);
}

Expand All @@ -74,10 +74,10 @@ abstract contract PermissionBuilder is Core {
bytes32 roleKey,
address targetAddress
) external onlyOwner {
roles[roleKey].targets[targetAddress] = TargetAddress(
Clearance.None,
ExecutionOptions.None
);
roles[roleKey].targets[targetAddress] = TargetAddress({
clearance: Clearance.None,
options: ExecutionOptions.None
});
emit RevokeTarget(roleKey, targetAddress);
}

Expand All @@ -88,10 +88,10 @@ abstract contract PermissionBuilder is Core {
bytes32 roleKey,
address targetAddress
) external onlyOwner {
roles[roleKey].targets[targetAddress] = TargetAddress(
Clearance.Function,
ExecutionOptions.None
);
roles[roleKey].targets[targetAddress] = TargetAddress({
clearance: Clearance.Function,
options: ExecutionOptions.None
});
emit ScopeTarget(roleKey, targetAddress);
}

Expand Down Expand Up @@ -125,10 +125,11 @@ abstract contract PermissionBuilder is Core {
emit RevokeFunction(roleKey, targetAddress, selector);
}

/// @dev Defines the values that can be called for a given function for each param.
/// @dev Sets conditions to enforce on calls to the specified target.
/// @param roleKey identifier of the role to be modified.
/// @param targetAddress Destination address of transaction.
/// @param selector 4 byte function selector.
/// @param conditions The conditions to enforce.
/// @param options designates if a transaction can send ether and/or delegatecall to target.
function scopeFunction(
bytes32 roleKey,
Expand Down
Loading

0 comments on commit 8582df9

Please sign in to comment.