Skip to content

Commit b285318

Browse files
authored
ChainGetPath (#529)
Signed-off-by: turuslan <[email protected]>
1 parent bef6947 commit b285318

File tree

14 files changed

+110
-35
lines changed

14 files changed

+110
-35
lines changed

core/api/full_node/make.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace fc::api {
6868
using primitives::kChainEpochUndefined;
6969
using primitives::block::MsgMeta;
7070
using primitives::sector::getPreferredSealProofTypeFromWindowPoStType;
71+
using primitives::tipset::HeadChangeType;
7172
using primitives::tipset::Tipset;
7273
using storage::ipld::kAllSelector;
7374
using vm::isVMExitCode;
@@ -84,6 +85,7 @@ namespace fc::api {
8485
using vm::actor::builtin::states::RewardActorStatePtr;
8586
using vm::actor::builtin::states::VerifiedRegistryActorStatePtr;
8687
using vm::actor::builtin::types::market::DealState;
88+
using vm::actor::builtin::types::miner::kChainFinality;
8789
using vm::actor::builtin::types::storage_power::kConsensusMinerMinPower;
8890
using vm::interpreter::InterpreterCache;
8991
using vm::runtime::Env;
@@ -293,6 +295,27 @@ namespace fc::api {
293295
OUTCOME_TRY(cbor, ipld->get(cid));
294296
return UnsignedMessage::decode(cbor);
295297
};
298+
api->ChainGetPath = [=](const TipsetKey &from_key, const TipsetKey &to_key)
299+
-> outcome::result<std::vector<HeadChange>> {
300+
std::vector<HeadChange> revert;
301+
std::vector<HeadChange> apply;
302+
OUTCOME_TRY(from, ts_load->load(from_key));
303+
OUTCOME_TRY(to, ts_load->load(to_key));
304+
while (from->key != to->key) {
305+
if (revert.size() > kChainFinality || apply.size() > kChainFinality) {
306+
return ERROR_TEXT("ChainGetPath finality limit");
307+
}
308+
if (from->height() > to->height()) {
309+
revert.emplace_back(HeadChange{HeadChangeType::REVERT, from});
310+
OUTCOME_TRYA(from, ts_load->load(from->getParents()));
311+
} else {
312+
apply.emplace_back(HeadChange{HeadChangeType::APPLY, to});
313+
OUTCOME_TRYA(to, ts_load->load(to->getParents()));
314+
}
315+
}
316+
revert.insert(revert.end(), apply.rbegin(), apply.rend());
317+
return std::move(revert);
318+
};
296319
api->ChainGetParentMessages =
297320
[=](auto &block_cid) -> outcome::result<std::vector<CidMessage>> {
298321
std::vector<CidMessage> messages;

core/api/full_node/node_api.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ namespace fc::api {
315315
jwt::kReadPermission,
316316
UnsignedMessage,
317317
const CID &)
318+
API_METHOD(ChainGetPath,
319+
jwt::kReadPermission,
320+
std::vector<HeadChange>,
321+
const TipsetKey &,
322+
const TipsetKey &)
318323
API_METHOD(ChainGetParentMessages,
319324
jwt::kReadPermission,
320325
std::vector<CidMessage>,
@@ -813,6 +818,7 @@ namespace fc::api {
813818
f(a.ChainGetGenesis);
814819
f(a.ChainGetMessage);
815820
f(a.ChainGetNode);
821+
f(a.ChainGetPath);
816822
f(a.ChainGetParentMessages);
817823
f(a.ChainGetParentReceipts);
818824
f(a.ChainGetRandomnessFromBeacon);

core/vm/actor/cgo/actors.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ namespace fc::vm::actor::cgo {
5555
cgoCall<cgoActorsConfigParams>(arg);
5656
}
5757

58+
void logLevel(LogLevel level) {
59+
CborEncodeStream arg;
60+
arg << level;
61+
cgoCall<cgoActorsSetLogLevel>(arg);
62+
}
63+
5864
constexpr auto kFatal{VMExitCode::kFatal};
5965
constexpr auto kOk{VMExitCode::kOk};
6066

@@ -394,4 +400,9 @@ namespace fc::vm::actor::cgo {
394400
ret << VMExitCode::kErrIllegalState;
395401
}
396402
}
403+
404+
RUNTIME_METHOD(gocRtLog) {
405+
spdlog::info("cgoActorsInvoke log: {}", arg.get<std::string>());
406+
ret << kOk;
407+
}
397408
} // namespace fc::vm::actor::cgo

core/vm/actor/cgo/actors.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ namespace fc::vm::actor::cgo {
1818
*/
1919
void configParams();
2020

21+
enum class LogLevel {
22+
kDebug = -1,
23+
kInfo,
24+
kWarn,
25+
kError,
26+
};
27+
void logLevel(LogLevel level);
28+
2129
outcome::result<Bytes> invoke(const CID &code,
2230
const std::shared_ptr<Runtime> &runtime);
2331
} // namespace fc::vm::actor::cgo

core/vm/actor/cgo/c_actors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Raw gocRtStateGet(Raw);
3333
Raw gocRtStateCommit(Raw);
3434
Raw gocRtDeleteActor(Raw);
3535
Raw gocRtCirc(Raw);
36+
Raw gocRtLog(Raw);
3637

3738
#ifdef __cplusplus
3839
}

core/vm/actor/cgo/cgo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ func (out *cborOut) bytes(b []byte) *cborOut {
157157
return out
158158
}
159159

160+
func (out *cborOut) str(s string) *cborOut {
161+
e := typegen.CborWriteHeader(out.w, typegen.MajTextString, uint64(len(s)))
162+
cgoAsserte(e)
163+
_, e = out.w.Write([]byte(s))
164+
cgoAsserte(e)
165+
return out
166+
}
167+
160168
func (out *cborOut) int(i int64) *cborOut {
161169
t := typegen.MajUnsignedInt
162170
if i < 0 {

core/vm/actor/cgo/go_actors.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,21 @@ func (rt *rt) ChargeGas(_ string, gas int64, _ int64) {
275275
rt.gocRet(C.gocRtCharge(rt.gocArg().int(gas).arg()))
276276
}
277277

278-
func (rt *rt) Log(rtt.LogLevel, string, ...interface{}) {
278+
var log_level rtt.LogLevel = rtt.WARN
279+
var log_levels = []string{"DEBUG", "INFO", "WARN", "ERROR"}
280+
281+
func (rt *rt) Log(level rtt.LogLevel, format string, args ...interface{}) {
282+
if level < rtt.DEBUG {
283+
return
284+
}
285+
if level > rtt.ERROR {
286+
level = rtt.ERROR
287+
}
288+
if level < log_level {
289+
return
290+
}
291+
msg := fmt.Sprintf("%s %s", log_levels[level+1], fmt.Sprintf(format, args...))
292+
rt.gocRet(C.gocRtLog(rt.gocArg().str(msg).arg()))
279293
}
280294

281295
var _ rt1.StateHandle = &rt{}
@@ -844,6 +858,13 @@ func cgoActorsConfigParams(raw C.Raw) C.Raw {
844858
return cgoRet(nil)
845859
}
846860

861+
//export cgoActorsSetLogLevel
862+
func cgoActorsSetLogLevel(raw C.Raw) C.Raw {
863+
arg := cgoArgCbor(raw)
864+
log_level = rtt.LogLevel(arg.int())
865+
return cgoRet(nil)
866+
}
867+
847868
func init() {
848869
for k, v := range _actors {
849870
actors[k] = export(v)

test/core/sector_storage/zerocomm/zerocomm_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace fc::sector_storage::zerocomm {
2929
params.expected)
3030
}
3131

32-
INSTANTIATE_TEST_CASE_P(
32+
INSTANTIATE_TEST_SUITE_P(
3333
ZerocommTestCases,
3434
ZerocommTest,
3535
::testing::Values(Params{1016,

test/core/storage/mpool/message_pool_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace fc::storage::mpool {
134134
EXPECT_FALSE(testMpoolSelectRevert(fix, ticket_quality).empty());
135135
}
136136

137-
INSTANTIATE_TEST_CASE_P(MpoolSelectQualityTest,
138-
MpoolSelectQualityTest,
139-
::testing::Values(0.8, 0.9));
137+
INSTANTIATE_TEST_SUITE_P(MpoolSelectQualityTest,
138+
MpoolSelectQualityTest,
139+
::testing::Values(0.8, 0.9));
140140
} // namespace fc::storage::mpool

test/core/storage/unixfs/unixfs_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TEST_P(UnixfsTest, MatchGo) {
2727
EXPECT_EQ(unwrapped.str(), data);
2828
}
2929

30-
INSTANTIATE_TEST_CASE_P(
30+
INSTANTIATE_TEST_SUITE_P(
3131
UnixfsTestCases,
3232
UnixfsTest,
3333
::testing::Values(

0 commit comments

Comments
 (0)