-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prover.sol
96 lines (82 loc) · 4.68 KB
/
Prover.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { MarketAPI } from "lib/filecoin-solidity/contracts/v0.8/MarketAPI.sol";
import { CommonTypes } from "lib/filecoin-solidity/contracts/v0.8/types/CommonTypes.sol";
import { MarketTypes } from "lib/filecoin-solidity/contracts/v0.8/types/MarketTypes.sol";
import { AccountTypes } from "lib/filecoin-solidity/contracts/v0.8/types/AccountTypes.sol";
import { CommonTypes } from "lib/filecoin-solidity/contracts/v0.8/types/CommonTypes.sol";
import { AccountCBOR } from "lib/filecoin-solidity/contracts/v0.8/cbor/AccountCbor.sol";
import { MarketCBOR } from "lib/filecoin-solidity/contracts/v0.8/cbor/MarketCbor.sol";
import { BytesCBOR } from "lib/filecoin-solidity/contracts/v0.8/cbor/BytesCbor.sol";
import { BigInts } from "lib/filecoin-solidity/contracts/v0.8/utils/BigInts.sol";
import { CBOR } from "solidity-cborutils/contracts/CBOR.sol";
import { Misc } from "lib/filecoin-solidity/contracts/v0.8/utils/Misc.sol";
import { FilAddresses } from "lib/filecoin-solidity/contracts/v0.8/utils/FilAddresses.sol";
import { DataAttestation, IBridgeContract } from "./Oracles.sol";
import {Strings} from "lib/openzeppelin-contracts/contracts/utils/Strings.sol";
using CBOR for CBOR.CBORBuffer;
contract DealClient {
using AccountCBOR for *;
using MarketCBOR for *;
uint64 public constant AUTHENTICATE_MESSAGE_METHOD_NUM = 2643134072;
uint64 public constant DATACAP_RECEIVER_HOOK_METHOD_NUM = 3726118371;
uint64 public constant MARKET_NOTIFY_DEAL_METHOD_NUM = 4186741094;
address public constant MARKET_ACTOR_ETH_ADDRESS = address(0xff00000000000000000000000000000000000005);
address public constant DATACAP_ACTOR_ETH_ADDRESS = address(0xfF00000000000000000000000000000000000007);
enum Status {
None,
DealPublished,
DealActivated,
DealTerminated
}
mapping(bytes => uint64) public pieceDeals; // commP -> deal ID
mapping(bytes => Status) public pieceStatus;
IBridgeContract public bridgeContract;
function setBridgeContract(address _bridgeContract) external {
if (address(bridgeContract) == address(0)) {
bridgeContract = IBridgeContract(_bridgeContract);
} else {
revert("Bridge contract already set");
}
}
// dealNotify is the callback from the market actor into the contract at the end
// of PublishStorageDeals. This message holds the previously approved deal proposal
// and the associated dealID. The dealID is stored as part of the contract state
// and the completion of this call marks the success of PublishStorageDeals
// @params - cbor byte array of MarketDealNotifyParams
function dealNotify(bytes memory params) internal {
require(msg.sender == MARKET_ACTOR_ETH_ADDRESS, "msg.sender needs to be market actor f05");
MarketTypes.MarketDealNotifyParams memory mdnp = MarketCBOR.deserializeMarketDealNotifyParams(params);
MarketTypes.DealProposal memory proposal = MarketCBOR.deserializeDealProposal(mdnp.dealProposal);
pieceDeals[proposal.piece_cid.data] = mdnp.dealId;
pieceStatus[proposal.piece_cid.data] = Status.DealPublished;
int64 duration = CommonTypes.ChainEpoch.unwrap(proposal.end_epoch) - CommonTypes.ChainEpoch.unwrap(proposal.start_epoch);
DataAttestation memory attest = DataAttestation(proposal.piece_cid.data, duration, mdnp.dealId, uint256(Status.DealPublished));
bridgeContract._execute('FIL', addressToHexString(address(this)), abi.encode(attest));
}
// handle_filecoin_method is the universal entry point for any evm based
// actor for a call coming from a builtin filecoin actor
// @method - FRC42 method number for the specific method hook
// @params - CBOR encoded byte array params
function handle_filecoin_method(uint64 method, uint64, bytes memory params) public returns (uint32, uint64, bytes memory) {
bytes memory ret;
uint64 codec;
// dispatch methods
if (method == AUTHENTICATE_MESSAGE_METHOD_NUM) {
// If we haven't reverted, we should return a CBOR true to indicate that verification passed.
// Always authenticate message
CBOR.CBORBuffer memory buf = CBOR.create(1);
buf.writeBool(true);
ret = buf.data();
codec = Misc.CBOR_CODEC;
} else if (method == MARKET_NOTIFY_DEAL_METHOD_NUM) {
dealNotify(params);
} else {
revert('the filecoin method that was called is not handled');
}
return (0, codec, ret);
}
function addressToHexString(address _addr) internal pure returns (string memory) {
return Strings.toHexString(uint256(uint160(_addr)), 20);
}
}