Skip to content

Commit 34dbd8b

Browse files
authored
Api related (#130)
Signed-off-by: turuslan <[email protected]>
1 parent 95e0e04 commit 34dbd8b

39 files changed

+655
-86
lines changed

cmake/Hunter/config.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ hunter_config(
3030
)
3131

3232
hunter_config(libp2p
33-
URL https://github.com/soramitsu/libp2p/archive/06a70dfcdc2649264f44c460a542e0c774db2290.zip
34-
SHA1 c6bbd0e55659fb0a7a84091a97d219f59ae37d14
33+
URL https://github.com/soramitsu/libp2p/archive/73211b62f42a8fab134d640c9e6711622f2a3f59.zip
34+
SHA1 355aa7b858169ec20ecf5ba72cf89a8a613e0907
3535
CMAKE_ARGS TESTING=OFF
3636
)

cmake/dependencies.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ find_package(libp2p CONFIG REQUIRED)
4545
# https://docs.hunter.sh/en/latest/packages/pkg/cppcodec.html
4646
hunter_add_package(cppcodec)
4747
find_package(cppcodec CONFIG REQUIRED)
48+
49+
# http://rapidjson.org
50+
hunter_add_package(RapidJSON)
51+
find_package(RapidJSON CONFIG REQUIRED)

core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
add_subdirectory(adt)
7+
add_subdirectory(api)
78
add_subdirectory(blockchain)
89
add_subdirectory(clock)
910
add_subdirectory(codec)

core/api/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(api
7+
make.cpp
8+
)
9+
target_link_libraries(api
10+
address
11+
chain_store
12+
cid
13+
)
14+
15+
add_library(rpc
16+
rpc/json_errors.cpp
17+
)
18+
target_link_libraries(rpc
19+
api
20+
)

core/api/api.hpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_API_API_HPP
7+
#define CPP_FILECOIN_CORE_API_API_HPP
8+
9+
#include "crypto/randomness/randomness_types.hpp"
10+
#include "primitives/block/block.hpp"
11+
#include "primitives/cid/comm_cid.hpp"
12+
#include "primitives/rle_bitset/rle_bitset.hpp"
13+
#include "primitives/ticket/epost_ticket.hpp"
14+
#include "primitives/ticket/ticket.hpp"
15+
#include "primitives/tipset/tipset.hpp"
16+
#include "vm/actor/builtin/market/actor.hpp"
17+
#include "vm/actor/builtin/payment_channel/payment_channel_actor_state.hpp"
18+
#include "vm/runtime/runtime_types.hpp"
19+
20+
#define API_METHOD(_name, _result, ...) \
21+
struct _##_name : std::function<outcome::result<_result>(__VA_ARGS__)> { \
22+
using Result = _result; \
23+
using Params = ParamsTuple<__VA_ARGS__>; \
24+
static constexpr auto name = "Filecoin." #_name; \
25+
} _name;
26+
27+
namespace fc::api {
28+
using common::Buffer;
29+
using common::Comm;
30+
using crypto::randomness::Randomness;
31+
using crypto::signature::Signature;
32+
using primitives::ChainEpoch;
33+
using primitives::DealId;
34+
using primitives::RleBitset;
35+
using primitives::SectorNumber;
36+
using primitives::SectorSize;
37+
using primitives::StoragePower;
38+
using primitives::TipsetWeight;
39+
using primitives::TokenAmount;
40+
using primitives::address::Address;
41+
using primitives::block::Block;
42+
using primitives::ticket::EPostProof;
43+
using primitives::ticket::Ticket;
44+
using primitives::tipset::HeadChange;
45+
using primitives::tipset::Tipset;
46+
using primitives::tipset::TipsetKey;
47+
using vm::actor::Actor;
48+
using vm::actor::builtin::market::OnChainDeal;
49+
using vm::actor::builtin::market::StorageParticipantBalance;
50+
using vm::actor::builtin::payment_channel::SignedVoucher;
51+
using vm::message::SignedMessage;
52+
using vm::message::UnsignedMessage;
53+
using vm::runtime::ExecutionResult;
54+
using vm::runtime::MessageReceipt;
55+
56+
template <typename... T>
57+
using ParamsTuple =
58+
std::tuple<std::remove_const_t<std::remove_reference_t<T>>...>;
59+
60+
template <typename T>
61+
struct Chan {
62+
uint64_t id;
63+
};
64+
65+
struct InvocResult {
66+
UnsignedMessage message;
67+
MessageReceipt receipt;
68+
std::vector<ExecutionResult> internal_executions;
69+
std::string error;
70+
};
71+
72+
using OnChainDealMap = std::map<std::string, OnChainDeal>;
73+
74+
struct MinerPower {
75+
StoragePower miner;
76+
StoragePower total;
77+
};
78+
79+
struct ChainSectorInfo {
80+
SectorNumber sector;
81+
Comm comm_d;
82+
Comm comm_r;
83+
};
84+
85+
struct MsgWait {
86+
MessageReceipt receipt;
87+
Tipset tipset;
88+
};
89+
90+
struct Api {
91+
API_METHOD(ChainGetRandomness, Randomness, const TipsetKey &, int64_t)
92+
API_METHOD(ChainHead, Tipset)
93+
API_METHOD(ChainNotify, Chan<HeadChange>)
94+
API_METHOD(ChainReadObj, Buffer, CID)
95+
API_METHOD(ChainTipSetWight, TipsetWeight, const TipsetKey &)
96+
97+
API_METHOD(MarketEnsureAvailable, void, const Address &, TokenAmount)
98+
99+
API_METHOD(MinerCreateBlock,
100+
Block,
101+
const Address &,
102+
const TipsetKey &,
103+
const Ticket &,
104+
const EPostProof &,
105+
const std::vector<SignedMessage> &,
106+
uint64_t,
107+
uint64_t)
108+
109+
API_METHOD(MpoolPending, std::vector<SignedMessage>, const TipsetKey &)
110+
API_METHOD(MpoolPushMessage, SignedMessage, const UnsignedMessage &)
111+
112+
API_METHOD(PaychVoucherAdd,
113+
TokenAmount,
114+
const Address &,
115+
const SignedVoucher &,
116+
const Buffer &,
117+
TokenAmount)
118+
119+
API_METHOD(StateCall,
120+
InvocResult,
121+
const UnsignedMessage &,
122+
const TipsetKey &)
123+
API_METHOD(StateGetActor, Actor, const Address &, const TipsetKey &)
124+
API_METHOD(StateMarketBalance,
125+
StorageParticipantBalance,
126+
const Address &,
127+
const TipsetKey &)
128+
API_METHOD(StateMarketDeals, OnChainDealMap, const TipsetKey &)
129+
API_METHOD(StateMarketStorageDeal, OnChainDeal, DealId, const TipsetKey &)
130+
API_METHOD(StateMinerElectionPeriodStart,
131+
ChainEpoch,
132+
const Address &,
133+
const TipsetKey &)
134+
API_METHOD(StateMinerFaults, RleBitset, const Address &, const TipsetKey &)
135+
API_METHOD(StateMinerPower, MinerPower, const Address &, const TipsetKey &)
136+
API_METHOD(StateMinerProvingSet,
137+
std::vector<ChainSectorInfo>,
138+
const Address &,
139+
const TipsetKey &)
140+
API_METHOD(StateMinerSectorSize,
141+
SectorSize,
142+
const Address &,
143+
const TipsetKey &)
144+
API_METHOD(StateMinerWorker, Address, const Address &, const TipsetKey &)
145+
API_METHOD(StateWaitMsg, MsgWait, const CID &)
146+
147+
API_METHOD(SyncSubmitBlock, void, const Block &)
148+
149+
API_METHOD(WalletSign, Signature, const Address &, const Buffer &)
150+
};
151+
} // namespace fc::api
152+
153+
#endif // CPP_FILECOIN_CORE_API_API_HPP

core/api/make.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "api/make.hpp"
7+
8+
#include "vm/actor/builtin/market/actor.hpp"
9+
#include "vm/actor/builtin/miner/types.hpp"
10+
#include "vm/state/impl/state_tree_impl.hpp"
11+
12+
namespace fc::api {
13+
using vm::actor::kStorageMarketAddress;
14+
using vm::actor::builtin::miner::MinerActorState;
15+
using vm::state::StateTreeImpl;
16+
17+
Api makeImpl(std::shared_ptr<ChainStore> chain_store,
18+
std::shared_ptr<WeightCalculator> weight_calculator,
19+
std::shared_ptr<IpfsDatastore> ipld,
20+
std::shared_ptr<KeyStore> key_store) {
21+
auto chain_randomness = chain_store->createRandomnessProvider();
22+
auto minerState = [&](auto &tipset_key,
23+
auto &address) -> outcome::result<MinerActorState> {
24+
OUTCOME_TRY(tipset, chain_store->loadTipset(tipset_key));
25+
OUTCOME_TRY(
26+
actor, StateTreeImpl{ipld, tipset.getParentStateRoot()}.get(address));
27+
return ipld->getCbor<MinerActorState>(actor.head);
28+
};
29+
return {
30+
.ChainGetRandomness = {[&](auto tipset_key, auto round) {
31+
return chain_randomness->sampleRandomness(tipset_key.cids, round);
32+
}},
33+
.ChainHead = {[&]() { return chain_store->heaviestTipset(); }},
34+
// TODO(turuslan): FIL-165 implement method
35+
.ChainNotify = {},
36+
// TODO(turuslan): FIL-165 implement method
37+
.ChainReadObj = {},
38+
.ChainTipSetWight = {[&](auto tipset_key)
39+
-> outcome::result<TipsetWeight> {
40+
OUTCOME_TRY(tipset, chain_store->loadTipset(tipset_key));
41+
return weight_calculator->calculateWeight(tipset);
42+
}},
43+
// TODO(turuslan): FIL-165 implement method
44+
.MarketEnsureAvailable = {},
45+
// TODO(turuslan): FIL-165 implement method
46+
.MinerCreateBlock = {},
47+
// TODO(turuslan): FIL-165 implement method
48+
.MpoolPending = {},
49+
// TODO(turuslan): FIL-165 implement method
50+
.MpoolPushMessage = {},
51+
// TODO(turuslan): FIL-165 implement method
52+
.PaychVoucherAdd = {},
53+
// TODO(turuslan): FIL-165 implement method
54+
.StateCall = {},
55+
// TODO(turuslan): FIL-165 implement method
56+
.StateGetActor = {},
57+
// TODO(turuslan): FIL-165 implement method
58+
.StateMarketBalance = {},
59+
// TODO(turuslan): FIL-165 implement method
60+
.StateMarketDeals = {},
61+
// TODO(turuslan): FIL-165 implement method
62+
.StateMarketStorageDeal = {},
63+
.StateMinerElectionPeriodStart = {[&](auto address, auto tipset_key)
64+
-> outcome::result<ChainEpoch> {
65+
OUTCOME_TRY(state, minerState(tipset_key, address));
66+
return state.post_state.proving_period_start;
67+
}},
68+
.StateMinerFaults = {[&](auto address, auto tipset_key)
69+
-> outcome::result<RleBitset> {
70+
OUTCOME_TRY(state, minerState(tipset_key, address));
71+
return state.fault_set;
72+
}},
73+
// TODO(turuslan): FIL-165 implement method
74+
.StateMinerPower = {},
75+
// TODO(turuslan): FIL-165 implement method
76+
.StateMinerProvingSet = {},
77+
.StateMinerSectorSize = {[&](auto address, auto tipset_key)
78+
-> outcome::result<SectorSize> {
79+
OUTCOME_TRY(state, minerState(tipset_key, address));
80+
return state.info.sector_size;
81+
}},
82+
.StateMinerWorker = {[&](auto address,
83+
auto tipset_key) -> outcome::result<Address> {
84+
OUTCOME_TRY(state, minerState(tipset_key, address));
85+
return state.info.worker;
86+
}},
87+
// TODO(turuslan): FIL-165 implement method
88+
.StateWaitMsg = {},
89+
// TODO(turuslan): FIL-165 implement method
90+
.SyncSubmitBlock = {},
91+
.WalletSign = {[&](auto address, auto data) {
92+
return key_store->sign(address, data);
93+
}},
94+
};
95+
}
96+
} // namespace fc::api

core/api/make.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_API_MAKE_HPP
7+
#define CPP_FILECOIN_CORE_API_MAKE_HPP
8+
9+
#include "api/api.hpp"
10+
#include "blockchain/weight_calculator.hpp"
11+
#include "storage/chain/chain_store.hpp"
12+
#include "storage/ipfs/datastore.hpp"
13+
#include "storage/keystore/keystore.hpp"
14+
15+
namespace fc::api {
16+
using blockchain::weight::WeightCalculator;
17+
using storage::blockchain::ChainStore;
18+
using storage::ipfs::IpfsDatastore;
19+
using storage::keystore::KeyStore;
20+
21+
Api makeImpl(std::shared_ptr<ChainStore> chain_store,
22+
std::shared_ptr<WeightCalculator> weight_calculator,
23+
std::shared_ptr<IpfsDatastore> ipld,
24+
std::shared_ptr<KeyStore> key_store);
25+
} // namespace fc::api
26+
27+
#endif // CPP_FILECOIN_CORE_API_MAKE_HPP

0 commit comments

Comments
 (0)