Skip to content

Commit ae8a1de

Browse files
author
Alexander Avramenko
committed
Merge branch 'dapsnet'
2 parents 435a3c6 + a7f4a35 commit ae8a1de

22 files changed

Lines changed: 480 additions & 790 deletions

api/include/apihandler.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,15 @@ public slots:
436436
const std::string& p_exec_ip, const std::string& p_exec_cmdline)
437437
: blockchain_(p_blockchain)
438438
, solver_(solver)
439-
, executorTransport_(new ::apache::thrift::transport::TBufferedTransport(
440-
::apache::thrift::stdcxx::make_shared<::apache::thrift::transport::TSocket>(p_exec_ip, p_exec_port)))
439+
, socket_(::apache::thrift::stdcxx::make_shared<::apache::thrift::transport::TSocket>(p_exec_ip, p_exec_port))
440+
, executorTransport_(new ::apache::thrift::transport::TBufferedTransport(socket_))
441441
, origExecutor_(
442442
std::make_unique<executor::ContractExecutorConcurrentClient>(::apache::thrift::stdcxx::make_shared<apache::thrift::protocol::TBinaryProtocol>(executorTransport_))) {
443443
std::string executorCmdline = p_exec_cmdline;
444444

445+
socket_->setSendTimeout(kSendTimeout);
446+
socket_->setRecvTimeout(kReceiveTimeout);
447+
445448
if (executorCmdline.empty()) {
446449
cswarning() << "Executor command line args are empty, process would not be created";
447450
return;
@@ -561,7 +564,10 @@ public slots:
561564
private:
562565
const BlockChain& blockchain_;
563566
const cs::SolverCore& solver_;
567+
568+
::apache::thrift::stdcxx::shared_ptr<::apache::thrift::transport::TSocket> socket_;
564569
::apache::thrift::stdcxx::shared_ptr<::apache::thrift::transport::TTransport> executorTransport_;
570+
565571
std::unique_ptr<executor::ContractExecutorConcurrentClient> origExecutor_;
566572
std::unique_ptr<cs::Process> executorProcess_;
567573

@@ -577,8 +583,13 @@ public slots:
577583

578584
std::condition_variable cvErrorConnect_;
579585
std::atomic_bool requestStop_{ false };
586+
580587
const int16_t EXECUTOR_VERSION = 2;
581588

589+
// timeout in ms
590+
const int kSendTimeout = 4000;
591+
const int kReceiveTimeout = 4000;
592+
582593
// temporary solution?
583594
std::mutex callExecutorLock_;
584595
};

api/src/apihandler.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,12 @@ api::SealedTransaction APIHandler::convertTransaction(const csdb::Transaction& t
273273
csdb::Currency currency = transaction.currency();
274274
csdb::Address address = transaction.source();
275275
if (address.is_wallet_id()) {
276-
BlockChain::WalletData data_to_fetch_pulic_key;
277-
s_blockchain.findWalletData(transaction.source().wallet_id(), data_to_fetch_pulic_key);
278-
address = csdb::Address::from_public_key(data_to_fetch_pulic_key.address_);
276+
address = s_blockchain.getAddressByType(transaction.source(), BlockChain::AddressType::PublicKey);
279277
}
280278

281279
csdb::Address target = transaction.target();
282280
if (target.is_wallet_id()) {
283-
BlockChain::WalletData data_to_fetch_pulic_key;
284-
s_blockchain.findWalletData(transaction.target().wallet_id(), data_to_fetch_pulic_key);
285-
target = csdb::Address::from_public_key(data_to_fetch_pulic_key.address_);
281+
target = s_blockchain.getAddressByType(transaction.target(), BlockChain::AddressType::PublicKey);
286282
}
287283

288284
result.id = convert_transaction_id(transaction.id());
@@ -1055,7 +1051,23 @@ bool APIHandler::updateSmartCachesTransaction(csdb::Transaction trxn, cs::Sequen
10551051
}
10561052

10571053
HashState res;
1058-
{ // signal to end waiting for a transaction
1054+
res.hash = cs::Zero::hash;
1055+
std::string newStateStr;
1056+
if (trxn.user_field_ids().count(cs::trx_uf::new_state::RetVal) > 0) {
1057+
res.retVal = trxn.user_field(cs::trx_uf::new_state::RetVal).template value<std::string>();
1058+
}
1059+
1060+
general::Variant var;
1061+
if (!res.retVal.empty()) {
1062+
std::string tmp = res.retVal;
1063+
var = deserialize<general::Variant>(std::move(tmp));
1064+
}
1065+
1066+
if (trxn.user_field_ids().count(cs::trx_uf::new_state::Value) > 0) {
1067+
// new_state value, not hash!
1068+
newStateStr = trxn.user_field(cs::trx_uf::new_state::Value).template value<std::string>();
1069+
}
1070+
else { // signal to end waiting for a transaction
10591071
auto hashStateInst(lockedReference(this->hashStateSL));
10601072
(*hashStateInst)[target_pk].updateHash([&](const HashState& oldHash) {
10611073
auto newHashStr = trxn.user_field(cs::trx_uf::new_state::Hash).template value<std::string>();
@@ -1068,16 +1080,18 @@ bool APIHandler::updateSmartCachesTransaction(csdb::Transaction trxn, cs::Sequen
10681080
});
10691081
}
10701082

1071-
if (res.hash != cs::Zero::hash) { // update tokens
1083+
if (!newStateStr.empty() || res.hash != cs::Zero::hash) { // update tokens
10721084
auto caller_pk = s_blockchain.getAddressByType(execTrans.source(), BlockChain::AddressType::PublicKey);
10731085

10741086
if (is_smart_deploy(smart))
10751087
tm.checkNewDeploy(target_pk, caller_pk, smart);
10761088

1077-
// state also will be updated in update_smart_state_slot()
1078-
std::string newState = cs::SmartContracts::get_contract_state(s_blockchain, target_pk);
1079-
if (!newState.empty())
1080-
tm.checkNewState(target_pk, caller_pk, smart, newState);
1089+
if (newStateStr.empty()) {
1090+
newStateStr = cs::SmartContracts::get_contract_state(s_blockchain, target_pk);
1091+
}
1092+
if (!newStateStr.empty()) {
1093+
tm.checkNewState(target_pk, caller_pk, smart, newStateStr);
1094+
}
10811095
}
10821096
}
10831097
}
@@ -1960,9 +1974,9 @@ void APIHandler::TokensListGet(api::TokensListResult& _return, int64_t offset, i
19601974
}
19611975

19621976
//////////Wallets
1963-
typedef std::list<std::pair<const cs::WalletsCache::WalletData::Address*, const cs::WalletsCache::WalletData*>> WCSortedList;
1977+
typedef std::list<std::pair<const cs::PublicKey*, const cs::WalletsCache::WalletData*>> WCSortedList;
19641978
template <typename T>
1965-
void walletStep(const cs::WalletsCache::WalletData::Address* addr, const cs::WalletsCache::WalletData* wd, const uint64_t num,
1979+
void walletStep(const cs::PublicKey* addr, const cs::WalletsCache::WalletData* wd, const uint64_t num,
19661980
std::function<const T&(const cs::WalletsCache::WalletData&)> getter, std::function<bool(const T&, const T&)> comparator, WCSortedList& lst) {
19671981
assert(num > 0);
19681982

@@ -1987,7 +2001,7 @@ void iterateOverWallets(std::function<const T&(const cs::WalletsCache::WalletDat
19872001
using Comparer = std::function<bool(const T&, const T&)>;
19882002
Comparer comparator = desc ? Comparer(std::greater<T>()) : Comparer(std::less<T>());
19892003

1990-
bc.iterateOverWallets([&lst, num, getter, comparator](const cs::WalletsCache::WalletData::Address& addr, const cs::WalletsCache::WalletData& wd) {
2004+
bc.iterateOverWallets([&lst, num, getter, comparator](const cs::PublicKey& addr, const cs::WalletsCache::WalletData& wd) {
19912005
if (!addr.empty() && wd.balance_ >= csdb::Amount(0)) {
19922006
walletStep(&addr, &wd, num, getter, comparator, lst);
19932007
}
@@ -2051,7 +2065,7 @@ void APIHandler::TrustedGet(TrustedGetResult& _return, int32_t _page) {
20512065
uint32_t limit = PER_PAGE;
20522066
uint32_t total = 0;
20532067

2054-
s_blockchain.iterateOverWriters([&_return, &offset, &limit, &total](const cs::WalletsCache::WalletData::Address& addr, const cs::WalletsCache::TrustedData& wd) {
2068+
s_blockchain.iterateOverWriters([&_return, &offset, &limit, &total](const cs::PublicKey& addr, const cs::WalletsCache::TrustedData& wd) {
20552069
if (addr.empty()) {
20562070
return true;
20572071
}

api/src/tokens.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ void TokensMaster::updateTokenChaches(const csdb::Address& addr, const std::stri
252252
[&retBalance](const std::string& balance) { retBalance = balance; });
253253

254254
if (!std::all_of(retBalance.begin(), retBalance.end(), [](char ch) { return (isdigit(ch) || ch == '.'); })) {
255+
csdebug() << "executor returns balance as " << retBalance;
255256
retBalance = "0";
256-
cserror() << "executor return text balance!";
257257
}
258258
return retBalance;
259259
};

csdb/include/csdb/database.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Database {
4747

4848
virtual bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) = 0;
4949
virtual bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) = 0;
50+
virtual bool removeLastFromTrxIndex(const cs::Bytes& key) = 0;
5051
virtual void truncateTransIndex() = 0;
5152

5253
virtual bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) = 0;

csdb/include/csdb/database_berkeleydb.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DatabaseBerkeleyDB : public Database {
4040

4141
bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) override final;
4242
bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) override final;
43+
bool removeLastFromTrxIndex(const cs::Bytes& key) override final;
4344
void truncateTransIndex() override final;
4445

4546
bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) override;

csdb/include/csdb/storage.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class Storage final {
227227
// And now for something completely different
228228
cs::Sequence get_previous_transaction_block(const Address&, cs::Sequence) const;
229229
void set_previous_transaction_block(const Address&, cs::Sequence currTransBlock, cs::Sequence prevTransBlock);
230+
void remove_last_from_trx_index(const Address&, cs::Sequence lastIndexed);
230231
void truncate_trxs_index();
231232

232233
/**

csdb/src/database_berkeleydb.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,24 @@ bool DatabaseBerkeleyDB::putToTransIndex(const cs::Bytes &key, const cs::Bytes &
497497
return true;
498498
}
499499

500+
bool DatabaseBerkeleyDB::removeLastFromTrxIndex(const cs::Bytes &key) {
501+
if (!db_trans_idx_) {
502+
set_last_error(NotOpen);
503+
return false;
504+
}
505+
506+
Dbt_copy<cs::Bytes> db_key(key);
507+
508+
int status = db_trans_idx_->del(nullptr, &db_key, 0);
509+
if (status != 0) {
510+
set_last_error_from_berkeleydb(status);
511+
return false;
512+
}
513+
514+
set_last_error();
515+
return true;
516+
}
517+
500518
bool DatabaseBerkeleyDB::getFromTransIndex(const cs::Bytes &key, cs::Bytes *value) {
501519
if (!db_trans_idx_) {
502520
set_last_error(NotOpen);

csdb/src/storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,10 @@ void Storage::set_previous_transaction_block(const Address& addr, cs::Sequence c
871871
d->db->putToTransIndex(key, os.buffer());
872872
}
873873

874+
void Storage::remove_last_from_trx_index(const Address& addr, cs::Sequence lastIndexed) {
875+
d->db->removeLastFromTrxIndex(get_trans_index_key(addr, lastIndexed));
876+
}
877+
874878
void Storage::truncate_trxs_index() {
875879
d->db->truncateTransIndex();
876880
}

csnode/include/csnode/blockchain.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class BlockChain {
115115
}
116116

117117
void removeWalletsInPoolFromCache(const csdb::Pool& pool);
118+
void removeLastBlockFromTrxIndex(const csdb::Pool&);
118119
void removeLastBlock();
119120

120121
// updates fees in every transaction
@@ -214,7 +215,7 @@ public slots:
214215
csdb::Pool loadBlock(const cs::Sequence sequence) const;
215216
csdb::Pool loadBlockMeta(const csdb::PoolHash&, size_t& cnt) const;
216217
csdb::Transaction loadTransaction(const csdb::TransactionID&) const;
217-
void iterateOverWallets(const std::function<bool(const cs::WalletsCache::WalletData::Address&, const cs::WalletsCache::WalletData&)>);
218+
void iterateOverWallets(const std::function<bool(const cs::PublicKey&, const cs::WalletsCache::WalletData&)>);
218219
csdb::Pool getLastBlock() const {
219220
return loadBlock(getLastSeq());
220221
}
@@ -234,11 +235,9 @@ public slots:
234235
bool findWalletId(const WalletAddress& address, WalletId& id) const;
235236
// wallet transactions: pools cache + db search
236237
void getTransactions(Transactions& transactions, csdb::Address address, uint64_t offset, uint64_t limit);
237-
// wallets modified by last new block
238-
bool getModifiedWallets(Mask& dest) const;
239238

240239
#ifdef MONITOR_NODE
241-
void iterateOverWriters(const std::function<bool(const cs::WalletsCache::WalletData::Address&, const cs::WalletsCache::TrustedData&)>);
240+
void iterateOverWriters(const std::function<bool(const cs::PublicKey&, const cs::WalletsCache::TrustedData&)>);
242241
void applyToWallet(const csdb::Address&, const std::function<void(const cs::WalletsCache::WalletData&)>);
243242
#endif
244243
uint32_t getTransactionsCount(const csdb::Address&);
@@ -257,6 +256,10 @@ public slots:
257256
bool updateContractData(const csdb::Address& abs_addr, const cs::Bytes& data) const;
258257
bool getContractData(const csdb::Address& abs_addr, cs::Bytes& data) const;
259258

259+
const cs::WalletsCache::Updater& getCacheUpdater() const {
260+
return *(walletsCacheUpdater_.get());
261+
}
262+
260263
private:
261264
void createCachesPath();
262265
bool findAddrByWalletId(const WalletId id, csdb::Address& addr) const;
@@ -271,9 +274,7 @@ public slots:
271274
void onReadFromDB(csdb::Pool block, bool* shouldStop);
272275
bool postInitFromDB();
273276

274-
template <typename WalletCacheProcessor>
275-
bool updateWalletIds(const csdb::Pool& pool, WalletCacheProcessor& proc);
276-
bool insertNewWalletId(const csdb::Address& newWallAddress, WalletId newWalletId, cs::WalletsCache::Initer& initer);
277+
bool updateWalletIds(const csdb::Pool& pool, cs::WalletsCache::Updater& updater);
277278
bool insertNewWalletId(const csdb::Address& newWallAddress, WalletId newWalletId, cs::WalletsCache::Updater& updater);
278279

279280
void addNewWalletToPool(const csdb::Address& walletAddress, const csdb::Pool::NewWalletInfo::AddressId& addressId, csdb::Pool::NewWallets& newWallets);

csnode/include/csnode/transactionstail.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "bitheap.hpp"
55

6+
#include <sstream>
7+
68
namespace cs {
79
class TransactionsTail {
810
public:

0 commit comments

Comments
 (0)