|
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
|
21 | 24 |
|
22 | 25 | import otsserver
|
23 | 26 | from opentimestamps.core.serialize import StreamSerializationContext
|
24 | 27 |
|
25 |
| - |
| 28 | +renderer = pystache.Renderer() |
26 | 29 | class RPCRequestHandler(http.server.BaseHTTPRequestHandler):
|
27 | 30 | MAX_DIGEST_LENGTH = 64
|
28 | 31 | """Largest digest that can be POSTed for timestamping"""
|
@@ -154,42 +157,62 @@ def do_GET(self):
|
154 | 157 | # need to investigate further, but this seems to work.
|
155 | 158 | str_wallet_balance = str(proxy._call("getbalance"))
|
156 | 159 |
|
157 |
| - welcome_page = """\ |
158 |
| -<html> |
| 160 | + transactions = proxy._call("listtransactions", "", 50) |
| 161 | + # We want only the confirmed txs containing an OP_RETURN, from most to least recent |
| 162 | + transactions = list(filter(lambda x: x["confirmations"] > 0 and x["amount"] == 0, transactions)) |
| 163 | + a_week_ago = (datetime.date.today() - datetime.timedelta(days=7)).timetuple() |
| 164 | + a_week_ago_posix = time.mktime(a_week_ago) |
| 165 | + transactions_in_last_week = list(filter(lambda x: x["time"] > a_week_ago_posix, transactions)) |
| 166 | + fees_in_last_week = reduce(lambda a,b: a-b["fee"], transactions_in_last_week, 0) |
| 167 | + time_between_transactions = round(168 / len(transactions_in_last_week)) # in hours based on 168 hours in a week |
| 168 | + transactions.sort(key=lambda x: x["confirmations"]) |
| 169 | + homepage_template = """<html> |
159 | 170 | <head>
|
160 | 171 | <title>OpenTimestamps Calendar Server</title>
|
161 | 172 | </head>
|
162 | 173 | <body>
|
163 |
| -<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> |
164 |
| -
|
| 174 | +<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> |
165 | 175 | <p>
|
166 |
| -Pending commitments: %d</br> |
167 |
| -Transactions waiting for confirmation: %d</br> |
168 |
| -Most recent timestamp tx: %s (%d prior versions)</br> |
169 |
| -Most recent merkle tree tip: %s</br> |
170 |
| -Best-block: %s, height %d</br> |
| 176 | +Pending commitments: {{ pending_commitments }}</br> |
| 177 | +Transactions waiting for confirmation: {{ txs_waiting_for_confirmation }}</br> |
| 178 | +Most recent timestamp tx: {{ most_recent_tx }} ({{ prior_versions }} prior versions)</br> |
| 179 | +Most recent merkle tree tip: {{ tip }}</br> |
| 180 | +Best-block: {{ best_block }}, height {{ block_height }}</br> |
171 | 181 | </br>
|
172 |
| -Wallet balance: %s BTC</br> |
| 182 | +Wallet balance: {{ balance }} BTC</br> |
173 | 183 | </p>
|
174 |
| -
|
175 | 184 | <p>
|
176 |
| -You can donate to the wallet by sending funds to: %s</br> |
| 185 | +You can donate to the wallet by sending funds to: {{ address }}</br> |
177 | 186 | This address changes after every donation.
|
178 | 187 | </p>
|
179 |
| -
|
| 188 | +<p> |
| 189 | +Average time between transactions in the last week: {{ time_between_transactions }} hour(s)</br> |
| 190 | +Fees used in the last week: {{ fees_in_last_week }} BTC</br> |
| 191 | +Latest transactions: </br> |
| 192 | +{{#transactions}} |
| 193 | + {{txid}} </br> |
| 194 | +{{/transactions}} |
| 195 | +</p> |
180 | 196 | </body>
|
181 |
| -</html> |
182 |
| -""" % (otsserver.__version__, |
183 |
| - len(self.calendar.stamper.pending_commitments), |
184 |
| - len(self.calendar.stamper.txs_waiting_for_confirmation), |
185 |
| - b2lx(self.calendar.stamper.unconfirmed_txs[-1].tx.GetTxid()) if self.calendar.stamper.unconfirmed_txs else 'None', |
186 |
| - max(0, len(self.calendar.stamper.unconfirmed_txs) - 1), |
187 |
| - b2x(self.calendar.stamper.unconfirmed_txs[-1].tip_timestamp.msg) if self.calendar.stamper.unconfirmed_txs else 'None', |
188 |
| - bitcoin.core.b2lx(proxy.getbestblockhash()), proxy.getblockcount(), |
189 |
| - str_wallet_balance, |
190 |
| - str(proxy.getaccountaddress(''))) |
191 |
| - |
192 |
| - self.wfile.write(welcome_page.encode()) |
| 197 | +</html>""" |
| 198 | + |
| 199 | + stats = { 'version': otsserver.__version__, |
| 200 | + 'pending_commitments': len(self.calendar.stamper.pending_commitments), |
| 201 | + 'txs_waiting_for_confirmation':len(self.calendar.stamper.txs_waiting_for_confirmation), |
| 202 | + 'most_recent_tx': b2lx(self.calendar.stamper.unconfirmed_txs[-1].tx.GetTxid()) if self.calendar.stamper.unconfirmed_txs else 'None', |
| 203 | + 'prior_versions': max(0, len(self.calendar.stamper.unconfirmed_txs) - 1), |
| 204 | + 'tip': b2x(self.calendar.stamper.unconfirmed_txs[-1].tip_timestamp.msg) if self.calendar.stamper.unconfirmed_txs else 'None', |
| 205 | + 'best_block': bitcoin.core.b2lx(proxy.getbestblockhash()), |
| 206 | + 'block_height': proxy.getblockcount(), |
| 207 | + 'balance': str_wallet_balance, |
| 208 | + 'address': str(proxy.getaccountaddress('')), |
| 209 | + 'transactions': transactions[:5], |
| 210 | + 'time_between_transactions': time_between_transactions, |
| 211 | + 'fees_in_last_week': fees_in_last_week, |
| 212 | + } |
| 213 | + welcome_page = renderer.render(homepage_template, stats) |
| 214 | + self.wfile.write(str.encode(welcome_page)) |
| 215 | + |
193 | 216 |
|
194 | 217 | elif self.path.startswith('/timestamp/'):
|
195 | 218 | self.get_timestamp()
|
|
0 commit comments