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;
18 public Integer version;
20 public Integer height;
22 @SerializedName("previous_block_hash")
23 public String previousBlockHash;
25 public Integer timestamp;
31 public String difficulty;
33 @SerializedName("transaction_merkle_root")
34 public String transactionsMerkleRoot;
36 @SerializedName("transaction_status_hash")
37 public String transactionStatusHash;
39 public List<BlockTx> transactions;
42 private static Logger logger = Logger.getLogger(Block.class);
44 public String toJson() {
45 return Utils.serializer.toJson(this);
49 * Call get-block-count api
53 * @throws BytomException
55 public static Integer getBlockCount(Client client) throws BytomException {
57 client.requestGet("get-block-count", null, "block_count", Integer.class);
59 logger.info("get-block-count:"+blockCount);
64 * Call get-block-hash api
68 * @throws BytomException
70 public static String getBlockHash(Client client) throws BytomException {
72 client.requestGet("get-block-hash", null, "block_hash", String.class);
74 logger.info("get-block-hash:"+blockHash);
79 public static class QueryBuilder {
82 * block_height, height of block.
84 @SerializedName("block_height")
85 public int blockHeight;
87 * block_hash, hash of block.
89 @SerializedName("block_hash")
90 public String blockHash;
92 public QueryBuilder setBlockHeight(int blockHeight) {
93 this.blockHeight = blockHeight;
97 public QueryBuilder setBlockHash(String blockHash) {
98 this.blockHash = blockHash;
107 * @throws BytomException
109 public Block getBlock(Client client) throws BytomException {
111 Block block = client.request("get-block", this, Block.class);
113 logger.info("get-block:");
114 logger.info(block.toJson());
120 * Call get-block-header api
124 * @throws BytomException
126 public BlockHeader getBlockHeader(Client client) throws BytomException {
127 BlockHeader blockHeader =
128 client.request("get-block-header", this, BlockHeader.class);
130 logger.info("get-block-header:");
131 logger.info(blockHeader.toJson());
137 * Call get-difficulty api
141 * @throws BytomException
143 public BlockDifficulty getBlockDifficulty(Client client) throws BytomException {
144 BlockDifficulty blockDifficulty =
145 client.request("get-difficulty", this, BlockDifficulty.class);
147 logger.info("get-difficulty:");
148 logger.info(blockDifficulty.toJson());
150 return blockDifficulty;
154 * Call get-hash-rate api
158 * @throws BytomException
160 public BlockHashRate getHashRate(Client client) throws BytomException {
161 BlockHashRate blockHashRate =
162 client.request("get-hash-rate", this, BlockHashRate.class);
164 logger.info("get-hash-rate:");
165 logger.info(blockHashRate.toJson());
167 return blockHashRate;
172 public static class BlockTx {
174 * Unique identifier, or transaction hash, of a transaction.
181 private Integer version;
186 private Integer size;
190 @SerializedName("time_range")
191 private Integer timeRange;
196 @SerializedName("status_fail")
197 private boolean statusFail;
200 * List of specified inputs for a transaction.
202 @SerializedName("inputs")
203 private List<AnnotatedInput> inputs;
206 * List of specified outputs for a transaction.
208 @SerializedName("outputs")
209 private List<AnnotatedOutput> outputs;
212 public static class AnnotatedInput {
215 * The number of units of the asset being issued or spent.
217 private Integer amount;
222 private String arbitrary;
225 * The definition of the asset being issued or spent (possibly null).
227 @SerializedName("asset_definition")
228 private Map<String, Object> assetDefinition;
231 * The id of the asset being issued or spent.
233 @SerializedName("asset_id")
234 private String assetId;
237 * The type of the input.<br>
238 * Possible values are "issue" and "spend".
243 public static class AnnotatedOutput {
246 * The number of units of the asset being controlled.
251 * The definition of the asset being controlled (possibly null).
253 @SerializedName("asset_definition")
254 private Map<String, Object> assetDefinition;
257 * The id of the asset being controlled.
259 @SerializedName("asset_id")
260 public String assetId;
263 * The control program which must be satisfied to transfer this output.
265 @SerializedName("control_program")
266 private String controlProgram;
269 * The id of the output.
271 @SerializedName("id")
275 * The output's position in a transaction's list of outputs.
277 private Integer position;
280 * The type the output.<br>
281 * Possible values are "control" and "retire".
287 public static class BlockHeader {
289 @SerializedName("block_header")
290 public String blockHeader;
292 @SerializedName("reward")
293 public Integer reward;
295 public String toJson() {
296 return Utils.serializer.toJson(this);
301 public static class BlockDifficulty {
303 public Integer height;
305 public String difficulty;
307 public String toJson() {
308 return Utils.serializer.toJson(this);
314 public static class BlockHashRate {
316 public Integer height;
317 public Integer hash_rate;
319 public String toJson() {
320 return Utils.serializer.toJson(this);