Skip to content

Commit ccab2a8

Browse files
authoredApr 6, 2018
Merge branch 'master' into online_backup

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed
 

‎otsserver/rpc.py

+49-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import socketserver
1616
import threading
1717
import time
18+
import pystache
19+
import datetime
20+
from functools import reduce
1821

1922
import bitcoin.core
2023
from bitcoin.core import b2lx, b2x
@@ -24,7 +27,7 @@
2427
from opentimestamps.core.serialize import StreamSerializationContext
2528

2629
from otsserver.calendar import Journal
27-
30+
renderer = pystache.Renderer()
2831

2932
class RPCRequestHandler(http.server.BaseHTTPRequestHandler):
3033
MAX_DIGEST_LENGTH = 64
@@ -181,42 +184,62 @@ def do_GET(self):
181184
# need to investigate further, but this seems to work.
182185
str_wallet_balance = str(proxy._call("getbalance"))
183186

184-
welcome_page = """\
185-
<html>
187+
transactions = proxy._call("listtransactions", "", 50)
188+
# We want only the confirmed txs containing an OP_RETURN, from most to least recent
189+
transactions = list(filter(lambda x: x["confirmations"] > 0 and x["amount"] == 0, transactions))
190+
a_week_ago = (datetime.date.today() - datetime.timedelta(days=7)).timetuple()
191+
a_week_ago_posix = time.mktime(a_week_ago)
192+
transactions_in_last_week = list(filter(lambda x: x["time"] > a_week_ago_posix, transactions))
193+
fees_in_last_week = reduce(lambda a,b: a-b["fee"], transactions_in_last_week, 0)
194+
time_between_transactions = round(168 / len(transactions_in_last_week)) # in hours based on 168 hours in a week
195+
transactions.sort(key=lambda x: x["confirmations"])
196+
homepage_template = """<html>
186197
<head>
187198
<title>OpenTimestamps Calendar Server</title>
188199
</head>
189200
<body>
190-
<p>This is an <a href="https://opentimestamps.org">OpenTimestamps</a> <a href="https://github.com/opentimestamps/opentimestamps-server">Calendar Server</a> (v%s)</p>
191-
201+
<p>This is an <a href="https://opentimestamps.org">OpenTimestamps</a> <a href="https://github.com/opentimestamps/opentimestamps-server">Calendar Server</a> (v{{ version }})</p>
192202
<p>
193-
Pending commitments: %d</br>
194-
Transactions waiting for confirmation: %d</br>
195-
Most recent timestamp tx: %s (%d prior versions)</br>
196-
Most recent merkle tree tip: %s</br>
197-
Best-block: %s, height %d</br>
203+
Pending commitments: {{ pending_commitments }}</br>
204+
Transactions waiting for confirmation: {{ txs_waiting_for_confirmation }}</br>
205+
Most recent timestamp tx: {{ most_recent_tx }} ({{ prior_versions }} prior versions)</br>
206+
Most recent merkle tree tip: {{ tip }}</br>
207+
Best-block: {{ best_block }}, height {{ block_height }}</br>
198208
</br>
199-
Wallet balance: %s BTC</br>
209+
Wallet balance: {{ balance }} BTC</br>
200210
</p>
201-
202211
<p>
203-
You can donate to the wallet by sending funds to: %s</br>
212+
You can donate to the wallet by sending funds to: {{ address }}</br>
204213
This address changes after every donation.
205214
</p>
206-
215+
<p>
216+
Average time between transactions in the last week: {{ time_between_transactions }} hour(s)</br>
217+
Fees used in the last week: {{ fees_in_last_week }} BTC</br>
218+
Latest transactions: </br>
219+
{{#transactions}}
220+
{{txid}} </br>
221+
{{/transactions}}
222+
</p>
207223
</body>
208-
</html>
209-
""" % (otsserver.__version__,
210-
len(self.calendar.stamper.pending_commitments),
211-
len(self.calendar.stamper.txs_waiting_for_confirmation),
212-
b2lx(self.calendar.stamper.unconfirmed_txs[-1].tx.GetTxid()) if self.calendar.stamper.unconfirmed_txs else 'None',
213-
max(0, len(self.calendar.stamper.unconfirmed_txs) - 1),
214-
b2x(self.calendar.stamper.unconfirmed_txs[-1].tip_timestamp.msg) if self.calendar.stamper.unconfirmed_txs else 'None',
215-
bitcoin.core.b2lx(proxy.getbestblockhash()), proxy.getblockcount(),
216-
str_wallet_balance,
217-
str(proxy.getaccountaddress('')))
218-
219-
self.wfile.write(welcome_page.encode())
224+
</html>"""
225+
226+
stats = { 'version': otsserver.__version__,
227+
'pending_commitments': len(self.calendar.stamper.pending_commitments),
228+
'txs_waiting_for_confirmation':len(self.calendar.stamper.txs_waiting_for_confirmation),
229+
'most_recent_tx': b2lx(self.calendar.stamper.unconfirmed_txs[-1].tx.GetTxid()) if self.calendar.stamper.unconfirmed_txs else 'None',
230+
'prior_versions': max(0, len(self.calendar.stamper.unconfirmed_txs) - 1),
231+
'tip': b2x(self.calendar.stamper.unconfirmed_txs[-1].tip_timestamp.msg) if self.calendar.stamper.unconfirmed_txs else 'None',
232+
'best_block': bitcoin.core.b2lx(proxy.getbestblockhash()),
233+
'block_height': proxy.getblockcount(),
234+
'balance': str_wallet_balance,
235+
'address': str(proxy.getaccountaddress('')),
236+
'transactions': transactions[:5],
237+
'time_between_transactions': time_between_transactions,
238+
'fees_in_last_week': fees_in_last_week,
239+
}
240+
welcome_page = renderer.render(homepage_template, stats)
241+
self.wfile.write(str.encode(welcome_page))
242+
220243

221244
elif self.path.startswith('/timestamp/'):
222245
self.get_timestamp()

‎otsserver/stamper.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,18 @@ def __do_bitcoin(self):
300300
self.pending_commitments.add(reorged_commitment_timestamp.msg)
301301

302302
# Check if this block contains any of the pending transactions
303-
304-
try:
305-
block = proxy.getblock(block_hash)
306-
except KeyError:
307-
# Must have been a reorg or something, return
308-
logging.error("Failed to get block")
309-
return
303+
block = None
304+
while block is None:
305+
try:
306+
block = proxy.getblock(block_hash)
307+
except KeyError:
308+
# Must have been a reorg or something, return
309+
logging.error("Failed to get block")
310+
return
311+
except BrokenPipeError:
312+
logging.error("BrokenPipeError to get block")
313+
time.sleep(5)
314+
proxy = bitcoin.rpc.Proxy()
310315

311316
# the following is an optimization, by pre computing the tx_id we rapidly check if our unconfirmed tx
312317
# is in the block

‎requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ opentimestamps>=0.2.1
22
GitPython>=2.0.8
33
leveldb>=0.20
44
pysha3>=1.0.2
5+
pystache>=0.5

0 commit comments

Comments
 (0)
Please sign in to comment.