|
15 | 15 | import socketserver
|
16 | 16 | import threading
|
17 | 17 | import time
|
| 18 | +import pystache |
| 19 | +import datetime |
| 20 | +from functools import reduce |
18 | 21 |
|
19 | 22 | import bitcoin.core
|
20 | 23 | from bitcoin.core import b2lx, b2x
|
|
24 | 27 | from opentimestamps.core.serialize import StreamSerializationContext
|
25 | 28 |
|
26 | 29 | from otsserver.calendar import Journal
|
27 |
| - |
| 30 | +renderer = pystache.Renderer() |
28 | 31 |
|
29 | 32 | class RPCRequestHandler(http.server.BaseHTTPRequestHandler):
|
30 | 33 | MAX_DIGEST_LENGTH = 64
|
@@ -181,42 +184,62 @@ def do_GET(self):
|
181 | 184 | # need to investigate further, but this seems to work.
|
182 | 185 | str_wallet_balance = str(proxy._call("getbalance"))
|
183 | 186 |
|
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> |
186 | 197 | <head>
|
187 | 198 | <title>OpenTimestamps Calendar Server</title>
|
188 | 199 | </head>
|
189 | 200 | <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> |
192 | 202 | <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> |
198 | 208 | </br>
|
199 |
| -Wallet balance: %s BTC</br> |
| 209 | +Wallet balance: {{ balance }} BTC</br> |
200 | 210 | </p>
|
201 |
| -
|
202 | 211 | <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> |
204 | 213 | This address changes after every donation.
|
205 | 214 | </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> |
207 | 223 | </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 | + |
220 | 243 |
|
221 | 244 | elif self.path.startswith('/timestamp/'):
|
222 | 245 | self.get_timestamp()
|
|
0 commit comments