forked from christophersanborn/HTLCProductsSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildHashTable.py
More file actions
117 lines (99 loc) · 4.58 KB
/
BuildHashTable.py
File metadata and controls
117 lines (99 loc) · 4.58 KB
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
#
# Hash Ladder Tool:
#
# Usage: python3 %s <target_date> <top_price> <tag> [reveal_price]\n
# Pair and Tag-specific config option are read from the config file
# (ladder.conf) in section [<pair> <tag>].\n
#
# Example: python3 %s "200110" "0.50 BTS:USD" Down\n
# Prints descending hash table (no preimages) for Jan 10, 2020 BTS:USD
# market, taking options from [BTS:USD Down] section.\n
#
# Example: python3 %s "200110" "32000 BTC:USD" Down 7965.37\n
# Prints preimage table for BTC:USD observed price of 7965.37\n
#
import configparser
import argparse
import datetime
import hashlib
import sys
import os.path
import PriceIterators
from HTLCProductsSim import *
from HashLadder import *
import TableFormatters
AppBanner = """(((
((( This is %s; A tool for building oracular hash
((( tables and conditional preimage reveal tables.
((("""%(sys.argv[0])
cfgfile='ladder.conf'
parser = argparse.ArgumentParser(
description="Hash Ladder Tool: Make [price, hash] tables and preimage tables.",
epilog="Pair and Tag-specific config option are read from the config file "
"(ladder.conf) in sections [<pair> <tag>].")
parser.add_argument('targetdate', metavar="DATE", help="Target date in YYMMDD")
parser.add_argument('topprice', metavar="PRICE", help="Extremum (top or bottom) price. Ex: \"32000 BTC:USD\"")
parser.add_argument('tagstring', metavar="TAG", help="Table tag, e.g., \"Up\" or \"Down\"")
parser.add_argument('observedprice', metavar="OBS_PRICE", nargs='?', help="Observed price (numeric, without currency pair)")
parser.add_argument('--outfile', help="File to write table to (default stdout)")
parser.add_argument('--priceargs', help="Additional args to price iterator (json)", default='{}')
parser.add_argument('--formatargs', help="Additional args to formatter plugin (json)", default='{}')
if __name__ == "__main__":
args = parser.parse_args()
print(AppBanner)
targetdate = args.targetdate # e.g. "200110" for Jan 10, 2020
topprice = args.topprice # e.g. "32000 BTC:USD"
tagstring = args.tagstring # e.g. "Down" for descending table
observedprice = float(args.observedprice) if args.observedprice else None
outfile = args.outfile
add_price = json.loads(args.priceargs)
add_format = json.loads(args.formatargs)
topP = Price(topprice)
cfgdefaults = {"bidirectional":"False"}
cfg = configparser.ConfigParser(cfgdefaults)
cfg.read(cfgfile)
section = str(topP.pair)+" "+tagstring
try:
secrettxt = cfg['default']['secret'].strip('"')
except:
print ("Could not read hash secret from config file '%s'."%cfgfile)
quit()
htcfg = ConfigArgsExtractor(cfg[section]).getHashTableArgs()
mtcfg = ConfigArgsExtractor(cfg[section]).getMultiTableArgs()
htcfg['priceargs'].update(add_price)
HT_list = []
for flip in [False, True] if mtcfg['bidirectional'] else [False]:
for plane in mtcfg['planes']:
PriceIter = PriceIterators.New(
startprice=topP.price, **htcfg['priceargs'],
plane=plane, flip=flip
)
HT_list.append(
HashTable(targetdate, topP.pair, PriceIter,
secrettxt, htcfg, plane=plane, flip=flip)
)
FT = TableFormatters.GetFMT(htcfg['formatter'], HT_list, **add_format)
print ("((( An oracle SECRET was read from file '%s'.\n((("%cfgfile)
HT_list[0].printFingerprint(lineleader="((( ")
print("(((")
def check_outfile():
if outfile is not None:
print ("((( Table output to be written to: %s"%outfile)
if os.path.exists(outfile):
print("Output file exists. Will not overwrite. Exiting.")
quit()
print("(((")
if observedprice:
print("((( You have requested: PREIMAGE TABLE from section [%s]"%section)
print("((( target date: %s, observed price: %g.\n((("%(targetdate, observedprice))
check_outfile()
FT.printPreimageRevealTable(observedprice, outfile)
print("(((\n((( This concludes: PREIMAGE TABLE from section [%s] target date: %s, observed price: %g.\n((("%(section, targetdate, observedprice))
else:
print("((( You have requested: HASH TABLE from section [%s] target date: %s.\n((("%(section, targetdate))
check_outfile()
FT.printPublicHashTable(outfile)
print("(((\n((( This concludes: HASH TABLE from section [%s] target date: %s.\n((("%(section, targetdate))
if outfile is None:
HT_list[0].printFingerprint(lineleader="((( ")
print("(((")