Skip to content

Commit

Permalink
<fix>(p2p,rpc): fix p2p message length check coredump, add more infor…
Browse files Browse the repository at this point in the history
…mation in web3 json rpc block response.
  • Loading branch information
kyonRay committed Nov 14, 2024
1 parent 402b5ee commit 4d35418
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 9 deletions.
1 change: 1 addition & 0 deletions bcos-executor/test/unittest/mock/MockBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MockBlock : public bcos::protocol::Block
uint64_t receiptsSize() const override { return 0; }
void setNonceList(RANGES::any_view<protocol::NonceType> nonces) override {}
RANGES::any_view<protocol::NonceType> nonceList() const override { return m_nodelist; }
size_t size() const override { return 0; }

private:
protocol::BlockHeader::Ptr m_blockHeader = std::make_shared<MockBlockHeader>(1);
Expand Down
1 change: 1 addition & 0 deletions bcos-executor/test/unittest/mock/MockBlockHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MockBlockHeader : public bcos::protocol::BlockHeader
void setExtraData(bytes&& _extraData) override {}
void setSignatureList(const gsl::span<const protocol::Signature>& _signatureList) override {}
void setSignatureList(protocol::SignatureList&& _signatureList) override {}
size_t size() const override { return 0; }

private:
protocol::BlockNumber m_blockNumber;
Expand Down
1 change: 1 addition & 0 deletions bcos-framework/bcos-framework/protocol/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Block
{
return blockHeaderConst()->number() < block.blockHeaderConst()->number();
}
virtual size_t size() const = 0;
};
using Blocks = std::vector<Block::Ptr>;
using BlocksPtr = std::shared_ptr<Blocks>;
Expand Down
2 changes: 2 additions & 0 deletions bcos-framework/bcos-framework/protocol/BlockHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,7 @@ class BlockHeader

virtual void setSignatureList(gsl::span<const Signature> const& _signatureList) = 0;
virtual void setSignatureList(SignatureList&& _signatureList) = 0;

virtual size_t size() const = 0;
};
} // namespace bcos::protocol
2 changes: 2 additions & 0 deletions bcos-framework/bcos-framework/protocol/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class Transaction
bool storeToBackend() const { return m_storeToBackend; }
void setStoreToBackend(bool _storeToBackend) const { m_storeToBackend = _storeToBackend; }

virtual size_t size() const { return 0; }

protected:
TxSubmitCallback m_submitCallback;
// the tx has been synced or not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TransactionReceipt
// calculation
virtual std::string const& message() const = 0;
virtual void setMessage(std::string message) = 0;
virtual size_t size() const = 0;

// NOTE: only use for trace log
virtual std::string toString() const
Expand Down
1 change: 0 additions & 1 deletion bcos-gateway/bcos-gateway/libnetwork/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ void Session::doRead()
else if (result == 0)
{
auto length = message->lengthDirect();
assert(length <= session->allowMaxMsgSize());
if (length > session->allowMaxMsgSize())
{
SESSION_LOG(ERROR)
Expand Down
2 changes: 1 addition & 1 deletion bcos-gateway/bcos-gateway/libp2p/P2PMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ int32_t P2PMessage::decode(const bytesConstRef& _buffer)
}

uint32_t length = _buffer.size();
CHECK_OFFSET_WITH_THROW_EXCEPTION(m_length, length);
CHECK_OFFSET_WITH_THROW_EXCEPTION(offset, m_length);
auto data = _buffer.getCroppedData(offset, m_length - offset);
// raw data cropped from buffer, maybe be compressed or not

Expand Down
2 changes: 1 addition & 1 deletion bcos-rpc/bcos-rpc/web3jsonrpc/endpoints/EthEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ task::Task<void> EthEndpoint::gasPrice(const Json::Value&, Json::Value& response
}
else
{
result = "0x5208"; // 21000
result = "0x0";
}
buildJsonContent(result, response);
co_return;
Expand Down
16 changes: 13 additions & 3 deletions bcos-rpc/bcos-rpc/web3jsonrpc/model/BlockResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ namespace bcos::rpc
result["transactionsRoot"] = block->blockHeader()->txsRoot().hexPrefixed();
result["stateRoot"] = block->blockHeader()->stateRoot().hexPrefixed();
result["receiptsRoot"] = block->blockHeader()->receiptsRoot().hexPrefixed();
result["miner"] = Address().hexPrefixed();
if (std::cmp_greater(block->blockHeader()->sealerList().size(), block->blockHeader()->sealer()))
{
auto pk = block->blockHeader()->sealerList()[block->blockHeader()->sealer()];
auto hash = crypto::keccak256Hash(bcos::ref(pk));
Address address = right160(hash);
auto addrString = address.hex();
auto addrHash = crypto::keccak256Hash(address.ref()).hex();
toChecksumAddress(addrString, addrHash);
result["miner"] = "0x" + addrString;
}
result["difficulty"] = "0x0";
result["totalDifficulty"] = "0x0";
result["extraData"] = toHexStringWithPrefix(block->blockHeader()->extraData());
result["size"] = "0xffff";
result["gasLimit"] = toQuantity(30000000ull);
result["size"] = toQuantity(block->size());
// TODO: change it wen block gas limit apply
result["gasLimit"] = toQuantity(30000000ULL);
result["gasUsed"] = toQuantity((uint64_t)block->blockHeader()->gasUsed());
result["timestamp"] = toQuantity(block->blockHeader()->timestamp() / 1000); // to seconds
if (fullTxs)
Expand Down
5 changes: 3 additions & 2 deletions bcos-rpc/bcos-rpc/web3jsonrpc/model/TransactionResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ static void combineTxResponse(Json::Value& result, bcos::protocol::Transaction::

if (tx->type() == bcos::protocol::TransactionType::BCOSTransaction) [[unlikely]]
{
result["type"] = toQuantity(UINT32_MAX);
result["nonce"] = tx->nonce();
result["type"] = toQuantity(0);
// web3 tools do not compatible with too long hex
// result["nonce"] = tx->nonce();
result["value"] = std::string(tx->value().empty() ? "0x0" : tx->value());
result["maxPriorityFeePerGas"] =
std::string(tx->maxPriorityFeePerGas().empty() ? "0x0" : tx->maxPriorityFeePerGas());
Expand Down
10 changes: 10 additions & 0 deletions bcos-tars-protocol/bcos-tars-protocol/protocol/BlockHeaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,13 @@ void bcostars::protocol::BlockHeaderImpl::clearDataHash()
{
m_inner()->dataHash.clear();
}

size_t bcostars::protocol::BlockHeaderImpl::size() const
{
size_t size = 0;
size += m_inner()->data.txsRoot.size();
size += m_inner()->data.stateRoot.size();
size += m_inner()->data.receiptRoot.size();
size += m_inner()->data.extraData.size();
return size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class BlockHeaderImpl : public bcos::protocol::BlockHeader
bcostars::BlockHeader& mutableInner();
void setInner(const bcostars::BlockHeader& blockHeader);
void setInner(bcostars::BlockHeader&& blockHeader);

size_t size() const override;

private:
// Note: When the field in the header used to calculate the hash changes, the dataHash needs to
Expand Down
15 changes: 15 additions & 0 deletions bcos-tars-protocol/bcos-tars-protocol/protocol/BlockImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,18 @@ bcos::crypto::HashType bcostars::protocol::BlockImpl::calculateReceiptRoot(

return receiptsRoot;
}

size_t bcostars::protocol::BlockImpl::size() const
{
size_t size = 0;
size += blockHeaderConst()->size();
for (uint64_t i = 0; i < transactionsSize(); ++i)
{
size += transaction(i)->size();
}
for (uint64_t i = 0; i < receiptsSize(); ++i)
{
size += receipt(i)->size();
}
return size;
}
2 changes: 2 additions & 0 deletions bcos-tars-protocol/bcos-tars-protocol/protocol/BlockImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class BlockImpl : public bcos::protocol::Block, public std::enable_shared_from_t

bcos::crypto::HashType calculateReceiptRoot(const bcos::crypto::Hash& hashImpl) const override;

size_t size() const override;

private:
std::shared_ptr<bcostars::Block> m_inner;
mutable bcos::SharedMutex x_blockHeader;
Expand Down
20 changes: 20 additions & 0 deletions bcos-tars-protocol/bcos-tars-protocol/protocol/TransactionImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,23 @@ void bcostars::protocol::TransactionImpl::setInner(bcostars::Transaction inner)
{
*m_inner() = std::move(inner);
}

size_t bcostars::protocol::TransactionImpl::size() const
{
size_t size = 0;
size += m_inner()->data.nonce.size();
size += m_inner()->data.to.size();
size += m_inner()->data.input.size();
size += m_inner()->data.abi.size();
size += m_inner()->data.value.size();
size += m_inner()->data.gasPrice.size();
size += m_inner()->data.maxFeePerGas.size();
size += m_inner()->data.maxPriorityFeePerGas.size();
size += m_inner()->data.extension.size();
size += m_inner()->signature.size();
size += m_inner()->sender.size();
size += m_inner()->extraData.size();
size += m_inner()->extraTransactionBytes.size();
size += m_inner()->extraTransactionHash.size();
return size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class TransactionImpl : public bcos::protocol::Transaction
bcostars::Transaction& mutableInner();
void setInner(bcostars::Transaction inner);

size_t size() const override;

private:
std::function<bcostars::Transaction*()> m_inner;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,19 @@ void bcostars::protocol::TransactionReceiptImpl::setMessage(std::string message)
{
m_inner()->message = std::move(message);
}
size_t bcostars::protocol::TransactionReceiptImpl::size() const
{
size_t size = 0;
size += m_inner()->data.output.size();
for (auto& it : m_inner()->data.logEntries)
{
size += it.data.size();
size += it.address.size();
for (auto& topic : it.topic)
{
size += topic.size();
}
}
size += m_inner()->message.size();
return size;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class TransactionReceiptImpl : public bcos::protocol::TransactionReceipt
void setLogEntries(std::vector<bcos::protocol::LogEntry> const& _logEntries);
std::string const& message() const override;
void setMessage(std::string message) override;
size_t size() const override;

private:
std::function<bcostars::TransactionReceipt*()> m_inner;
Expand Down

0 comments on commit 4d35418

Please sign in to comment.