Created
August 24, 2013 18:44
-
-
Save svasva/6329785 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Mysql = Npm.require 'mysql' | |
Future = Npm.require 'fibers/future' | |
class Mining | |
constructor: (host, port, dbname, dbuser, dbpass) -> | |
@dbSettings = | |
host: host | |
port: port | |
user: dbuser | |
password: dbpass | |
database: dbname | |
@connect() | |
bindCb: (mcb) -> Meteor.bindEnvironment mcb, (e) -> console.log e | |
connect: -> | |
@db = Mysql.createConnection @dbSettings | |
stop: -> | |
fut = new Future | |
@db.end (err) -> | |
if err then fut.return(err) else fut.return(true) | |
fut.wait() | |
getRewards: (table, since, pplns_interval, cb) -> | |
f = @bindCb (err, rows) -> | |
blocks = [] | |
for i in [0 ... rows.length] by 2 | |
[blockData, rewardsData] = rows[i .. i + 1] | |
continue unless blockData[0] | |
block = | |
stats: {} | |
time: blockData[0].block_time | |
userId: blockData[0].username.split('.')[0] | |
shares: blockData[0].shares | |
for stat in rewardsData | |
userId = stat.username.split('.')[0] | |
block.stats[userId] ||= {shares: 0, reward: 0} | |
block.stats[userId].reward += stat.reward | |
block.stats[userId].shares += stat.shares | |
blocks.push block | |
cb(blocks) | |
q = "call get_rewards(?, FROM_UNIXTIME(?), ?)" | |
@db.query q, [table, since, pplns_interval], f | |
getPoolHashrate: (table, interval, share_diff, cb) -> | |
f = @bindCb (err, rows) -> | |
if err then cb(false) else cb(rows[0]?[0]?.hashrate) | |
q = "call pool_hashrate(?, ?, ?)" | |
@db.query q, [table, interval, share_diff], f | |
getWorkerHashrates: (table, interval, share_diff, cb) -> | |
f = @bindCb (err, rows) -> | |
if err then cb(false) else cb(rows[0]) | |
q = "call worker_hashrates(?, ?, ?)" | |
@db.query q, [table, interval, share_diff], f | |
addWorker: (name, pass, cb) -> | |
ff = @bindCb (err, rows) -> | |
if err then cb(false) else cb(rows[0]?.id) | |
f = @bindCb (err, rows) => | |
if err then cb(false) | |
else | |
qId = "select id from pool_worker where username = ?" | |
@db.query qId, [name], ff | |
q = "insert into pool_worker (username, password) values (?, ?)" | |
@db.query q, [name, pass], f | |
updateWorker: (id, name, pass, cb = null) -> | |
q = "update pool_worker set username=?, password=? where id=?" | |
f = @bindCb (err, rows) -> | |
return unless cb | |
if err then cb(false) else cb(true) | |
@db.query q, [name, pass, id], f | |
removeWorker: (id, cb = null) -> | |
f = @bindCb (err, rows) -> | |
return unless cb | |
if err then cb(false) else cb(true) | |
q = "delete from pool_worker where id=?" | |
@db.query q, [id], f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment