-
Notifications
You must be signed in to change notification settings - Fork 1
/
helga_britcoin.py
292 lines (207 loc) · 7.66 KB
/
helga_britcoin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#####################
# made with love <3 #
#####################
import datetime as date
import hashlib
import humanize
import pymongo
from collections import defaultdict, OrderedDict
from helga import settings, log
from helga.db import db
from helga.plugins import Command
logger = log.getLogger(__name__)
DIFFICULTY = int(getattr(settings, 'BRITCOIN_DIFFICULTY', 2))
IGNORED = getattr(settings, 'IGNORED', [])
INITIAL_DATA = getattr(settings, 'BRITCOIN_INITIAL_DATA', {'data': 'Genesis Block'})
DEBUG = getattr(settings, 'HELGA_DEBUG', False)
CMD_PREFIX = getattr(settings, 'COMMAND_PREFIX_CHAR', '!')
def timestamp2datetime(timestamp):
return date.datetime.strptime(
timestamp,
'%Y-%m-%d %H:%M:%S'
)
def work(prev_hash, message):
"""
hash the message with the previous hash to produce the hash
attempt.
"""
hasher = hashlib.sha256()
try:
hasher.update(prev_hash + message)
except UnicodeEncodeError:
return ''
hash_attempt = hasher.hexdigest()
return hash_attempt
def proof_of_conversation(prev_hash, message):
"""
r/AnAttemptWasMade
"""
attempt = work(prev_hash, message)
if attempt.startswith('0' * DIFFICULTY):
return attempt
class BritBlock(object):
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()
def hash_block(self):
sha = hashlib.sha256()
if isinstance(self.data, dict):
data = OrderedDict(sorted(self.data.items(), key=lambda t: t[0]))
else:
data = self.data
block_to_hash = str(self.index) +\
str(self.timestamp) +\
str(data) +\
str(self.previous_hash)
sha.update(block_to_hash)
return sha.hexdigest()
class BritChain(list):
def __init__(self):
"""
Load blocks from mongodb. If none are found, create a genesis
block.
"""
super(BritChain, self).__init__()
self.pending_transactions = []
for block in db.britcoin.find().sort([('index', pymongo.ASCENDING)]):
potential_block = BritBlock(
block['index'],
block['timestamp'],
block['data'],
block['previous_hash']
)
if block['index'] > 0:
logger.debug('verifying block index = {}'.format(block['index']))
if self[block['index'] - 1].hash == block['previous_hash']:
self.append(potential_block)
else:
logger.debug('invalid block found: {}'.format(
potential_block.__dict__
))
else:
self.append(potential_block)
if not len(self):
self.create_genesis_block()
def append(self, block, persist=False):
"""
When blocks are added to the chain, add the block to mongodb.
"""
super(BritChain, self).append(block)
if persist:
logger.debug('adding block: {}'.format(block.__dict__))
db.britcoin.insert(block.__dict__)
else:
logger.debug('loaded hash: {}'.format(block.hash))
def create_genesis_block(self):
# Manually construct a block with
# index zero and arbitrary previous hash
self.append(
BritBlock(
0,
str(date.datetime.now().replace(microsecond=0)),
INITIAL_DATA,
"0"
),
persist=True
)
def latest_block(self):
"""
Return the most recent block.
"""
return self[-1]
def mine(self, nick, message):
"""
hashes the current message with the pending transactions.
If the hash begins with `DIFFICULTY` zero(s), it is considered mined and a
britcoin is sent to the miner.
"""
# Get the last block
last_block = self.latest_block()
proof = proof_of_conversation(last_block.hash_block(), message)
if proof:
# reward the miner
self.pending_transactions.append(
{ u'from': u'network', u'to': nick, u'amount': 1 }
)
# Now we can gather the data needed
# to create the new block
new_block_data = {
u'proof-of-work': unicode(proof),
u'transactions': list(self.pending_transactions)
}
new_block_index = last_block.index + 1
new_block_timestamp = str(date.datetime.now().replace(microsecond=0))
# Empty pending transaction list
self.pending_transactions[:] = []
# Now create the new block!
mined_block = BritBlock(
new_block_index,
new_block_timestamp,
new_block_data,
last_block.hash
)
self.append(mined_block, persist=True)
def calculate_balances(self):
balances = defaultdict(int)
for block in self:
for transaction in block.data.get('transactions', []):
balances[transaction['to']] += transaction['amount']
balances[transaction['from']] -= transaction['amount']
return balances
def stats(self):
chain_balances = self.calculate_balances()
coins_mined = abs(chain_balances['network'])
blockchain_start = timestamp2datetime(self[0].timestamp)
blockchain_end = timestamp2datetime(self[-1].timestamp)
total_duration = blockchain_start - blockchain_end
return u'{} britcoins | {} per britcoin'.format(
coins_mined,
humanize.naturaldelta(total_duration.total_seconds() / coins_mined)
)
class BritCoinPlugin(Command):
command = 'britcoin'
help = "subcommands: stats, send, balances, balance"
def __init__(self, *args, **kwargs):
super(BritCoinPlugin, self).__init__(*args, **kwargs)
self.blockchain = BritChain()
def preprocess(self, client, channel, nick, message):
if nick not in IGNORED and not message.startswith(CMD_PREFIX):
self.blockchain.mine(nick, message)
return channel, nick, message
def run(self, client, channel, nick, message, cmd, args):
"""
Subcommands:
<bigjust> !britcoin stats
<aineko> 23 britcoins, 3 hrs 57 minutes / 57 msgs per block
<bigjust> !britcoin send brit 5
<aineko> added bigjust -> brit (5 britcoins) to pending transactions
<bigjust> !britcoin balances
<aineko> bigjust: 2
<aineko> brit: 5
"""
if not args:
return
if args[0] == 'stats':
output = self.blockchain.stats()
if args[0] == 'send':
output = u'bug bigjust to implement this.'
if len(args) != 3:
output = u'usage: send <nick> <number of britcoins>'
if args[0] == 'balance':
balances = self.blockchain.calculate_balances()
nick_balance = balances.get(nick, 0)
output = '{}, you have {} britcoin{}.'.format(
nick,
nick_balance,
's' if nick_balance > 1 else '',
)
if args[0] == 'balances':
for chain_nick, balance in self.blockchain.calculate_balances().iteritems():
if chain_nick == 'network':
continue
client.msg(channel, '{}: {}'.format(chain_nick, balance))
return
return output