miner_mover 0.1.0.1 → 0.1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -10
- data/VERSION +1 -1
- data/lib/miner_mover/timer.rb +8 -8
- data/lib/miner_mover/worker.rb +38 -30
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e2daa3ba8e778e20c25e3a6363683b3163e1141f9438b53be890845acf5440e
|
4
|
+
data.tar.gz: b72dc662be9c50caa281e087b3708c72a53aa5e593eeb053b59a0679238d568b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3203db4674a641c4a53ebed0cea4aaa445ef9e120ad582bc1cfcc787541ee53a8677eff1e4936429c4eaa72ef686ef97941a7a929d2d5db4e88a678684005962
|
7
|
+
data.tar.gz: e0052a82fa40bcf19b3382ead683087e52f90e6065751a65ff9475aadd141beb0ebe098673acffe48784f90c672aa5990fe2c0e117a3db4bd1782252af7cec91
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ results as well. Ore is gathered at each depth; either a fixed amount or
|
|
18
18
|
randomized, based on depth. The amount of time spent mining each level is
|
19
19
|
independent and may be randomized.
|
20
20
|
|
21
|
-
https://github.com/rickhull/miner_mover/blob/
|
21
|
+
https://github.com/rickhull/miner_mover/blob/4f2a62f6d77316e780c7c13248698d4c57bb392e/lib/miner_mover/worker.rb#L96-L104
|
22
22
|
|
23
23
|
In this case, miners are rewarded by calculating `fibonacci(depth)`, using
|
24
24
|
classic, inefficient fibonacci.
|
@@ -31,7 +31,7 @@ A mover has a batch size, say 10. As the mover accumulates ore over time,
|
|
31
31
|
once the batch size is reached, the mover delivers the ore to the destination.
|
32
32
|
Larger batches take longer. The delivery time can be randomized.
|
33
33
|
|
34
|
-
https://github.com/rickhull/miner_mover/blob/
|
34
|
+
https://github.com/rickhull/miner_mover/blob/4f2a62f6d77316e780c7c13248698d4c57bb392e/lib/miner_mover/worker.rb#L152-L183
|
35
35
|
|
36
36
|
The time and work spent delivering ore can be simulated three ways,
|
37
37
|
configured via `:work_type`
|
@@ -44,7 +44,7 @@ configured via `:work_type`
|
|
44
44
|
|
45
45
|
## Install
|
46
46
|
|
47
|
-
You'll want to use **Ruby 3.x** to make the most of Fibers.
|
47
|
+
You'll want to use **Ruby 3.x** (CRuby) to make the most of Fibers.
|
48
48
|
|
49
49
|
### Dependencies
|
50
50
|
|
@@ -53,8 +53,6 @@ You'll want to use **Ruby 3.x** to make the most of Fibers.
|
|
53
53
|
* dotcfg
|
54
54
|
* fiber_scheduler
|
55
55
|
|
56
|
-
`gem install miner_mover` will take care of the following:
|
57
|
-
|
58
56
|
`gem install rake minitest dotcfg fiber_scheduler`
|
59
57
|
|
60
58
|
### Clone
|
@@ -85,6 +83,8 @@ rake test # Run tests
|
|
85
83
|
rake thread # run demo/thread
|
86
84
|
```
|
87
85
|
|
86
|
+
Try: `rake test`
|
87
|
+
|
88
88
|
## Rake Tasks
|
89
89
|
|
90
90
|
Included demonstration scripts can be executed via Rake tasks.
|
@@ -113,7 +113,7 @@ Rake tasks take care of `LOAD_PATH`, so the following is
|
|
113
113
|
|
114
114
|
## Exploration in `irb`
|
115
115
|
|
116
|
-
`$ irb -
|
116
|
+
`$ irb -I lib`
|
117
117
|
|
118
118
|
```
|
119
119
|
irb(main):001:0> include MinerMover
|
@@ -379,11 +379,15 @@ others. My favorites:
|
|
379
379
|
|
380
380
|
* `Process.fork` - when called with a block, the block is only executed in the
|
381
381
|
child subprocess
|
382
|
-
* `Process.spawn` - extensive options, nonblocking, call Process.wait(pid)
|
382
|
+
* `Process.spawn` - extensive options, nonblocking, call `Process.wait(pid)`
|
383
383
|
to get the result
|
384
|
-
* `Open3.popen3` - for access to STDIN
|
384
|
+
* `Open3.popen3` - for access to `STDIN` `STDOUT` `STDERR`
|
385
385
|
|
386
386
|
### IPC
|
387
387
|
|
388
|
-
* Pipes
|
389
|
-
|
388
|
+
* Pipes
|
389
|
+
- `IO.pipe` (streaming / bytes / unidirectional)
|
390
|
+
* Unix sockets
|
391
|
+
- `UNIXSocket.pair :RAW`
|
392
|
+
- `UNIXSocket.pair :DGRAM` (datagram / message / "like UDP")
|
393
|
+
- `UNIXSocket.pair :STREAM` (streaming / bytes / "like TCP")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1.1
|
data/lib/miner_mover/timer.rb
CHANGED
@@ -44,22 +44,22 @@ module MinerMover
|
|
44
44
|
t.strftime "%Y-%m-%d %H:%M:%S.%L"
|
45
45
|
end
|
46
46
|
|
47
|
-
def restart(
|
48
|
-
@start =
|
47
|
+
def restart(f = Timer.now)
|
48
|
+
@start = f
|
49
49
|
self
|
50
50
|
end
|
51
51
|
alias_method :initialize, :restart
|
52
52
|
|
53
|
-
def elapsed(
|
54
|
-
|
53
|
+
def elapsed(f = Timer.now)
|
54
|
+
f - @start
|
55
55
|
end
|
56
56
|
|
57
|
-
def elapsed_ms(
|
58
|
-
elapsed(
|
57
|
+
def elapsed_ms(f = Timer.now)
|
58
|
+
elapsed(f) * 1000
|
59
59
|
end
|
60
60
|
|
61
|
-
def elapsed_display(
|
62
|
-
Timer.elapsed_display(elapsed_ms(
|
61
|
+
def elapsed_display(f = Timer.now)
|
62
|
+
Timer.elapsed_display(elapsed_ms(f))
|
63
63
|
end
|
64
64
|
alias_method :to_s, :elapsed_display
|
65
65
|
alias_method :inspect, :elapsed_display
|
data/lib/miner_mover/worker.rb
CHANGED
@@ -77,32 +77,36 @@ module MinerMover
|
|
77
77
|
class Miner < Worker
|
78
78
|
attr_accessor :depth, :partial_reward
|
79
79
|
|
80
|
-
def initialize(depth:
|
81
|
-
partial_reward:
|
80
|
+
def initialize(depth: 5,
|
81
|
+
partial_reward: false,
|
82
82
|
variance: 0,
|
83
83
|
logging: false,
|
84
84
|
debugging: false,
|
85
85
|
timer: nil)
|
86
86
|
@partial_reward = partial_reward
|
87
87
|
@depth = depth
|
88
|
-
super(variance: variance,
|
89
|
-
|
88
|
+
super(variance: variance, timer: timer,
|
89
|
+
logging: logging, debugging: debugging)
|
90
90
|
end
|
91
91
|
|
92
92
|
def state
|
93
93
|
super.merge(depth: @depth, partial_reward: @partial_reward)
|
94
94
|
end
|
95
95
|
|
96
|
+
# return an array of integers representing ore mined at each depth
|
97
|
+
def mine_ores(depth = @depth)
|
98
|
+
# every new depth is a new mining operation
|
99
|
+
Array.new(depth) { |d|
|
100
|
+
# mine ore by calculating fibonacci for that depth
|
101
|
+
mined = MinerMover.fib(self.varied(d).round)
|
102
|
+
@partial_reward ? rand(1 + mined) : mined
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
106
|
+
# wrap the above method with logging, timing, and summing
|
96
107
|
def mine_ore(depth = @depth)
|
97
108
|
log format("MINE Depth %i", depth)
|
98
|
-
ores, elapsed = Timer.elapsed {
|
99
|
-
# every new depth is a new mining operation
|
100
|
-
Array.new(depth) { |d|
|
101
|
-
# mine ore by calculating fibonacci for that depth
|
102
|
-
mined = MinerMover.fib self.varied(d).round
|
103
|
-
@partial_reward ? rand(1 + mined) : mined
|
104
|
-
}
|
105
|
-
}
|
109
|
+
ores, elapsed = Timer.elapsed { self.mine_ores(depth) }
|
106
110
|
total = ores.sum
|
107
111
|
log format("MIND %s %s (%.2f s)",
|
108
112
|
Ore.display(total), ores.inspect, elapsed)
|
@@ -124,8 +128,8 @@ module MinerMover
|
|
124
128
|
@rate = rate.to_f * Ore::BLOCK
|
125
129
|
@work_type = work_type
|
126
130
|
@batch, @batches, @ore_moved = 0, 0, 0
|
127
|
-
super(variance: variance,
|
128
|
-
|
131
|
+
super(variance: variance, timer: timer,
|
132
|
+
logging: logging, debugging: debugging)
|
129
133
|
end
|
130
134
|
|
131
135
|
def state
|
@@ -145,33 +149,37 @@ module MinerMover
|
|
145
149
|
].join(' | ')
|
146
150
|
end
|
147
151
|
|
148
|
-
|
149
|
-
|
152
|
+
# accept some ore for moving; just accumulate unless we have a full batch
|
153
|
+
def load_ore amount
|
154
|
+
log format("LOAD %s | %s", Ore.display(amount), self.status)
|
155
|
+
@batch += amount
|
150
156
|
move_batch if @batch >= @batch_size
|
151
|
-
log format("
|
157
|
+
log format("LDED %s | %s", Ore.display(amount), self.status)
|
152
158
|
@batch
|
153
159
|
end
|
154
160
|
|
155
|
-
|
156
|
-
MinerMover.work(duration, @work_type)
|
157
|
-
end
|
158
|
-
|
161
|
+
# return the amount moved
|
159
162
|
def move_batch
|
160
163
|
raise "unexpected batch: #{@batch}" if @batch <= 0
|
161
|
-
|
162
|
-
duration = self.varied(amt / @rate)
|
164
|
+
amount = [@batch, @batch_size].min
|
163
165
|
|
164
|
-
|
165
|
-
_, elapsed = Timer.elapsed { self.move(duration) }
|
166
|
-
log format("MOVD %s (%.2f s)", Ore.display(amt), elapsed)
|
166
|
+
self.move amount
|
167
167
|
|
168
168
|
# accounting
|
169
|
-
@ore_moved +=
|
170
|
-
@batch -=
|
169
|
+
@ore_moved += amount
|
170
|
+
@batch -= amount
|
171
171
|
@batches += 1
|
172
172
|
|
173
|
-
|
174
|
-
|
173
|
+
amount
|
174
|
+
end
|
175
|
+
|
176
|
+
# perform the work of moving the amount of ore
|
177
|
+
def move amount
|
178
|
+
duration = self.varied(amount / @rate)
|
179
|
+
log format("MOVE %s (%.2f s)", Ore.display(amount), duration)
|
180
|
+
_, elapsed = Timer.elapsed { MinerMover.work(duration, @work_type) }
|
181
|
+
log format("MOVD %s (%.2f s)", Ore.display(amount), elapsed)
|
182
|
+
self
|
175
183
|
end
|
176
184
|
end
|
177
185
|
end
|