-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c794f6
commit 91efc85
Showing
8 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class ValidatorCache < ApplicationRecord | ||
validates :name, presence: true, uniqueness: true | ||
|
||
# increase a validator's count | ||
# | ||
# @param name [String] Block header proposer | ||
# @return [void] | ||
def self.increase(name) | ||
# add transaction for lock cache | ||
ApplicationRecord.transaction do | ||
return if name.blank? | ||
cache = ValidatorCache.find_by(name: name) | ||
if cache.nil? | ||
ValidatorCache.create(name: name, counter: 1) | ||
else | ||
# lock it | ||
cache.lock! | ||
cache.update!(counter: cache.counter + 1) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class CreateValidatorCaches < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :validator_caches do |t| | ||
t.string :name, null: false, index: { unique: true } | ||
t.integer :counter | ||
|
||
t.timestamps | ||
end | ||
|
||
reversible do |dir| | ||
dir.up do | ||
# create validator cache for present blocks | ||
ApplicationRecord.transaction do | ||
Block.where.not(block_number: 0).group("header ->> 'proposer'").count.each do |k, v| | ||
cache = ValidatorCache.find_or_create_by(name: k) | ||
cache.lock! | ||
cache.update(counter: v) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :validator_cache, class: 'ValidatorCache' do | ||
name "0x91827976af27e1fd405469b00dc8d3b0ea2203f6" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ValidatorCache, type: :model do | ||
let(:proposer) { attributes_for(:validator_cache)[:name] } | ||
|
||
context "increase" do | ||
it "counter be 1 if not exist" do | ||
expect(ValidatorCache.exists?(name: proposer)).to be false | ||
expect { | ||
ValidatorCache.increase(proposer) | ||
}.to change { ValidatorCache.count }.by(1) | ||
cache = ValidatorCache.find_by(name: proposer) | ||
expect(cache.counter).to eq 1 | ||
end | ||
|
||
it "counter add 1 if exist" do | ||
counter = 5 | ||
create :validator_cache, counter: counter | ||
expect { | ||
ValidatorCache.increase(proposer) | ||
}.to change { ValidatorCache.count }.by(0) | ||
cache = ValidatorCache.find_by(name: proposer) | ||
expect(cache.counter).to eq (counter + 1) | ||
end | ||
end | ||
end |