Skip to content

Commit

Permalink
feat: 🎸 Handling of the addCanceled function in the Storages
Browse files Browse the repository at this point in the history
✅ Closes: #218
  • Loading branch information
siriusyim committed Mar 13, 2024
1 parent 02d02c9 commit 273ed31
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 21 deletions.
32 changes: 19 additions & 13 deletions src/v0.8/core/carstore/Carstore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,7 @@ contract Carstore is Initializable, UUPSUpgradeable, CarstoreBase {
uint64 _id,
uint64 _matchingId,
bool _matchingState
)
external
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
onlyCarExist(this, _id)
onlyNotZero(_matchingId)
onlyCarReplicaExist(this, _id, _matchingId)
{
) external onlyRole(roles, RolesType.DATASWAP_CONTRACT) {
if (_matchingState) {
_emitRepicaEvent(
_id,
Expand All @@ -186,6 +180,24 @@ contract Carstore is Initializable, UUPSUpgradeable, CarstoreBase {
);
}
}
/// @dev Reports a failure in car replica storage.
/// @param _id The ID associated with the car replica.
/// @param _matchingId The ID of the matching process related to the storage failure.
function __reportCarReplicaStorageFailed(
uint64 _id,
uint64 _matchingId
)
external
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
onlyCarReplicaState(
this,
_id,
_matchingId,
CarReplicaType.State.Matched
)
{
_emitRepicaEvent(_id, _matchingId, CarReplicaType.Event.StorageFailed);
}

function _checkCarReplicaDealState(
uint64 _id,
Expand All @@ -211,9 +223,6 @@ contract Carstore is Initializable, UUPSUpgradeable, CarstoreBase {
)
external
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
onlyCarExist(this, _id)
onlyNotZero(_matchingId)
onlyCarReplicaExist(this, _id, _matchingId)
onlyCarReplicaState(this, _id, _matchingId, CarReplicaType.State.Stored)
{
_checkCarReplicaDealState(
Expand All @@ -240,9 +249,6 @@ contract Carstore is Initializable, UUPSUpgradeable, CarstoreBase {
)
external
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
onlyCarExist(this, _id)
onlyNotZero(_matchingId)
onlyCarReplicaExist(this, _id, _matchingId)
onlyCarReplicaState(this, _id, _matchingId, CarReplicaType.State.Stored)
{
_checkCarReplicaDealState(
Expand Down
25 changes: 20 additions & 5 deletions src/v0.8/core/finance/escrow/EscrowDataTradingFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ contract EscrowDataTradingFee is EscrowBase {
_datasetId
);

uint256 amount = __getRequirement(_datasetId,_destMatchingId,payer, _token);
uint256 amount = __getRequirement(
_datasetId,
_destMatchingId,
payer,
_token
);

FinanceType.PayeeInfo[] memory payees = new FinanceType.PayeeInfo[](1);
payees[0] = FinanceType.PayeeInfo(payer, amount);
Expand All @@ -194,13 +199,21 @@ contract EscrowDataTradingFee is EscrowBase {
uint64 _matchingId,
address _payer,
address _token
) public view override onlyRole(roles, RolesType.DATASWAP_CONTRACT) returns (uint256 amount) {
)
public
view
override
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
returns (uint256 amount)
{
if (_payer == roles.matchingsBids().getMatchingWinner(_matchingId)) {
amount = roles.matchingsBids().getMatchingBidAmount(
_matchingId,
_payer
);
} else if (_payer == roles.datasets().getDatasetMetadataSubmitter(_datasetId)) {
} else if (
_payer == roles.datasets().getDatasetMetadataSubmitter(_datasetId)
) {
// Source account balance
(, , uint256 balance, ) = roles.finance().getAccountEscrow(
_datasetId,
Expand All @@ -222,7 +235,9 @@ contract EscrowDataTradingFee is EscrowBase {
_datasetId,
DatasetType.DataType.Source
) *
roles.datasetsRequirement().getDatasetReplicasCount(_datasetId) -
roles.datasetsRequirement().getDatasetReplicasCount(
_datasetId
) -
usedSize;

amount = (balance / unusedSize) * matchingSize;
Expand Down Expand Up @@ -342,7 +357,7 @@ contract EscrowDataTradingFee is EscrowBase {
function _isEscrowRefund(uint64 _matchingId) internal view returns (bool) {
return
_matchingId != 0 &&
roles.storages().isStorageExpiration(_matchingId);
roles.storages().isStorageCompleted(_matchingId);
}

/// @dev Internal function to check if a parent account refund is applicable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ contract EscrowDatacapChunkLandCollateral is EscrowBase {
uint64 _matchingId,
address _payer,
address _token
) public view override onlyRole(roles, RolesType.DATASWAP_CONTRACT) returns (uint256 amount) {
)
public
view
override
onlyRole(roles, RolesType.DATASWAP_CONTRACT)
returns (uint256 amount)
{
(, , uint256 current, ) = roles.finance().getAccountEscrow(
_datasetId,
_matchingId,
Expand Down Expand Up @@ -141,7 +147,7 @@ contract EscrowDatacapChunkLandCollateral is EscrowBase {
uint64 _matchingId
) internal view override returns (bool refund) {
return ((_matchingId != 0 &&
roles.storages().isStorageExpiration(_matchingId)) ||
roles.storages().isStorageCompleted(_matchingId)) ||
roles.datasets().getDatasetState(_datasetId) ==
DatasetType.State.Rejected);
}
Expand All @@ -154,6 +160,6 @@ contract EscrowDatacapChunkLandCollateral is EscrowBase {
uint64 _matchingId
) internal view override returns (bool burn) {
return (_matchingId != 0 &&
roles.storages().isStorageExpiration(_matchingId));
roles.storages().isStorageCompleted(_matchingId));
}
}
8 changes: 8 additions & 0 deletions src/v0.8/interfaces/core/ICarstore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ interface ICarstore is ICarstoreReadOnly {
bool _matchingState
) external;

/// @dev Reports a failure in car replica storage.
/// @param _id The ID associated with the car replica.
/// @param _matchingId The ID of the matching process related to the storage failure.
function __reportCarReplicaStorageFailed(
uint64 _id,
uint64 _matchingId
) external;

/// @notice Report that storage deal for a replica has expired.
/// @dev This function allows reporting that the storage deal for a replica has expired.
/// @param _id Car ID associated with the replica.
Expand Down
12 changes: 12 additions & 0 deletions src/v0.8/interfaces/module/IStorages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ interface IStorages is IStorageStatistics {
uint64[] memory _claimIds
) external;

/// @dev Completes the storage process for a given matching ID.
/// @param _matchingId The ID of the matching.
/// @param _ids An array of content identifiers of the matched data.
function completeStorage(uint64 _matchingId, uint64[] memory _ids) external;

/// @dev Gets the list of done cars in the matchedstore.
/// @param _matchingId The ID of the matching.
/// @return An array of content identifiers of the done cars.
Expand All @@ -59,6 +64,13 @@ interface IStorages is IStorageStatistics {
uint64 _matchingId
) external view returns (bool);

/// @dev Checks if the storage process is completed for a given matching ID.
/// @param _matchingId The ID of the matching.
/// @return A boolean indicating whether the storage process is completed or not.
function isStorageCompleted(
uint64 _matchingId
) external view returns (bool);

/// @dev Requests the allocation of matched datacap for a matching process.
/// @param _matchingId The ID of the matching process.
function requestAllocateDatacap(
Expand Down
64 changes: 64 additions & 0 deletions src/v0.8/module/storage/Storages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,62 @@ contract Storages is
);
}

/// @dev Completes the storage process for a given matching ID.
/// @param _matchingId The ID of the matching.
function completeStorage(uint64 _matchingId, uint64[] memory _ids) public {
StorageType.Storage storage storage_ = storages[_matchingId];
(
uint64 datasetId,
uint64[] memory cars,
,
,
,
uint16 replicaIndex
) = roles.matchingsTarget().getMatchingTarget(_matchingId);

if (storage_.doneCars.length < cars.length) {
require(isStorageExpiration(_matchingId), "Storage is in progress");
require(
storage_.doneCars.length + _ids.length == cars.length,
"invalid cars number"
);

for (uint256 i = 0; i < _ids.length; i++) {
require(
CarReplicaType.State.Matched ==
roles.carstore().getCarReplicaState(
_ids[i],
_matchingId
),
"Invalid Replica State"
);
roles.carstore().__reportCarReplicaStorageFailed(
_ids[i],
_matchingId
);
uint64 carSize = roles.carstore().getCarSize(_ids[i]);
_addCanceled(datasetId, replicaIndex, _matchingId, carSize);
}
}

storage_.completed = true;

// Payment data trading fee
roles.finance().claimEscrow(
datasetId,
_matchingId,
FinanceType.FIL,
FinanceType.Type.EscrowDatacapChunkLandCollateral
);

roles.finance().claimEscrow(
datasetId,
_matchingId,
FinanceType.FIL,
FinanceType.Type.EscrowDataTradingFee
);
}

/// @dev Gets the list of done cars in the matchedstore.
function getStoredCars(
uint64 _matchingId
Expand Down Expand Up @@ -213,6 +269,14 @@ contract Storages is
}
}

/// @dev Checks if the storage process is completed for a given matching ID.
/// @param _matchingId The ID of the matching.
/// @return A boolean indicating whether the storage process is completed or not.
function isStorageCompleted(uint64 _matchingId) public view returns (bool) {
StorageType.Storage storage storage_ = storages[_matchingId];
return storage_.completed;
}

/// @dev Internal function to allocate matched datacap.
// solhint-disable-next-line
function _allocateDatacap(
Expand Down
1 change: 1 addition & 0 deletions src/v0.8/types/StorageType.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ library StorageType {
/// @notice Struct representing a storage deal.
struct Storage {
uint64[] doneCars;
bool completed;
}
}

0 comments on commit 273ed31

Please sign in to comment.