generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaddress_selector.hpp
49 lines (44 loc) · 1.53 KB
/
address_selector.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
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "api/full_node/node_api.hpp"
#include "common/outcome.hpp"
#include "primitives/address/address.hpp"
#include "vm/actor/builtin/types/miner/policy.hpp"
namespace fc::mining {
using api::FullNodeApi;
using api::MinerInfo;
using primitives::TokenAmount;
using primitives::address::Address;
/**
* SelectAddress takes the maximal possible transaction fee from configs and
* chooses one of control addresses with minimal balance that is more than
* good funds to make miner work as long as possible. If no suitble control
* address were found, function returns worker address.
*/
inline outcome::result<Address> SelectAddress(
const MinerInfo &miner_info,
const TokenAmount &good_funds,
const std::shared_ptr<FullNodeApi>
&api) { // TODO update from lotus (Markuuu-s)
TokenAmount finder_balance;
auto finder = miner_info.control.end();
for (auto address = miner_info.control.begin();
address != miner_info.control.end();
++address) {
OUTCOME_TRY(address_balance, api->WalletBalance(*address));
if (address_balance >= good_funds
&& (finder == miner_info.control.end()
|| finder_balance > address_balance)) {
finder = address;
finder_balance = address_balance;
}
}
if (finder == miner_info.control.end()) {
return miner_info.worker;
}
return *finder;
}
} // namespace fc::mining