OSDN Git Service

d6972b53db538c42649abebdb214275a6750e093
[bytom/bytom-java-sdk.git] / java-sdk / src / main / java / io / bytom / api / Block.java
1 package io.bytom.api;
2
3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.offline.common.Utils;
5 import io.bytom.offline.exception.BytomException;
6 import io.bytom.http.Client;
7 import org.apache.log4j.Logger;
8
9 import java.util.List;
10 import java.util.Map;
11
12 public class Block {
13
14     public String hash;
15
16     public Integer size;
17
18     public Integer version;
19
20     public Integer height;
21
22     @SerializedName("previous_block_hash")
23     public String previousBlockHash;
24
25     public Integer timestamp;
26
27     public Integer nonce;
28
29     public long bits;
30
31     public String difficulty;
32
33     @SerializedName("transaction_merkle_root")
34     public String transactionsMerkleRoot;
35
36     @SerializedName("transaction_status_hash")
37     public String transactionStatusHash;
38
39     public List<BlockTx> transactions;
40
41
42     private static Logger logger = Logger.getLogger(Block.class);
43
44     public String toJson() {
45         return Utils.serializer.toJson(this);
46     }
47
48     /**
49      * Call get-block-count api
50      *
51      * @param client
52      * @return
53      * @throws BytomException
54      */
55     public static Integer getBlockCount(Client client) throws BytomException {
56         Integer blockCount =
57                 client.requestGet("get-block-count", null, "block_count", Integer.class);
58
59         logger.info("get-block-count:"+blockCount);
60         return blockCount;
61     }
62
63     /**
64      * Call get-block-hash api
65      *
66      * @param client
67      * @return
68      * @throws BytomException
69      */
70     public static String getBlockHash(Client client) throws BytomException {
71         String blockHash =
72                 client.requestGet("get-block-hash", null, "block_hash", String.class);
73
74         logger.info("get-block-hash:"+blockHash);
75
76         return blockHash;
77     }
78
79     public static class QueryBuilder {
80
81         /**
82          * block_height, height of block.
83          */
84         @SerializedName("block_height")
85         public int blockHeight;
86         /**
87          * block_hash, hash of block.
88          */
89         @SerializedName("block_hash")
90         public String blockHash;
91
92         public QueryBuilder setBlockHeight(int blockHeight) {
93             this.blockHeight = blockHeight;
94             return this;
95         }
96
97         public QueryBuilder setBlockHash(String blockHash) {
98             this.blockHash = blockHash;
99             return this;
100         }
101
102         /**
103          * Call get-block api
104          *
105          * @param client
106          * @return
107          * @throws BytomException
108          */
109         public Block getBlock(Client client) throws BytomException {
110
111             Block block = client.request("get-block", this, Block.class);
112
113             logger.info("get-block:");
114             logger.info(block.toJson());
115
116             return block;
117         }
118
119         /**
120          * Call get-block-header api
121          *
122          * @param client
123          * @return
124          * @throws BytomException
125          */
126         public BlockHeader getBlockHeader(Client client) throws BytomException {
127             BlockHeader blockHeader =
128                     client.request("get-block-header", this, BlockHeader.class);
129
130             logger.info("get-block-header:");
131             logger.info(blockHeader.toJson());
132
133             return blockHeader;
134         }
135
136         /**
137          * Call get-difficulty api
138          *
139          * @param client
140          * @return
141          * @throws BytomException
142          */
143         public BlockDifficulty getBlockDifficulty(Client client) throws BytomException {
144             BlockDifficulty blockDifficulty =
145                     client.request("get-difficulty", this, BlockDifficulty.class);
146
147             logger.info("get-difficulty:");
148             logger.info(blockDifficulty.toJson());
149
150             return blockDifficulty;
151         }
152
153         /**
154          * Call get-hash-rate api
155          *
156          * @param client
157          * @return
158          * @throws BytomException
159          */
160         public BlockHashRate getHashRate(Client client) throws BytomException {
161             BlockHashRate blockHashRate =
162                     client.request("get-hash-rate", this, BlockHashRate.class);
163
164             logger.info("get-hash-rate:");
165             logger.info(blockHashRate.toJson());
166
167             return blockHashRate;
168         }
169
170     }
171
172     public static class BlockTx {
173         /**
174          * Unique identifier, or transaction hash, of a transaction.
175          */
176         private String id;
177
178         /**
179          * version
180          */
181         private Integer version;
182
183         /**
184          * size
185          */
186         private Integer size;
187         /**
188          * time_range
189          */
190         @SerializedName("time_range")
191         private Integer timeRange;
192
193         /**
194          * status
195          */
196         @SerializedName("status_fail")
197         private boolean statusFail;
198
199         /**
200          * List of specified inputs for a transaction.
201          */
202         @SerializedName("inputs")
203         private List<AnnotatedInput> inputs;
204
205         /**
206          * List of specified outputs for a transaction.
207          */
208         @SerializedName("outputs")
209         private List<AnnotatedOutput> outputs;
210     }
211
212     public static class AnnotatedInput {
213
214         /**
215          * The number of units of the asset being issued or spent.
216          */
217         private Integer amount;
218
219         /**
220          * inputs param
221          */
222         private String arbitrary;
223
224         /**
225          * The definition of the asset being issued or spent (possibly null).
226          */
227         @SerializedName("asset_definition")
228         private Map<String, Object> assetDefinition;
229
230         /**
231          * The id of the asset being issued or spent.
232          */
233         @SerializedName("asset_id")
234         private String assetId;
235
236         /**
237          * The type of the input.<br>
238          * Possible values are "issue" and "spend".
239          */
240         private String type;
241     }
242
243     public static class AnnotatedOutput {
244
245         /**
246          * The number of units of the asset being controlled.
247          */
248         private long amount;
249
250         /**
251          * The definition of the asset being controlled (possibly null).
252          */
253         @SerializedName("asset_definition")
254         private Map<String, Object> assetDefinition;
255
256         /**
257          * The id of the asset being controlled.
258          */
259         @SerializedName("asset_id")
260         public String assetId;
261
262         /**
263          * The control program which must be satisfied to transfer this output.
264          */
265         @SerializedName("control_program")
266         private String controlProgram;
267
268         /**
269          * The id of the output.
270          */
271         @SerializedName("id")
272         private String id;
273
274         /**
275          * The output's position in a transaction's list of outputs.
276          */
277         private Integer position;
278
279         /**
280          * The type the output.<br>
281          * Possible values are "control" and "retire".
282          */
283         private String type;
284
285     }
286
287     public static class BlockHeader {
288
289         @SerializedName("block_header")
290         public String blockHeader;
291
292         @SerializedName("reward")
293         public Integer reward;
294
295         public String toJson() {
296             return Utils.serializer.toJson(this);
297         }
298
299     }
300
301     public static class BlockDifficulty {
302         public String hash;
303         public Integer height;
304         public Integer bits;
305         public String difficulty;
306
307         public String toJson() {
308             return Utils.serializer.toJson(this);
309         }
310
311
312     }
313
314     public static class BlockHashRate {
315         public String hash;
316         public Integer height;
317         public Integer hash_rate;
318
319         public String toJson() {
320             return Utils.serializer.toJson(this);
321         }
322
323     }
324 }