Line data Source code
1 : /*
2 : * Package : Ethereum
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 08/01/2017
5 : * Copyright : S.Hamblett
6 : *
7 : * A JSON RPC 2.0 client for Ethereum
8 : */
9 :
10 : part of ethereum;
11 :
12 : /// An ethereum block descriptor message
13 : class EthereumBlock {
14 1 : EthereumBlock();
15 :
16 2 : EthereumBlock.fromMap(Map result) {
17 2 : construct(result);
18 : }
19 :
20 : /// The block number. Null when its a pending block.
21 : int _number;
22 :
23 2 : int get number => _number;
24 :
25 : /// Hash of the block. Null when its a pending block.
26 : BigInteger _hash;
27 :
28 2 : BigInteger get hash => _hash;
29 :
30 : /// Parent hash. Hash of the parent block.
31 : BigInteger _parentHash;
32 :
33 2 : BigInteger get parentHash => _parentHash;
34 :
35 : /// nonce. Hash of the generated proof-of-work. Null when its pending block.
36 : BigInteger _nonce;
37 :
38 1 : BigInteger get nonce => _nonce;
39 :
40 : /// Sha3 Uncles. SHA3 of the uncles data in the block.
41 : BigInteger _sha3Uncles;
42 :
43 1 : BigInteger get sha3Uncles => _sha3Uncles;
44 :
45 : /// Logs bloom. The bloom filter for the logs of the block. Null when its pending block.
46 : BigInteger _logsBloom;
47 :
48 1 : BigInteger get logsBloom => _logsBloom;
49 :
50 : /// Transactions root. The root of the transaction tree of the block.
51 : BigInteger _transactionsRoot;
52 :
53 1 : BigInteger get transactionsRoot => _transactionsRoot;
54 :
55 : /// State root. The root of the final state tree of the block.
56 : BigInteger _stateRoot;
57 :
58 1 : BigInteger get stateRoot => _stateRoot;
59 :
60 : /// Receipts root. The root of the receipts tree of the block.
61 : BigInteger _receiptsRoot;
62 :
63 1 : BigInteger get receiptsRoot => _receiptsRoot;
64 :
65 : /// Miner. The address of the beneficiary to whom the mining rewards were given.
66 : BigInteger _miner;
67 :
68 2 : BigInteger get miner => _miner;
69 :
70 : /// Difficulty. Integer of the difficulty for this block.
71 : int _difficulty;
72 :
73 2 : int get difficulty => _difficulty;
74 :
75 : /// Total difficulty. Integer of the total difficulty of the chain until this block.
76 : int _totalDifficulty;
77 :
78 1 : int get totalDifficulty => _totalDifficulty;
79 :
80 : /// Extra data. The "extra data" field of this block.
81 : BigInteger _extraData;
82 :
83 1 : BigInteger get extraData => _extraData;
84 :
85 : /// Size. Integer the size of this block in bytes.
86 : int _size;
87 :
88 1 : int get size => _size;
89 :
90 : /// Gas limit. The maximum gas allowed in this block.
91 : int _gasLimit;
92 :
93 1 : int get gasLimit => _gasLimit;
94 :
95 : /// Gas used. The total used gas by all transactions in this block.
96 : int _gasUsed;
97 :
98 2 : int get gasUsed => _gasUsed;
99 :
100 : /// Timestamp. The unix timestamp for when the block was collated.
101 : DateTime _timestamp;
102 :
103 2 : DateTime get timestamp => _timestamp;
104 :
105 : /// Transactions. A list of transaction objects, or 32 Bytes transaction hashes
106 : /// depending on the last given parameter.
107 : List<dynamic> _transactions;
108 :
109 1 : List<dynamic> get transactions => _transactions;
110 :
111 : /// Indicates if the transactions are hashes or transaction objects
112 : bool _transactionsAreHashes = false;
113 :
114 1 : bool get transactionsAreHashes => _transactionsAreHashes;
115 :
116 : /// Uncles. A list of uncle hashes.
117 : List<BigInteger> _uncles;
118 :
119 1 : List<BigInteger> get uncles => _uncles;
120 :
121 : /// Construct from the supplied Map, only check for the keys we need.
122 : void construct(Map data) {
123 2 : if ((data == null) || (data[ethResultKey] == null)) {
124 : return;
125 : }
126 2 : if (data[ethResultKey].containsKey('number')) {
127 4 : _number = EthereumUtilities.hexToInt(data[ethResultKey]['number']);
128 : }
129 2 : if (data[ethResultKey].containsKey('hash')) {
130 4 : _hash = new BigInteger(data[ethResultKey]['hash']);
131 : }
132 2 : if (data[ethResultKey].containsKey('parentHash')) {
133 4 : _parentHash = new BigInteger(data[ethResultKey]['parentHash']);
134 : }
135 2 : if (data[ethResultKey].containsKey('nonce')) {
136 4 : _nonce = new BigInteger(data[ethResultKey]['nonce']);
137 : }
138 2 : if (data[ethResultKey].containsKey('sha3Uncles')) {
139 4 : _sha3Uncles = new BigInteger(data[ethResultKey]['sha3Uncles']);
140 : }
141 2 : if (data[ethResultKey].containsKey('logsBloom')) {
142 4 : _logsBloom = new BigInteger(data[ethResultKey]['logsBloom']);
143 : }
144 2 : if (data[ethResultKey].containsKey('transactionsRoot')) {
145 1 : _transactionsRoot =
146 3 : new BigInteger(data[ethResultKey]['transactionsRoot']);
147 : }
148 2 : if (data[ethResultKey].containsKey('stateRoot')) {
149 4 : _stateRoot = new BigInteger(data[ethResultKey]['stateRoot']);
150 : }
151 2 : if (data[ethResultKey].containsKey('receiptsRoot')) {
152 4 : _receiptsRoot = new BigInteger(data[ethResultKey]['receiptsRoot']);
153 : }
154 2 : if (data[ethResultKey].containsKey('miner')) {
155 4 : _miner = new BigInteger(data[ethResultKey]['miner']);
156 : }
157 2 : if (data[ethResultKey].containsKey('difficulty')) {
158 1 : _difficulty =
159 3 : EthereumUtilities.hexToInt(data[ethResultKey]['difficulty']);
160 : }
161 2 : if (data[ethResultKey].containsKey('totalDifficulty')) {
162 1 : _totalDifficulty =
163 3 : EthereumUtilities.hexToInt(data[ethResultKey]['totalDifficulty']);
164 : }
165 2 : if (data[ethResultKey].containsKey('extraData')) {
166 4 : _extraData = new BigInteger(data[ethResultKey]['extraData']);
167 : }
168 2 : if (data[ethResultKey].containsKey('size')) {
169 4 : _size = EthereumUtilities.hexToInt(data[ethResultKey]['size']);
170 : }
171 2 : if (data[ethResultKey].containsKey('gasLimit')) {
172 4 : _gasLimit = EthereumUtilities.hexToInt(data[ethResultKey]['gasLimit']);
173 : }
174 2 : if (data[ethResultKey].containsKey('gasUsed')) {
175 4 : _gasUsed = EthereumUtilities.hexToInt(data[ethResultKey]['gasUsed']);
176 : }
177 2 : if (data[ethResultKey].containsKey('timestamp')) {
178 2 : _timestamp = new DateTime.fromMillisecondsSinceEpoch(
179 3 : EthereumUtilities.hexToInt(data[ethResultKey]['timestamp']));
180 : }
181 2 : if (data[ethResultKey].containsKey('uncles')) {
182 1 : _uncles =
183 3 : EthereumUtilities.hexToBigIntegerList(data[ethResultKey]['uncles']);
184 : }
185 2 : if (data[ethResultKey].containsKey('transactions')) {
186 2 : if ((data[ethResultKey]['transactions'] != null) &&
187 3 : (data[ethResultKey]['transactions'].isNotEmpty)) {
188 4 : if (data[ethResultKey]['transactions'][0] is String) {
189 : // Hashes
190 1 : _transactionsAreHashes = true;
191 1 : _transactions = EthereumUtilities
192 3 : .hexToBigIntegerList(data[ethResultKey]['transactions']);
193 : } else {
194 : // Transaction objects
195 2 : _transactions = new List<EthereumTransaction>();
196 4 : for (Map transaction in data[ethResultKey]['transactions']) {
197 1 : final Map buildTrans = {ethResultKey: transaction};
198 : final EthereumTransaction entry =
199 1 : new EthereumTransaction.fromMap(buildTrans);
200 2 : _transactions.add(entry);
201 : }
202 : }
203 : }
204 : }
205 : }
206 :
207 : // To string
208 : String toString() {
209 2 : final String ret = "Ethereum Block :" +
210 2 : "\n" +
211 4 : " Number : $number" +
212 2 : "\n" +
213 4 : " Hash : $hash" +
214 2 : "\n" +
215 4 : " Parent Hash : $parentHash" +
216 2 : "\n" +
217 4 : " Miner : $miner" +
218 2 : "\n" +
219 4 : " Difficulty : $difficulty" +
220 2 : "\n" +
221 4 : " Gas Used : $gasUsed" +
222 2 : "\n" +
223 4 : " Time : $timestamp" +
224 : "\n";
225 : return ret;
226 : }
227 : }
|