Skip to content

Commit

Permalink
add effectiveGasPrice to transactionReceipt protocol (FISCO-BCOS#4013)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Nov 16, 2023
1 parent bb79377 commit 43858f8
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum SchedulerError
Stopped,
InvalidBlockVersion,
BlockIsCommitting,
InvalidTransactionVersion,
};
} // namespace scheduler
} // namespace bcos
2 changes: 2 additions & 0 deletions bcos-framework/bcos-framework/protocol/TransactionReceipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class TransactionReceipt
virtual bcos::bytesConstRef output() const = 0;
virtual gsl::span<const LogEntry> logEntries() const = 0;
virtual protocol::BlockNumber blockNumber() const = 0;
virtual std::string_view effectiveGasPrice() const = 0;
virtual void setEffectiveGasPrice(std::string effectiveGasPrice) = 0;

// additional information on transaction execution, no need to be involved in the hash
// calculation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class TransactionReceiptFactory
virtual TransactionReceipt::Ptr createReceipt(u256 const& gasUsed, std::string contractAddress,
const std::vector<LogEntry>& logEntries, int32_t status, bcos::bytesConstRef output,
BlockNumber blockNumber) const = 0;
virtual TransactionReceipt::Ptr createReceipt2(u256 const& gasUsed, std::string contractAddress,
const std::vector<LogEntry>& logEntries, int32_t status, bcos::bytesConstRef output,
BlockNumber blockNumber, std::string effectiveGasPrice = "1") const = 0;
};

} // namespace bcos::protocol
51 changes: 40 additions & 11 deletions bcos-scheduler/src/BlockExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ bcos::protocol::ExecutionMessage::UniquePtr BlockExecutive::buildMessage(
message->setGasPrice(std::string(tx->gasPrice()));
message->setMaxFeePerGas(std::string(tx->maxFeePerGas()));
message->setMaxPriorityFeePerGas(std::string(tx->maxPriorityFeePerGas()));
message->setEffectiveGasPrice(std::string(tx->gasPrice()));

return message;
}
Expand Down Expand Up @@ -290,6 +291,7 @@ void BlockExecutive::buildExecutivesFromNormalTransaction()
{
auto tx = m_block->transaction(i);
m_executiveResults[i].transactionHash = tx->hash();
m_executiveResults[i].version = tx->version();

auto contextID = i + m_startContextID;
auto& [to, message, enableDAG] = results[i];
Expand Down Expand Up @@ -1626,17 +1628,44 @@ void BlockExecutive::onTxFinish(bcos::protocol::ExecutionMessage::UniquePtr outp
txGasUsed = 0;
}
m_gasUsed.fetch_add(txGasUsed);
auto receipt = m_scheduler->m_blockFactory->receiptFactory()->createReceipt(txGasUsed,
std::string(output->newEVMContractAddress()), output->takeLogEntries(), output->status(),
output->data(), number());

// write receipt in results
SCHEDULER_LOG(TRACE) << " 6.GenReceipt:\t [^^] " << output->toString()
<< " -> contextID:" << output->contextID() - m_startContextID
<< ", receipt: " << receipt->hash() << ", gasUsed: " << receipt->gasUsed()
<< ", version: " << receipt->version()
<< ", status: " << receipt->status();
m_executiveResults[output->contextID() - m_startContextID].receipt = std::move(receipt);
auto version = m_executiveResults[output->contextID() - m_startContextID].version;
switch (version)
{
case int32_t(bcos::protocol::TransactionVersion::V0_VERSION):
{
auto receipt = m_scheduler->m_blockFactory->receiptFactory()->createReceipt(txGasUsed,
std::string(output->newEVMContractAddress()), output->takeLogEntries(),
output->status(), output->data(), number());
// write receipt in results
SCHEDULER_LOG(TRACE) << " 6.GenReceipt:\t [^^] " << output->toString()
<< " -> contextID:" << output->contextID() - m_startContextID
<< ", receipt: " << receipt->hash()
<< ", gasUsed: " << receipt->gasUsed()
<< ", version: " << receipt->version()
<< ", status: " << receipt->status();
m_executiveResults[output->contextID() - m_startContextID].receipt = std::move(receipt);
break;
}
case int32_t(bcos::protocol::TransactionVersion::V1_VERSION):
{
auto receipt = m_scheduler->m_blockFactory->receiptFactory()->createReceipt2(txGasUsed,
std::string(output->newEVMContractAddress()), output->takeLogEntries(),
output->status(), output->data(), number(), std::string(output->effectiveGasPrice()));
// write receipt in results
SCHEDULER_LOG(TRACE) << " 6.GenReceipt:\t [^^] " << output->toString()
<< " -> contextID:" << output->contextID() - m_startContextID
<< ", receipt: " << receipt->hash()
<< ", gasUsed: " << receipt->gasUsed()
<< ", version: " << receipt->version()
<< ", status: " << receipt->status()
<< ", effectiveGasPrice: " << receipt->effectiveGasPrice();
m_executiveResults[output->contextID() - m_startContextID].receipt = std::move(receipt);
break;
}
default:
BOOST_THROW_EXCEPTION(BCOS_ERROR(SchedulerError::InvalidTransactionVersion,
"Invalid receipt version: " + std::to_string(version)));
}
}


Expand Down
1 change: 1 addition & 0 deletions bcos-scheduler/src/Executive.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ struct ExecutiveResult
bcos::protocol::TransactionReceipt::Ptr receipt;
bcos::crypto::HashType transactionHash;
std::string source;
int32_t version;
};
} // namespace bcos::scheduler
6 changes: 3 additions & 3 deletions bcos-scheduler/test/testBlockExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(asyncExecuteTest1)
{
std::string inputStr = "hello world!";
bytes input(inputStr.begin(), inputStr.end());
auto tx = transactionFactory->createTransaction(20,
auto tx = transactionFactory->createTransaction(0,
"contract" + boost::lexical_cast<std::string>((i + 1) % 10), input, std::to_string(i),
200, "chainID", "groupID", 400, keyPair);
// tx->setAttribute(bcos::protocol::Transaction::Attribute::DAG);
Expand Down Expand Up @@ -442,7 +442,7 @@ BOOST_AUTO_TEST_CASE(dagByMessage)
{
std::string inputStr = "hello world!";
bytes input(inputStr.begin(), inputStr.end());
auto tx = transactionFactory->createTransaction(20,
auto tx = transactionFactory->createTransaction(0,
"contract" + boost::lexical_cast<std::string>((i + 1) % 10), input, std::to_string(i),
200, "chainID", "groupID", 400, keyPair);
tx->setAttribute(bcos::protocol::Transaction::Attribute::DAG);
Expand Down Expand Up @@ -506,7 +506,7 @@ BOOST_AUTO_TEST_CASE(executeWithSystemError)
block->blockHeader()->calculateHash(*blockFactory->cryptoSuite()->hashImpl());

auto tx = blockFactory->transactionFactory()->createTransaction(
3, "0xaabbccdd", {}, std::to_string(1), 500, "chainId", "groupId", utcTime());
0, "0xaabbccdd", {}, std::to_string(1), 500, "chainId", "groupId", utcTime());
block->appendTransaction(std::move(tx));

// Add Executor
Expand Down
4 changes: 2 additions & 2 deletions bcos-scheduler/test/testSchedulerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ BOOST_AUTO_TEST_CASE(testDeploySysContract)
block->blockHeader()->setNumber(0);
block->blockHeader()->calculateHash(*blockFactory->cryptoSuite()->hashImpl());

auto tx = blockFactory->transactionFactory()->createTransaction(3,
auto tx = blockFactory->transactionFactory()->createTransaction(0,
precompiled::AUTH_COMMITTEE_ADDRESS, {}, std::to_string(1), 500, "chainId", "groupId",
utcTime());
block->appendTransaction(std::move(tx));
Expand Down Expand Up @@ -595,7 +595,7 @@ BOOST_AUTO_TEST_CASE(testCallSysContract)
auto executor1 = std::make_shared<MockParallelExecutorForCall>("executor1");
executorManager->addExecutor("executor1", executor1);

auto tx = blockFactory->transactionFactory()->createTransaction(3,
auto tx = blockFactory->transactionFactory()->createTransaction(0,
precompiled::AUTH_COMMITTEE_ADDRESS, {}, std::to_string(1), 500, "chainId", "groupId",
utcTime());

Expand Down
6 changes: 3 additions & 3 deletions bcos-scheduler/test/testShardingBlockExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ BOOST_AUTO_TEST_CASE(asyncExecuteTest1)
{
std::string inputStr = "hello world!";
bytes input(inputStr.begin(), inputStr.end());
auto tx = transactionFactory->createTransaction(20,
auto tx = transactionFactory->createTransaction(0,
"contract" + boost::lexical_cast<std::string>((i + 1) % 10), input, std::to_string(i),
200, "chainID", "groupID", 400, keyPair);
// tx->setAttribute(bcos::protocol::Transaction::Attribute::DAG);
Expand Down Expand Up @@ -461,7 +461,7 @@ BOOST_AUTO_TEST_CASE(dagByMessage)
{
std::string inputStr = "hello world!";
bytes input(inputStr.begin(), inputStr.end());
auto tx = transactionFactory->createTransaction(20,
auto tx = transactionFactory->createTransaction(0,
"contract" + boost::lexical_cast<std::string>((i + 1) % 10), input, std::to_string(i),
200, "chainID", "groupID", 400, keyPair);
tx->setAttribute(bcos::protocol::Transaction::Attribute::DAG);
Expand Down Expand Up @@ -525,7 +525,7 @@ BOOST_AUTO_TEST_CASE(executeWithSystemError)
block->blockHeader()->calculateHash(*blockFactory->cryptoSuite()->hashImpl());

auto tx = blockFactory->transactionFactory()->createTransaction(
3, "0xaabbccdd", {}, std::to_string(1), 500, "chainId", "groupId", utcTime());
0, "0xaabbccdd", {}, std::to_string(1), 500, "chainId", "groupId", utcTime());
block->appendTransaction(std::move(tx));

// Add Executor
Expand Down
14 changes: 13 additions & 1 deletion bcos-sdk/bcos-cpp-sdk/utilities/receipt/TransactionReceipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct TransactionReceiptData : public tars::TarsStructBase
output.clear();
logEntries.clear();
blockNumber = 0;
effectiveGasPrice = "";
}
template <typename WriterT>
void writeTo(tars::TarsOutputStream<WriterT>& _os) const
Expand Down Expand Up @@ -174,6 +175,10 @@ struct TransactionReceiptData : public tars::TarsStructBase
{
_os.write(blockNumber, 7);
}
if (effectiveGasPrice != "")
{
_os.write(effectiveGasPrice, 8);
}
}
template <typename ReaderT>
void readFrom(tars::TarsInputStream<ReaderT>& _is)
Expand All @@ -186,6 +191,7 @@ struct TransactionReceiptData : public tars::TarsStructBase
_is.read(output, 5, false);
_is.read(logEntries, 6, false);
_is.read(blockNumber, 7, false);
_is.read(effectiveGasPrice, 8, false);
}
[[nodiscard]] tars::JsonValueObjPtr writeToJson() const
{
Expand All @@ -197,6 +203,7 @@ struct TransactionReceiptData : public tars::TarsStructBase
p->value["output"] = tars::JsonOutput::writeJson(bcos::toHexStringWithPrefix(output));
p->value["logEntries"] = tars::JsonOutput::writeJson(logEntries);
p->value["blockNumber"] = tars::JsonOutput::writeJson(blockNumber);
p->value["effectiveGasPrice"] = tars::JsonOutput::writeJson(effectiveGasPrice);
return p;
}
[[nodiscard]] std::string writeToJsonString() const
Expand Down Expand Up @@ -227,6 +234,7 @@ struct TransactionReceiptData : public tars::TarsStructBase

tars::JsonInput::readJson(logEntries, pObj->value["logEntries"], false);
tars::JsonInput::readJson(blockNumber, pObj->value["blockNumber"], true);
tars::JsonInput::readJson(effectiveGasPrice, pObj->value["effectiveGasPrice"], false);
}
void readFromJsonString(const std::string& str) { readFromJson(tars::TC_Json::getValue(str)); }
std::ostream& display(std::ostream& _os, int _level = 0) const
Expand All @@ -239,6 +247,7 @@ struct TransactionReceiptData : public tars::TarsStructBase
_ds.display(output, "output");
_ds.display(logEntries, "logEntries");
_ds.display(blockNumber, "blockNumber");
_ds.display(effectiveGasPrice, "effectiveGasPrice");
return _os;
}
std::ostream& displaySimple(std::ostream& _os, int _level = 0) const
Expand All @@ -251,6 +260,7 @@ struct TransactionReceiptData : public tars::TarsStructBase
_ds.displaySimple(output, true);
_ds.displaySimple(logEntries, true);
_ds.displaySimple(blockNumber, false);
_ds.displaySimple(effectiveGasPrice, false);
return _os;
}

Expand Down Expand Up @@ -303,12 +313,14 @@ struct TransactionReceiptData : public tars::TarsStructBase
std::vector<tars::Char> output;
std::vector<bcostars::LogEntry> logEntries;
tars::Int64 blockNumber;
std::string effectiveGasPrice;
};
inline bool operator==(const TransactionReceiptData& l, const TransactionReceiptData& r)
{
return l.version == r.version && l.gasUsed == r.gasUsed &&
l.contractAddress == r.contractAddress && l.status == r.status && l.output == r.output &&
l.logEntries == r.logEntries && l.blockNumber == r.blockNumber;
l.logEntries == r.logEntries && l.blockNumber == r.blockNumber &&
l.effectiveGasPrice == r.effectiveGasPrice;
}
inline bool operator!=(const TransactionReceiptData& l, const TransactionReceiptData& r)
{
Expand Down
32 changes: 26 additions & 6 deletions bcos-tars-protocol/bcos-tars-protocol/impl/TarsHashable.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,32 @@ void impl_calculate(bcos::crypto::hasher::Hasher auto hasher,

auto const& hashFields = receipt.data;
int32_t version = boost::endian::native_to_big((int32_t)hashFields.version);
hasher.update(version);
hasher.update(hashFields.gasUsed);
hasher.update(hashFields.contractAddress);
int32_t status = boost::endian::native_to_big((int32_t)hashFields.status);
hasher.update(status);
hasher.update(hashFields.output);
switch (version)
{
case int32_t(bcos::protocol::TransactionVersion::V0_VERSION):
{
hasher.update(version);
hasher.update(hashFields.gasUsed);
hasher.update(hashFields.contractAddress);
int32_t status = boost::endian::native_to_big((int32_t)hashFields.status);
hasher.update(status);
hasher.update(hashFields.output);
break;
}
case int32_t(bcos::protocol::TransactionVersion::V1_VERSION):
{
hasher.update(version);
hasher.update(hashFields.gasUsed);
hasher.update(hashFields.contractAddress);
int32_t status = boost::endian::native_to_big((int32_t)hashFields.status);
hasher.update(status);
hasher.update(hashFields.output);
hasher.update(hashFields.effectiveGasPrice);
break;
}
default:
BOOST_THROW_EXCEPTION(std::runtime_error("not support version"));
}
// vector<LogEntry> logEntries: 6
for (auto const& log : hashFields.logEntries)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ class TransactionReceiptFactoryImpl : public bcos::protocol::TransactionReceiptF
return transactionReceipt;
}

TransactionReceiptImpl::Ptr createReceipt2(bcos::u256 const& gasUsed,
std::string contractAddress, const std::vector<bcos::protocol::LogEntry>& logEntries,
int32_t status, bcos::bytesConstRef output, bcos::protocol::BlockNumber blockNumber,
std::string effectiveGasPrice = "1") const override
{
auto transactionReceipt = std::make_shared<TransactionReceiptImpl>(
[m_receipt = bcostars::TransactionReceipt()]() mutable { return &m_receipt; });
auto& inner = transactionReceipt->mutableInner();
inner.data.version = 1;
inner.data.gasUsed = boost::lexical_cast<std::string>(gasUsed);
inner.data.contractAddress = std::move(contractAddress);
inner.data.status = status;
inner.data.output.assign(output.begin(), output.end());
transactionReceipt->setLogEntries(logEntries);
inner.data.blockNumber = blockNumber;
inner.data.effectiveGasPrice = std::move(effectiveGasPrice);

// Update the hash field
bcos::concepts::hash::calculate(m_hashImpl->hasher(), inner, inner.dataHash);
return transactionReceipt;
}

private:
bcos::crypto::Hash::Ptr m_hashImpl;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ bcos::protocol::BlockNumber bcostars::protocol::TransactionReceiptImpl::blockNum
{
return m_inner()->data.blockNumber;
}
std::string_view bcostars::protocol::TransactionReceiptImpl::effectiveGasPrice() const
{
return m_inner()->data.effectiveGasPrice;
}
void bcostars::protocol::TransactionReceiptImpl::setEffectiveGasPrice(std::string effectiveGasPrice)
{
m_inner()->data.effectiveGasPrice = std::move(effectiveGasPrice);
}
const bcostars::TransactionReceipt& bcostars::protocol::TransactionReceiptImpl::inner() const
{
return *m_inner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TransactionReceiptImpl : public bcos::protocol::TransactionReceipt
bcos::bytesConstRef output() const override;
gsl::span<const bcos::protocol::LogEntry> logEntries() const override;
bcos::protocol::BlockNumber blockNumber() const override;
std::string_view effectiveGasPrice() const override;
void setEffectiveGasPrice(std::string effectiveGasPrice) override;

const bcostars::TransactionReceipt& inner() const;
bcostars::TransactionReceipt& mutableInner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ module bcostars {
};

struct TransactionReceiptData {
1 require int version;
2 optional string gasUsed;
3 optional string contractAddress;
4 optional int status;
5 optional vector<byte> output;
1 require int version;
2 optional string gasUsed;
3 optional string contractAddress;
4 optional int status;
5 optional vector<byte> output;
6 optional vector<LogEntry> logEntries;
7 optional long blockNumber;
7 optional long blockNumber;
8 optional string effectiveGasPrice;
};

struct TransactionReceipt {
Expand Down
6 changes: 3 additions & 3 deletions libinitializer/AuthInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class AuthInitializer
bytes input = code + abi.abiIn("", initGovernors, weights, codec::toString32(h256(0)),
codec::toString32(h256(0)));

auto tx = _protocol->blockFactory()->transactionFactory()->createTransaction(3,
precompiled::AUTH_COMMITTEE_ADDRESS, input, u256(_number).str(), 500, _nodeConfig->chainId(),
_nodeConfig->groupId(), utcTime());
auto tx = _protocol->blockFactory()->transactionFactory()->createTransaction(0,
precompiled::AUTH_COMMITTEE_ADDRESS, input, u256(_number).str(), 500,
_nodeConfig->chainId(), _nodeConfig->groupId(), utcTime());
tx->forceSender(authAdmin.asBytes());
block->appendTransaction(tx);
}
Expand Down
2 changes: 1 addition & 1 deletion libinitializer/BfsInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BfsInitializer
_protocol->cryptoSuite()->hashImpl(), _nodeConfig->isWasm());
bytes input = codecWrapper.encodeWithSig("initBfs()");

auto transaction = _protocol->blockFactory()->transactionFactory()->createTransaction(3,
auto transaction = _protocol->blockFactory()->transactionFactory()->createTransaction(0,
_nodeConfig->isWasm() ? precompiled::BFS_NAME : precompiled::BFS_ADDRESS, input,
u256(_number).str(), 500, _nodeConfig->chainId(), _nodeConfig->groupId(), utcTime());
_block->appendTransaction(std::move(transaction));
Expand Down
2 changes: 1 addition & 1 deletion tests/perf/mini_consensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void createTxs(bcos::initializer::Initializer::Ptr const& init)
{
bytes input = {0};
auto tx = init->protocolInitializer()->blockFactory()->transactionFactory()->createTransaction(
3, precompiled::AUTH_COMMITTEE_ADDRESS, input, "", 1000, init->nodeConfig()->chainId(),
0, precompiled::AUTH_COMMITTEE_ADDRESS, input, "", 1000, init->nodeConfig()->chainId(),
init->nodeConfig()->groupId(), utcSteadyTime());
auto sender = Address(precompiled::AUTH_COMMITTEE_ADDRESS);
tx->forceSender(sender.asBytes());
Expand Down

0 comments on commit 43858f8

Please sign in to comment.