Skip to content

Commit

Permalink
Some improvements (Zilliqa#3903)
Browse files Browse the repository at this point in the history
Co-authored-by: bzawisto <bartosz@zilliqa.com>
  • Loading branch information
bzawisto and bzawisto authored Dec 19, 2023
1 parent dbe2406 commit 4c64ea0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 64 deletions.
15 changes: 9 additions & 6 deletions src/libData/AccountData/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ struct TransactionCoreInfo {
const uint128_t& amountInput,
const uint128_t& gasPriceInput,
const uint64_t& gasLimitInput, const zbytes& codeInput,
const zbytes& dataInput, const AccessList& accessListInput,
const uint128_t& maxPriorityFeePerGasInput, const uint128_t& maxFeePerGasInput)
const zbytes& dataInput,
const AccessList& accessListInput,
const uint128_t& maxPriorityFeePerGasInput,
const uint128_t& maxFeePerGasInput)
: version(versionInput),
nonce(nonceInput),
toAddr(toAddrInput),
Expand Down Expand Up @@ -72,6 +74,7 @@ class Transaction : public SerializableDataBlock {
bool SetHash(const zbytes& txnData);

public:
static constexpr auto AVERAGE_TXN_SIZE_BYTES = 192;
/// Default constructor.
Transaction();

Expand Down Expand Up @@ -102,16 +105,16 @@ class Transaction : public SerializableDataBlock {
const Address& toAddr, const PubKey& senderPubKey,
const uint128_t& amount, const uint128_t& gasPrice,
const uint64_t& gasLimit, const zbytes& code, const zbytes& data,
const Signature& signature, const AccessList &accessList);
const Signature& signature, const AccessList& accessList);

/// Constructor with specified transaction fields.
Transaction(const uint32_t& version, const uint64_t& nonce,
const Address& toAddr, const PubKey& senderPubKey,
const uint128_t& amount, const uint128_t& gasPrice,
const uint64_t& gasLimit, const zbytes& code, const zbytes& data,
const Signature& signature, const AccessList &accessList,
const uint128_t& maxPriorityFeePerGas, const uint128_t& maxFeePerGas,
uint32_t signature_validation = 0);
const Signature& signature, const AccessList& accessList,
const uint128_t& maxPriorityFeePerGas,
const uint128_t& maxFeePerGas, uint32_t signature_validation = 0);

/// Constructor with core information.
Transaction(const TxnHash& tranID, const TransactionCoreInfo& coreInfo,
Expand Down
19 changes: 15 additions & 4 deletions src/libMessage/Messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ void TransactionArrayToProtobuf(const deque<pair<Transaction, uint32_t>>& txns,
bool ProtobufToTransactionArray(
const ProtoTransactionArray& protoTransactionArray,
std::vector<Transaction>& txns) {
txns.reserve(protoTransactionArray.transactions_size());
for (const auto& protoTransaction : protoTransactionArray.transactions()) {
Transaction txn;
if (!ProtobufToTransaction(protoTransaction, txn)) {
Expand Down Expand Up @@ -3559,6 +3560,8 @@ bool Messenger::SetNodeForwardTxnBlock(zbytes& dst, const unsigned int offset,
Signature signature;
if (result.transactions().size() > 0) {
zbytes tmp;
tmp.reserve(Transaction::AVERAGE_TXN_SIZE_BYTES *
result.transactions_size());
if (!RepeatableToArray(result.transactions(), tmp, 0)) {
LOG_GENERAL(WARNING, "Failed to serialize transactions");
return false;
Expand Down Expand Up @@ -3653,6 +3656,8 @@ bool Messenger::GetNodeForwardTxnBlock(

if (result.transactions().size() > 0) {
zbytes tmp;
tmp.reserve(Transaction::AVERAGE_TXN_SIZE_BYTES *
result.transactions_size());
if (!RepeatableToArray(result.transactions(), tmp, 0)) {
LOG_GENERAL(WARNING, "Failed to serialize transactions");
return false;
Expand All @@ -3663,7 +3668,7 @@ bool Messenger::GetNodeForwardTxnBlock(
LOG_GENERAL(WARNING, "Invalid signature in transactions");
return false;
}

txns.reserve(result.transactions_size());
for (const auto& txn : result.transactions()) {
Transaction t;
if (!ProtobufToTransaction(txn, t)) {
Expand Down Expand Up @@ -3810,10 +3815,13 @@ bool Messenger::SetNodeMissingTxnsErrorMsg(
NodeMissingTxnsErrorMsg result;

for (const auto& hash : missingTxnHashes) {
LOG_EPOCH(INFO, epochNum, "Missing txn: " << hash);
result.add_txnhashes(hash.data(), hash.size);
}

if (result.txnhashes_size() > 0) {
LOG_EPOCH(INFO, epochNum, "Missing txns: " << result.txnhashes_size());
}

result.set_epochnum(epochNum);
result.set_listenport(listenPort);

Expand Down Expand Up @@ -5400,7 +5408,6 @@ bool Messenger::GetForwardTxnBlockFromSeed(const zbytes& src,
LOG_GENERAL(WARNING, "LookupForwardTxnsFromSeed initialization failed");
return false;
}

if (!ProtobufToTransactionArray(result.transactions(), txnsContainer)) {
LOG_GENERAL(WARNING, "ProtobufToTransactionArray failed");
return false;
Expand Down Expand Up @@ -5885,6 +5892,8 @@ bool Messenger::SetLookupSetTxnsFromLookup(
Signature signature;
if (result.transactions().size() > 0) {
zbytes tmp;
tmp.reserve(Transaction::AVERAGE_TXN_SIZE_BYTES *
result.transactions_size());
if (!RepeatableToArray(result.transactions(), tmp, 0)) {
LOG_GENERAL(WARNING, "Failed to serialize transactions");
return false;
Expand Down Expand Up @@ -5934,6 +5943,8 @@ bool Messenger::GetLookupSetTxnsFromLookup(

if (result.transactions().size() > 0) {
zbytes tmp;
tmp.reserve(Transaction::AVERAGE_TXN_SIZE_BYTES *
result.transactions_size());
if (!RepeatableToArray(result.transactions(), tmp, 0)) {
LOG_GENERAL(WARNING, "Failed to serialize transactions");
return false;
Expand All @@ -5944,7 +5955,7 @@ bool Messenger::GetLookupSetTxnsFromLookup(
return false;
}
}

txns.reserve(result.transactions_size());
for (auto const& protoTxn : result.transactions()) {
TransactionWithReceipt txn;
PROTOBUFBYTEARRAYTOSERIALIZABLE(protoTxn, txn);
Expand Down
61 changes: 33 additions & 28 deletions src/libNode/MicroBlockPreProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ void Node::ProcessTransactionWhenShardLeader(
vector<pair<TxnHash, TxnStatus>> droppedTxns;
unsigned int count_addrNonceTxnMap = 0;
unsigned int count_createdTxns = 0;
uint32_t highNonce = 0;

AccountStore::GetInstance().CleanStorageRootUpdateBufferTemp();

Expand Down Expand Up @@ -481,9 +482,8 @@ void Node::ProcessTransactionWhenShardLeader(

continue;
} else {
LOG_GENERAL(WARNING,
"Adding to dropped Txns failed transaction with id: "
<< txn.GetTranID());
LOG_GENERAL(DEBUG, "Adding to dropped Txns failed transaction with id: "
<< txn.GetTranID());
droppedTxns.emplace_back(txn.GetTranID(), error_code);
}
}
Expand All @@ -498,10 +498,11 @@ void Node::ProcessTransactionWhenShardLeader(
if (txn.GetNonce() >
AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1) {
LOG_GENERAL(
INFO, "High nonce: "
<< txn.GetNonce() << " cur sender " << senderAddr.hex()
<< " nonce: "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr));
DEBUG, "High nonce: "
<< txn.GetNonce() << " cur sender " << senderAddr.hex()
<< " nonce: "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr));
highNonce++;
auto it1 = t_addrNonceTxnMap.find(senderAddr);
if (it1 != t_addrNonceTxnMap.end()) {
auto it2 = it1->second.find(txn.GetNonce());
Expand All @@ -520,7 +521,7 @@ void Node::ProcessTransactionWhenShardLeader(
else if (txn.GetNonce() <
AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1) {
LOG_GENERAL(
INFO,
DEBUG,
"Nonce too small"
<< " Expected "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1
Expand Down Expand Up @@ -557,7 +558,7 @@ void Node::ProcessTransactionWhenShardLeader(
}
appendOne(txn, txnReceipt);
} else {
LOG_GENERAL(WARNING,
LOG_GENERAL(DEBUG,
"Adding to dropped Txns failed transaction with id: "
<< txn.GetTranID());
droppedTxns.emplace_back(txn.GetTranID(), error_code);
Expand All @@ -571,6 +572,7 @@ void Node::ProcessTransactionWhenShardLeader(

LOG_GENERAL(INFO, "AddrNonceTxnMap # txns = " << count_addrNonceTxnMap);
LOG_GENERAL(INFO, "t_createdTxns # txns = " << count_createdTxns);
LOG_GENERAL(INFO, "HighNonce # txns = " << highNonce);
LOG_GENERAL(INFO, "m_gasUsedTotal = " << m_gasUsedTotal);

AccountStore::GetInstance().ProcessStorageRootUpdateBufferTemp();
Expand All @@ -583,7 +585,8 @@ void Node::ProcessTransactionWhenShardLeader(
}

// Put txns in map back into pool
ReinstateMemPool(t_addrNonceTxnMap, gasLimitExceededTxnBuffer, droppedTxns);
ReinstateMemPool(t_addrNonceTxnMap, gasLimitExceededTxnBuffer,
std::move(droppedTxns));
}

bool Node::VerifyTxnsOrdering(const vector<TxnHash>& tranHashes,
Expand All @@ -602,13 +605,13 @@ bool Node::VerifyTxnsOrdering(const vector<TxnHash>& tranHashes,

for (const auto& tranHash : tranHashes) {
if (!m_createdTxns.exist(tranHash)) {
LOG_GENERAL(WARNING,
"Missing transaction hash in my pool: " << tranHash.hex());
missingtranHashes.emplace_back(tranHash);
}
}

if (!missingtranHashes.empty()) {
LOG_GENERAL(WARNING,
"Missing txn in my pool: " << std::size(missingtranHashes));
if (m_prePrepRunning) {
if (missingtranHashes.size() + m_createdTxns.size() !=
tranHashes.size()) {
Expand Down Expand Up @@ -742,6 +745,8 @@ void Node::ProcessTransactionWhenShardBackup(
unsigned int count_addrNonceTxnMap = 0;
unsigned int count_createdTxns = 0;

uint32_t highNonce = 0;

AccountStore::GetInstance().CleanStorageRootUpdateBufferTemp();

LOG_GENERAL(INFO, "microblock_gas_limit = " << microblock_gas_limit);
Expand Down Expand Up @@ -806,10 +811,11 @@ void Node::ProcessTransactionWhenShardBackup(
if (t.GetNonce() >
AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1) {
LOG_GENERAL(
INFO, "High nonce: "
<< t.GetNonce() << " cur sender " << senderAddr.hex()
<< " nonce: "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr));
DEBUG, "High nonce: "
<< t.GetNonce() << " cur sender " << senderAddr.hex()
<< " nonce: "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr));
highNonce++;
auto it1 = t_addrNonceTxnMap.find(senderAddr);
if (it1 != t_addrNonceTxnMap.end()) {
auto it2 = it1->second.find(t.GetNonce());
Expand All @@ -828,7 +834,7 @@ void Node::ProcessTransactionWhenShardBackup(
else if (t.GetNonce() <
AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1) {
LOG_GENERAL(
INFO,
DEBUG,
"Nonce too small"
<< " Expected "
<< AccountStore::GetInstance().GetNonceTemp(senderAddr) + 1
Expand Down Expand Up @@ -876,6 +882,7 @@ void Node::ProcessTransactionWhenShardBackup(

LOG_GENERAL(INFO, "AddrNonceTxnMap # txns = " << count_addrNonceTxnMap);
LOG_GENERAL(INFO, "t_createdTxns # txns = " << count_createdTxns);
LOG_GENERAL(INFO, "HighNonce # txns = " << highNonce);
LOG_GENERAL(INFO, "m_gasUsedTotal = " << m_gasUsedTotal);

AccountStore::GetInstance().ProcessStorageRootUpdateBufferTemp();
Expand All @@ -886,7 +893,8 @@ void Node::ProcessTransactionWhenShardBackup(

PutTxnsInTempDataBase(t_processedTransactions);

ReinstateMemPool(t_addrNonceTxnMap, gasLimitExceededTxnBuffer, droppedTxns);
ReinstateMemPool(t_addrNonceTxnMap, gasLimitExceededTxnBuffer,
std::move(droppedTxns));
}

void Node::PutTxnsInTempDataBase(
Expand Down Expand Up @@ -949,33 +957,30 @@ std::string Node::GetAwsS3CpString(const std::string& uploadFilePath) {
void Node::ReinstateMemPool(
const map<Address, map<uint64_t, Transaction>>& addrNonceTxnMap,
const vector<Transaction>& gasLimitExceededTxnBuffer,
const vector<pair<TxnHash, TxnStatus>>& droppedTxns) {
vector<pair<TxnHash, TxnStatus>> droppedTxns) {
unique_lock<shared_timed_mutex> g(m_unconfirmedTxnsMutex);

MempoolInsertionStatus status;
// Put remaining txns back in pool
for (const auto& kv : addrNonceTxnMap) {
for (const auto& nonceTxn : kv.second) {
t_createdTxns.insert(nonceTxn.second, status);
LOG_GENERAL(INFO, "Txn " << nonceTxn.second.GetTranID() << ", Status: "
<< status.first << " " << status.second);
LOG_GENERAL(DEBUG, "Txn " << nonceTxn.second.GetTranID() << ", Status: "
<< status.first << " " << status.second);
m_unconfirmedTxns.emplace(nonceTxn.second.GetTranID(),
TxnStatus::PRESENT_NONCE_HIGH);
}
}

for (const auto& t : gasLimitExceededTxnBuffer) {
t_createdTxns.insert(t, status);
LOG_GENERAL(INFO, "Txn " << t.GetTranID() << ", Status: " << status.first
<< " " << status.second);
LOG_GENERAL(DEBUG, "Txn " << t.GetTranID() << ", Status: " << status.first
<< " " << status.second);
m_unconfirmedTxns.emplace(t.GetTranID(), TxnStatus::PRESENT_GAS_EXCEEDED);
}

for (const auto& txnHashStatus : droppedTxns) {
LOG_GENERAL(INFO,
"[DTXN]" << txnHashStatus.first << " " << txnHashStatus.second);
m_unconfirmedTxns.emplace(txnHashStatus);
}
m_unconfirmedTxns.insert(std::make_move_iterator(std::begin(droppedTxns)),
std::make_move_iterator(std::end(droppedTxns)));
}

void Node::PutAllTxnsInUnconfirmedTxns() {
Expand Down
Loading

0 comments on commit 4c64ea0

Please sign in to comment.