Skip to content

Commit

Permalink
feat: add bool abi decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
as-iotex authored and as-iotex committed Apr 17, 2022
1 parent 738dfae commit 20f1a58
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/abi/abiDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,10 @@ iotex::ResultCode iotex::abi::decode::decodeAddress(const char data[64], char ou
memcpy(out, pStart, ETH_ADDRESS_C_STRING_SIZE - 1);

return ResultCode::SUCCESS;
}

bool iotex::abi::decode::decodeBool(const char data[64])
{
// Bool is encoded as uint8. With the value of 1 for true and the value of 0 for false.
return data[63] == '1';
}
1 change: 1 addition & 0 deletions src/abi/abiDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ iotex::ResultCode decodeUintGeneric(const char* pData, size_t uintSize, uint64_t
iotex::ResultCode decodeIntGeneric(const char* pData, size_t uintSize, int64_t* out);
iotex::ResultCode decodeString(const char* pData, size_t size, IotexString& out);
iotex::ResultCode decodeAddress(const char data[64], char out[ETH_ADDRESS_C_STRING_SIZE]);
bool decodeBool(const char data[64]);

template<uint8_t size>
ResultCode decodeUint(const char* pData, size_t dataSize, uint64_t* out)
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set (IOTEX_UNIT_TESTS_SOURCES
src/abi/decode/abiDecodeIntTests.cpp
src/abi/decode/abiDecodeStringTests.cpp
src/abi/decode/abiDecodeAddressTests.cpp
src/abi/decode/abiDecodeBoolTests.cpp
src/contract/contractTests.cpp
src/contract/xrc20ContractTests.cpp
src/account/accountTests_Execution.cpp
Expand Down
32 changes: 32 additions & 0 deletions tests/src/abi/decode/abiDecodeBoolTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "abi/abiDecode.h"
#include "contract/contract.h"
#include "signer/signer.h"

using namespace std;
using namespace testing;
using namespace iotex;
using namespace iotex::abi::decode;

class AbiDecodeBoolTests : public Test
{
void SetUp() override {}

void TearDown() override {}
};

TEST_F(AbiDecodeBoolTests, DecodesFalse)
{
const char encoded[] = "0000000000000000000000000000000000000000000000000000000000000000";
bool decoded = decodeBool(encoded);
ASSERT_EQ(false, decoded);
}

TEST_F(AbiDecodeBoolTests, DecodesTrue)
{
const char encoded[] = "0000000000000000000000000000000000000000000000000000000000000001";
bool decoded = decodeBool(encoded);
ASSERT_EQ(true, decoded);
}

0 comments on commit 20f1a58

Please sign in to comment.