Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/Unofficial miners/Ruby_Miner.rb
920 views
1
#!/usr/bin/env ruby
2
# Minimal version of Duino-Coin PC Miner, useful for developing own apps
3
# revox 2020-2022
4
require 'socket'
5
require 'digest'
6
require 'net/http'
7
require 'json' # Only default ruby libraries
8
require 'time'
9
require 'colorize' # gem install colorize
10
11
username = "revox" # Replace this with your username
12
minerId = "None" # Custom miner identifier
13
14
# Disable debug output
15
$VERBOSE = nil
16
17
puts("\n ‖ Minimal DUCO-S1 Ruby Miner")
18
puts(" ‖ Duino-Coin community 2020-2022")
19
puts(" ‖ https://github.com/revoxhere/duino-coin\n\n")
20
21
# Server IP file url
22
url = 'https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt'
23
uri = URI(url)
24
# Receive server IP and port
25
response = Net::HTTP.get(uri)
26
response = response.split("\n")
27
serverip = response[0]
28
serverport = response[1]
29
sharecount = 0
30
31
# Connect to the server
32
s = TCPSocket.open(serverip, serverport)
33
# Read server version
34
SERVER_VER = s.gets(3)
35
puts(" net ".colorize(:color => :white, :background => :blue) + " Connected".colorize(:color => :yellow) + " to the master Duino-Coin server ("+ SERVER_VER+ ")")
36
37
# Mining loop
38
puts(" cpu ".colorize(:color => :white, :background => :yellow) + " Mining thread is starting".colorize(:color => :yellow) + " using DUCO-S1 algorithm")
39
loop do
40
# Send job request
41
s.puts("JOB,"+String(username)+",MEDIUM,")
42
# Read job
43
job = s.read(87)
44
# Split into previous block hash, result hash and difficulty
45
job = job.split(',')
46
difficulty = job[2]
47
# Measure starting time
48
timeStart = Process.clock_gettime(Process::CLOCK_MONOTONIC)
49
for result in 0..100 * Integer(difficulty) + 1 do
50
# DUCO-S1 loop - find the numeric result
51
# By checking if last block hash + n is result hash
52
sha1 = Digest::SHA1.hexdigest String(String(job[0])+String(result))
53
# If result is found
54
if sha1 == String(job[1])
55
# Measure ending time
56
timeStop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
57
timeDiff = timeStop - timeStart
58
# Calculate hashrate
59
hashrate = result / timeDiff
60
# Send numeric result to the server
61
s.write(String(result) + "," + String(hashrate) + ",Minimal Ruby Miner (DUCO-S1)," + String(minerId))
62
# Receive result feedback
63
SHAREFEED = s.read(4)
64
sharecount += 1
65
# Check wheter it was accepted or not
66
if SHAREFEED == "GOOD" or SHAREFEED == "BLOCK"
67
puts(" cpu ".colorize(:color => :white, :background => :yellow) + " Accepted".colorize(:color => :green) + " share #" + String(sharecount) + ", speed: " + String(Integer(hashrate / 1000)) + "kH/s @ diff " + String(difficulty))
68
break
69
if SHAREFEED == "INVU"
70
puts "Invalid username!"
71
exit
72
if SHAREFEED == "BAD"
73
puts(" cpu ".colorize(:color => :white, :background => :yellow) + " Rejected".colorize(:color => :red) + " share #" + String(sharecount) + ", speed: " + String(Integer(hashrate / 1000)) + "kH/s @ diff " + String(difficulty))
74
break
75
end
76
end
77
end
78
end
79
end
80
end
81
82