Skip to content

Commit

Permalink
<feat>(sdk): add new v2 transaction builder, add windows cpp sdk buil…
Browse files Browse the repository at this point in the history
…der.
  • Loading branch information
kyonRay committed Nov 14, 2023
1 parent 5d9163d commit 2e7d04b
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 39 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
name: Others CI check
on:
push:
paths-ignore:
- "docs/**"
- "Changelog.md"
- "README.md"
pull_request:
paths-ignore:
- "docs/**"
- "Changelog.md"
- "README.md"
release:
types: [published, push]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build_cppsdk_with_windows:
name: build_cppsdk_with_windows
runs-on: ${{ matrix.os }}
env:
VCPKG_ROOT: c:/vcpkg
strategy:
matrix:
os: [ windows-2019 ]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 5
- uses: actions/cache@v2
id: cache
with:
path: |
deps/
c:/vcpkg
c:/vcpkg/buildtrees
c:/vcpkg/packages
c:/vcpkg/downloads
ccache
key: hunter-msvc-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }}
restore-keys: |
hunter-msvc-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }}
- name: update vcpkg
run: |
cd ${{ env.VCPKG_ROOT }} && git fetch --all && git checkout master && git pull
cd -
- name: Add MSbuild to PATH
uses: microsoft/[email protected]
- name: configure
run: mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DALLOCATOR=default -DTESTS=OFF -DFULLNODE=OFF -DWITH_LIGHTNODE=OFF -DWITH_TARS_SERVICES=OFF -DWITH_CPPSDK=ON -DWITH_WASM=OFF -DWITH_TIKV=OFF -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake ../ && cmake --build . --parallel 3
- name: compile
run: cd build && MSBuild bcos-cpp-sdk.sln /p:Configuration=Release /p:Platform=x64
#name: Others CI check
#on:
# push:
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ add_subdirectory(bcos-utilities)
add_subdirectory(concepts)
add_subdirectory(libtask)
add_subdirectory(bcos-protocol)
add_subdirectory(bcos-tars-protocol)
if(NOT ONLY_CPP_SDK AND NOT WITH_SWIG_SDK)
add_subdirectory(bcos-tars-protocol)
endif ()
add_subdirectory(bcos-codec)
include(ProjectITTAPI)
include(ProjectSDF)
Expand Down
4 changes: 3 additions & 1 deletion bcos-crypto/bcos-crypto/hasher/AnyHasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Hasher.h"
#include "bcos-crypto/TrivialObject.h"
#include <bcos-concepts/ByteBuffer.h>
#include <memory>
#include <span>

namespace bcos::crypto::hasher
Expand Down Expand Up @@ -75,6 +76,7 @@ class AnyHasher
size_t hashSize() const { return m_anyHasher->hashSize(); }
};

#if !_WIN32 || !_WIN64
static_assert(Hasher<AnyHasher>, "Not a valid Hasher!");

#endif
} // namespace bcos::crypto::hasher
2 changes: 2 additions & 0 deletions bcos-crypto/bcos-crypto/hasher/OpenSSLHasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ using OpenSSL_SHA2_256_Hasher = OpenSSLHasher<SHA2_256>;
using OpenSSL_SM3_Hasher = OpenSSLHasher<SM3>;
using OpenSSL_Keccak256_Hasher = OpenSSLHasher<Keccak256>;

#if !_WIN32 || !_WIN64
static_assert(Hasher<OpenSSL_SHA3_256_Hasher>, "Assert OpenSSLHasher type");
static_assert(Hasher<OpenSSL_SHA2_256_Hasher>, "Assert OpenSSLHasher type");
static_assert(Hasher<OpenSSL_SM3_Hasher>, "Assert OpenSSLHasher type");
static_assert(Hasher<OpenSSL_Keccak256_Hasher>, "Assert OpenSSLHasher type");
#endif

} // namespace bcos::crypto::hasher::openssl
4 changes: 4 additions & 0 deletions bcos-framework/bcos-framework/protocol/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <bcos-crypto/interfaces/crypto/KeyInterface.h>
#include <bcos-utilities/Common.h>
#include <bcos-utilities/Error.h>
#if !ONLY_CPP_SDK
#include <bcos-utilities/ITTAPI.h>
#endif
#include <boost/throw_exception.hpp>
#include <concepts>
#include <shared_mutex>
Expand Down Expand Up @@ -69,8 +71,10 @@ class Transaction

virtual void verify(crypto::Hash& hashImpl, crypto::SignatureCrypto& signatureImpl) const
{
#if !ONLY_CPP_SDK
ittapi::Report report(ittapi::ITT_DOMAINS::instance().TRANSACTION,
ittapi::ITT_DOMAINS::instance().VERIFY_TRANSACTION);
#endif
// The tx has already been verified
if (!sender().empty())
{
Expand Down
19 changes: 10 additions & 9 deletions bcos-sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
file(GLOB_RECURSE SRC_LIST "bcos-cpp-sdk/*.cpp")
list(APPEND LINK_LIB_LIST bcos-crypto bcos-boostssl bcos-utilities jsoncpp_static OpenSSL::SSL OpenSSL::Crypto)
if (ONLY_CPP_SDK AND (NOT WITH_SWIG_SDK))
list(REMOVE_ITEM SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/bcos-cpp-sdk/tarsRPC/CoRPCClient.cpp")
list(REMOVE_ITEM SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/bcos-cpp-sdk/tarsRPC/RPCClient.cpp")
list(REMOVE_ITEM SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/bcos-cpp-sdk/tarsRPC/detail/Core.cpp")
else ()
list(APPEND LINK_LIB_LIST ${TARS_PROTOCOL_TARGET})
find_package(range-v3 REQUIRED)
endif ()

find_package(Boost REQUIRED log serialization)
find_package(wedprcrypto REQUIRED)
Expand All @@ -12,15 +21,7 @@ add_library(${BCOS_CPP_SDK_TARGET} ${SRC_LIST})
target_include_directories(${BCOS_CPP_SDK_TARGET} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/bcos-cpp-sdk>)
target_link_libraries(${BCOS_CPP_SDK_TARGET} PUBLIC
wedprcrypto::crypto
${TARS_PROTOCOL_TARGET}
bcos-crypto
bcos-boostssl
bcos-utilities
jsoncpp_static
OpenSSL::SSL
OpenSSL::Crypto)
target_link_libraries(${BCOS_CPP_SDK_TARGET} PUBLIC ${LINK_LIB_LIST})

if (TESTS)
enable_testing()
Expand Down
8 changes: 8 additions & 0 deletions bcos-sdk/bcos-cpp-sdk/utilities/receipt/ReceiptBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ bcostars::ReceiptDataUniquePtr bcos::cppsdk::utilities::ReceiptBuilder::createRe
return _receipt;
}

bcos::crypto::HashType bcos::cppsdk::utilities::ReceiptBuilder::calculateReceiptDataHashWithJson(
bcos::cppsdk::utilities::CryptoType _cryptoType, const std::string& _json)
{
auto _receipt = std::make_unique<bcostars::TransactionReceiptData>();
_receipt->readFromJsonString(_json);
return calculateReceiptDataHash(_cryptoType, *_receipt);
}

bcos::crypto::HashType bcos::cppsdk::utilities::ReceiptBuilder::calculateReceiptDataHash(
bcos::cppsdk::utilities::CryptoType _cryptoType,
const bcostars::TransactionReceiptData& _receiptData)
Expand Down
4 changes: 4 additions & 0 deletions bcos-sdk/bcos-cpp-sdk/utilities/receipt/ReceiptBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ReceiptBuilder : public ReceiptBuilderInterface
* @return bcostars::TransactionDataUniquePtr
*/
bcostars::ReceiptDataUniquePtr createReceiptDataWithJson(const std::string& _json) override;

crypto::HashType calculateReceiptDataHashWithJson(
bcos::cppsdk::utilities::CryptoType _cryptoType, const std::string& _json) override;

crypto::HashType calculateReceiptDataHash(
CryptoType _cryptoType, const bcostars::TransactionReceiptData& _receiptData) override;
bytesConstPtr encodeReceipt(const bcostars::TransactionReceiptData& _receipt) override;
Expand Down
10 changes: 10 additions & 0 deletions bcos-sdk/bcos-cpp-sdk/utilities/receipt/ReceiptBuilderInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class ReceiptBuilderInterface
*/
virtual bcostars::ReceiptDataUniquePtr createReceiptDataWithJson(const std::string& _json) = 0;

/**
* @brief calculate receipt data hash with json
*
* @param _cryptoType
* @param _json
* @return crypto::HashType
*/
virtual crypto::HashType calculateReceiptDataHashWithJson(
bcos::cppsdk::utilities::CryptoType _cryptoType, const std::string& _json) = 0;

/**
* @brief
*
Expand Down
86 changes: 84 additions & 2 deletions bcos-sdk/bcos-cpp-sdk/utilities/tx/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct TransactionData : public tars::TarsStructBase
{
public:
static std::string className() { return "bcostars.TransactionData"; }
static std::string MD5() { return "ea41d47be6b852a5c3edcfe7a805be85"; }
static std::string MD5() { return "8147ae64a9fd777c7d425e7796bf503b"; }
TransactionData() { resetDefault(); }
void resetDefault()
{
Expand All @@ -36,6 +36,11 @@ struct TransactionData : public tars::TarsStructBase
to = "";
input.clear();
abi = "";
value = "";
gasPrice = "";
gasLimit = 0;
maxFeePerGas = "";
maxPriorityFeePerGas = "";
}
template <typename WriterT>
void writeTo(tars::TarsOutputStream<WriterT>& _os) const
Expand All @@ -54,6 +59,26 @@ struct TransactionData : public tars::TarsStructBase
{
_os.write(abi, 8);
}
if (value != "")
{
_os.write(value, 9);
}
if (gasPrice != "")
{
_os.write(gasPrice, 10);
}
if (gasLimit != 0)
{
_os.write(gasLimit, 11);
}
if (maxFeePerGas != "")
{
_os.write(maxFeePerGas, 12);
}
if (maxPriorityFeePerGas != "")
{
_os.write(maxPriorityFeePerGas, 13);
}
}
template <typename ReaderT>
void readFrom(tars::TarsInputStream<ReaderT>& _is)
Expand All @@ -67,6 +92,11 @@ struct TransactionData : public tars::TarsStructBase
_is.read(to, 6, false);
_is.read(input, 7, true);
_is.read(abi, 8, false);
_is.read(value, 9, false);
_is.read(gasPrice, 10, false);
_is.read(gasLimit, 11, false);
_is.read(maxFeePerGas, 12, false);
_is.read(maxPriorityFeePerGas, 13, false);
}
tars::JsonValueObjPtr writeToJson() const
{
Expand All @@ -79,6 +109,14 @@ struct TransactionData : public tars::TarsStructBase
p->value["to"] = tars::JsonOutput::writeJson(to);
p->value["input"] = tars::JsonOutput::writeJson(bcos::toHexStringWithPrefix(input));
p->value["abi"] = tars::JsonOutput::writeJson(abi);
if ((int)version == 1)
{
p->value["value"] = tars::JsonOutput::writeJson(value);
p->value["gasPrice"] = tars::JsonOutput::writeJson(gasPrice);
p->value["gasLimit"] = tars::JsonOutput::writeJson(gasLimit);
p->value["maxFeePerGas"] = tars::JsonOutput::writeJson(maxFeePerGas);
p->value["maxPriorityFeePerGas"] = tars::JsonOutput::writeJson(maxPriorityFeePerGas);
}
return p;
}
std::string writeToJsonString() const { return tars::TC_Json::writeValue(writeToJson()); }
Expand All @@ -104,6 +142,15 @@ struct TransactionData : public tars::TarsStructBase
auto inputBytes = bcos::fromHexString(inputHex);
std::copy(inputBytes->begin(), inputBytes->end(), std::back_inserter(input));
tars::JsonInput::readJson(abi, pObj->value["abi"], false);
if ((int)version == 1)
{
tars::JsonInput::readJson(value, pObj->value["value"], false);
tars::JsonInput::readJson(gasPrice, pObj->value["gasPrice"], false);
tars::JsonInput::readJson(gasLimit, pObj->value["gasLimit"], false);
tars::JsonInput::readJson(maxFeePerGas, pObj->value["maxFeePerGas"], false);
tars::JsonInput::readJson(
maxPriorityFeePerGas, pObj->value["maxPriorityFeePerGas"], 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 @@ -117,6 +164,11 @@ struct TransactionData : public tars::TarsStructBase
_ds.display(to, "to");
_ds.display(input, "input");
_ds.display(abi, "abi");
_ds.display(value, "value");
_ds.display(gasPrice, "gasPrice");
_ds.display(gasLimit, "gasLimit");
_ds.display(maxFeePerGas, "maxFeePerGas");
_ds.display(maxPriorityFeePerGas, "maxPriorityFeePerGas");
return _os;
}
std::ostream& displaySimple(std::ostream& _os, int _level = 0) const
Expand All @@ -130,6 +182,11 @@ struct TransactionData : public tars::TarsStructBase
_ds.displaySimple(to, true);
_ds.displaySimple(input, true);
_ds.displaySimple(abi, false);
_ds.displaySimple(value, true);
_ds.displaySimple(gasPrice, true);
_ds.displaySimple(gasLimit, true);
_ds.displaySimple(maxFeePerGas, true);
_ds.displaySimple(maxPriorityFeePerGas, false);
return _os;
}

Expand All @@ -156,6 +213,24 @@ struct TransactionData : public tars::TarsStructBase
// encode abi
hasher.update(bcos::bytesConstRef((bcos::byte*)abi.data(), abi.size()));

if ((int)version == 1)
{
// encode value
hasher.update(bcos::bytesConstRef((bcos::byte*)value.data(), value.size()));
// encode gasPrice
hasher.update(bcos::bytesConstRef((bcos::byte*)gasPrice.data(), gasPrice.size()));
// encode gasLimit
int64_t networkGasLimit = boost::endian::native_to_big((int64_t)gasLimit);
hasher.update(bcos::bytesConstRef(
(bcos::byte*)(&networkGasLimit), sizeof(networkGasLimit) / sizeof(uint8_t)));
// encode maxFeePerGas
hasher.update(
bcos::bytesConstRef((bcos::byte*)maxFeePerGas.data(), maxFeePerGas.size()));
// encode maxPriorityFeePerGas
hasher.update(bcos::bytesConstRef(
(bcos::byte*)maxPriorityFeePerGas.data(), maxPriorityFeePerGas.size()));
}

hasher.final(hashResult);

return hashResult;
Expand All @@ -170,12 +245,19 @@ struct TransactionData : public tars::TarsStructBase
std::string to;
std::vector<tars::Char> input;
std::string abi;
std::string value;
std::string gasPrice;
tars::Int64 gasLimit;
std::string maxFeePerGas;
std::string maxPriorityFeePerGas;
};
inline bool operator==(const TransactionData& l, const TransactionData& r)
{
return l.version == r.version && l.chainID == r.chainID && l.groupID == r.groupID &&
l.blockLimit == r.blockLimit && l.nonce == r.nonce && l.to == r.to &&
l.input == r.input && l.abi == r.abi;
l.input == r.input && l.abi == r.abi && l.value == r.value && l.gasPrice == r.gasPrice &&
l.gasLimit == r.gasLimit && l.maxFeePerGas == r.maxFeePerGas &&
l.maxPriorityFeePerGas == r.maxPriorityFeePerGas;
}
inline bool operator!=(const TransactionData& l, const TransactionData& r)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once
#include <bcos-cpp-sdk/Sdk.h>
#include <bcos-cpp-sdk/utilities/tx/TransactionBuilder.h>
//#include <bcos-cpp-sdk/utilities/tx/TransactionBuilderV2.h>
#include <bcos-cpp-sdk/ws/Service.h>
#include <bcos-utilities/Common.h>
#include <cstddef>
Expand Down
Loading

0 comments on commit 2e7d04b

Please sign in to comment.