Skip to content

Commit

Permalink
[ᚬmaster] Rc/v0.4.1 (#401)
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.4.1
  • Loading branch information
shaojunda authored Sep 16, 2019
2 parents 3c0938a + 8d12b64 commit 6ee9de9
Show file tree
Hide file tree
Showing 47 changed files with 520 additions and 221 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ inherit_from:
inherit_mode:
merge:
- Exclude
require: rubocop-performance
require:
- rubocop-performance
- rubocop-rails
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.3
2.6.4
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
addons:
postgresql: "10"
rvm:
- 2.5.3
- 2.6.4
env:
global:
- RAILS_ENV=test
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# [](https://github.com/shaojunda/ckb-explorer/compare/v0.4.0...v) (2019-09-16)


### Features

* add consumed_tx_hash and generated_tx_hash to display_output ([80964de](https://github.com/shaojunda/ckb-explorer/commit/80964de))
* add generated_tx_hash to display_inputs ([a8ea5b0](https://github.com/shaojunda/ckb-explorer/commit/a8ea5b0))
* update api doc ([be22e6d](https://github.com/shaojunda/ckb-explorer/commit/be22e6d))
* upgrade to Rails 6.0 ([33d8ff8](https://github.com/shaojunda/ckb-explorer/commit/33d8ff8))


### Performance Improvements

* add cache on display info ([a39f282](https://github.com/shaojunda/ckb-explorer/commit/a39f282))
* cache cell input ([4fbdda6](https://github.com/shaojunda/ckb-explorer/commit/4fbdda6))
* enable parallel testing ([a1a696c](https://github.com/shaojunda/ckb-explorer/commit/a1a696c))
* get ckb transactions sql ([c61676e](https://github.com/shaojunda/ckb-explorer/commit/c61676e))
* handling race condition ([a455538](https://github.com/shaojunda/ckb-explorer/commit/a455538))
* use cache on serializer ([3ed1b4b](https://github.com/shaojunda/ckb-explorer/commit/3ed1b4b))



# (2019-09-07)


Expand Down
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "2.5.3"
ruby "2.6.4"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem "rails", "~> 5.2.2", ">= 5.2.2.1"
gem "rails", "~> 6.0.0"
# Use postgresql as the database for Active Record
gem "pg", ">= 0.18", "< 2.0"
# Use Puma as the app server
gem "puma", "~> 3.11"
gem "puma", "~> 4.1.1"
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
Expand Down Expand Up @@ -90,6 +90,7 @@ group :development do
gem "spring"
gem "spring-watcher-listen", "~> 2.0.0"
gem "rubocop", require: false
gem "rubocop-rails"
gem "rubocop-performance"
gem "awesome_print"
gem "annotate"
Expand Down
119 changes: 70 additions & 49 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,69 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.3)
actionpack (= 5.2.3)
actioncable (6.0.0)
actionpack (= 6.0.0)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.3)
actionpack (= 5.2.3)
actionview (= 5.2.3)
activejob (= 5.2.3)
actionmailbox (6.0.0)
actionpack (= 6.0.0)
activejob (= 6.0.0)
activerecord (= 6.0.0)
activestorage (= 6.0.0)
activesupport (= 6.0.0)
mail (>= 2.7.1)
actionmailer (6.0.0)
actionpack (= 6.0.0)
actionview (= 6.0.0)
activejob (= 6.0.0)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.3)
actionview (= 5.2.3)
activesupport (= 5.2.3)
actionpack (6.0.0)
actionview (= 6.0.0)
activesupport (= 6.0.0)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.3)
activesupport (= 5.2.3)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.0.0)
actionpack (= 6.0.0)
activerecord (= 6.0.0)
activestorage (= 6.0.0)
activesupport (= 6.0.0)
nokogiri (>= 1.8.5)
actionview (6.0.0)
activesupport (= 6.0.0)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.2.3)
activesupport (= 5.2.3)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.0.0)
activesupport (= 6.0.0)
globalid (>= 0.3.6)
activemodel (5.2.3)
activesupport (= 5.2.3)
activerecord (5.2.3)
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
activemodel (6.0.0)
activesupport (= 6.0.0)
activerecord (6.0.0)
activemodel (= 6.0.0)
activesupport (= 6.0.0)
activerecord-import (1.0.2)
activerecord (>= 3.2)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
activestorage (6.0.0)
actionpack (= 6.0.0)
activejob (= 6.0.0)
activerecord (= 6.0.0)
marcel (~> 0.3.1)
activesupport (5.2.3)
activesupport (6.0.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.1, >= 2.1.8)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
annotate (2.7.5)
activerecord (>= 3.2, < 7.0)
rake (>= 10.4, < 13.0)
ansi (1.5.0)
arel (9.0.0)
ast (2.4.0)
awesome_print (1.8.0)
bitcoin-secp256k1 (0.5.2)
Expand Down Expand Up @@ -180,7 +193,7 @@ GEM
rake
mina-multistage (1.0.3)
mina (~> 1.0)
mini_mime (1.0.1)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest-reporters (1.3.6)
Expand All @@ -194,7 +207,7 @@ GEM
net-http-persistent (3.0.1)
connection_pool (~> 2.2)
newrelic_rpm (6.4.0.356)
nio4r (2.3.1)
nio4r (2.4.0)
nokogiri (1.10.4)
mini_portile2 (~> 2.4.0)
open4 (1.3.4)
Expand All @@ -211,7 +224,8 @@ GEM
pry-nav (0.3.0)
pry (>= 0.9.10, < 0.13.0)
public_suffix (3.1.0)
puma (3.12.1)
puma (4.1.1)
nio4r (~> 2.0)
rack (2.0.7)
rack-accept (0.4.5)
rack (>= 0.4)
Expand All @@ -220,32 +234,34 @@ GEM
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.3)
actioncable (= 5.2.3)
actionmailer (= 5.2.3)
actionpack (= 5.2.3)
actionview (= 5.2.3)
activejob (= 5.2.3)
activemodel (= 5.2.3)
activerecord (= 5.2.3)
activestorage (= 5.2.3)
activesupport (= 5.2.3)
rails (6.0.0)
actioncable (= 6.0.0)
actionmailbox (= 6.0.0)
actionmailer (= 6.0.0)
actionpack (= 6.0.0)
actiontext (= 6.0.0)
actionview (= 6.0.0)
activejob (= 6.0.0)
activemodel (= 6.0.0)
activerecord (= 6.0.0)
activestorage (= 6.0.0)
activesupport (= 6.0.0)
bundler (>= 1.3.0)
railties (= 5.2.3)
railties (= 6.0.0)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
rails-html-sanitizer (1.2.0)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.3)
actionpack (= 5.2.3)
activesupport (= 5.2.3)
railties (6.0.0)
actionpack (= 6.0.0)
activesupport (= 6.0.0)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
thor (>= 0.20.3, < 2.0)
rainbow (3.0.0)
rake (12.3.2)
rake (12.3.3)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
Expand All @@ -261,6 +277,9 @@ GEM
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-performance (1.3.0)
rubocop (>= 0.68.0)
rubocop-rails (2.0.1)
rack (>= 1.1)
rubocop (>= 0.70.0)
ruby-progressbar (1.10.1)
ruby_dep (1.5.0)
safe_yaml (1.0.5)
Expand Down Expand Up @@ -329,6 +348,7 @@ GEM
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
zeitwerk (2.1.9)

PLATFORMS
ruby
Expand Down Expand Up @@ -358,12 +378,13 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
pry
pry-nav
puma (~> 3.11)
puma (~> 4.1.1)
rack-cors
rails (~> 5.2.2, >= 5.2.2.1)
rails (~> 6.0.0)
redis (~> 4.0, >= 4.0.3)
rubocop
rubocop-performance
rubocop-rails
ruby-progressbar
shoulda-context
shoulda-matchers
Expand All @@ -379,7 +400,7 @@ DEPENDENCIES
webmock

RUBY VERSION
ruby 2.5.3p105
ruby 2.6.4p104

BUNDLED WITH
2.0.1
2 changes: 1 addition & 1 deletion app/controllers/api/v1/cell_input_data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CellInputDataController < ApplicationController
before_action :validate_query_params

def show
cell_input = CellInput.find(params[:id])
cell_input = CellInput.cached_find(params[:id])
cell_output = cell_input.find_cell_output!

render json: CellOutputDataSerializer.new(cell_output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CellInputLockScriptsController < ApplicationController
before_action :validate_query_params

def show
cell_input = CellInput.find(params[:id])
cell_input = CellInput.cached_find(params[:id])
lock_script = cell_input.find_lock_script!

render json: LockScriptSerializer.new(lock_script)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CellInputTypeScriptsController < ApplicationController
before_action :validate_query_params

def show
cell_input = CellInput.find(params[:id])
cell_input = CellInput.cached_find(params[:id])
type_script = cell_input.find_type_script!

render json: TypeScriptSerializer.new(type_script)
Expand Down
4 changes: 2 additions & 2 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.find_or_create_address(lock_script)
address_hash = CkbUtils.generate_address(lock_script)
lock_hash = lock_script.compute_hash

Rails.cache.fetch(lock_hash, expires_in: 1.day) do
Rails.cache.fetch(lock_hash, expires_in: 1.day, race_condition_ttl: 3.seconds) do
Address.find_or_create_by!(address_hash: address_hash, lock_hash: lock_hash)
end
end
Expand All @@ -27,7 +27,7 @@ def self.find_address!(query_key)
end

def self.cached_find(query_key)
Rails.cache.fetch([name, query_key]) do
Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do
if QueryKeyUtils.valid_hex?(query_key)
find_by(lock_hash: query_key)
else
Expand Down
2 changes: 1 addition & 1 deletion app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.find_block!(query_key)
end

def self.cached_find(query_key)
Rails.cache.fetch([name, query_key]) do
Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do
if QueryKeyUtils.valid_hex?(query_key)
block = where(block_hash: query_key).first
else
Expand Down
23 changes: 20 additions & 3 deletions app/models/cell_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ class CellInput < ApplicationRecord
belongs_to :ckb_transaction
belongs_to :block

after_commit :flush_cache

def find_lock_script!
previous_cell_output!.lock_script
Rails.cache.fetch(["CellInput", id, "lock_script"], race_condition_ttl: 3.seconds) do
previous_cell_output!.lock_script
end
end

def find_type_script!
previous_cell_output!.type_script
Rails.cache.fetch(["CellInput", id, "type_script"], race_condition_ttl: 3.seconds) do
previous_cell_output!.type_script
end
end

def find_cell_output!
Expand All @@ -23,6 +29,15 @@ def previous_cell_output
CellOutput.find_by(tx_hash: tx_hash, cell_index: cell_index)
end

def self.cached_find(id)
Rails.cache.fetch([name, id], race_condition_ttl: 3.seconds) { find(id) }
end

def flush_cache
Rails.cache.delete_matched("CellInput/#{id}*")
Rails.cache.delete_matched("previous_cell_output*")
end

private

def previous_cell_output!
Expand All @@ -31,7 +46,9 @@ def previous_cell_output!
tx_hash = previous_output["tx_hash"]
cell_index = previous_output["index"].to_i

CellOutput.find_by!(tx_hash: tx_hash, cell_index: cell_index)
Rails.cache.fetch("previous_cell_output/#{tx_hash}/#{cell_index}", race_condition_ttl: 3.seconds) do
CellOutput.find_by!(tx_hash: tx_hash, cell_index: cell_index)
end
end
end

Expand Down
Loading

0 comments on commit 6ee9de9

Please sign in to comment.