Skip to content

Commit 080b80e

Browse files
author
Anatol Sevostyan
committed
miner: Chain and TxPool interfaces stubs
1 parent 7bf0110 commit 080b80e

File tree

11 files changed

+125
-60
lines changed

11 files changed

+125
-60
lines changed

chain/chain.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace beam
44
{
5+
Chain::Chain()
6+
{
7+
m_blockChain.emplace_back(std::make_unique<Block>());
8+
}
9+
510
const Block& Chain::getHeadBlock() const
611
{
7-
return m_blockChain.back();
12+
return *m_blockChain.back();
813
}
914

10-
15+
void Chain::processBlock(BlockUniquePtr&& block)
16+
{
17+
m_blockChain.emplace_back(std::move(block));
18+
}
1119
};

chain/chain.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ namespace beam
77
class Chain
88
{
99
public:
10+
Chain();
1011
const Block& getHeadBlock() const;
12+
void processBlock(BlockUniquePtr&& block);
1113
private:
1214
// TODO: replace
13-
std::vector<Block> m_blockChain;
15+
std::vector<BlockUniquePtr> m_blockChain;
1416
};
1517
}

core/block.h

+54-26
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,81 @@ namespace beam
99
class BlockHeader
1010
{
1111
public:
12-
// Version of the block
13-
uint16_t version;
12+
// Version of the block
13+
uint16_t version;
1414

15-
// Height of this block since the genesis block (height 0)
16-
uint64_t height;
15+
// Height of this block since the genesis block (height 0)
16+
uint64_t height;
1717

18-
// Timestamp at which the block was built.
19-
Timestamp timestamp;
18+
// Timestamp at which the block was built.
19+
Timestamp timestamp;
2020

21-
// Hash of the block previous to this in the chain.
22-
Hash previous;
21+
// Hash of the block previous to this in the chain.
22+
Hash previous;
2323

24-
// Merklish root of all the commitments in the TxHashSet
25-
// Hash output_root;
24+
// Merklish root of all the commitments in the TxHashSet
25+
// Hash output_root;
2626

27-
// Merklish root of all range proofs in the TxHashSet
28-
// Hash range_proof_root;
27+
// Merklish root of all range proofs in the TxHashSet
28+
// Hash range_proof_root;
2929

30-
// Merklish root of all transaction kernels in the TxHashSet
31-
// Hash kernel_root;
30+
// Merklish root of all transaction kernels in the TxHashSet
31+
// Hash kernel_root;
3232

33-
// Total accumulated difficulty since genesis block
34-
Difficulty total_difficulty;
33+
// Total accumulated difficulty since genesis block
34+
Difficulty total_difficulty;
3535

36-
// Total accumulated sum of kernel offsets since genesis block.
37-
// We can derive the kernel offset sum for *this* block from
38-
// the total kernel offset of the previous block header.
39-
// total_kernel_offset: BlindingFactor,
36+
// Total accumulated sum of kernel offsets since genesis block.
37+
// We can derive the kernel offset sum for *this* block from
38+
// the total kernel offset of the previous block header.
39+
// total_kernel_offset: BlindingFactor,
4040

41-
// Proof of work data.
42-
Proof pow;
41+
// Proof of work data.
42+
Proof pow;
43+
44+
BlockHeader(){}
45+
46+
BlockHeader(const BlockHeader& other) = delete;
47+
BlockHeader(BlockHeader&& other) = delete;
48+
BlockHeader& operator=(const BlockHeader&) = delete;
49+
BlockHeader& operator=(BlockHeader&&) = delete;
50+
51+
template<typename Buffer>
52+
void serializeTo(Buffer& b)
53+
{
54+
55+
}
4356
};
4457

4558
class Block
4659
{
4760
public:
48-
// The header with metadata and commitments to the rest of the data
61+
// The header with metadata and commitments to the rest of the data
4962
BlockHeader header;
5063

51-
// List of transaction inputs
64+
// List of transaction inputs
5265
Inputs inputs;
5366

54-
// List of transaction outputs
67+
// List of transaction outputs
5568
Outputs outputs;
5669

57-
// List of kernels with associated proofs (note these are offset from tx_kernels)
70+
// List of kernels with associated proofs (note these are offset from tx_kernels)
5871
Kernels kernels;
72+
73+
Block() {}
74+
75+
Block(const Block& other) = delete;
76+
Block(Block&& other) = delete;
77+
Block& operator=(const Block&) = delete;
78+
Block& operator=(Block&&) = delete;
79+
80+
template<typename Buffer>
81+
void serializeTo(Buffer& b)
82+
{
83+
84+
}
5985
};
6086

87+
using BlockUniquePtr = std::unique_ptr<Block>;
88+
6189
}

core/common.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
#include <vector>
44
#include <array>
5+
#include <utility>
56
#include <cstdint>
7+
#include <memory>
68

79
namespace beam
810
{
9-
1011
// 256 bit hash
1112
using Hash = std::array<uint8_t, 256/8>;
1213

@@ -18,6 +19,8 @@ using Difficulty = uint64_t;
1819

1920
using Solution = std::vector<uint8_t>;
2021

22+
using ByteBuffer = std::vector<uint8_t>;
23+
2124
struct Proof
2225
{
2326
// Nonce increment used to mine this block.
@@ -26,7 +29,13 @@ struct Proof
2629
Solution solution;
2730

2831
Proof() = default;
29-
Proof(Proof &&other) : nonce(std::move(nonce)), solution(std::move(solution)) {}
32+
Proof(Proof&& other) : nonce(std::move(nonce)), solution(std::move(solution)) {}
33+
const Proof& operator=(Proof&& other)
34+
{
35+
nonce = std::move(other.nonce);
36+
solution = std::move(other.solution);
37+
return *this;
38+
}
3039
};
3140

3241
}

miner/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(MINER_SRC
66
)
77

88
add_library(miner STATIC ${MINER_SRC})
9-
add_dependencies(miner pow)
10-
target_link_libraries(miner pow)
9+
add_dependencies(miner pow chain pool)
10+
target_link_libraries(miner pow chain pool)
1111

1212
add_subdirectory(unittests)

miner/miner.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include "miner.h"
22

33
#include "pow/equihash.h"
4+
#include "chain/chain.h"
5+
#include "pool/txpool.h"
46

57
namespace beam
68
{
79

8-
Miner::Miner()
10+
Miner::Miner(Chain& blockChain, TxPool& txPool)
11+
: m_blockChain(blockChain)
12+
, m_txPool(txPool)
913
{
1014
}
1115

@@ -14,17 +18,26 @@ void Miner::start()
1418

1519
}
1620

17-
void Miner::mine()
21+
void Miner::mineBlock()
1822
{
19-
equi::ByteBuffer input{1, 2, 3, 4, 56};
20-
beam::uint256_t nonce{1, 2, 4};
23+
const Block& headBlock = m_blockChain.getHeadBlock();
2124

22-
equi::is_valid_proof(input, equi::get_solution(input, nonce));
25+
BlockUniquePtr newBlock = createBlock(headBlock.header);
26+
27+
ByteBuffer input { 1, 2, 3, 4, 56 };
28+
newBlock->header.serializeTo(input);
29+
uint256_t nonce{1, 2, 4};
30+
31+
Proof solution = equi::getSolution(input, nonce);
32+
if (equi::isValidProof(input, solution)) {
33+
newBlock->header.pow = std::move(solution);
34+
m_blockChain.processBlock(std::move(newBlock));
35+
}
2336
}
2437

25-
Block Miner::createBlock(const BlockHeader& prevHeader)
38+
BlockUniquePtr Miner::createBlock(const BlockHeader& prevHeader)
2639
{
27-
Block block;
40+
BlockUniquePtr block = std::make_unique<Block>();
2841
// TODO: get transactions
2942
// TODO: calc merkle root hash
3043

miner/miner.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
namespace beam
77
{
88

9+
class Chain;
10+
class TxPool;
911
class Miner
1012
{
1113
public:
12-
Miner();
14+
Miner(Chain& blockChain, TxPool& txPool);
1315

1416
void start();
15-
void mine();
16-
17-
Block createBlock(const BlockHeader& prevHeader);
18-
17+
void mineBlock();
18+
private:
19+
BlockUniquePtr createBlock(const BlockHeader& prevHeader);
20+
private:
21+
Chain& m_blockChain;
22+
TxPool& m_txPool;
1923
};
2024

2125
}

miner/pow/equihash.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace equi
1111
const int N = 200;
1212
const int K = 9;
1313

14-
beam::Proof get_solution(const ByteBuffer &input, const beam::uint256_t &initial_nonce, const Cancel cancel)
14+
beam::Proof getSolution(const beam::ByteBuffer &input, const beam::uint256_t &initial_nonce, const Cancel cancel)
1515
{
1616

1717
beam::Proof proof;
1818
proof.nonce = initial_nonce;
1919

20-
std::function<bool(ByteBuffer)> valid_block =
21-
[&proof](ByteBuffer soln) {
20+
std::function<bool(beam::ByteBuffer)> valid_block =
21+
[&proof](beam::ByteBuffer soln) {
2222
proof.solution = soln;
2323
return true;
2424
};
@@ -53,7 +53,7 @@ beam::Proof get_solution(const ByteBuffer &input, const beam::uint256_t &initial
5353
return proof;
5454
}
5555

56-
bool is_valid_proof(const ByteBuffer &input, const beam::Proof &proof)
56+
bool isValidProof(const beam::ByteBuffer &input, const beam::Proof &proof)
5757
{
5858
blake2b_state state;
5959
EhInitialiseState(N, K, state);

miner/pow/equihash.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
namespace equi
1010
{
1111

12-
using ByteBuffer = std::vector<uint8_t>;
1312
using Cancel = std::function<bool()>;
1413

15-
beam::Proof get_solution(const ByteBuffer &input, const beam::uint256_t &initial_nonce, const Cancel = []{ return false; });
16-
bool is_valid_proof(const ByteBuffer &input, const beam::Proof &proof);
14+
beam::Proof getSolution(const beam::ByteBuffer &input, const beam::uint256_t &initial_nonce, const Cancel = []{ return false; });
15+
bool isValidProof(const beam::ByteBuffer &input, const beam::Proof &proof);
1716

1817
}

miner/unittests/equihash_test.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
int main()
55
{
6-
equi::ByteBuffer input{1, 2, 3, 4, 56};
6+
beam::ByteBuffer input{1, 2, 3, 4, 56};
77

88
beam::uint256_t nonce{1, 2, 4};
9-
auto proof = equi::get_solution(input, nonce);
9+
auto proof = equi::getSolution(input, nonce);
1010
std::cout << proof.solution.size() << std::endl;
1111

12-
if (equi::is_valid_proof(input, proof))
12+
if (equi::isValidProof(input, proof))
1313
{
1414
std::cout << "Solution is correct\n";
1515
return 0;

miner/unittests/miner_test.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#include "miner/miner.h"
2+
#include "chain/chain.h"
3+
#include "pool/txpool.h"
24
#include <iostream>
35

46
int main()
57
{
6-
beam::Miner miner;
7-
beam::BlockHeader header;
8+
using namespace beam;
9+
Chain chain;
10+
TxPool txPool;
11+
Miner miner{ chain, txPool };
812

9-
10-
11-
beam::Block block = miner.createBlock(header);
13+
miner.mineBlock();
1214

1315
return 0;
1416
}

0 commit comments

Comments
 (0)