Skip to content

Commit

Permalink
Merge pull request #255 from AntelopeIO/hotstuff_integration
Browse files Browse the repository at this point in the history
Hotstuff integration
  • Loading branch information
arhag authored Apr 30, 2024
2 parents 26289ff + 4eee901 commit 4911e5f
Show file tree
Hide file tree
Showing 21 changed files with 1,056 additions and 281 deletions.
2 changes: 1 addition & 1 deletion .cicd/defaults.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"leap-dev":{
"target":"main",
"target":"hotstuff_integration",
"prerelease":false
}
}
2 changes: 2 additions & 0 deletions libraries/eosiolib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ file(GLOB HEADERS "*.hpp"
add_library(eosio
eosiolib.cpp
crypto.cpp
base64.cpp
${HEADERS})

add_library(eosio_malloc
Expand Down Expand Up @@ -40,6 +41,7 @@ if (ENABLE_NATIVE_COMPILER)
eosiolib.cpp
crypto.cpp
malloc.cpp
base64.cpp
${HEADERS})

add_dependencies( native_eosio eosio )
Expand Down
189 changes: 189 additions & 0 deletions libraries/eosiolib/base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include "core/eosio/base64.hpp"

namespace eosio::detail {

unsigned int pos_of_char(const unsigned char chr, bool url) {
// Return the position of chr within base64_encode()
constexpr unsigned char from_base64_chars[2][256] = {
{ // for base64
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
},
{ // for base64url
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 63,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
}};

auto c = from_base64_chars[url][chr];
eosio::check(c != 64, "encountered non-base64 character");

return c;
}

std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) {

const size_t len_encoded = (in_len +2) / 3 * 4;
const unsigned char trailing_char = '=';

// Includes performance improvement from unmerged PR: https://github.com/ReneNyffenegger/cpp-base64/pull/27

// Depending on the url parameter in base64_chars, one of
// two sets of base64 characters needs to be chosen.
// They differ in their last two characters.
const char* base64_chars[2] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-_"};

// Choose set of base64 characters. They differ
// for the last two positions, depending on the url
// parameter.
// A bool (as is the parameter url) is guaranteed
// to evaluate to either 0 or 1 in C++ therefore,
// the correct character set is chosen by subscripting
// base64_chars with url.
const char* base64_chars_ = base64_chars[url];

std::string ret;
ret.reserve(len_encoded);

unsigned int pos = 0;

while (pos < in_len) {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]);

if (pos+1 < in_len) {
ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + ((bytes_to_encode[pos + 1] & 0xf0) >> 4)]);

if (pos+2 < in_len) {
ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + ((bytes_to_encode[pos + 2] & 0xc0) >> 6)]);
ret.push_back(base64_chars_[ bytes_to_encode[pos + 2] & 0x3f]);
}
else {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]);
if (!url) ret.push_back(trailing_char);
}
}
else {

ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]);
if (!url) ret.push_back(trailing_char);
if (!url) ret.push_back(trailing_char);
}

pos += 3;
}


return ret;
}

std::string base64_decode(std::string_view encoded_string, bool remove_linebreaks, bool url) {
if (encoded_string.empty()) return std::string{};

if (remove_linebreaks) {

std::string copy(encoded_string);

copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end());

return base64_decode(copy, false, url);
}

size_t length_of_string = encoded_string.length();
size_t pos = 0;

//
// The approximate length (bytes) of the decoded string might be one or
// two bytes smaller, depending on the amount of trailing equal signs
// in the encoded string. This approximation is needed to reserve
// enough space in the string to be returned.
//
size_t approx_length_of_decoded_string = length_of_string / 4 * 3;
std::string ret;
ret.reserve(approx_length_of_decoded_string);

while (pos < length_of_string) {
//
// Iterate over encoded input string in chunks. The size of all
// chunks except the last one is 4 bytes.
//
// The last chunk might be padded with equal signs or dots
// in order to make it 4 bytes in size as well, but this
// is not required as per RFC 2045.
//
// All chunks except the last one produce three output bytes.
//
// The last chunk produces at least one and up to three bytes.
//
eosio::check(pos+1 < length_of_string, "wrong encoded string size");
size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1), url);

//
// Emit the first output byte that is produced in each chunk:
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string.at(pos+0), url) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4)));

if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045)
encoded_string.at(pos+2) != '=' &&
encoded_string.at(pos+2) != '.' // accept URL-safe base 64 strings, too, so check for '.' also.
)
{
//
// Emit a chunk's second byte (which might not be produced in the last chunk).
//
unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2), url);
ret.push_back(static_cast<std::string::value_type>( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2)));

if ( ( pos + 3 < length_of_string ) &&
encoded_string.at(pos+3) != '=' &&
encoded_string.at(pos+3) != '.'
)
{
//
// Emit a chunk's third byte (which might not be produced in the last chunk).
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3), url)));
}
}

pos += 4;
}

return ret;
}

}//eosio::detail
26 changes: 26 additions & 0 deletions libraries/eosiolib/capi/eosio/instant_finality.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "types.h"
#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup instant_finality_c Instant_finality C API
* @ingroup c_api
* @brief Defines %C Instant_finality API
*/

/**
* Submits a finalizer policy change to Instant Finality
*
* @param data - pointer finalizer_policy object packed as bytes
* @param len - size of packed finalazer_policy object
* @pre `data` is a valid pointer to a range of memory at least `len` bytes long that contains packed abi_finalizer_policy data
* abi_finalizer_policy structure is defined in instant_finality.hpp
*/
__attribute__((eosio_wasm_import))
void set_finalizers( const char* data, uint32_t len );

#ifdef __cplusplus
}
#endif
50 changes: 50 additions & 0 deletions libraries/eosiolib/contracts/eosio/instant_finality.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include "../../capi/eosio/types.h"
#include "../../core/eosio/crypto_bls_ext.hpp"

#include <string>
#include <vector>

/**
* @defgroup instant_finality Instant_finality
* @ingroup instant_finality
* @ingroup contracts
* @brief Defines C++ Instant Finality API
*/

namespace eosio {
namespace internal_use_do_not_use {
extern "C" {
__attribute__((eosio_wasm_import))
void set_finalizers( const char* data, uint32_t len );
} // extern "C"
} //internal_use_do_not_use

struct finalizer_authority {
std::string description;
uint64_t weight = 0; // weight that this finalizer's vote has for meeting threshold
std::vector<char> public_key; // Affine little endian non-montgomery g1

EOSLIB_SERIALIZE(finalizer_authority, (description)(weight)(public_key));
};
struct finalizer_policy {
uint64_t threshold = 0;
std::vector<finalizer_authority> finalizers;

EOSLIB_SERIALIZE(finalizer_policy, (threshold)(finalizers));
};

/**
* Submits a finalizer policy change to Instant Finality
*
* @param finalizer_policy - finalizer policy to be set
*/
inline void set_finalizers( const finalizer_policy& finalizer_policy ) {
for (const auto& finalizer : finalizer_policy.finalizers)
eosio::check(finalizer.public_key.size() == sizeof(bls_g1), "public key has a wrong size" );
auto packed = eosio::pack(finalizer_policy);
internal_use_do_not_use::set_finalizers(packed.data(), packed.size());
}

} //eosio
59 changes: 59 additions & 0 deletions libraries/eosiolib/core/eosio/base64.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
base64 encoding and decoding with C++.
More information at
https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
https://github.com/ReneNyffenegger/cpp-base64
Version: 2.rc.09 (release candidate)
Copyright (C) 2004-2017, 2020-2022 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/

#include "check.hpp"

#include <algorithm>
#include <string_view>

namespace eosio {

namespace detail {
std::string base64_encode(unsigned char const*, size_t len, bool url = false);
std::string base64_decode(std::string_view s, bool remove_linebreaks = false, bool url = false);

} // detail namespace

inline std::string base64_encode(std::string_view enc) {
return detail::base64_encode(reinterpret_cast<const unsigned char*>(enc.data()), enc.size(), false);
}
inline std::string base64_decode(std::string_view encoded_string) {
return detail::base64_decode(encoded_string, false);
}
inline std::string base64url_encode(std::string_view enc) {
return detail::base64_encode(reinterpret_cast<const unsigned char*>(enc.data()), enc.size(), true);
}
inline std::string base64url_decode(std::string_view encoded_string) {
return detail::base64_decode(encoded_string, true, true);
}

} // namespace eosio
Loading

0 comments on commit 4911e5f

Please # to comment.