From 1ce8f281052eb2993cadc6e3397d50214f103393 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sun, 8 Jan 2023 16:35:03 +0100 Subject: [PATCH] Added sidechain extra --- src/block_template.cpp | 1 - src/keccak.cpp | 20 ++------------------ src/keccak.h | 20 +++++++++++++------- src/pool_block.cpp | 6 ++++++ src/pool_block.h | 5 ++++- src/pool_block_parser.inl | 5 +++++ tests/src/block_template_tests.cpp | 4 ++-- 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index fc833d7..5c7bff7 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -575,7 +575,6 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_poolBlockTemplate->m_minerWallet = *miner_wallet; - m_poolBlockTemplate->serialize_sidechain_data(); m_poolBlockTemplate->m_sidechainId = calc_sidechain_hash(); const int sidechain_hash_offset = static_cast(m_extraNonceOffsetInTemplate + m_poolBlockTemplate->m_extraNonceSize) + 2; diff --git a/src/keccak.cpp b/src/keccak.cpp index 9c756cf..082ecd0 100644 --- a/src/keccak.cpp +++ b/src/keccak.cpp @@ -24,7 +24,7 @@ namespace p2pool { #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) #endif -const uint64_t keccakf_rndc[24] = +static const uint64_t keccakf_rndc[24] = { 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, @@ -36,7 +36,7 @@ const uint64_t keccakf_rndc[24] = 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 }; -NOINLINE void keccakf(uint64_t* st) +NOINLINE void keccakf(uint64_t (&st)[25]) { for (int round = 0; round < KeccakParams::ROUNDS; ++round) { uint64_t bc[5]; @@ -150,20 +150,4 @@ NOINLINE void keccak_finish(const uint8_t* in, int inlen, uint64_t (&st)[25]) keccakf(st); } -NOINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[32]) -{ - uint64_t st[25] = {}; - keccak_step(in, inlen, st); - keccak_finish(in, inlen, st); - memcpy(md, st, 32); -} - -NOINLINE void keccak(const uint8_t* in, int inlen, uint8_t(&md)[200]) -{ - uint64_t st[25] = {}; - keccak_step(in, inlen, st); - keccak_finish(in, inlen, st); - memcpy(md, st, sizeof(md)); -} - } // namespace p2pool diff --git a/src/keccak.h b/src/keccak.h index b3a2d33..7274d61 100644 --- a/src/keccak.h +++ b/src/keccak.h @@ -24,23 +24,29 @@ enum KeccakParams { ROUNDS = 24, }; -void keccakf(uint64_t* st); -void keccak(const uint8_t *in, int inlen, uint8_t (&md)[32]); -void keccak(const uint8_t *in, int inlen, uint8_t (&md)[200]); - +void keccakf(uint64_t (&st)[25]); void keccak_step(const uint8_t* &in, int &inlen, uint64_t (&st)[25]); void keccak_finish(const uint8_t* in, int inlen, uint64_t (&st)[25]); +template +FORCEINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[N]) +{ + static_assert((N == 32) || (N == 200), "invalid size"); + + uint64_t st[25] = {}; + keccak_step(in, inlen, st); + keccak_finish(in, inlen, st); + memcpy(md, st, N); +} + template FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen) { - uint64_t st[25]; + uint64_t st[25] = {}; const int rsiz = sizeof(st) == mdlen ? KeccakParams::HASH_DATA_AREA : 200 - 2 * mdlen; const int rsizw = rsiz / 8; - memset(st, 0, sizeof(st)); - int offset = 0; for (; inlen >= rsiz; inlen -= rsiz, offset += rsiz) { diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 218c712..64995c3 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -43,6 +43,7 @@ PoolBlock::PoolBlock() , m_sidechainHeight(0) , m_difficulty{} , m_cumulativeDifficulty{} + , m_sidechainExtraBuf{} , m_sidechainId{} , m_depth(0) , m_verified(false) @@ -96,6 +97,7 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b) m_sidechainHeight = b.m_sidechainHeight; m_difficulty = b.m_difficulty; m_cumulativeDifficulty = b.m_cumulativeDifficulty; + memcpy(m_sidechainExtraBuf, b.m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); m_sidechainId = b.m_sidechainId; m_depth = b.m_depth; m_verified = b.m_verified; @@ -252,6 +254,10 @@ std::vector PoolBlock::serialize_sidechain_data() const writeVarint(m_cumulativeDifficulty.lo, data); writeVarint(m_cumulativeDifficulty.hi, data); + if (get_sidechain_version() > 1) { + data.insert(data.end(), m_sidechainExtraBuf, m_sidechainExtraBuf + sizeof(m_sidechainExtraBuf)); + } + #if POOL_BLOCK_DEBUG if (!m_sideChainDataDebug.empty() && (data != m_sideChainDataDebug)) { LOGERR(1, "serialize_sidechain_data() has a bug, fix it!"); diff --git a/src/pool_block.h b/src/pool_block.h index d0cae76..748c0bf 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -115,6 +115,9 @@ struct PoolBlock difficulty_type m_difficulty; difficulty_type m_cumulativeDifficulty; + // Arbitrary extra data + uint8_t m_sidechainExtraBuf[16]; + // HASH (see diagram in the comment above) hash m_sidechainId; @@ -146,7 +149,7 @@ struct PoolBlock // but P2Pool can switch to using only TXOUT_TO_TAGGED_KEY for miner payouts starting from v15 FORCEINLINE uint8_t get_tx_type() const { return (m_majorVersion < HARDFORK_VIEW_TAGS_VERSION) ? TXOUT_TO_KEY : TXOUT_TO_TAGGED_KEY; } - FORCEINLINE uint8_t get_sidechain_version() const + FORCEINLINE int get_sidechain_version() const { // P2Pool forks to v2 at 2023-03-18 21:00 UTC // Different miners can have different timestamps, diff --git a/src/pool_block_parser.inl b/src/pool_block_parser.inl index 3a7c83d..f0f7e70 100644 --- a/src/pool_block_parser.inl +++ b/src/pool_block_parser.inl @@ -317,6 +317,11 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si READ_VARINT(m_cumulativeDifficulty.lo); READ_VARINT(m_cumulativeDifficulty.hi); + const int sidechain_version = get_sidechain_version(); + if (sidechain_version > 1) { + READ_BUF(m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); + } + #undef READ_BYTE #undef EXPECT_BYTE #undef READ_VARINT diff --git a/tests/src/block_template_tests.cpp b/tests/src/block_template_tests.cpp index 0d92e4a..c250d19 100644 --- a/tests/src/block_template_tests.cpp +++ b/tests/src/block_template_tests.cpp @@ -59,7 +59,7 @@ TEST(block_template, update) tpl.update(data, mempool, &wallet); const PoolBlock* b = tpl.pool_block_template(); - ASSERT_EQ(b->m_sidechainId, H("b708e3e456d97c43a7fcbd7b4e7aa29bdf45cd909bba07f915cb5f1d805433e6")); + ASSERT_EQ(b->m_sidechainId, H("ba055380a4be77577504241e0c912e9492ddcf5cc1a52f665d8f04bf3d68ef3d")); std::vector blobs; uint64_t height; @@ -78,7 +78,7 @@ TEST(block_template, update) hash blobs_hash; keccak(blobs.data(), static_cast(blobs.size()), blobs_hash.h); - ASSERT_EQ(blobs_hash, H("e9154971a27c412175562d23ab458b0d3cf780a8bcecf62ff3f667fed9d3bc1d")); + ASSERT_EQ(blobs_hash, H("a3e18f64b6a45c8f559c9c56d318fe4d31ef95fb52e149e6658e5a22810aabae")); // Test 2: mempool with high fee and low fee transactions, it must choose high fee transactions for (uint64_t i = 0; i < 512; ++i) {