Skip to content

Commit

Permalink
add sync error records
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 7, 2018
1 parent 9b9ea8d commit 972f530
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 5 deletions.
37 changes: 34 additions & 3 deletions app/models/cita_sync/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class << self
def save_block(hex_num_str)
data = CitaSync::Api.get_block_by_number(hex_num_str, true)
result = data["result"]
return if result.nil?

# handle error
return handle_error("getBlockByNumber", [hex_num_str, true], data) if result.nil?

block_number_hex_str = result.dig("header", "number")
block_number = HexUtils.to_decimal(block_number_hex_str)
Block.create(
Expand All @@ -31,7 +34,10 @@ def save_block(hex_num_str)
def save_transaction(hash, block = nil)
data = CitaSync::Api.get_transaction(hash)
result = data["result"]
return if result.nil?

# handle error
return handle_error("getTransaction", [hash], data) if result.nil?

block ||= Block.find_by_block_number(HexUtils.to_decimal(result["blockNumber"]))
content = result["content"]
message = Message.new(content)
Expand Down Expand Up @@ -66,7 +72,10 @@ def save_transaction(hash, block = nil)
def save_meta_data(block_number, block = nil)
data = CitaSync::Api.get_meta_data(block_number)
result = data["result"]
return if result.nil?

# handle error
return handle_error("getMetaData", [block_number], data) if result.nil?

# block number in decimal system
block_number_decimal = HexUtils.to_decimal(block_number)
block ||= Block.find_by_block_number(block_number_decimal)
Expand Down Expand Up @@ -94,6 +103,10 @@ def save_balance(addr, block_number)
addr_downcase = addr.downcase
# height number in decimal system
data = CitaSync::Api.get_balance(addr_downcase, block_number)

# handle error
return handle_error("getBalance", [addr_downcase, block_number], data) unless data["error"].nil?

return [nil, data] unless block_number.start_with?("0x")
value = data["result"]
balance = Balance.create(
Expand All @@ -113,6 +126,10 @@ def save_abi(addr, block_number)
addr_downcase = addr.downcase
# block_number in decimal system
data = CitaSync::Api.get_abi(addr_downcase, block_number)

# handle error
return handle_error("getAbi", [addr_downcase, block_number], data) unless data["error"].nil?

return [nil, data] unless block_number.start_with?("0x")
value = data["result"]
abi = Abi.create(
Expand All @@ -123,6 +140,20 @@ def save_abi(addr, block_number)
[abi, data]
end

private def handle_error(method, params, data)
error = data["error"]
return if error.nil?
code = error["code"]
message = error["message"]

SyncError.create(
method: method,
params: params,
code: code,
message: message
)
end

# save one block with it's transactions and meta data
#
# @param block_number_hex_str [String] hex string with "0x" prefix
Expand Down
2 changes: 2 additions & 0 deletions app/models/sync_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class SyncError < ApplicationRecord
end
13 changes: 13 additions & 0 deletions db/migrate/20180807030924_create_sync_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateSyncErrors < ActiveRecord::Migration[5.2]
def change
create_table :sync_errors do |t|
t.string :method
# array should be same type
t.json :params
t.integer :code
t.string :message

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_07_11_075733) do
ActiveRecord::Schema.define(version: 2018_08_07_030924) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -62,6 +62,15 @@
t.index ["block_id"], name: "index_meta_data_on_block_id"
end

create_table "sync_errors", force: :cascade do |t|
t.string "method"
t.json "params"
t.integer "code"
t.string "message"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "transactions", force: :cascade do |t|
t.string "cita_hash", null: false
t.text "content"
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/sync_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :sync_error do

end
end
72 changes: 72 additions & 0 deletions spec/models/cita_sync/persist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
CitaSync::Persist.save_block("0x0")
}.to change { ::Block.count }.by(1)
end

it "with error params" do
sync_error = CitaSync::Persist.save_block("a")
expect(sync_error.method).to eq "getBlockByNumber"
expect(sync_error.params).to eq ["a", true]
expect(sync_error.code).to eq block_zero_params_error_code
expect(sync_error.message).to eq block_zero_params_error_message
end
end

context "save transaction" do
Expand All @@ -35,6 +43,16 @@
transaction = CitaSync::Persist.save_transaction(transaction_hash)
expect(transaction.errors.full_messages).not_to be_empty
end

it "with error params" do
params = ["0x0"]
sync_error = CitaSync::Persist.save_transaction(*params)

expect(sync_error.method).to eq "getTransaction"
expect(sync_error.params).to eq params
expect(sync_error.code).to eq transaction_params_error_code
expect(sync_error.message).to eq transaction_params_error_message
end
end

context "save meta data" do
Expand All @@ -43,20 +61,47 @@
meta_data = CitaSync::Persist.save_meta_data("0x0")
expect(meta_data.errors.full_messages).to be_empty
end

it "with error params" do
params = ["a"]
sync_error = CitaSync::Persist.save_meta_data(*params)
expect(sync_error.method).to eq "getMetaData"
expect(sync_error.params).to eq params
expect(sync_error.code).to eq meta_data_params_error_code
expect(sync_error.message).to eq meta_data_params_error_message
end
end

context "save balance" do
it "save success" do
balance, = CitaSync::Persist.save_balance(account_address, "0x0")
expect(balance.errors.full_messages).to be_empty
end

it "with error params" do
params = ["0x0", "0x0"]
sync_error = CitaSync::Persist.save_balance(*params)
expect(sync_error.method).to eq "getBalance"
expect(sync_error.params).to eq params
expect(sync_error.code).to eq balance_params_error_code
expect(sync_error.message).to eq balance_params_error_message
end
end

context "save abi" do
it "save abi" do
abi, = CitaSync::Persist.save_abi(account_address, "0x0")
expect(abi.errors.full_messages).to be_empty
end

it "with error params" do
params = ["0x0", "0x0"]
sync_error = CitaSync::Persist.save_abi(*params)
expect(sync_error.method).to eq "getAbi"
expect(sync_error.params).to eq params
expect(sync_error.code).to eq abi_params_error_code
expect(sync_error.message).to eq abi_params_error_message
end
end

context "save block with infos" do
Expand Down Expand Up @@ -88,4 +133,31 @@
expect(Transaction.count).to eq 1
end
end

context "handle error" do
let(:method) { "getBlockByNumber" }
let(:params) { [123, false] }
let(:code) { -124 }
let(:message) { "invalid params" }
let(:data) do
{
"error" => {
"code" => code,
"message" => message
}
}
end
it "save an error" do
sync_error = CitaSync::Persist.send(:handle_error, method, params, data)
expect(sync_error.method).to eq method
expect(sync_error.params).to match_array(params)
expect(sync_error.code).to eq code
expect(sync_error.message).to eq message
end

it "with no error info" do
sync_error = CitaSync::Persist.send(:handle_error, method, params, {})
expect(sync_error).to be nil
end
end
end
4 changes: 4 additions & 0 deletions spec/models/sync_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'

RSpec.describe SyncError, type: :model do
end
84 changes: 83 additions & 1 deletion spec/supports/block_mock_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def stub_request_wrapper(method, params, result)
)
end

def stub_request_error_wrapper(method, params, error, status: 200, json_rpc: "2.0", id: 83)
include_hash = if params.nil?
{ method: method }
else
{ method: method, params: params }
end

stub_request(:post, ENV["CITA_URL"])
.with(
body: hash_including(include_hash),
headers: { "Content-Type": "application/json" }
)
.to_return(
status: status,
body: { jsonrpc: json_rpc, id: id, error: error }.to_json
)
end

let(:block_number_result) { "0x1" }
let(:mock_block_number) do
stub_request_wrapper("blockNumber", nil, block_number_result)
Expand Down Expand Up @@ -49,6 +67,18 @@ def stub_request_wrapper(method, params, result)
stub_request_wrapper("getBlockByNumber", ["0x0", true], block_zero_result)
end

let(:block_zero_params_error_code) { -32700 }
let(:block_zero_params_error_message) { "data did not match any variant of untagged enum BlockNumber" }
let(:block_zero_params_error) do
{
code: block_zero_params_error_code,
message: block_zero_params_error_message,
}
end
let(:mock_get_block_by_number_zero_params_error) do
stub_request_error_wrapper("getBlockByNumber", ["a", true], block_zero_params_error)
end

let(:block_one_hash) { "0xa18f9c384107d9a4fcd2fae656415928bd921047519fea5650cba394f6b6142b" }
let(:block_one_result) do
{
Expand Down Expand Up @@ -93,6 +123,18 @@ def stub_request_wrapper(method, params, result)
stub_request_wrapper("getTransaction", [transaction_hash], transaction_result)
end

let(:transaction_params_error_code) { -32700 }
let(:transaction_params_error_message) { "invalid format: [0x0]" }
let(:transaction_params_error) do
{
code: transaction_params_error_code,
message: transaction_params_error_message
}
end
let(:mock_get_transaction_params_error) do
stub_request_error_wrapper("getTransaction", ["0x0"], transaction_params_error)
end

let(:transaction_receipt_result) do
{
"contractAddress": "0x89be88054e2ee94911549be521ab1241c7700a1b",
Expand Down Expand Up @@ -127,6 +169,18 @@ def stub_request_wrapper(method, params, result)
stub_request_wrapper("getMetaData", ["0x1"], meta_data_result)
end

let(:meta_data_params_error_code) { -32700 }
let(:meta_data_params_error_message) { "data did not match any variant of untagged enum BlockNumber" }
let(:meta_data_params_error) do
{
code: meta_data_params_error_code,
message: meta_data_params_error_message
}
end
let(:mock_get_meta_data_params_error) do
stub_request_error_wrapper("getMetaData", ["a"], meta_data_params_error)
end

let(:account_address) { "0x0dcf740686de1fe9e9faa4b519767a872e1cf69e" }
let(:balance_result) do
"0x0"
Expand All @@ -135,23 +189,51 @@ def stub_request_wrapper(method, params, result)
stub_request_wrapper("getBalance", [account_address, "0x0"], balance_result)
end

let(:balance_params_error_code) { -32700 }
let(:balance_params_error_message) { "invalid format: [0x0]" }
let(:balance_params_error) do
{
code: balance_params_error_code,
message: balance_params_error_message
}
end
let(:mock_get_balance_params_error) do
stub_request_error_wrapper("getBalance", ["0x0", "0x0"], balance_params_error)
end

let(:abi_result) do
"0x"
end
let(:mock_get_abi) do
stub_request_wrapper("getAbi", [account_address, "0x0"], abi_result)
end

let(:abi_params_error_code) { -32700 }
let(:abi_params_error_message) { "invalid format: [0x0]" }
let(:abi_params_error) do
{
code: abi_params_error_code,
message: abi_params_error_message
}
end
let(:mock_get_abi_params_error) do
stub_request_error_wrapper("getAbi", ["0x0", "0x0"], abi_params_error)
end

let(:mock_all) do
mock_block_number
mock_get_block_by_number_zero
mock_get_block_by_number_zero_params_error
mock_get_block_by_number_one
mock_get_transaction
mock_get_transaction
mock_get_transaction_params_error
mock_get_transaction_receipt
mock_get_meta_data
mock_get_meta_data_params_error
mock_get_balance
mock_get_balance_params_error
mock_get_abi
mock_get_abi_params_error
end
end
end

0 comments on commit 972f530

Please sign in to comment.