-
Notifications
You must be signed in to change notification settings - Fork 26
/
NodeProvider.sol
118 lines (103 loc) · 3.59 KB
/
NodeProvider.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: Apache-2.0
/// @title NodeProvider
pragma solidity ^0.8.13;
import {CartesiDApp} from "@cartesi/rollups/contracts/dapp/CartesiDApp.sol";
import {ICartesiDApp} from "@cartesi/rollups/contracts/dapp/ICartesiDApp.sol";
import {ICartesiDAppFactory} from "@cartesi/rollups/contracts/dapp/ICartesiDAppFactory.sol";
import {IConsensus} from "@cartesi/rollups/contracts/consensus/IConsensus.sol";
import {ENS} from "@ensdomains/ens-contracts/contracts/registry/ENS.sol";
import {ADDR_REVERSE_NODE} from "@ensdomains/ens-contracts/contracts/reverseRegistrar/ReverseRegistrar.sol";
import {IReverseRegistrar} from "@ensdomains/ens-contracts/contracts/reverseRegistrar/IReverseRegistrar.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IMachineProtocol} from "../protocol/IMachineProtocol.sol";
import {INodeProvider} from "./INodeProvider.sol";
import {IReaderNodeProvider} from "./INodeProvider.sol";
import {IValidatorNodeProvider} from "./INodeProvider.sol";
import {Vault} from "../payment/Vault.sol";
abstract contract NodeProvider is INodeProvider, Vault {
ENS public immutable ens;
constructor(
address _owner,
ENS _ens,
IERC20 _token,
address _payee,
uint256 _price
) Vault(_owner, _token, _payee, _price) {
ens = _ens;
}
function setName(string calldata name) external override onlyOwner {
IReverseRegistrar ensReverseRegistrar = IReverseRegistrar(
ens.owner(ADDR_REVERSE_NODE)
);
// call the ENS reverse registrar to register name of address
ensReverseRegistrar.setName(name);
}
}
contract ReaderNodeProvider is
NodeProvider,
IReaderNodeProvider,
IMachineProtocol
{
constructor(
address _owner,
ENS _ens,
IERC20 _token,
address _payee,
uint256 _price
) NodeProvider(_owner, _ens, _token, _payee, _price) {}
function register(
ICartesiDApp application,
string calldata location
) external {
emit MachineLocation(address(application), location);
}
}
contract ValidatorNodeProvider is
NodeProvider,
IMachineProtocol,
IValidatorNodeProvider
{
ICartesiDAppFactory public immutable factory;
IConsensus public immutable consensus;
constructor(
address _owner,
ENS _ens,
IERC20 _token,
address _payee,
uint256 _price,
ICartesiDAppFactory _factory,
IConsensus _consensus
) NodeProvider(_owner, _ens, _token, _payee, _price) {
factory = _factory;
consensus = _consensus;
}
function register(
ICartesiDApp application,
string calldata location
) external {
require(
application.getConsensus() == consensus,
"ValidatorNodeProvider: wrong consensus"
);
emit MachineLocation(address(application), location);
}
/// @dev This function deploys a new application, and registers it with the payment system
function deploy(
address _owner,
bytes32 _templateHash,
string calldata _location,
uint256 _initialRunway
) external override whenNotPaused returns (CartesiDApp) {
// delegate call to factory
CartesiDApp application = factory.newApplication(
consensus,
_owner,
_templateHash
);
// emit event with machine location
emit MachineLocation(address(application), _location);
// set initial runway
_extendRunway(msg.sender, application, _initialRunway);
return application;
}
}