/
Base.sol
92 lines (80 loc) · 4.4 KB
/
Base.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
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.4;
import {Coordinator} from "../Coordinator.sol";
/// @title BaseConsumer
/// @notice Handles receiving container compute responses from Infernet coordinator
/// @dev Contains a single public entrypoint `rawReceiveCompute` callable by only the Infernet coordinator. Once
/// call origin is verified, parameters are proxied to internal function `_receiveCompute`
abstract contract BaseConsumer {
/*//////////////////////////////////////////////////////////////
IMMUTABLE
//////////////////////////////////////////////////////////////*/
/// @notice Infernet Coordinator
/// @dev Internal visibility since COORDINATOR is consumed by inheriting contracts
Coordinator internal immutable COORDINATOR;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
/// @notice Thrown if attempting to call `rawReceiveCompute` from a `msg.sender != address(COORDINATOR)`
/// @dev 4-byte signature: `0x9ec853e6`
error NotCoordinator();
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/// @notice Initialize new BaseConsumer
/// @param coordinator coordinator address
constructor(address coordinator) {
// Setup Coordinator
COORDINATOR = Coordinator(coordinator);
}
/*//////////////////////////////////////////////////////////////
VIRTUAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Callback entrypoint to receive container compute responses from validated Coordinator source
/// @dev Called by `rawReceiveCompute` once validated that `msg.sender == address(COORDINATOR)`
/// @dev Same function parameters as `rawReceiveCompute`
/// @param subscriptionId id of subscription being responded to
/// @param interval subscription interval
/// @param redundancy after this call succeeds, how many nodes will have delivered a response for this interval
/// @param node address of responding Infernet node
/// @param input optional off-chain container input recorded by Infernet node (empty, hashed input, processed input, or both)
/// @param output optional off-chain container output (empty, hashed output, processed output, both, or fallback: all encodeable data)
/// @param proof optional off-chain container execution proof (or arbitrary metadata)
function _receiveCompute(
uint32 subscriptionId,
uint32 interval,
uint16 redundancy,
address node,
bytes calldata input,
bytes calldata output,
bytes calldata proof
) internal virtual {}
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Callback entrypoint called by Infernet Coordinator to return container compute responses
/// @dev Callable only by `address(COORDINATOR)`, else throws `NotCoordinator()` error
/// @param subscriptionId id of subscription being responded to
/// @param interval subscription interval
/// @param redundancy after this call succeeds, how many nodes will have delivered a response for this interval
/// @param node address of responding Infernet node
/// @param input optional off-chain container input recorded by Infernet node (empty, hashed input, processed input, or both)
/// @param output optional off-chain container output (empty, hashed output, processed output, both, or fallback: all encodeable data)
/// @param proof optional off-chain container execution proof (or arbitrary metadata)
function rawReceiveCompute(
uint32 subscriptionId,
uint32 interval,
uint16 redundancy,
address node,
bytes calldata input,
bytes calldata output,
bytes calldata proof
) external {
// Ensure caller is coordinator
if (msg.sender != address(COORDINATOR)) {
revert NotCoordinator();
}
// Call internal receive function, since caller is validated
_receiveCompute(subscriptionId, interval, redundancy, node, input, output, proof);
}
}