Added sidechain extra

This commit is contained in:
SChernykh 2023-01-08 16:35:03 +01:00
parent a525f34fec
commit 1ce8f28105
7 changed files with 32 additions and 29 deletions

View file

@ -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<int>(m_extraNonceOffsetInTemplate + m_poolBlockTemplate->m_extraNonceSize) + 2;

View file

@ -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

View file

@ -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<size_t N>
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<typename T>
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) {

View file

@ -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<uint8_t> 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!");

View file

@ -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,

View file

@ -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

View file

@ -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<uint8_t> blobs;
uint64_t height;
@ -78,7 +78,7 @@ TEST(block_template, update)
hash blobs_hash;
keccak(blobs.data(), static_cast<int>(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) {