-
Notifications
You must be signed in to change notification settings - Fork 34
/
offchain_worker.hpp
95 lines (70 loc) · 3.14 KB
/
offchain_worker.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/optional.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <libp2p/peer/peer_id.hpp>
#include "common/buffer.hpp"
#include "offchain/types.hpp"
#include "outcome/outcome.hpp"
#include "primitives/common.hpp"
#include "primitives/extrinsic.hpp"
namespace kagome::offchain {
/**
*
* The offchain workers allow the execution of long-running and possibly
* non-deterministic tasks (e.g. web requests, encryption/decryption and
* signing of data, random number generation, CPU-intensive computations,
* enumeration/aggregation of on-chain data, etc.) which could otherwise
* require longer than the block execution time. Offchain workers have their
* own execution environment. This separation of concerns is to make sure that
* the block production is not impacted by the long-running tasks.
*/
class OffchainWorker {
public:
virtual ~OffchainWorker() = default;
virtual void run(std::function<void()> &&func, std::string label) = 0;
// ------------------------- Off-Chain API methods -------------------------
virtual bool isValidator() const = 0;
virtual Result<Success, Failure> submitTransaction(
const primitives::Extrinsic &ext) = 0;
virtual Result<OpaqueNetworkState, Failure> networkState() = 0;
virtual Timestamp timestamp() = 0;
virtual void sleepUntil(Timestamp) = 0;
virtual RandomSeed randomSeed() = 0;
virtual void localStorageSet(StorageType storage_type,
const common::BufferView &key,
common::Buffer value) = 0;
virtual void localStorageClear(StorageType storage_type,
const common::BufferView &key) = 0;
virtual bool localStorageCompareAndSet(
StorageType storage_type,
const common::BufferView &key,
std::optional<common::BufferView> expected,
common::Buffer value) = 0;
virtual outcome::result<common::Buffer> localStorageGet(
StorageType storage_type, const common::BufferView &key) = 0;
virtual Result<RequestId, Failure> httpRequestStart(
HttpMethod method, std::string_view uri, common::Buffer meta) = 0;
virtual Result<Success, Failure> httpRequestAddHeader(
RequestId id, std::string_view name, std::string_view value) = 0;
virtual Result<Success, HttpError> httpRequestWriteBody(
RequestId id,
common::Buffer chunk,
std::optional<Timestamp> deadline) = 0;
virtual std::vector<HttpStatus> httpResponseWait(
const std::vector<RequestId> &ids,
std::optional<Timestamp> deadline) = 0;
virtual std::vector<std::pair<std::string, std::string>>
httpResponseHeaders(RequestId id) = 0;
virtual Result<uint32_t, HttpError> httpResponseReadBody(
RequestId id,
common::Buffer &chunk,
std::optional<Timestamp> deadline) = 0;
virtual void setAuthorizedNodes(std::vector<libp2p::peer::PeerId> nodes,
bool authorized_only) = 0;
};
} // namespace kagome::offchain