概要

OnRampContractコントラクト(ファイル名とコントラクト名が異なってるコントラクトになっています)は、他のブロックチェーン(ソースチェーン)上でFilecoinへのデータ保存要求(オファー)を受け付け、管理するためのスマートコントラクトです。 クライアントはデータとその対価となる報酬をオファーとして提示し、データアグリゲーター(オフチェーンの主体)が複数のオファーを一つにまとめてFilecoinに保存します。 最終的に、外部のオラクル(ブリッジ)を介してFilecoin上でのデータ保存が証明されると、アグリゲーターへの報酬支払いが実行されます。 このコントラクトは、PODSIVerifierコントラクトを継承しており、マークルプルーフを検証する機能を内蔵しています。

このコントラクトは、Filecoinへのデータオンボーディングプロセスの出発点(オンランプ)として機能します。全体のワークフローは3つの主要なステップで構成されます。

  1. オファーの提示 データをFilecoinに保存したいクライアントがofferData関数を呼び出します。 この時、データの識別子(commP)、サイズ、データの取得元(location)、報酬として支払うERC20トークンとその額を指定します。 提出されたオファーはコントラクト内に記録され、一意のIDが割り振られます。
  2. オファーの集約 データアグリゲーター(またはパッカー)と呼ばれるオフチェーンの主体が、保留中の複数のオファーを収集し、それらを一つの大きなデータセット(アグリゲート)にまとめます。 アグリゲーターは、この集約データ全体のcommPを計算し、commitAggregate関数を呼び出します。 このとき、各個別のオファーがこの集約データに確かに含まれていることを証明するため、マークル包含証明(Merkle Inclusion Proof)を提出します。 コントラクトは、継承したPODSIVerifierのverifyロジックを使ってこの証明を検証し、集約をコミットします。
  3. 保存の証明と支払い アグリゲーターが集約データをFilecoinに保存し、ストレージディールが成立すると、その証明情報(アテステーション)がFilecoinチェーン上のコントラクトからブリッジ(オラクル)を経由してこのOnRampContractコントラクトに送られてきます。 proveDataStored関数が、信頼できるオラクルからのみ呼び出され、引数として渡されたアテステーションを検証します。 証明が正当であれば、集約は「証明済み」となり、クライアントから預かっていた報酬トークンが集約時に指定されたアグリゲーターの支払いアドレスに転送されます。

このようにして、OnRampContractはソースチェーン上でデータ保存のマーケットプレイスを形成し、マークルプルーフと外部オラクルを活用して、オフチェーンおよびクロスチェーンでのプロセスを安全に検証・実行します。

機能

  • データ保存オファーの作成、管理、照会。
  • 複数のオファーを一つにまとめた「アグリゲート」のコミット。
  • マークルプルーフを用いた、アグリゲートへのオファーの包含証明検証。
  • 外部オラクルからのFilecoin上でのデータ保存証明の受信。
  • 保存証明完了時の、データアグリゲーターへの報酬(ERC20トークン)の支払い。

フロー

データオンランプのライフサイクル

このシーケンス図は、クライアントがデータ保存をオファーし、アグリゲーターがそれを集約、最終的にオラクルが保存を証明して報酬が支払われるまでの一連の主要なフローを示しています。

sequenceDiagram
    participant Client
    participant OnRampContract
    participant Aggregator as Data Aggregator (Off-chain)
    participant Oracle as Oracle / Bridge

    Client->>OnRampContract: offerData(offer)
    Note over OnRampContract: 新しいオファーを記録し、<br>DataReadyイベントを発行

    Aggregator->>OnRampContract: getPendingOffers()
    Note right of Aggregator: 保留中のオファーを取得

    Note right of Aggregator: オフチェーンで複数のオファーを<br>一つのデータセットに集約し、<br>集約CommPとマークルプルーフを計算

    Aggregator->>OnRampContract: commitAggregate(commP, offerIDs, proofs, payoutAddr)
    Note over OnRampContract: マークルプルーフを検証し、<br>集約をコミット

    Note right of Aggregator: 集約データをFilecoinに保存し、<br>ディールを成立させる

    Oracle->>OnRampContract: proveDataStored(attestation)
    Note over OnRampContract: オラクルからの証明を受信
    Note over OnRampContract: 集約を「証明済み」に更新
    OnRampContract->>Aggregator: (via ERC20.transfer)
    Note over OnRampContract: 報酬トークンを<br>アグリゲーターのpayoutAddrに送金

コード

text
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Cid} from "../Cid.sol";
import {TRUNCATOR} from "../Const.sol";
import {DataAttestation} from "./Oracles.sol";

// Adapted from https://github.com/lighthouse-web3/raas-starter-kit/blob/main/contracts/data-segment/Proof.sol
// adapted rather than imported to
//  1) avoid build issues
//  2) avoid npm deps
//3)  avoid use of deprecated @zondax/filecoin-solidity
contract PODSIVerifier {
    // ProofData is a Merkle proof
    struct ProofData {
        uint64 index;
        bytes32[] path;
    }

    // verify verifies that the given leaf is present in the merkle tree with the given root.
    function verify(
        ProofData memory proof,
        bytes32 root,
        bytes32 leaf
    ) public pure returns (bool) {
        return computeRoot(proof, leaf) == root;
    }

    // computeRoot computes the root of a Merkle tree given a leaf and a Merkle proof.
    function computeRoot(
        ProofData memory d,
        bytes32 subtree
    ) internal pure returns (bytes32) {
        require(
            d.path.length < 64,
            "merkleproofs with depths greater than 63 are not supported"
        );
        require(
            d.index >> d.path.length == 0,
            "index greater than width of the tree"
        );

        bytes32 carry = subtree;
        uint64 index = d.index;
        uint64 right = 0;

        for (uint64 i = 0; i < d.path.length; i++) {
            (right, index) = (index & 1, index >> 1);
            if (right == 1) {
                carry = computeNode(d.path[i], carry);
            } else {
                carry = computeNode(carry, d.path[i]);
            }
        }

        return carry;
    }

    // computeNode computes the parent node of two child nodes
    function computeNode(
        bytes32 left,
        bytes32 right
    ) internal pure returns (bytes32) {
        bytes32 digest = sha256(abi.encodePacked(left, right));
        return truncate(digest);
    }

    // truncate truncates a node to 254 bits.
    function truncate(bytes32 n) internal pure returns (bytes32) {
        // Set the two lowest-order bits of the last byte to 0
        return n & TRUNCATOR;
    }
}

contract OnRampContract is PODSIVerifier {
    enum OfferStatus {
        Pending,
        Aggregated,
        Proven
    }

    struct Offer {
        bytes commP;
        uint64 size;
        string location;
        uint256 amount;
        IERC20 token;
        OfferStatus status;
    }
    // Possible rearrangement:
    // struct Hint {string location, uint64 size} ?
    // struct Payment {uint256 amount, IERC20 token}?

    event DataReady(Offer offer, uint64 id);
    event AggregationCommitted(
        uint64 aggId, 
        bytes commP,
        uint64[] offerIDs, 
        address payoutAddr);
    event ProveDataStored(bytes commP, uint64 dealID);

    uint64 private nextOfferId = 1;
    uint64 private nextAggregateID = 1;
    address public dataProofOracle;
    mapping(uint64 => Offer) public offers;
    mapping(uint64 => uint64[]) public aggregations;
    mapping(uint64 => address) public aggregationPayout;
    mapping(uint64 => bool) public provenAggregations;
    mapping(bytes => uint64) public commPToAggregateID;
    mapping(address => uint64[]) private clientOffers;
    mapping(uint64 => bool) public isOfferAggregated;
    mapping(uint64 => uint64) private offerToAggregationId;
    mapping(uint64 => uint64) public aggregationDealIds;

    function setOracle(address oracle_) external {
        if (dataProofOracle == address(0)) {
            dataProofOracle = oracle_;
        } else {
            revert("Oracle already set");
        }
    }

    function offerData(Offer calldata offer) external payable returns (uint64) {
        // NOTE: This require is commented out for testing purposes.
        // Make sure to uncomment before deploying!

        // require(
        //     offer.token.transferFrom(msg.sender, address(this), offer.amount),
        //     "Payment transfer failed"
        // );

        uint64 id = nextOfferId++;
        Offer memory newOffer = Offer({
            commP: offer.commP,
            size: offer.size,
            location: offer.location,
            amount: offer.amount,
            token: offer.token,
            status: OfferStatus.Pending
        });
        
        offers[id] = newOffer;
        clientOffers[msg.sender].push(id);

        emit DataReady(newOffer, id);
        return id;
    }

    function getClientOffers(address client) external view returns (uint64[] memory) {
        return clientOffers[client];
    }

    function getPendingOffers() external view returns (uint64[] memory) {
        uint64[] memory pending = new uint64[](nextOfferId - 1);
        uint64 count = 0;
        
        for (uint64 i = 1; i < nextOfferId; i++) {
            if (!isOfferAggregated[i]) {
                pending[count] = i;
                count++;
            }
        }
        
        assembly {
            mstore(pending, count)
        }
        return pending;
    }

    function getTotalOffers() external view returns (uint64) {
        return nextOfferId - 1;
    }

    function getOfferDetails(uint64 offerId) external view returns (
        bytes memory commP,
        uint64 size,
        string memory location,
        uint256 amount,
        IERC20 token,
        bool exists,
        OfferStatus status
    ) {
        Offer memory offer = offers[offerId];
        exists = offer.size != 0;
        
        if (exists) {
            return (
                offer.commP,
                offer.size,
                offer.location,
                offer.amount,
                offer.token,
                true,
                offer.status
            );
        }
    }

    function getOfferStatus(uint64 offerId) external view returns (
        bool exists,
        OfferStatus status
    ) {
        Offer memory offer = offers[offerId];
        exists = offer.size != 0;
        
        if (exists) {
            status = offer.status;
        }
    }

    function commitAggregate(
        bytes calldata commP,
        uint64[] calldata claimedIDs,
        ProofData[] calldata inclusionProofs,
        address payoutAddr
    ) external {
        uint64[] memory offerIDs = new uint64[](claimedIDs.length);
        uint64 aggId = nextAggregateID++;
        // Prove all offers are committed by aggregate commP
        for (uint64 i = 0; i < claimedIDs.length; i++) {
            uint64 offerID = claimedIDs[i];
            offerIDs[i] = offerID;
            require(
                verify(
                    inclusionProofs[i],
                    Cid.cidToPieceCommitment(commP),
                    Cid.cidToPieceCommitment(offers[offerID].commP)
                ),
                "Proof verification failed"
            );
            isOfferAggregated[offerID] = true;
            offerToAggregationId[offerID] = aggId;
            
            offers[offerID].status = OfferStatus.Aggregated;
        }
        aggregations[aggId] = offerIDs;
        aggregationPayout[aggId] = payoutAddr;
        commPToAggregateID[commP] = aggId;
        emit AggregationCommitted(aggId, commP, offerIDs, payoutAddr);
    }

    function getAggregationDetails(uint64 aggId) external view returns (
        address payoutAddress,
        bool isProven,
        uint64 offerCount
    ) {
        payoutAddress = aggregationPayout[aggId];
        isProven = provenAggregations[aggId];
        offerCount = uint64(aggregations[aggId].length);
    }

    function getAggregationOffers(uint64 aggId) external view returns (uint64[] memory) {
        return aggregations[aggId];
    }

    function verifyDataStored(
        uint64 aggID,
        uint idx,
        uint64 offerID
    ) external view returns (bool) {
        require(provenAggregations[aggID], "Provided aggregation not proven");
        require(
            aggregations[aggID][idx] == offerID,
            "Aggregation does not include offer"
        );

        return true;
    }

    // Called by oracle to prove the data is stored
    function proveDataStored(DataAttestation calldata attestation) external {
        require(
            msg.sender == dataProofOracle,
            "Only oracle can prove data stored"
        );
        uint64 aggID = commPToAggregateID[attestation.commP];
        require(aggID != 0, "Aggregate not found");
        emit ProveDataStored(attestation.commP, attestation.dealID);

        aggregationDealIds[aggID] = attestation.dealID;

        //transfer payment to the receiver if the payment amount > 0
        for (uint i = 0; i < aggregations[aggID].length; i++) {
            uint64 offerID = aggregations[aggID][i];
            
            offers[offerID].status = OfferStatus.Proven;
            
            if(offers[offerID].amount > 0){
                require(offers[offerID].token.transfer(
                            aggregationPayout[aggID],
                            offers[offerID].amount),
                "Payment transfer failed"
                );
            }
        }
        provenAggregations[aggID] = true;
    }

    function getOfferDealId(uint64 offerId) external view returns (uint64 dealId, bool exists) {
        if (isOfferAggregated[offerId]) {
            uint64 aggId = offerToAggregationId[offerId];
            dealId = aggregationDealIds[aggId];
            exists = dealId != 0;
        }
    }
}

構造体

ProofData

solidity
struct ProofData {
    uint64 index;
    bytes32[] path;
}

マークルツリーにおける特定のリーフの包含を証明するためのデータを保持します。 この構造体はPODSIVerifierベースコントラクトで定義されています。

  • index マークルツリー内でのリーフの位置(インデックス)。
  • path ルートハッシュを再計算するために必要な、兄弟ノードのハッシュ値の配列。

Offer

solidity
struct Offer {
    bytes commP;
    uint64 size;
    string location;
    uint256 amount;
    IERC20 token;
    OfferStatus status;
}

クライアントから提示された個別のデータ保存オファーの詳細を定義します。

  • commP 保存対象データのピースコミットメント。
  • size データのサイズ。
  • location データの取得元を示すURLなどの情報。
  • amount 報酬として支払われるトークンの量。
  • token 報酬として使用されるERC20トークンのコントラクトアドレス。
  • status オファーの現在の状態(Pending, Aggregated, Proven)。

変数

dataProofOracle

solidity
address public dataProofOracle;

Filecoin上でのデータ保存を証明する権限を持つ、信頼されたオラクル(またはブリッジ)コントラクトのアドレスです。 proveDataStored関数はこのアドレスからのみ呼び出すことができます。

offers

solidity
mapping(uint64 => Offer) public offers;

オファーID(uint64)から、そのオファーの詳細を格納したOffer構造体へのマッピングです。

aggregations

solidity
mapping(uint64 => uint64[]) public aggregations;

集約ID(uint64)から、その集約に含まれるオファーIDの配列へのマッピングです。

aggregationPayout

solidity
mapping(uint64 => address) public aggregationPayout;

集約ID(uint64)から、その集約の報酬を受け取るべきデータアグリゲーターの支払いアドレスへのマッピングです。

provenAggregations

solidity
mapping(uint64 => bool) public provenAggregations;

集約ID(uint64)をキーとして、その集約がオラクルによって既に証明されたかどうかを示すフラグ(bool)を格納するマッピングです。

commPToAggregateID

solidity
mapping(bytes => uint64) public commPToAggregateID;

集約データ全体のピースコミットメント(bytes)から、対応する集約ID(uint64)へのマッピングです。 オラクルからの証明を特定の集約に関連付けるために使用されます。

aggregationDealIds

solidity
mapping(uint64 => uint64) public aggregationDealIds;

集約ID(uint64)から、Filecoin上で成立した対応するディールID(uint64)へのマッピングです。 proveDataStored関数で設定されます。

イベント

DataReady

solidity
event DataReady(Offer offer, uint64 id);

offerData関数によって新しいデータ保存オファーが正常に受け付けられた時に発行されるイベント。

AggregationCommitted

solidity
event AggregationCommitted(
    uint64 aggId, 
    bytes commP,
    uint64[] offerIDs, 
    address payoutAddr
);

commitAggregate関数によって新しいオファーの集約が正常にコミットされた時に発行されるイベント。

ProveDataStored

solidity
event ProveDataStored(bytes commP, uint64 dealID);

proveDataStored関数がオラクルによって呼び出され、特定の集約データ(commP)がFilecoin上でディールID(dealID)として保存されたことが証明された時に発行されるイベント。

関数

verify

solidity
function verify(
    ProofData memory proof,
    bytes32 root,
    bytes32 leaf
) public pure returns (bool) {
    return computeRoot(proof, leaf) == root;
}

特定のリーフ(データ)が、与えられたマークルルートを持つマークルツリー内に存在することを検証する関数。

マークル包含証明の正当性を検証するための主要なエントリポイントです。 ProofData構造体(リーフのインデックスと兄弟ノードのパスを含む)、マークルツリーのルートハッシュ、そして検証したいリーフのハッシュを引数として受け取ります。 内部でcomputeRoot関数を呼び出して、提供されたリーフとプルーフからルートハッシュを再計算し、その結果が引数で与えられたrootと一致するかどうかを比較します。 一致すればtrueを、そうでなければfalseを返します。

引数

  • proof
    • 検証に使用するマークルプルーフデータ(ProofData構造体)。
  • root
    • 比較対象となるマークルツリーのルートハッシュ。
  • leaf
    • ツリー内に存在するかを検証したいリーフのハッシュ。

戻り値

  • bool
    • リーフがツリー内に存在すればtrue、そうでなければfalse。

computeRoot

solidity
function computeRoot(
    ProofData memory d,
    bytes32 subtree
) internal pure returns (bytes32) {
    require(
        d.path.length < 64,
        "merkleproofs with depths greater than 63 are not supported"
    );
    require(
        d.index >> d.path.length == 0,
        "index greater than width of the tree"
    );

    bytes32 carry = subtree;
    uint64 index = d.index;
    uint64 right = 0;

    for (uint64 i = 0; i < d.path.length; i++) {
        (right, index) = (index & 1, index >> 1);
        if (right == 1) {
            carry = computeNode(d.path[i], carry);
        } else {
            carry = computeNode(carry, d.path[i]);
        }
    }

    return carry;
}

リーフとマークルプルーフからマークルツリーのルートハッシュを計算する関数。

マークルプルーフの検証ロジックのコアです。 まず、プルーフの深さが63を超えていないこと、リーフのインデックスがツリーの幅を超えていないことをrequire文で検証します。 次に、与えられたリーフ(subtree)を初期値として、プルーフのパス(d.path)を一番下から順に辿ります。 ループの各ステップで、リーフのインデックスの最下位ビットを評価して、現在のノードが左の子か右の子かを判断し、それに応じて兄弟ノード(d.path[i])とのハッシュ計算(computeNode)を正しい順序で行います。 この計算をパスの最上層まで繰り返すことで、最終的にルートハッシュを算出し、その値を返します。

引数

  • d
    • 計算に使用するマークルプルーフデータ(ProofData構造体)。
  • subtree
    • 計算の起点となるリーフのハッシュ。

戻り値

  • bytes32
    • 計算されたマークルツリーのルートハッシュ。

computeNode

solidity
function computeNode(
    bytes32 left,
    bytes32 right
) internal pure returns (bytes32) {
    bytes32 digest = sha256(abi.encodePacked(left, right));
    return truncate(digest);
}

2つの子ノードから親ノードのハッシュを計算する関数。

マークルツリーの基本的なハッシュ計算ステップを実行します。 左の子ノードと右の子ノードのハッシュを引数として受け取り、abi.encodePackedでそれらを連結し、sha256ハッシュアルゴリズムを適用します。 得られたハッシュダイジェストは、truncate関数に渡されてFilecoinの仕様に合わせた切り捨てが行われ、最終的な親ノードのハッシュとして返されます。

引数

  • left
    • 左の子ノードのハッシュ。
  • right
    • 右の子ノードのハッシュ。

戻り値

  • bytes32
    • 計算された親ノードのハッシュ。

truncate

solidity
function truncate(bytes32 n) internal pure returns (bytes32) {
    // Set the two lowest-order bits of the last byte to 0
    return n & TRUNCATOR;
}

ノードのハッシュを254ビットに切り捨てる関数。

Filecoinのピースコミットメント計算の仕様に準拠するため、bytes32(256ビット)のハッシュ値の下位2ビットをゼロに設定します。 これは、定数TRUNCATOR(0xff...3f)とのビット単位のAND演算(&)によって実現されます。 この操作により、ハッシュ値は実質的に254ビットの情報量を持つことになります。

引数

  • n
    • 切り捨て対象のbytes32のハッシュ値。

戻り値

  • bytes32
    • 下位2ビットがゼロに設定されたハッシュ値。

setOracle

solidity
function setOracle(address oracle_) external {
    if (dataProofOracle == address(0)) {
        dataProofOracle = oracle_;
    } else {
        revert("Oracle already set");
    }
}

データ保存証明を行う権限を持つオラクルコントラクトのアドレスを設定する関数。

コントラクトの所有者(または管理者)が、信頼できるオラクルコントラクトを指定するために呼び出します。 dataProofOracle変数がまだ設定されていない(ゼロアドレスである)場合にのみ、引数で渡されたアドレスを設定します。 一度設定されると、再度呼び出してもrevertするため、変更することはできません。 これにより、proveDataStoredを呼び出せる権限を持つアドレスを不変なものとして固定します。

引数

  • oracle_
    • 設定するオラクルコントラクトのアドレス。

offerData

solidity
function offerData(Offer calldata offer) external payable returns (uint64) {
    // NOTE: This require is commented out for testing purposes.
    // Make sure to uncomment before deploying!

    // require(
    //     offer.token.transferFrom(msg.sender, address(this), offer.amount),
    //     "Payment transfer failed"
    // );

    uint64 id = nextOfferId++;
    Offer memory newOffer = Offer({
        commP: offer.commP,
        size: offer.size,
        location: offer.location,
        amount: offer.amount,
        token: offer.token,
        status: OfferStatus.Pending
    });
    
    offers[id] = newOffer;
    clientOffers[msg.sender].push(id);

    emit DataReady(newOffer, id);
    return id;
}

クライアントがFilecoinへのデータ保存オファーを提示する関数。

クライアントがデータ保存を依頼する際の主要なエントリポイントです。 入力として、データのCommP、サイズ、取得元、報酬額、報酬トークンの種類を含むOffer構造体を受け取ります。 (コメントアウトされていますが)本番環境では、まず指定された報酬トークンをtransferFromでクライアントからこのコントラクトへ送金させます。 次に、nextOfferIdを使って新しいオファーに一意のIDを割り当て、カウンターをインクリメントします。 受け取ったオファー情報をoffersマッピングに保存し、クライアントごとのオファーリストclientOffersにもIDを追加します。 最後にDataReadyイベントを発行し、生成されたオファーIDを返します。

引数

  • offer
    • 提示するオファーの詳細を含むOffer構造体。

戻り値

  • uint64
    • 生成された一意のオファーID。

getClientOffers

solidity
function getClientOffers(address client) external view returns (uint64[] memory) {
    return clientOffers[client];
}

特定のクライアントが提示したすべてのオファーIDのリストを取得する関数。

clientアドレスを引数に取り、プライベートなclientOffersマッピングから、そのクライアントが過去に作成したすべてのオファーIDを含む動的配列を返します。

引数

  • client
    • オファーリストを取得したいクライアントのアドレス。

戻り値

  • uint64[] memory
    • 指定されたクライアントが作成したオファーIDの配列。

getPendingOffers

solidity
function getPendingOffers() external view returns (uint64[] memory) {
    uint64[] memory pending = new uint64[](nextOfferId - 1);
    uint64 count = 0;
    
    for (uint64 i = 1; i < nextOfferId; i++) {
        if (!isOfferAggregated[i]) {
            pending[count] = i;
            count++;
        }
    }
    
    assembly {
        mstore(pending, count)
    }
    return pending;
}

まだ集約されていない保留中のすべてのオファーIDのリストを取得する関数。

データアグリゲーターが処理対象のオファーを見つけるために使用します。 nextOfferIdまでの全てのオファーをループ処理し、isOfferAggregatedマッピングを参照して、まだ集約されていない(値がfalseの)オファーのIDのみをpending配列に格納します。 最終的に、インラインアセンブリを使って配列の実際の長さを更新し、その配列を返します。

戻り値

  • uint64[] memory
    • 保留中のオファーIDの配列。

getTotalOffers

solidity
function getTotalOffers() external view returns (uint64) {
    return nextOfferId - 1;
}

これまでに作成されたオファーの総数を取得する関数。

次のオファーIDを保持するnextOfferIdカウンターから1を引くことで、作成されたオファーの総数を返します(IDは1から始まるため)。

戻り値

  • uint64
    • オファーの総数。

getOfferDetails

solidity
function getOfferDetails(uint64 offerId) external view returns (
    bytes memory commP,
    uint64 size,
    string memory location,
    uint256 amount,
    IERC20 token,
    bool exists,
    OfferStatus status
) {
    Offer memory offer = offers[offerId];
    exists = offer.size != 0;
    
    if (exists) {
        return (
            offer.commP,
            offer.size,
            offer.location,
            offer.amount,
            offer.token,
            true,
            offer.status
        );
    }
}

特定のオファーIDに関する詳細な情報をすべて取得する関数。

offerIdを引数に取り、offersマッピングから対応するOffer構造体を取得します。 オファーが存在するかどうか(サイズが0でないか)を判断し、存在する場合はCommP、サイズ、ロケーション、報酬額、トークンアドレス、存在フラグ、ステータスといった全てのフィールドを返します。

引数

  • offerId
    • 詳細を取得したいオファーのID。

戻り値

  • commP, size, location, amount, token, exists, status
    • オファーの詳細情報。

getOfferStatus

solidity
function getOfferStatus(uint64 offerId) external view returns (
    bool exists,
    OfferStatus status
) {
    Offer memory offer = offers[offerId];
    exists = offer.size != 0;
    
    if (exists) {
        status = offer.status;
    }
}

特定のオファーIDの現在のステータスを取得する関数。

getOfferDetailsと同様にoffersマッピングを検索しますが、返すのはオファーの存在を示すbool値と、現在のOfferStatusのみです。

引数

  • offerId
    • ステータスを取得したいオファーのID。

戻り値

  • exists
    • オファーが存在するかどうか。
  • status
    • オファーの現在のステータス。

commitAggregate

solidity
function commitAggregate(
    bytes calldata commP,
    uint64[] calldata claimedIDs,
    ProofData[] calldata inclusionProofs,
    address payoutAddr
) external {
    uint64[] memory offerIDs = new uint64[](claimedIDs.length);
    uint64 aggId = nextAggregateID++;
    // Prove all offers are committed by aggregate commP
    for (uint64 i = 0; i < claimedIDs.length; i++) {
        uint64 offerID = claimedIDs[i];
        offerIDs[i] = offerID;
        require(
            verify(
                inclusionProofs[i],
                Cid.cidToPieceCommitment(commP),
                Cid.cidToPieceCommitment(offers[offerID].commP)
            ),
            "Proof verification failed"
        );
        isOfferAggregated[offerID] = true;
        offerToAggregationId[offerID] = aggId;
        
        offers[offerID].status = OfferStatus.Aggregated;
    }
    aggregations[aggId] = offerIDs;
    aggregationPayout[aggId] = payoutAddr;
    commPToAggregateID[commP] = aggId;
    emit AggregationCommitted(aggId, commP, offerIDs, payoutAddr);
}

データアグリゲーターが、複数のオファーを一つにまとめた「アグリゲート」をコミットする関数。

データアグリゲーターによって呼び出されます。 アグリゲーターは、集約したデータ全体のCommP、それに含まれるオファーのIDリスト(claimedIDs)、各オファーが確かにその集約に含まれていることを示すマークルプルーフのリスト(inclusionProofs)、そして報酬の支払い先アドレス(payoutAddr)を引数として渡します。 関数はループ内で各claimedIDsについて、verify関数(PODSIVerifierから継承)を呼び出してマークルプルーフを検証します。 すべての証明が有効であれば、新しい集約IDを生成し、関連情報をaggregations、aggregationPayout、commPToAggregateIDなどのマッピングに記録します。 また、含まれたオファーのステータスをAggregatedに更新します。 最後にAggregationCommittedイベントを発行します。

引数

  • commP
    • 集約されたデータ全体のピースコミットメント。
  • claimedIDs
    • 集約に含まれるオファーのIDの配列。
  • inclusionProofs
    • 各オファーの包含を証明するマークルプルーフの配列。
  • payoutAddr
    • 報酬の支払い先となるアグリゲーターのアドレス。

getAggregationDetails

solidity
function getAggregationDetails(uint64 aggId) external view returns (
    address payoutAddress,
    bool isProven,
    uint64 offerCount
) {
    payoutAddress = aggregationPayout[aggId];
    isProven = provenAggregations[aggId];
    offerCount = uint64(aggregations[aggId].length);
}

特定の集約IDに関する詳細情報を取得する関数。

aggIdを引数に取り、aggregationPayoutマッピングから報酬支払い先アドレス、provenAggregationsマッピングから証明済みかどうか、そしてaggregationsマッピングから含まれるオファーの数を取得して返します。

引数

  • aggId
    • 詳細を取得したい集約のID。

戻り値

  • payoutAddress, isProven, offerCount
    • 集約の詳細情報。

getAggregationOffers

solidity
function getAggregationOffers(uint64 aggId) external view returns (uint64[] memory) {
    return aggregations[aggId];
}

特定の集約に含まれるすべてのオファーIDのリストを取得する関数。

aggIdを引数に取り、aggregationsマッピングから対応するオファーIDの配列を返します。

引数

  • aggId
    • オファーリストを取得したい集約のID。

戻り値

  • uint64[] memory
    • 集約に含まれるオファーIDの配列。

verifyDataStored

solidity
function verifyDataStored(
    uint64 aggID,
    uint idx,
    uint64 offerID
) external view returns (bool) {
    require(provenAggregations[aggID], "Provided aggregation not proven");
    require(
        aggregations[aggID][idx] == offerID,
        "Aggregation does not include offer"
    );

    return true;
}

特定のオファーが、証明済みの集約に正しく含まれているかを確認する関数。

第三者が特定のオファー(offerID)の保存状態を検証するために使用します。 まず、指定されたaggIDがprovenAggregationsマッピングで証明済みであることを確認します。 次に、aggregationsマッピングを参照し、aggIDの配列のidx番目にofferIDが确实に存在することを確認します。 両方の条件を満たす場合にtrueを返します。

引数

  • aggID
    • 対象となる集約のID。
  • idx
    • aggIDのオファー配列内でのインデックス。
  • offerID
    • 検証したいオファーのID。

戻り値

  • bool
    • オファーが集約に含まれ、かつその集約が証明済みであればtrue。

proveDataStored

solidity
function proveDataStored(DataAttestation calldata attestation) external {
    require(
        msg.sender == dataProofOracle,
        "Only oracle can prove data stored"
    );
    uint64 aggID = commPToAggregateID[attestation.commP];
    require(aggID != 0, "Aggregate not found");
    emit ProveDataStored(attestation.commP, attestation.dealID);

    aggregationDealIds[aggID] = attestation.dealID;

    //transfer payment to the receiver if the payment amount > 0
    for (uint i = 0; i < aggregations[aggID].length; i++) {
        uint64 offerID = aggregations[aggID][i];
        
        offers[offerID].status = OfferStatus.Proven;
        
        if(offers[offerID].amount > 0){
            require(offers[offerID].token.transfer(
                        aggregationPayout[aggID],
                        offers[offerID].amount),
            "Payment transfer failed"
            );
        }
    }
    provenAggregations[aggID] = true;
}

オラクルからFilecoin上でのデータ保存証明を受け取り、報酬の支払いを実行する関数。

このコントラクトのライフサイクルの最終段階を実行します。 まず、require文で呼び出し元がsetOracleで設定された信頼できるオラクルであることを検証します。 次に、アテステーションに含まれるCommPを使ってcommPToAggregateIDマッピングから対応するaggIDを検索します。 aggIDが見つかれば、aggregationDealIdsにFilecoinのディールIDを保存し、ProveDataStoredイベントを発行します。 その後、その集約に含まれるすべてのオファーをループ処理し、ステータスをProvenに更新します。 そして、もしオファーに報酬(amount > 0)が設定されていれば、IERC20.transferを呼び出して、クライアントから預かっていた報酬トークンをaggregationPayoutに記録されているアグリゲーターのアドレスに送金します。 最後に、provenAggregationsマッピングをtrueに設定し、プロセスを完了します。

引数

  • attestation
    • オラクルから渡される、Filecoin上でのディールの詳細を含むDataAttestation構造体。

getOfferDealId

solidity
function getOfferDealId(uint64 offerId) external view returns (uint64 dealId, bool exists) {
    if (isOfferAggregated[offerId]) {
        uint64 aggId = offerToAggregationId[offerId];
        dealId = aggregationDealIds[aggId];
        exists = dealId != 0;
    }
}

特定のオファーIDに対応する、Filecoin上で成立した最終的なディールIDを取得する関数。

クライアントなどが自分のオファーが最終的にどのFilecoinディールになったかを確認するために使用します。 まず、isOfferAggregatedマッピングでオファーが集約済みかを確認します。 集約済みであれば、offerToAggregationIdマッピングで集約IDを取得し、次にaggregationDealIdsマッピングでその集約IDに対応するdealIdを取得します。 ディールIDが見つかったかどうかを示すbool値と共に返します。

引数

  • offerId
    • ディールIDを照会したいオファーのID。

戻り値

  • dealId
    • Filecoin上で成立したディールID。見つからない場合は0。
  • exists
    • ディールIDが見つかったかどうか。