From 727bcd3e1734ce59cb7c813c1e67d3f184cc34e5 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Fri, 2 Nov 2018 10:40:12 +0800 Subject: [PATCH] Rename gas_used to quota_used --- README.md | 4 +++- app/models/cita_sync/persist.rb | 2 +- app/models/erc20_transfer.rb | 4 +++- app/models/transaction.rb | 2 ++ app/serializers/api/erc20_transfer_serializer.rb | 1 + app/serializers/api/transaction_serializer.rb | 1 + ...101095358_rename_quota_used_to_transactions.rb | 6 ++++++ db/schema.rb | 6 +++--- lib/tasks/zero20.rake | 15 +++++++++++++++ spec/factories/blocks.rb | 6 +++--- spec/factories/erc20_transfers.rb | 2 +- spec/factories/transactions.rb | 2 +- 12 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20181101095358_rename_quota_used_to_transactions.rb create mode 100644 lib/tasks/zero20.rake diff --git a/README.md b/README.md index bc3bca1..15dc4a6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 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 😄 @@ -14,6 +14,8 @@ Now upgrade to work with CITA v0.18 ⚠️ 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) diff --git a/app/models/cita_sync/persist.rb b/app/models/cita_sync/persist.rb index fa760c8..2d82d6b 100644 --- a/app/models/cita_sync/persist.rb +++ b/app/models/cita_sync/persist.rb @@ -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 diff --git a/app/models/erc20_transfer.rb b/app/models/erc20_transfer.rb index 9e740e7..54df996 100644 --- a/app/models/erc20_transfer.rb +++ b/app/models/erc20_transfer.rb @@ -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 @@ -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], diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 7f739df..0c815bb 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -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 diff --git a/app/serializers/api/erc20_transfer_serializer.rb b/app/serializers/api/erc20_transfer_serializer.rb index 39ed4ba..5a41c97 100644 --- a/app/serializers/api/erc20_transfer_serializer.rb +++ b/app/serializers/api/erc20_transfer_serializer.rb @@ -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 diff --git a/app/serializers/api/transaction_serializer.rb b/app/serializers/api/transaction_serializer.rb index a845504..f38a159 100644 --- a/app/serializers/api/transaction_serializer.rb +++ b/app/serializers/api/transaction_serializer.rb @@ -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 diff --git a/db/migrate/20181101095358_rename_quota_used_to_transactions.rb b/db/migrate/20181101095358_rename_quota_used_to_transactions.rb new file mode 100644 index 0000000..9067601 --- /dev/null +++ b/db/migrate/20181101095358_rename_quota_used_to_transactions.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index df28ca5..0085b59 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" @@ -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" diff --git a/lib/tasks/zero20.rake b/lib/tasks/zero20.rake new file mode 100644 index 0000000..11b7614 --- /dev/null +++ b/lib/tasks/zero20.rake @@ -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 diff --git a/spec/factories/blocks.rb b/spec/factories/blocks.rb index 27a4ee2..19a3b03 100644 --- a/spec/factories/blocks.rb +++ b/spec/factories/blocks.rb @@ -7,7 +7,7 @@ header { { "proof" => nil, "number" => "0x0", - "gasUsed" => "0x0", + "quotaUsed" => "0x0", "prevHash" => "0x0000000000000000000000000000000000000000000000000000000000000000", "proposer" => "0x0000000000000000000000000000000000000000", "stateRoot" => "0xc9509aed05b800c7d9a27395b1f7cdde9428f56a72e9f07661c1f1731d7dda44", @@ -33,7 +33,7 @@ "stateRoot": "0x9b3609aca48d23cadcbab0d768fa0d2187807a23f4ae19742db128a9a64f3bfc", "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", - "gasUsed": "0x0", + "quotaUsed": "0x0", "proof": nil, "proposer": "0x0000000000000000000000000000000000000000" } } @@ -54,7 +54,7 @@ "stateRoot": "0x048523e8326427968d05673210cc77a8f76e60d0b9170d1bdc1d49c131da9c85", "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", - "gasUsed": "0x0", + "quotaUsed": "0x0", "proof": nil, "proposer": "0x91827976af27e1fd405469b00dc8d3b0ea2203f6" } } diff --git a/spec/factories/erc20_transfers.rb b/spec/factories/erc20_transfers.rb index 6f2eccb..fc9d8b8 100644 --- a/spec/factories/erc20_transfers.rb +++ b/spec/factories/erc20_transfers.rb @@ -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 diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb index 910cf7c..1e0b355 100644 --- a/spec/factories/transactions.rb +++ b/spec/factories/transactions.rb @@ -10,7 +10,7 @@ data { "0x" } value { "0x0000000000000000000000000000000000000000000000000000000000001000" } contract_address { "0x89be88054e2ee94911549be521ab1241c7700a1b" } - gas_used { "0x2d483" } + quota_used { "0x2d483" } association :block, factory: :block_one end