From 9b9ea8d9284e0c1fccd7b721a77402c4e08ea4f7 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Tue, 7 Aug 2018 11:02:40 +0800 Subject: [PATCH] add hex utils --- app/controllers/api/status_controller.rb | 2 +- app/controllers/local_infos_controller.rb | 8 ++++---- app/models/cita_sync/basic.rb | 17 ----------------- app/models/cita_sync/persist.rb | 14 +++++++------- app/utils/hex_utils.rb | 19 +++++++++++++++++++ config/spring.rb | 1 + spec/models/cita_sync/basic_spec.rb | 15 --------------- spec/utils/hex_utils_spec.rb | 18 ++++++++++++++++++ 8 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 app/utils/hex_utils.rb create mode 100644 spec/utils/hex_utils_spec.rb diff --git a/app/controllers/api/status_controller.rb b/app/controllers/api/status_controller.rb index 43bc399..0023f0f 100644 --- a/app/controllers/api/status_controller.rb +++ b/app/controllers/api/status_controller.rb @@ -5,7 +5,7 @@ def index render json: { result: { status: process ? "running" : "not running", - currentBlockNumber: CitaSync::Basic.number_to_hex_str(Block.current_block_number || 0), + currentBlockNumber: HexUtils.to_hex(Block.current_block_number || 0), currentChainBlockNumber: CitaSync::Api.block_number["result"] } } diff --git a/app/controllers/local_infos_controller.rb b/app/controllers/local_infos_controller.rb index 626ed40..d2e5d6f 100644 --- a/app/controllers/local_infos_controller.rb +++ b/app/controllers/local_infos_controller.rb @@ -7,7 +7,7 @@ class << self # @return [ActiveModelSerializers::SerializableResource, nil] BlockSerializer, return nil if not found. def get_block_by_number(params) block_number_hex, flag = params - block_number = CitaSync::Basic.hex_str_to_number(block_number_hex) + block_number = HexUtils.to_decimal(block_number_hex) block = Block.find_by(block_number: block_number) return nil if block.nil? # BlockSerializer.new(block).as_json @@ -43,7 +43,7 @@ def get_transaction(params) # @return [ActiveModelSerializers::SerializableResource, nil] MetaDataSerializer, return nil if not found. def get_meta_data(params) block_number_hex, = params - block_number = CitaSync::Basic.hex_str_to_number(block_number_hex) + block_number = HexUtils.to_decimal(block_number_hex) meta_data = MetaData.find_by(block_number: block_number) return nil if meta_data.nil? # MetaDataSerializer.new(meta_data, key_transform: :camel_lower).as_json @@ -57,7 +57,7 @@ def get_meta_data(params) def get_balance(params) address, block_number_hex = params address_downcase = address.downcase - block_number = CitaSync::Basic.hex_str_to_number(block_number_hex) + block_number = HexUtils.to_decimal(block_number_hex) balance = Balance.find_by(address: address_downcase, block_number: block_number) balance&.value end @@ -69,7 +69,7 @@ def get_balance(params) def get_abi(params) address, block_number_hex = params address_downcase = address.downcase - block_number = CitaSync::Basic.hex_str_to_number(block_number_hex) + block_number = HexUtils.to_decimal(block_number_hex) abi = Abi.find_by(address: address_downcase, block_number: block_number) abi&.value end diff --git a/app/models/cita_sync/basic.rb b/app/models/cita_sync/basic.rb index 5614a7b..dfb5ac0 100644 --- a/app/models/cita_sync/basic.rb +++ b/app/models/cita_sync/basic.rb @@ -1,23 +1,6 @@ module CitaSync module Basic class << self - - # convert decimal number to hex string with "0x" prefix - # - # @param number [Integer] - # @return [String] - def number_to_hex_str(number) - "0x" + number.to_s(16) - end - - # convert hex string to decimal number - # - # @param str [String] hex string - # @return [Integer] decimal number - def hex_str_to_number(str) - str.to_i(16) - end - # post params to cita and get response # # @param method [String, Symbol] diff --git a/app/models/cita_sync/persist.rb b/app/models/cita_sync/persist.rb index 0b549e6..69e23ef 100644 --- a/app/models/cita_sync/persist.rb +++ b/app/models/cita_sync/persist.rb @@ -11,7 +11,7 @@ def save_block(hex_num_str) result = data["result"] return if result.nil? block_number_hex_str = result.dig("header", "number") - block_number = Basic.hex_str_to_number(block_number_hex_str) + block_number = HexUtils.to_decimal(block_number_hex_str) Block.create( version: result["version"], cita_hash: result["hash"], @@ -32,7 +32,7 @@ def save_transaction(hash, block = nil) data = CitaSync::Api.get_transaction(hash) result = data["result"] return if result.nil? - block ||= Block.find_by_block_number(CitaSync::Basic.hex_str_to_number(result["blockNumber"])) + block ||= Block.find_by_block_number(HexUtils.to_decimal(result["blockNumber"])) content = result["content"] message = Message.new(content) transaction = Transaction.new( @@ -68,7 +68,7 @@ def save_meta_data(block_number, block = nil) result = data["result"] return if result.nil? # block number in decimal system - block_number_decimal = CitaSync::Basic.hex_str_to_number(block_number) + block_number_decimal = HexUtils.to_decimal(block_number) block ||= Block.find_by_block_number(block_number_decimal) MetaData.create( chain_id: result["chainId"], @@ -98,7 +98,7 @@ def save_balance(addr, block_number) value = data["result"] balance = Balance.create( address: addr_downcase, - block_number: CitaSync::Basic.hex_str_to_number(block_number), + block_number: HexUtils.to_decimal(block_number), value: value ) [balance, data] @@ -117,7 +117,7 @@ def save_abi(addr, block_number) value = data["result"] abi = Abi.create( address: addr_downcase, - block_number: CitaSync::Basic.hex_str_to_number(block_number), + block_number: HexUtils.to_decimal(block_number), value: value ) [abi, data] @@ -144,13 +144,13 @@ def save_block_with_infos(block_number_hex_str) # @return [void] def save_blocks_with_infos block_number_hex_str = CitaSync::Api.block_number["result"] - block_number = CitaSync::Basic.hex_str_to_number(block_number_hex_str) + block_number = HexUtils.to_decimal(block_number_hex_str) # current biggest block number in database last_block = ::Block.order(block_number: :desc).first last_block_number = last_block&.block_number || -1 ((last_block_number + 1)..block_number).each do |num| - hex_str = CitaSync::Basic.number_to_hex_str(num) + hex_str = HexUtils.to_hex(num) save_block_with_infos(hex_str) end end diff --git a/app/utils/hex_utils.rb b/app/utils/hex_utils.rb new file mode 100644 index 0000000..1f4891e --- /dev/null +++ b/app/utils/hex_utils.rb @@ -0,0 +1,19 @@ +class HexUtils + class << self + # convert decimal number to hex string with "0x" prefix + # + # @param number [Integer] + # @return [String] + def to_hex(number) + "0x" + number.to_s(16) + end + + # convert hex string to decimal number + # + # @param str [String] hex string + # @return [Integer] decimal number + def to_decimal(str) + str.hex + end + end +end diff --git a/config/spring.rb b/config/spring.rb index 9fa7863..03f897b 100644 --- a/config/spring.rb +++ b/config/spring.rb @@ -3,4 +3,5 @@ .rbenv-vars tmp/restart.txt tmp/caching-dev.txt + app/utils/** ].each { |path| Spring.watch(path) } diff --git a/spec/models/cita_sync/basic_spec.rb b/spec/models/cita_sync/basic_spec.rb index 257abf4..b768519 100644 --- a/spec/models/cita_sync/basic_spec.rb +++ b/spec/models/cita_sync/basic_spec.rb @@ -1,19 +1,4 @@ require 'rails_helper' RSpec.describe CitaSync::Basic, type: :model do - let(:number) { 100 } - let(:hex_str) { "0x64" } - - context "number_to_hex_str" do - it 'convert decimal number to hex number string' do - expect(CitaSync::Basic.number_to_hex_str(100)).to eq hex_str - end - end - - context "hex_str_to_number" do - it "convert hex number string to decimal number" do - expect(CitaSync::Basic.hex_str_to_number(hex_str)).to eq number - end - end - end diff --git a/spec/utils/hex_utils_spec.rb b/spec/utils/hex_utils_spec.rb new file mode 100644 index 0000000..28076f4 --- /dev/null +++ b/spec/utils/hex_utils_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe HexUtils do + let(:number) { 100 } + let(:hex_str) { "0x64" } + + context "to_hex" do + it 'convert decimal number to hex number string' do + expect(HexUtils.to_hex(100)).to eq hex_str + end + end + + context "to_decimal" do + it "convert hex number string to decimal number" do + expect(HexUtils.to_decimal(hex_str)).to eq number + end + end +end