Skip to content

Commit

Permalink
refactor RiskId from encoded bytes32 to incremental uint64 (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Dec 23, 2024
1 parent 4f4b737 commit 1fbc2c6
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 79 deletions.
3 changes: 1 addition & 2 deletions contracts/examples/crop/CropProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ contract CropProduct is
if (seasonEndAt < TimestampLib.current()) { revert ErrorInvalidSeasonEndAt(seasonEndAt); }

// create risk, if new
bytes32 riskKey = keccak256(abi.encode(id));
CropRisk memory cropRisk = CropRisk({
seasonId: seasonId,
locationId: locationId,
Expand All @@ -205,7 +204,7 @@ contract CropProduct is
payoutDefined: false
});

riskId = _createRisk(riskKey, abi.encode(cropRisk));
riskId = _createRisk(abi.encode(cropRisk));
_riskId[id] = riskId;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/fire/FireProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ contract FireProduct is
return _riskMapping[cityName];
}
_cities.push(cityName);
risk = _createRisk(bytes32(abi.encodePacked(cityName)), "");
risk = _createRisk("");
_riskMapping[cityName] = risk;
}

Expand Down
50 changes: 11 additions & 39 deletions contracts/examples/flight/FlightLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ library FlightLib {


function getFlightRisk(
InstanceReader reader,
NftId productNftId,
Str flightData,
Timestamp departureTime,
string memory departureTimeLocal,
Expand All @@ -254,28 +252,20 @@ library FlightLib {
public
view
returns (
RiskId riskId,
bool exists,
FlightProduct.FlightRisk memory flightRisk
)
{
riskId = getRiskId(productNftId, flightData);
(exists, flightRisk) = getFlightRisk(reader, productNftId, riskId, false);

// create new risk if not existing
if (!exists) {
flightRisk = FlightProduct.FlightRisk({
flightData: flightData,
departureTime: departureTime,
departureTimeLocal: departureTimeLocal,
arrivalTime: arrivalTime,
arrivalTimeLocal: arrivalTimeLocal,
sumOfSumInsuredAmounts: AmountLib.toAmount(0),
status: bytes1(0),
delayMinutes: 0,
payoutOption: uint8(0),
statusUpdatedAt: TimestampLib.zero()});
}
flightRisk = FlightProduct.FlightRisk({
flightData: flightData,
departureTime: departureTime,
departureTimeLocal: departureTimeLocal,
arrivalTime: arrivalTime,
arrivalTimeLocal: arrivalTimeLocal,
sumOfSumInsuredAmounts: AmountLib.toAmount(0),
status: bytes1(0),
delayMinutes: 0,
payoutOption: uint8(0),
statusUpdatedAt: TimestampLib.zero()});
}


Expand Down Expand Up @@ -306,20 +296,6 @@ library FlightLib {
}
}


function getRiskId(
NftId productNftId,
Str flightData
)
public
view
returns (RiskId riskId)
{
bytes32 riskKey = getRiskKey(flightData);
riskId = getRiskId(productNftId, riskKey);
}


function getRiskKey(
Str flightData
)
Expand All @@ -330,8 +306,4 @@ library FlightLib {
return keccak256(abi.encode(flightData));
}


function getRiskId(NftId productNftId, bytes32 riskKey) internal view returns (RiskId riskId) {
return RiskIdLib.toRiskId(productNftId, riskKey);
}
}
13 changes: 7 additions & 6 deletions contracts/examples/flight/FlightProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ contract FlightProduct is
bool internal _testMode;

mapping(RiskId riskId => RequestId requestId) internal _requests;
mapping(bytes32 encodedRisk => RiskId) internal _risks;

// GIF V3 specifics
NftId internal _defaultBundleNftId;
Expand Down Expand Up @@ -507,20 +508,20 @@ contract FlightProduct is
returns (RiskId riskId)
{
bool exists;
FlightRisk memory flightRisk;
(riskId, exists, flightRisk) = FlightLib.getFlightRisk(
_getInstanceReader(),
getNftId(),
FlightRisk memory flightRisk = FlightLib.getFlightRisk(
flightData,
departureTime,
departureTimeLocal,
arrivalTime,
arrivalTimeLocal);

bytes32 riskKey = FlightLib.getRiskKey(flightData);
exists = _risks[riskKey].gtz();

// create risk, if new
if (!exists) {
bytes32 riskKey = FlightLib.getRiskKey(flightData);
_createRisk(riskKey, abi.encode(flightRisk));
riskId = _createRisk(abi.encode(flightRisk));
_risks[riskKey] = riskId;
}

FlightLib.checkClusterRisk(
Expand Down
6 changes: 1 addition & 5 deletions contracts/examples/unpermissioned/SimpleProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,9 @@ contract SimpleProduct is


function createRisk(
string memory id,
bytes memory data
) public returns (RiskId) {
return _createRisk(
bytes32(abi.encodePacked(id)),
data
);
return _createRisk(data);
}

function updateRisk(
Expand Down
3 changes: 2 additions & 1 deletion contracts/instance/ProductStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ contract ProductStore is
}

//--- Risk --------------------------------------------------------------//
function createRisk(RiskId riskId, IRisk.RiskInfo memory info) external restricted() {
function createRisk(IRisk.RiskInfo memory info) external restricted() returns (RiskId riskId) {
riskId = _createNextRiskId(info.productNftId);
Key32 key = riskId.toKey32();
_createMetadata(key);
_risks[key] = info;
Expand Down
12 changes: 7 additions & 5 deletions contracts/instance/base/ObjectCounter.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {Blocknumber, BlocknumberLib} from "../../type/Blocknumber.sol";
import {NftId} from "../../type/NftId.sol";
import {Amount} from "../../type/Amount.sol";
import {RequestId, RequestIdLib} from "../../type/RequestId.sol";
import {RiskId, RiskIdLib} from "../../type/RiskId.sol";

contract ObjectCounter {

// TODO refactor risk id
// mapping(NftId productNftId => uint64 risks) private _riskCounter;

mapping(NftId productNftId => uint64 risks) private _riskCounter;
uint256 private _requestCounter = 0;

function _createNextRequestId() internal returns (RequestId requestId) {
_requestCounter++;
requestId = RequestIdLib.toRequestId(_requestCounter);
}

function _createNextRiskId(NftId productNftId) internal returns (RiskId riskId) {
_riskCounter[productNftId]++;
riskId = RiskIdLib.toRiskId(_riskCounter[productNftId]);
}
}
1 change: 0 additions & 1 deletion contracts/product/IRiskService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface IRiskService is IService {
/// The key of the risk derived from the risk id in comination with the product NftId.
/// Risk data is stored in the instance store.
function createRisk(
bytes32 id,
bytes memory data
) external returns (RiskId riskId);

Expand Down
6 changes: 1 addition & 5 deletions contracts/product/Product.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,13 @@ abstract contract Product is


function _createRisk(
bytes32 id,
bytes memory data
)
internal
virtual
returns (RiskId riskId)
{
return _getProductStorage()._riskService.createRisk(
id,
data
);
return _getProductStorage()._riskService.createRisk(data);
}

function _updateRisk(
Expand Down
7 changes: 1 addition & 6 deletions contracts/product/RiskService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ contract RiskService is

/// @inheritdoc IRiskService
function createRisk(
bytes32 id,
bytes memory data
)
external
Expand All @@ -52,16 +51,12 @@ contract RiskService is
(NftId productNftId, IInstance instance) = _getAndVerifyActiveComponent(PRODUCT());

// effects
riskId = RiskIdLib.toRiskId(productNftId, id);
IRisk.RiskInfo memory riskInfo = IRisk.RiskInfo({
productNftId: productNftId,
createdAt: TimestampLib.current(),
data: data});

instance.getProductStore().createRisk(
riskId,
riskInfo
);
riskId = instance.getProductStore().createRisk(riskInfo);

// add risk to RiskSet
RiskSet riskSet = instance.getRiskSet();
Expand Down
16 changes: 8 additions & 8 deletions contracts/type/RiskId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
pragma solidity ^0.8.20;

import {Key32, KeyId, Key32Lib} from "./Key32.sol";
import {NftId} from "./NftId.sol";
import {RISK} from "./ObjectType.sol";

type RiskId is bytes8;
type RiskId is uint64;

// type bindings
using {
Expand Down Expand Up @@ -33,7 +32,7 @@ function neRiskId(RiskId a, RiskId b) pure returns (bool isDifferent) {

library RiskIdLib {
function zero() public pure returns (RiskId) {
return RiskId.wrap(bytes8(0));
return RiskId.wrap(uint64(0));
}

// @dev Converts a risk id into a uint256.
Expand All @@ -42,8 +41,8 @@ library RiskIdLib {
}

// @dev Converts a risk id string with a product NftId into a risk id.
function toRiskId(NftId productNftId, bytes32 risk) public pure returns (RiskId) {
return RiskId.wrap(bytes8(keccak256(abi.encode(productNftId, risk))));
function toRiskId(uint256 id) public pure returns (RiskId) {
return RiskId.wrap(uint64(id));
}

/// @dev Returns the key32 value for the specified risk id.
Expand All @@ -53,12 +52,13 @@ library RiskIdLib {

/// @dev Returns the key id value for the specified nft id
function toKeyId(RiskId id) public pure returns (KeyId keyId) {
return KeyId.wrap(bytes31(RiskId.unwrap(id)));
return KeyId.wrap(bytes31(uint248(RiskId.unwrap(id))));
}

function toRiskId(KeyId keyId) public pure returns (RiskId riskId) {
riskId = RiskId.wrap(bytes8(KeyId.unwrap(keyId)));
assert(toInt(riskId) < 2**64);
uint248 keyIdInt = uint248(bytes31(KeyId.unwrap(keyId)));
assert(keyIdInt < type(uint64).max);
return RiskId.wrap(uint64(keyIdInt));
}

function eq(RiskId a, RiskId b) public pure returns (bool isSame) {
Expand Down

0 comments on commit 1fbc2c6

Please sign in to comment.