aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/auxpow_miner.h
blob: 159495730d8acdd4aa6c5f807526888123d4449c (plain) (blame)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2018 Daniel Kraft
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_RPC_AUXPOW_MINER_H
#define BITCOIN_RPC_AUXPOW_MINER_H

#include <miner.h>
#include <script/script.h>
#include <sync.h>
#include <uint256.h>
#include <univalue.h>

#include <map>
#include <memory>
#include <string>
#include <vector>

namespace auxpow_tests
{
class AuxpowMinerForTest;
}

/**
 * This class holds "global" state used to construct blocks for the auxpow
 * mining RPCs and the map of already constructed blocks to look them up
 * in the submitauxblock RPC.
 *
 * It is used as a singleton that is initialised during startup, taking the
 * place of the previously real global and static variables.
 */
class AuxpowMiner
{

private:

  /** The lock used for state in this object.  */
  mutable CCriticalSection cs;
  /** All currently "active" block templates.  */
  std::vector<std::unique_ptr<CBlockTemplate>> templates;
  /** Maps block hashes to pointers in vTemplates.  Does not own the memory.  */
  std::map<uint256, const CBlock*> blocks;

  /**
   * The block we are "currently" working on.  This does not own the memory,
   * instead, it points into an element of templates.
   */
  CBlock* pblockCur = nullptr;
  /** The current extra nonce for block creation.  */
  unsigned extraNonce = 0;

  /* Some data about when the current block (pblock) was constructed.  */
  unsigned txUpdatedLast;
  const CBlockIndex* pindexPrev = nullptr;
  uint64_t startTime;

  /**
   * Constructs a new current block if necessary (checking the current state to
   * see if "enough changed" for this), and returns a pointer to the block
   * that should be returned to a miner for working on at the moment.  Also
   * fills in the difficulty target value.
   */
  const CBlock* getCurrentBlock (const CScript& scriptPubKey, uint256& target);

  /**
   * Looks up a previously constructed block by its (hex-encoded) hash.  If the
   * block is found, it is returned.  Otherwise, a JSONRPCError is thrown.
   */
  const CBlock* lookupSavedBlock (const std::string& hashHex) const;

  friend class auxpow_tests::AuxpowMinerForTest;

public:

  AuxpowMiner () = default;

  /**
   * Performs the main work for the "createauxblock" RPC:  Construct a new block
   * to work on with the given address for the block reward and return the
   * necessary information for the miner to construct an auxpow for it.
   */
  UniValue createAuxBlock (const CScript& scriptPubKey);

  /**
   * Performs the main work for the "submitauxblock" RPC:  Look up the block
   * previously created for the given hash, attach the given auxpow to it
   * and try to submit it.  Returns true if all was successful and the block
   * was accepted.
   */
  bool submitAuxBlock (const std::string& hashHex,
                       const std::string& auxpowHex) const;

};

#endif // BITCOIN_RPC_AUXPOW_MINER_H