-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBitcoinNode.php
More file actions
122 lines (103 loc) · 2.63 KB
/
BitcoinNode.php
File metadata and controls
122 lines (103 loc) · 2.63 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace BitWasp\Bitcoin\Node;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Chain\ParamsInterface;
use BitWasp\Bitcoin\Chain\ProofOfWork;
use BitWasp\Bitcoin\Node\Chain\ChainContainer;
use BitWasp\Bitcoin\Node\Chain\ChainsInterface;
use BitWasp\Bitcoin\Node\Chain\ChainView;
use BitWasp\Bitcoin\Node\Db\DbInterface;
use Evenement\EventEmitter;
use Packaged\Config\ConfigProviderInterface;
class BitcoinNode extends EventEmitter implements NodeInterface
{
/**
* @var DbInterface
*/
private $db;
/**
* @var Index\Blocks
*/
protected $blocks;
/**
* @var Index\Headers
*/
protected $headers;
/**
* @var Index\Transactions
*/
protected $transactions;
/**
* @var ChainsInterface
*/
protected $chains;
/**
* BetterNode constructor.
* @param ConfigProviderInterface $config
* @param ParamsInterface $params
* @param DbInterface $db
*/
public function __construct(ConfigProviderInterface $config, ParamsInterface $params, DbInterface $db)
{
$math = Bitcoin::getMath();
$adapter = Bitcoin::getEcAdapter($math);
$this->chains = new ChainContainer($math, $params);
$consensus = new Consensus($math, $params);
$pow = new ProofOfWork($math, $params);
$this->headers = new Index\Headers($db, $adapter, $this->chains, $consensus, $pow);
$this->blocks = new Index\Blocks($db, $config, $adapter, $this->chains, $consensus);
$this->transactions = new Index\Transactions($db);
$genesis = $params->getGenesisBlock();
$this->headers->init($genesis->getHeader());
$this->blocks->init($genesis);
$this->db = $db;
$segments = $this->db->fetchChainSegments();
foreach ($segments as $segment) {
$this->chains->addSegment($segment);
}
$this->chains->initialize($this->db);
}
/**
* @return void
*/
public function stop()
{
$this->db->stop();
}
/**
* @return Index\Transactions
*/
public function transactions()
{
return $this->transactions;
}
/**
* @return Index\Headers
*/
public function headers()
{
return $this->headers;
}
/**
* @return Index\Blocks
*/
public function blocks()
{
return $this->blocks;
}
/**
* @return ChainView
*/
public function chain()
{
$best = $this->chains->best(Bitcoin::getMath());
return $best;
}
/**
* @return ChainsInterface
*/
public function chains()
{
return $this->chains;
}
}