-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 289f880
Showing
11 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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,94 @@ | ||
(ns status-im.ethereum.json-rpc | ||
(:require [clojure.string :as string] | ||
[re-frame.core :as re-frame] | ||
[status-im.ethereum.abi-spec :as abi-spec] | ||
[status-im.ethereum.decode :as decode] | ||
[status-im.native-module.core :as status] | ||
[status-im.utils.money :as money] | ||
[status-im.utils.types :as types] | ||
[taoensso.timbre :as log])) | ||
|
||
(def json-rpc-api | ||
{"eth_call" {} | ||
"eth_getBalance" | ||
{:on-result money/bignumber} | ||
"eth_estimateGas" | ||
{:on-result #(money/bignumber (int (* % 1.2)))} | ||
"eth_gasPrice" | ||
{:on-result money/bignumber} | ||
"eth_getBlockByHash" | ||
{:on-result #(-> (update % :number decode/uint) | ||
(update :timestamp decode/uint))} | ||
"eth_getTransactionByHash" {} | ||
"eth_getTransactionReceipt" {} | ||
"eth_newBlockFilter" {:subscription? true} | ||
"eth_newFilter" {:subscription? true} | ||
"eth_syncing" {} | ||
"net_version" {} | ||
"shhext_enableInstallation" {} | ||
"shhext_disableInstallation" {} | ||
"shhext_getOurInstallations" {} | ||
"shhext_setInstallationMetadata" {} | ||
"shhext_loadFilters" {} | ||
"shhext_loadFilter" {} | ||
"shhext_removeFilters" {} | ||
"status_joinPublicChat" {} | ||
"status_chats" {} | ||
"status_startOneOnOneChat" {} | ||
"status_removeChat" {}}) | ||
|
||
(defn call | ||
[{:keys [method params on-success on-error]}] | ||
(when-let [method-options (json-rpc-api method)] | ||
(let [{:keys [id on-result subscription?] | ||
:or {on-result identity | ||
id 1 | ||
params []}} method-options | ||
on-error (or on-error | ||
#(log/warn :json-rpc/error method :params params :error %))] | ||
(if (nil? method) | ||
(log/error :json-rpc/method-not-found method) | ||
(status/call-private-rpc | ||
(types/clj->json {:jsonrpc "2.0" | ||
:id id | ||
:method (if subscription? | ||
"eth_subscribeSignal" | ||
method) | ||
:params (if subscription? | ||
[method params] | ||
params)}) | ||
(fn [response] | ||
(if (string/blank? response) | ||
(on-error {:message "Blank response"}) | ||
(let [{:keys [error result] :as response2} (types/json->clj response)] | ||
(if error | ||
(on-error error) | ||
(if subscription? | ||
(re-frame/dispatch | ||
[:ethereum.callback/subscription-success | ||
result on-success]) | ||
(on-success (on-result result)))))))))))) | ||
|
||
(defn eth-call | ||
[{:keys [contract method params outputs on-success on-error block] | ||
:or {block "latest" | ||
params []}}] | ||
(call {:method "eth_call" | ||
:params [{:to contract | ||
:data (abi-spec/encode method params)} | ||
(if (int? block) | ||
(abi-spec/number-to-hex block) | ||
block)] | ||
:on-success | ||
(if outputs | ||
#(on-success (abi-spec/decode % outputs)) | ||
on-success) | ||
:on-error | ||
on-error})) | ||
|
||
;; effects | ||
(re-frame/reg-fx | ||
:json-rpc/call | ||
(fn [params] | ||
(doseq [param params] | ||
(call param)))) |
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,55 @@ | ||
(ns status-im.ethereum.macros | ||
(:require [clojure.string :as string] | ||
[clojure.java.io :as io])) | ||
|
||
(defn token-icon-path | ||
[network symbol] | ||
(let [s (str "./resources/images/tokens/" (name network) "/" (name symbol) ".png") | ||
image (gensym)] | ||
(if (.exists (io/file s)) | ||
`(let [~image (atom nil)] | ||
(fn [] | ||
(or @~image | ||
(reset! ~image (js/require ~s))))) | ||
`(let [~image (atom nil)] | ||
(fn [] | ||
(or | ||
@~image | ||
(reset! ~image | ||
(js/require "./resources/images/tokens/default-token.png")))))))) | ||
|
||
(defn- token->icon [network {:keys [icon symbol]}] | ||
;; Tokens can define their own icons. | ||
;; If not try to make one using a local image as resource, if it does not exist fallback to default. | ||
(or icon (token-icon-path network symbol))) | ||
|
||
(defmacro resolve-icons | ||
"In react-native arguments to require must be static strings. | ||
Resolve all icons at compilation time so no variable is used." | ||
[network tokens] | ||
(mapv #(assoc-in % [:icon :source] (token->icon network %)) tokens)) | ||
|
||
(defn network->icon [network] | ||
(let [s (str "./resources/images/tokens/" (name network) "/0-native.png") | ||
image (gensym)] | ||
(if (.exists (io/file s)) | ||
`(let [~image (atom nil)] | ||
(fn [] | ||
(or @~image | ||
(reset! ~image (js/require ~s))))) | ||
`(let [~image (atom nil)] | ||
(fn [] | ||
(or | ||
@~image | ||
(reset! ~image | ||
(js/require "./resources/images/tokens/default-native.png")))))))) | ||
|
||
(defmacro resolve-native-currency-icons | ||
"In react-native arguments to require must be static strings. | ||
Resolve all icons at compilation time so no variable is used." | ||
[all-native-currencies] | ||
(into {} | ||
(map (fn [[network native-currency]] | ||
[network (assoc-in native-currency | ||
[:icon :source] | ||
(network->icon network))]) all-native-currencies))) |
Binary file not shown.
Submodule dbo
added at
c8571b
Submodule issues
added at
886f02
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,34 @@ | ||
defmodule SimpleQueue do | ||
use GenServer | ||
|
||
### GenServer API | ||
|
||
def init(state), do: {:ok, state} | ||
|
||
def handle_call(:deq, _from, [value | state]) do | ||
{:reply, value, state} | ||
end | ||
|
||
def handle_call(:deq, _from, []), do: {:reply, nil, []} | ||
|
||
def handle_call(:queue, _from, state), do: {:reply, state, state} | ||
|
||
|
||
##ASYNC: | ||
|
||
def handle_cast({:enq, value}, state) do | ||
{:noreply, state ++ [value]} | ||
end | ||
|
||
|
||
### Client API | ||
|
||
def start_link(state \\ []) do | ||
GenServer.start_link(__MODULE__, state, name: __MODULE__) | ||
end | ||
|
||
def queue, do: GenServer.call(__MODULE__, :queue) | ||
def enq(value), do: GenServer.cast(__MODULE__, {:enq, value}) | ||
def deq, do: GenServer.call(__MODULE__, :deq) | ||
|
||
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,33 @@ | ||
defmodule Tasks do | ||
use GenServer | ||
|
||
### GenServer API | ||
|
||
def init(state), do: {:ok, state} | ||
|
||
def handle_call(:done, _from, [value | state]) do | ||
{:reply, value, state} | ||
end | ||
|
||
def handle_call(:done, _from, []), do: {:reply, nil, []} | ||
|
||
def handle_call(:list, _from, state), do: {:reply, state, state} | ||
|
||
|
||
## ASYNC | ||
|
||
def handle_cast({:push, value}, state) do | ||
{:noreply, state ++ [value]} | ||
end | ||
|
||
## CLIENT API | ||
|
||
def start_link(state \\ []) do | ||
GenServer.start_link(__MODULE__, state, name: __MODULE__) | ||
end | ||
|
||
def list, do: GenServer.call(__MODULE__, :list) | ||
def push(value), do: GenServer.cast(__MODULE__, {:push, value}) | ||
def done, do: GenServer.call(__MODULE__, :done) | ||
|
||
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,44 @@ | ||
class IrisController | ||
require 'rmagick' | ||
require "open-uri" | ||
|
||
include Magick | ||
include Secured | ||
|
||
def check | ||
@user = session[:userinfo] | ||
@iris_url = get_iris_url | ||
@upload_url = get_upload_url | ||
@compare = compare(@upload_url, @iris_url) | ||
end | ||
|
||
private | ||
|
||
def get_iris_url | ||
@user = session[:userinfo] | ||
url = @user[:extra][:raw_info][:app_metadata][:iris_image_url] | ||
return url | ||
end | ||
|
||
def compare(iris_new, iris_stored) | ||
img1 = Magick::Image.read(iris_new).first | ||
|
||
impimg = ImageList.new(iris_stored) | ||
img2 = impimg.cur_image | ||
|
||
res = img1.compare_channel(img2, Magick::MeanAbsoluteErrorMetric, AllChannels) | ||
diff = res[1] | ||
w, h = img1.columns, img1.rows | ||
pixelcount = w * h | ||
perc = (diff * 100) | ||
percentage = perc/pixelcount | ||
|
||
return percentage | ||
end | ||
|
||
def get_upload_url | ||
dir = Dir.glob("public/uploads/iris/*").sort_by { |f| File.mtime(f) }.reverse | ||
return dir[1] | ||
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,109 @@ | ||
#!/usr/bin/env ruby -w | ||
|
||
require 'thread' | ||
require 'yaml' | ||
|
||
require 'rrd_schema' | ||
require 'memcache_rrd' | ||
|
||
class Job | ||
|
||
attr_reader :name, :freq, :next | ||
|
||
def initialize(freq, name, block) | ||
@name=name | ||
@freq=freq | ||
@block=block | ||
|
||
initial_delay=rand [freq, 30].min | ||
@next=Time.now.to_i + initial_delay | ||
end | ||
|
||
def run | ||
# Compute the next time in such a way that doesn't drift | ||
now = Time.now.to_i | ||
while @next <= now | ||
@next=now + @freq | ||
end | ||
# And invoke the job | ||
@block.call | ||
end | ||
|
||
end | ||
|
||
class MonitorDaemon | ||
|
||
def initialize | ||
@jobs = [] | ||
end | ||
|
||
def add_task(freq, name, &block) | ||
@jobs << Job.new(freq, name, block) | ||
end | ||
|
||
def run_all | ||
while true | ||
now = Time.now.to_i | ||
# O(n) selection of current jobs to run | ||
@jobs.select {|j| j.next <= now}.each do |j| | ||
begin | ||
j.run | ||
$stdout.flush | ||
rescue => e | ||
$stderr.puts "Problem running #{j.name}: #{e}" | ||
end | ||
end | ||
|
||
# And an O(n) computation of the next job to run | ||
delay = @jobs.min {|a,b| a.next <=> b.next}.next - Time.now.to_i | ||
sleep delay if delay > 0 | ||
end | ||
end | ||
|
||
def add_proc_task(freq, url, user, pass) | ||
lh=LinuxHostInfo.new(url, user, pass) | ||
h=LinuxHostRRD.new(lh) | ||
add_task(freq, "proc #{url}") { h.rrd_inserts } | ||
end | ||
|
||
def add_memcached_task(freq, servers) | ||
m=MemCacheRRD.new(servers) | ||
add_task(freq, "memcached [#{servers.join(', ')}]") { m.rrd_inserts } | ||
end | ||
|
||
def add_rails_poll(freq, c) | ||
add_task(freq, "rails poll") do | ||
h=c['host'] | ||
u=c['user'] | ||
p=c['pass'] || '' | ||
d=c['database'] | ||
RailsLogRRD.new(RailsLog.new(h, u, p, d)).rrd_inserts | ||
end | ||
end | ||
|
||
end | ||
|
||
if $0 == __FILE__ | ||
raise "Need config file" if $*.empty? | ||
conf=YAML.load_file($*[0]) | ||
|
||
md=MonitorDaemon.new | ||
|
||
if conf['linux'] | ||
require 'sysinfo_rrd' | ||
conf['linux'].each do |h| | ||
md.add_proc_task 60, h['url'], h['user'], h['pass'] | ||
end | ||
end | ||
|
||
conf['memcached'].each do |server| | ||
md.add_memcached_task 60, [server] | ||
end | ||
|
||
if conf['db'] | ||
require 'rails_log' | ||
md.add_rails_poll 300, conf['db'] | ||
end | ||
|
||
md.run_all | ||
end |