diff --git a/bcos-executor/src/executive/TransactionExecutive.cpp b/bcos-executor/src/executive/TransactionExecutive.cpp index d1cbe21374..1411b787d9 100644 --- a/bcos-executor/src/executive/TransactionExecutive.cpp +++ b/bcos-executor/src/executive/TransactionExecutive.cpp @@ -229,7 +229,6 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << LOG_DESC("Execute begin") << LOG_KV("callParameters", callParameters->toFullString()) << LOG_KV("blockNumber", m_blockContext.number()); - } m_storageWrapper->setRecoder(m_recoder); std::unique_ptr hostContext; @@ -237,30 +236,35 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt if (c_fileLogLevel <= LogLevel::TRACE) { EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << LOG_DESC("Execute begin") - << LOG_KV("feature_balance", m_blockContext.features().get(ledger::Features::Flag::feature_balance)) + << LOG_KV( + "feature_balance", m_blockContext.features().get( + ledger::Features::Flag::feature_balance)) << LOG_KV("value", callParameters->value); } - if (m_blockContext.features().get(ledger::Features::Flag::feature_balance) && callParameters->value > 0) + if (m_blockContext.features().get(ledger::Features::Flag::feature_balance) && + callParameters->value > 0) { - if (false == transferBalance(callParameters->origin, callParameters->senderAddress, - callParameters->receiveAddress, static_cast(callParameters->value), callParameters->gas)) - { - EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << LOG_DESC("transferBalance failed and will revet"); + if (false == transferBalance(callParameters->origin, callParameters->senderAddress, + callParameters->receiveAddress, static_cast(callParameters->value), + callParameters->gas)) + { + EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") + << LOG_DESC("transferBalance failed and will revet"); revert(); - callResults = std::move(callParameters); + callResults = std::move(callParameters); callResults->type = CallParameters::REVERT; callResults->status = (int32_t)TransactionStatus::RevertInstruction; } - else + else { - hostContext = nullptr; - callResults = std::move(callParameters); + hostContext = nullptr; + callResults = std::move(callParameters); callResults->type = CallParameters::FINISHED; callResults->status = (int32_t)TransactionStatus::None; } } - + else if (callParameters->create) { std::tie(hostContext, callResults) = create(std::move(callParameters)); @@ -288,19 +292,16 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt } -bool TransactionExecutive::transferBalance(std::string_view origin, std::string_view sender, +bool TransactionExecutive::transferBalance(std::string_view origin, std::string_view sender, std::string_view receiver, const u256& value, int64_t gas) { // origin 是发送方 // sender 是合约地址 // receiver 是转账接收方 - EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") - << "now to transferBalance" - << LOG_KV("subAccount", origin) - << LOG_KV("addAccount", receiver) - << LOG_KV("receiveAddress", ACCOUNT_ADDRESS) - << LOG_KV("value", value) - << LOG_KV("gas", gas); + EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "now to transferBalance" + << LOG_KV("subAccount", origin) << LOG_KV("addAccount", receiver) + << LOG_KV("receiveAddress", ACCOUNT_ADDRESS) << LOG_KV("value", value) + << LOG_KV("gas", gas); // first subAccountBalance, then addAccountBalance // origin = origin - value @@ -310,10 +311,8 @@ bool TransactionExecutive::transferBalance(std::string_view origin, std::string_ std::vector fromTableNameVector = {formTableName}; auto inputParams = codec.encode(fromTableNameVector, params); auto subParams = codec.encode(std::string(ACCOUNT_ADDRESS), inputParams); - EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") - << "transferBalance start, now is sub." - << LOG_KV("tableName", formTableName) - << LOG_KV("will sub balance", value); + EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "transferBalance start, now is sub." + << LOG_KV("tableName", formTableName) << LOG_KV("will sub balance", value); auto reposeSub = externalRequest(shared_from_this(), ref(subParams), origin, std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true); @@ -321,10 +320,8 @@ bool TransactionExecutive::transferBalance(std::string_view origin, std::string_ codec.decode(ref(reposeSub->data), result); if (result != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS)) { - EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") - << LOG_DESC("transferBalance sub failed") - << LOG_KV("subAccount", origin) - << LOG_KV("tablename", formTableName); + EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << LOG_DESC("transferBalance sub failed") + << LOG_KV("subAccount", origin) << LOG_KV("tablename", formTableName); return false; } @@ -336,45 +333,41 @@ bool TransactionExecutive::transferBalance(std::string_view origin, std::string_ auto inputParams1 = codec.encode(toTableNameVector, params1); auto addParams = codec.encode(std::string(ACCOUNT_ADDRESS), inputParams1); - EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") - << "transferBalance start, now is add." - << LOG_KV("tableName", toTableName) - << LOG_KV("will add balance", value); + EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "transferBalance start, now is add." + << LOG_KV("tableName", toTableName) << LOG_KV("will add balance", value); auto reposeAdd = externalRequest(shared_from_this(), ref(addParams), origin, - std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true); + std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true); codec.decode(ref(reposeAdd->data), result); if (result != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS)) { EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") - << LOG_DESC("transferBalance add failed, need to restore") - << LOG_KV("addAccount", receiver) - << LOG_KV("tablename", toTableName); + << LOG_DESC("transferBalance add failed, need to restore") + << LOG_KV("addAccount", receiver) << LOG_KV("tablename", toTableName); // if receiver add failed, sender need to restore // sender = sender + value auto revertParams = codec.encode(fromTableNameVector, params1); auto addParams1 = codec.encode(std::string(ACCOUNT_ADDRESS), revertParams); auto reponseRestore = externalRequest(shared_from_this(), ref(addParams1), origin, - std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true); + std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true); s256 addResult; codec.decode(ref(reponseRestore->data), addResult); if (addResult != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS)) { - EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") - << LOG_DESC("transferBalance to sub success but add failed, strike a balance failed.") - << LOG_KV("restoreAccount", origin) - << LOG_KV("tablename", formTableName); - BOOST_THROW_EXCEPTION(PrecompiledError("transferBalance to sub success but add failed, strike a balance failed.")); + EXECUTIVE_LOG(DEBUG) + << LOG_BADGE("Execute") + << LOG_DESC( + "transferBalance to sub success but add failed, strike a balance failed.") + << LOG_KV("restoreAccount", origin) << LOG_KV("tablename", formTableName); + BOOST_THROW_EXCEPTION(PrecompiledError( + "transferBalance to sub success but add failed, strike a balance failed.")); } return false; } - EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") - << "transferBalance finished." - << LOG_KV("subAccount", origin) - << LOG_KV("addAccount", receiver) - << LOG_KV("receiveAddress", ACCOUNT_ADDRESS) - << LOG_KV("value", value) - << LOG_KV("gas", gas); + EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << "transferBalance finished." + << LOG_KV("subAccount", origin) << LOG_KV("addAccount", receiver) + << LOG_KV("receiveAddress", ACCOUNT_ADDRESS) << LOG_KV("value", value) + << LOG_KV("gas", gas); return true; } diff --git a/bcos-executor/src/executive/TransactionExecutive.h b/bcos-executor/src/executive/TransactionExecutive.h index 81e9dcfe24..6d94724118 100644 --- a/bcos-executor/src/executive/TransactionExecutive.h +++ b/bcos-executor/src/executive/TransactionExecutive.h @@ -150,7 +150,7 @@ class TransactionExecutive : public std::enable_shared_from_this, CallParameters::UniquePtr> call( CallParameters::UniquePtr callParameters); @@ -163,10 +163,10 @@ class TransactionExecutive : public std::enable_shared_from_this( m_blockContext, m_evmPrecompiled, m_precompiled, m_staticPrecompiled, m_gasInjector); diff --git a/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp b/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp index 278be622fd..017c2a9137 100644 --- a/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp +++ b/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp @@ -64,11 +64,11 @@ std::shared_ptr AccountPrecompiled::call( uint32_t func = getParamFunc(originParam); bytesConstRef data = getParamData(originParam); auto table = _executive->storage().openTable(accountTableName); -// FIXME: for fake debug, temporary comment -// if (!table.has_value()) [[unlikely]] -// { -// BOOST_THROW_EXCEPTION(PrecompiledError(accountTableName + " does not exist")); -// } + // FIXME: for fake debug, temporary comment + // if (!table.has_value()) [[unlikely]] + // { + // BOOST_THROW_EXCEPTION(PrecompiledError(accountTableName + " does not exist")); + // } if (func == name2Selector[AM_METHOD_SET_ACCOUNT_STATUS]) { @@ -269,7 +269,8 @@ void AccountPrecompiled::addAccountBalance(const std::string& accountTableName, // check sender const auto* addAccountBalanceSender = blockContext.isWasm() ? BALANCE_PRECOMPILED_NAME : BALANCE_PRECOMPILED_ADDRESS; - if (!(_callParameters->m_sender == addAccountBalanceSender || _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS)) + if (!(_callParameters->m_sender == addAccountBalanceSender || + _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS)) { getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec); return; @@ -333,7 +334,8 @@ void AccountPrecompiled::subAccountBalance(const std::string& accountTableName, // check sender const auto* subAccountBalanceSender = blockContext.isWasm() ? BALANCE_PRECOMPILED_NAME : BALANCE_PRECOMPILED_ADDRESS; - if (!(_callParameters->m_sender == subAccountBalanceSender || _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS)) + if (!(_callParameters->m_sender == subAccountBalanceSender || + _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS)) { getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec); return; diff --git a/bcos-executor/src/vm/EVMHostInterface.cpp b/bcos-executor/src/vm/EVMHostInterface.cpp index 2f35a0d8f8..3a3f5d6562 100644 --- a/bcos-executor/src/vm/EVMHostInterface.cpp +++ b/bcos-executor/src/vm/EVMHostInterface.cpp @@ -25,11 +25,11 @@ #include "EVMHostInterface.h" #include "../Common.h" -#include "HostContext.h" -#include "bcos-framework/executor/PrecompiledTypeDef.h" #include "../precompiled/common/Common.h" -#include "bcos-codec/wrapper/CodecWrapper.h" +#include "HostContext.h" #include "bcos-codec/abi/ContractABICodec.h" +#include "bcos-codec/wrapper/CodecWrapper.h" +#include "bcos-framework/executor/PrecompiledTypeDef.h" #include #include #include @@ -98,26 +98,26 @@ evmc_bytes32 getBalance(evmc_host_context* _context, const evmc_address* _addr) _msg.sender = unhexAddress(hostContext.myAddress()); _msg.recipient = *_addr; _msg.gas = hostContext.gas(); - _msg.value = toEvmC(h256(0)); + _msg.value = toEvmC(h256(0)); const auto& codec = bcos::CodecWrapper(blockContext.hashHandler(), blockContext.isWasm()); // get balance from account table auto params = codec.encodeWithSig("getAccountBalance()"); - auto tableName = getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); + auto tableName = + getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); std::vector tableNameVector = {tableName}; auto input = codec.encode(tableNameVector, params); bytes_view getBalance = bytes_view(input.data(), input.size()); _msg.input_data = getBalance.data(); _msg.input_size = getBalance.size(); - EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM getbalance") - << LOG_KV("tableName" ,tableName); + EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM getbalance") << LOG_KV("tableName", tableName); evmc_result output = hostContext.externalRequest(&_msg); bytes resultBytes = bytes(output.output_data, output.output_data + output.output_size); u256 result; codec.decode(ref(resultBytes), result); - EXECUTIVE_LOG(DEBUG) << LOG_DESC("EVM getbalance successful") - << LOG_KV("balance result" , result); + EXECUTIVE_LOG(DEBUG) << LOG_DESC("EVM getbalance successful") + << LOG_KV("balance result", result); return toEvmC(result); } else @@ -192,18 +192,21 @@ bool selfdestruct(evmc_host_context* _context, const evmc_address* _addr, evmc_message _msg; _msg.kind = evmc_call_kind::EVMC_CALL; _msg.code_address = unhexAddress(bcos::precompiled::ACCOUNT_ADDRESS); - _msg.sender = unhexAddress(bcos::precompiled::EVM_BALANCE_SENDER_ADDRESS); //std::string(EVM_BALANCE_SENDER_ADDRESS) + _msg.sender = unhexAddress(bcos::precompiled:: + EVM_BALANCE_SENDER_ADDRESS); // std::string(EVM_BALANCE_SENDER_ADDRESS) _msg.recipient = *_addr; _msg.gas = hostContext.gas(); _msg.value = toEvmC(h256(0)); // get _addr balance const auto& codec = bcos::CodecWrapper(blockContext.hashHandler(), blockContext.isWasm()); bytes params = codec.encodeWithSig("getAccountBalance()"); - auto tableName = getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); + auto tableName = + getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); std::vector tableNameVector = {tableName}; auto getBalanceIn = codec.encode(tableNameVector, params); - EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM selfdestruct, first getBalance") << LOG_KV("tableName", tableName); + EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM selfdestruct, first getBalance") + << LOG_KV("tableName", tableName); bytes_view getBalance = bytes_view(getBalanceIn.data(), getBalanceIn.size()); _msg.input_data = getBalance.data(); _msg.input_size = getBalance.size(); @@ -213,15 +216,15 @@ bool selfdestruct(evmc_host_context* _context, const evmc_address* _addr, codec.decode(ref(resultBytes), balance); EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM selfdestruct, second subBalance, set to 0 ") - << LOG_KV("tableName", tableName) - << LOG_KV("will sub balance", balance); + << LOG_KV("tableName", tableName) + << LOG_KV("will sub balance", balance); // _addr -= balance, set to 0; bytes subParams = codec.encodeWithSig("subAccountBalance(uint256)", balance); auto subBalanceIn = codec.encode(tableNameVector, subParams); bytes_view subBalance = bytes_view(subBalanceIn.data(), subBalanceIn.size()); _msg.input_data = subBalance.data(); _msg.input_size = subBalance.size(); - _msg.value = toEvmC(h256(0)); + _msg.value = toEvmC(h256(0)); output = hostContext.externalRequest(&_msg); resultBytes = bytes(output.output_data, output.output_data + output.output_size); s256 value; @@ -229,29 +232,32 @@ bool selfdestruct(evmc_host_context* _context, const evmc_address* _addr, if (value != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS)) { EXECUTIVE_LOG(DEBUG) << "EVM subAccountBalance failed"; - BOOST_THROW_EXCEPTION(bcos::protocol::PrecompiledError("selfdestruct ,call AccountPrecompiled to subAccountBalance(uint256) failed")); + BOOST_THROW_EXCEPTION(bcos::protocol::PrecompiledError( + "selfdestruct ,call AccountPrecompiled to subAccountBalance(uint256) failed")); } // _beneficiary += balance _msg.recipient = *_beneficiary; bytes addParams = codec.encodeWithSig("addAccountBalance(uint256)", balance); - auto addTableName = getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); + auto addTableName = + getContractTableName(executor::USER_APPS_PREFIX, address2HexString(_msg.recipient)); std::vector addTableNameVector = {addTableName}; auto addBalanceIn = codec.encode(addTableNameVector, addParams); bytes_view addBalance = bytes_view(addBalanceIn.data(), addBalanceIn.size()); _msg.input_data = addBalance.data(); _msg.input_size = addBalance.size(); EXECUTIVE_LOG(TRACE) << LOG_DESC("EVM selfdestruct, then addBalance") - << LOG_KV("addTableName", addTableName) - << LOG_KV("will add balance", balance); - + << LOG_KV("addTableName", addTableName) + << LOG_KV("will add balance", balance); + output = hostContext.externalRequest(&_msg); resultBytes = bytes(output.output_data, output.output_data + output.output_size); codec.decode(ref(resultBytes), value); if (value != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS)) { EXECUTIVE_LOG(DEBUG) << "EVM addAccountBalance failed"; - BOOST_THROW_EXCEPTION(bcos::protocol::PrecompiledError("selfdestruct ,call AccountPrecompiled to addAccountBalance(uint256) failed")); + BOOST_THROW_EXCEPTION(bcos::protocol::PrecompiledError( + "selfdestruct ,call AccountPrecompiled to addAccountBalance(uint256) failed")); } } EXECUTIVE_LOG(DEBUG) << "selfdestruct successful"; diff --git a/bcos-executor/src/vm/HostContext.cpp b/bcos-executor/src/vm/HostContext.cpp index 616f5b4cbf..3b544357f0 100644 --- a/bcos-executor/src/vm/HostContext.cpp +++ b/bcos-executor/src/vm/HostContext.cpp @@ -130,7 +130,8 @@ evmc_result HostContext::externalRequest(const evmc_message* _msg) request->value = fromEvmC(_msg->value); const auto& blockContext = m_executive->blockContext(); if (blockContext.features().get(ledger::Features::Flag::feature_balance) && - evmAddress2String(_msg->sender) == std::string(bcos::precompiled::EVM_BALANCE_SENDER_ADDRESS)) + evmAddress2String(_msg->sender) == + std::string(bcos::precompiled::EVM_BALANCE_SENDER_ADDRESS)) { // for AccountPrecompiled to sub and add request->senderAddress = std::string(bcos::precompiled::EVM_BALANCE_SENDER_ADDRESS); @@ -157,7 +158,7 @@ evmc_result HostContext::externalRequest(const evmc_message* _msg) } else { - request->receiveAddress = evmAddress2String(_msg->code_address); + request->receiveAddress = evmAddress2String(_msg->code_address); } request->codeAddress = request->receiveAddress; diff --git a/bcos-executor/src/vm/HostContext.h b/bcos-executor/src/vm/HostContext.h index 239535c0a2..8c97e9cdf9 100644 --- a/bcos-executor/src/vm/HostContext.h +++ b/bcos-executor/src/vm/HostContext.h @@ -161,7 +161,10 @@ class HostContext : public evmc_host_context static crypto::Hash::Ptr& hashImpl() { return GlobalHashImpl::g_hashImpl; } bool isWasm(); - const std::shared_ptr& getTransactionExecutive() const { return m_executive; } + const std::shared_ptr& getTransactionExecutive() const + { + return m_executive; + } bcos::bytes codeAt(const std::string_view& address) { return externalCodeRequest(address); } const bcos::ledger::Features& features() const diff --git a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp index d9488691a8..f0e54e0eb5 100644 --- a/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp +++ b/bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp @@ -276,6 +276,7 @@ void bcos::rpc::toJsonResp(Json::Value& jResp, std::string_view _txHash, { jResp["hash"] = "0x"; } + jResp["effectiveGasPrice"] = std::string(transactionReceipt.effectiveGasPrice()); jResp["logEntries"] = Json::Value(Json::arrayValue); for (const auto& logEntry : transactionReceipt.logEntries()) diff --git a/bcos-sdk/bcos-cpp-sdk/Sdk.h b/bcos-sdk/bcos-cpp-sdk/Sdk.h index 649bdb050f..489b985b64 100644 --- a/bcos-sdk/bcos-cpp-sdk/Sdk.h +++ b/bcos-sdk/bcos-cpp-sdk/Sdk.h @@ -123,7 +123,7 @@ class Sdk [[nodiscard]] bcos::cppsdk::amop::AMOP::Ptr amop() const { return m_amop; } [[nodiscard]] bcos::cppsdk::event::EventSub::Ptr eventSub() const { return m_eventSub; } - uint64_t localProtocolInfo() const { return m_service->localProtocolInfo(); } + uint32_t localProtocolInfo() const { return m_service->localProtocolInfo(); } uint32_t negotiatedProtocolInfo() { return m_service->negotiatedProtocolInfo(); } }; diff --git a/bcos-sdk/bcos-cpp-sdk/utilities/receipt/TransactionReceipt.h b/bcos-sdk/bcos-cpp-sdk/utilities/receipt/TransactionReceipt.h index e20a84f981..1ebd2f4ecf 100644 --- a/bcos-sdk/bcos-cpp-sdk/utilities/receipt/TransactionReceipt.h +++ b/bcos-sdk/bcos-cpp-sdk/utilities/receipt/TransactionReceipt.h @@ -7,6 +7,7 @@ #include "bcos-cpp-sdk/utilities/tx/tars/tup/Tars.h" #include "bcos-cpp-sdk/utilities/tx/tars/tup/TarsJson.h" #include +#include #include #include #include @@ -284,7 +285,7 @@ struct TransactionReceiptData : public tars::TarsStructBase hasher.update(bcos::bytesConstRef((bcos::byte*)output.data(), output.size())); // FIXME: use TarsHashable to calculate hash - if (version == 1) + if (version == (uint32_t)bcos::protocol::TransactionVersion::V1_VERSION) { // effectiveGasPrice hasher.update(bcos::bytesConstRef( diff --git a/bcos-sdk/bcos-cpp-sdk/utilities/tx/Transaction.h b/bcos-sdk/bcos-cpp-sdk/utilities/tx/Transaction.h index 8709308f8f..4ee6fbc039 100644 --- a/bcos-sdk/bcos-cpp-sdk/utilities/tx/Transaction.h +++ b/bcos-sdk/bcos-cpp-sdk/utilities/tx/Transaction.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -213,7 +214,7 @@ struct TransactionData : public tars::TarsStructBase // encode abi hasher.update(bcos::bytesConstRef((bcos::byte*)abi.data(), abi.size())); - if ((int)version == 1) + if ((uint32_t)version == (uint32_t)bcos::protocol::TransactionVersion::V1_VERSION) { // encode value hasher.update(bcos::bytesConstRef((bcos::byte*)value.data(), value.size())); diff --git a/bcos-sdk/bcos-cpp-sdk/ws/Service.h b/bcos-sdk/bcos-cpp-sdk/ws/Service.h index a93fb13f65..0d507007dc 100644 --- a/bcos-sdk/bcos-cpp-sdk/ws/Service.h +++ b/bcos-sdk/bcos-cpp-sdk/ws/Service.h @@ -134,7 +134,7 @@ class Service : public bcos::boostssl::ws::WsService m_strictConnectVersion = _strictVersion; } - uint64_t localProtocolInfo() const { return m_localProtocol->version(); } + uint32_t localProtocolInfo() const { return m_localProtocol->version(); } uint32_t negotiatedProtocolInfo() { uint16_t maxVersion = 0; diff --git a/bcos-sdk/tests/unittests/tx/TransactionTest.cpp b/bcos-sdk/tests/unittests/tx/TransactionTest.cpp index a09685b5d8..9c3828276d 100644 --- a/bcos-sdk/tests/unittests/tx/TransactionTest.cpp +++ b/bcos-sdk/tests/unittests/tx/TransactionTest.cpp @@ -19,6 +19,7 @@ */ #include #include +#include #include #include @@ -71,6 +72,51 @@ BOOST_AUTO_TEST_CASE(test_transaction) BOOST_CHECK_EQUAL(hash3, "0359a5588c5e9c9dcfd2f4ece850d6f4c41bc88e2c27cc051890f26ef0ef118f"); } +BOOST_AUTO_TEST_CASE(test_transaction_v2) +{ + auto cryptoSuite = + std::make_shared(std::make_shared(), + std::make_shared(), nullptr); + + auto fixedSec1 = h256( + "722ecc9325297d801632938d7d1bd0a922f8db10e86f5953431686ad9e37db04f47aac0468c655e678dd85869e" + "abaab14b034f79df97ccc6e9c7714d574aed89"); + auto sec1 = std::make_shared(fixedSec1.asBytes()); + auto signatureImpl = std::make_shared(); + auto keyPair1 = signatureImpl->createKeyPair(sec1); + TransactionBuilderV2 transactionBuilderV2; + // clang-format off + bcos::bytes input = fromHex(std::string("1a10012606636861696e30360667726f7570304100855628313331343736363932313039343437323233313631323634333933363134333635383636353733317d00010426608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033b0010b2d000020f03b8fdfa96a9b2128a83c22ddc691e6f1d9e0589b7c34088675e33cb059dbb23d000041313563c18509553f1d83747c004f29ec67f1137bb1c3104e8551ecc4c057187609c58a9d0220f02971abcf3f9bc6ba44423de56cb435b20b1fcf5eee4587840b01")); + // clang-format on + auto tx = transactionBuilderV2.decodeTransaction(input); + auto txData = tx->data; + auto hash = transactionBuilderV2.calculateTransactionDataHash(CryptoType::Secp256K1, tx->data); + + + auto hex1 = hash.hex(); + std::string hex = toHex(tx->dataHash); + BOOST_CHECK(hex == hex1); + BOOST_CHECK(hex == "f03b8fdfa96a9b2128a83c22ddc691e6f1d9e0589b7c34088675e33cb059dbb2"); + + bytes txDataInput; + txDataInput.insert(txDataInput.begin(), txData.input.begin(), txData.input.end()); + + auto newTxData = + transactionBuilderV2.createTransactionData(txData.version, txData.groupID, txData.chainID, + txData.to, txData.nonce, txDataInput, txData.abi, txData.blockLimit, txData.value, + txData.gasPrice, txData.gasLimit, txData.maxFeePerGas, txData.maxPriorityFeePerGas); + + auto newHash = transactionBuilderV2.TransactionBuilder::calculateTransactionDataHash( + CryptoType::Secp256K1, *newTxData); + BOOST_CHECK(newHash.hex() == hex); + + auto newHash2 = transactionBuilderV2.calculateTransactionDataHash(CryptoType::Secp256K1, + txData.version, txData.groupID, txData.chainID, txData.to, txData.nonce, txDataInput, + txData.abi, txData.blockLimit, txData.value, txData.gasPrice, txData.gasLimit, + txData.maxFeePerGas, txData.maxPriorityFeePerGas); + BOOST_CHECK(newHash2.hex() == hex); +} + BOOST_AUTO_TEST_CASE(test_receipt) { auto receiptBuilder = std::make_unique(); diff --git a/bcos-tars-protocol/bcos-tars-protocol/impl/TarsHashable.h b/bcos-tars-protocol/bcos-tars-protocol/impl/TarsHashable.h index 6ed5ab9e03..dd4911ba48 100644 --- a/bcos-tars-protocol/bcos-tars-protocol/impl/TarsHashable.h +++ b/bcos-tars-protocol/bcos-tars-protocol/impl/TarsHashable.h @@ -36,11 +36,12 @@ void impl_calculate(bcos::crypto::hasher::Hasher auto hasher, hasher.update(hashFields.abi); // if version == 1, update value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas to // hashBuffer calculate hash - if (version == 1) + if (hashFields.version == (uint32_t)bcos::protocol::TransactionVersion::V1_VERSION) { hasher.update(hashFields.value); hasher.update(hashFields.gasPrice); - hasher.update(hashFields.gasLimit); + int64_t bigEndGasLimit = boost::endian::native_to_big((int64_t)hashFields.gasLimit); + hasher.update(bigEndGasLimit); hasher.update(hashFields.maxFeePerGas); hasher.update(hashFields.maxPriorityFeePerGas); } @@ -58,7 +59,7 @@ 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); - switch (version) + switch (hashFields.version) { case int32_t(bcos::protocol::TransactionVersion::V0_VERSION): {