Skip to content

Commit

Permalink
Rename gas_used to quota_used
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 2, 2018
1 parent f549b8b commit 727bcd3
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

A blockchain explorer cache server for [Nervos AppChain](https://docs.nervos.org/nervos-appchain-docs/#/).

Now upgrade to work with CITA v0.18
Now upgrade to work with CITA v0.20

⚠️ NOTE: If you upgrade your chain to 0.18 from 0.17, after upgrade, you should stop sync task(`rake daemons:sync:stop`) and run `rake zero18:update` to update your old data, and start your sync task (`rake daemons:sync:start`), then restart your server 😄

⚠️ NOTE: when update this version, you should run `bundle exec rake event_logs:fix_old` to sync event logs that it's transaction already saved.

⚠️ NOTE: add `errorMessage` to `transactions`, run `bundle exec rake transactions:add_error_message` to add.

⚠️ NOTE: If you upgrade your chain to 0.20, after upgrade, you should stop sync task(`rake daemons:sync:stop`) and run `rake zero20:update` to update your old data, and start your sync task (`rake daemons:sync:start`), then restart your server

## Docker

If you just want to run this, just use [docker 🐳](https://docs.docker.com/install)
Expand Down
2 changes: 1 addition & 1 deletion app/models/cita_sync/persist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def save_transaction(hash)
receipt_result = receipt_data["result"]
unless receipt_result.nil?
transaction.contract_address = receipt_result["contractAddress"]
transaction.gas_used = receipt_result["gasUsed"]
transaction.quota_used = receipt_result["quotaUsed"] || receipt_result["gasUsed"]
transaction.error_message = receipt_result["errorMessage"]
end
transaction.save
Expand Down
4 changes: 3 additions & 1 deletion app/models/erc20_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Erc20Transfer < ApplicationRecord

before_save :downcase_before_save

alias_attribute :gas_used, :quota_used

# validates :event_log, uniqueness: true

# first of topics: event signature
Expand Down Expand Up @@ -65,7 +67,7 @@ def save_from_event_log(event_log)
address: event_log.address,
transaction_hash: event_log.transaction_hash,
block_number: event_log.block_number,
gas_used: event_log.tx.gas_used,
quota_used: event_log.tx.quota_used,
from: info[:from],
to: info[:to],
value: info[:value],
Expand Down
2 changes: 2 additions & 0 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class Transaction < ApplicationRecord

# validates :block, presence: true
validates :cita_hash, presence: true, uniqueness: true

alias_attribute :gas_used, :quota_used
end
1 change: 1 addition & 0 deletions app/serializers/api/erc20_transfer_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Api::Erc20TransferSerializer < ActiveModel::Serializer
attributes :address, :from, :to, :value, :timestamp
attribute :block_number, key: :blockNumber
attribute :gas_used, key: :gasUsed
attribute :quota_used, key: :quotaUsed
attribute :transaction_hash, key: :hash
attribute :chain_id, key: :chainId
attribute :chain_name, key: :chainName
Expand Down
1 change: 1 addition & 0 deletions app/serializers/api/transaction_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Api::TransactionSerializer < ActiveModel::Serializer
attributes :value, :to, :from, :content
attribute :cita_hash, key: :hash
attribute :gas_used, key: :gasUsed
attribute :quota_used, key: :quotaUsed
attribute :block_number, key: :blockNumber
attributes :timestamp
attribute :chain_id, key: :chainId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameQuotaUsedToTransactions < ActiveRecord::Migration[5.2]
def change
rename_column :transactions, :gas_used, :quota_used
rename_column :erc20_transfers, :gas_used, :quota_used
end
end
6 changes: 3 additions & 3 deletions 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_11_01_035658) do
ActiveRecord::Schema.define(version: 2018_11_01_095358) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -54,7 +54,7 @@
t.string "transaction_hash"
t.bigint "timestamp"
t.string "block_number"
t.string "gas_used"
t.string "quota_used"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "event_log_id"
Expand Down Expand Up @@ -120,7 +120,7 @@
t.text "data"
t.string "value"
t.string "contract_address"
t.string "gas_used"
t.string "quota_used"
t.string "error_message"
t.bigint "version", default: 0
t.jsonb "chain_id"
Expand Down
15 changes: 15 additions & 0 deletions lib/tasks/zero20.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

namespace :zero20 do
desc "fix 0.19 block data to 0.20 style, if you upgrade your chain to 0.20 and want to fix old data, run this command"
task update: :environment do
sql = %{ UPDATE blocks SET header = replace(header::TEXT,'"gasUsed":','"quotaUsed":')::jsonb; }
ApplicationRecord.connection.execute(sql)
end

desc "rollback, chain 0.20 style block data to 0.19"
task rollback: :environment do
sql = %{ UPDATE blocks SET header = replace(header::TEXT,'"quotaUsed":','"gasUsed":')::jsonb; }
ApplicationRecord.connection.execute(sql)
end
end
6 changes: 3 additions & 3 deletions spec/factories/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
header { {
"proof" => nil,
"number" => "0x0",
"gasUsed" => "0x0",
"quotaUsed" => "0x0",
"prevHash" => "0x0000000000000000000000000000000000000000000000000000000000000000",
"proposer" => "0x0000000000000000000000000000000000000000",
"stateRoot" => "0xc9509aed05b800c7d9a27395b1f7cdde9428f56a72e9f07661c1f1731d7dda44",
Expand All @@ -33,7 +33,7 @@
"stateRoot": "0x9b3609aca48d23cadcbab0d768fa0d2187807a23f4ae19742db128a9a64f3bfc",
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"gasUsed": "0x0",
"quotaUsed": "0x0",
"proof": nil,
"proposer": "0x0000000000000000000000000000000000000000"
} }
Expand All @@ -54,7 +54,7 @@
"stateRoot": "0x048523e8326427968d05673210cc77a8f76e60d0b9170d1bdc1d49c131da9c85",
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"gasUsed": "0x0",
"quotaUsed": "0x0",
"proof": nil,
"proposer": "0x91827976af27e1fd405469b00dc8d3b0ea2203f6"
} }
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/erc20_transfers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
value { 10 }
transaction_hash { "0x14b06be4067ba65d05e41d8821e2cf7d572a65b1bf53857a6a504ec42e69fdfd" }
block_number { "0x18a1ec" }
gas_used { "0x2d483" }
quota_used { "0x2d483" }

association :tx, factory: :transaction
association :event_log, factory: :erc20_event_log
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
data { "0x" }
value { "0x0000000000000000000000000000000000000000000000000000000000001000" }
contract_address { "0x89be88054e2ee94911549be521ab1241c7700a1b" }
gas_used { "0x2d483" }
quota_used { "0x2d483" }

association :block, factory: :block_one
end
Expand Down

0 comments on commit 727bcd3

Please sign in to comment.