|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module CKB |
| 4 | + module Types |
| 5 | + class BlockTemplate |
| 6 | + attr_accessor :version, :difficulty, :current_time, :number, :epoch, :parent_hash, :cycles_limit, |
| 7 | + :bytes_limit, :uncles_count_limit, :uncles, :transactions, :proposals, :cellbase, :work_id, :dao |
| 8 | + |
| 9 | + # @param version [String | Integer] integer or hex number |
| 10 | + # @param difficulty [String | Integer] integer or hex number |
| 11 | + # @param current_time [String | Integer] integer or hex number |
| 12 | + # @param number [String | Integer] integer or hex number |
| 13 | + # @param epoch [String | Integer] integer or hex number |
| 14 | + # @param parent_hash [String] 0x... |
| 15 | + # @param cycles_limit [String | Integer] integer or hex number |
| 16 | + # @param bytes_limit [String | Integer] integer or hex number |
| 17 | + # @param uncles_count_limit [String | Integer] integer or hex number |
| 18 | + # @param uncles [CKB::Types::UncleTemplate[]] |
| 19 | + # @param transactions [CKB::Types::TransactionTemplate[]] |
| 20 | + # @param proposals [String[]] 0x... |
| 21 | + # @param cellbase [CellbaseTemplate] |
| 22 | + # @param work_id [String | Integer] integer or hex number |
| 23 | + # @param dao [String] 0x... |
| 24 | + def initialize( |
| 25 | + version:, |
| 26 | + difficulty:, |
| 27 | + current_time:, |
| 28 | + number:, |
| 29 | + epoch:, |
| 30 | + parent_hash:, |
| 31 | + cycles_limit:, |
| 32 | + bytes_limit:, |
| 33 | + uncles_count_limit:, |
| 34 | + uncles:, |
| 35 | + transactions:, |
| 36 | + proposals:, |
| 37 | + cellbase:, |
| 38 | + work_id:, |
| 39 | + dao: |
| 40 | + ) |
| 41 | + @version = Utils.to_int(version) |
| 42 | + @difficulty = Utils.to_int(difficulty) |
| 43 | + @current_time = Utils.to_int(current_time) |
| 44 | + @number = Utils.to_int(number) |
| 45 | + @epoch = Utils.to_int(epoch) |
| 46 | + @parent_hash = parent_hash |
| 47 | + @cycles_limit = Utils.to_int(cycles_limit) |
| 48 | + @bytes_limit = Utils.to_int(bytes_limit) |
| 49 | + @uncles_count_limit = Utils.to_int(uncles_count_limit) |
| 50 | + @uncles = uncles |
| 51 | + @transactions = transactions |
| 52 | + @proposals = proposals |
| 53 | + @cellbase = cellbase |
| 54 | + @work_id = Utils.to_int(work_id) |
| 55 | + @dao = dao |
| 56 | + end |
| 57 | + |
| 58 | + def to_h |
| 59 | + { |
| 60 | + version: Utils.to_hex(version), |
| 61 | + difficulty: Utils.to_hex(difficulty), |
| 62 | + current_time: Utils.to_hex(current_time), |
| 63 | + number: Utils.to_hex(number), |
| 64 | + epoch: Utils.to_hex(epoch), |
| 65 | + parent_hash: parent_hash, |
| 66 | + cycles_limit: Utils.to_hex(cycles_limit), |
| 67 | + bytes_limit: Utils.to_hex(bytes_limit), |
| 68 | + uncles_count_limit: Utils.to_hex(uncles_count_limit), |
| 69 | + uncles: uncles.map(&:to_h), |
| 70 | + transactions: transactions.map(&:to_h), |
| 71 | + proposals: proposals, |
| 72 | + cellbase: cellbase.to_h, |
| 73 | + work_id: work_id, |
| 74 | + dao: dao |
| 75 | + } |
| 76 | + end |
| 77 | + |
| 78 | + def self.from_h(hash) |
| 79 | + return if hash.nil? |
| 80 | + |
| 81 | + new( |
| 82 | + version: hash[:version], |
| 83 | + difficulty: hash[:difficulty], |
| 84 | + current_time: hash[:current_time], |
| 85 | + number: hash[:number], |
| 86 | + epoch: hash[:epoch], |
| 87 | + parent_hash: hash[:parent_hash], |
| 88 | + cycles_limit: hash[:cycles_limit], |
| 89 | + bytes_limit: hash[:bytes_limit], |
| 90 | + uncles_count_limit: hash[:uncles_count_limit], |
| 91 | + uncles: hash[:uncles], |
| 92 | + transactions: hash[:transactions], |
| 93 | + proposals: hash[:proposals], |
| 94 | + cellbase: hash[:cellbase], |
| 95 | + work_id: hash[:work_id], |
| 96 | + dao: hash[:dao] |
| 97 | + ) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments