-
Notifications
You must be signed in to change notification settings - Fork 2
/
xian_abci.py
382 lines (315 loc) · 13.6 KB
/
xian_abci.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import asyncio
import json
import time
import gc
import logging
import os
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
from tendermint.abci.types_pb2 import (
ResponseInfo,
ResponseInitChain,
ResponseCheckTx,
ResponseDeliverTx,
ResponseQuery,
ResponseCommit,
RequestBeginBlock,
ResponseBeginBlock,
RequestEndBlock,
ResponseEndBlock,
ResponseCommit,
)
from xian.validators import ValidatorHandler
from abci.server import ABCIServer
from abci.application import BaseApplication, OkCode, ErrorCode
from xian.driver_api import (
get_latest_block_hash,
set_latest_block_hash,
get_latest_block_height,
set_latest_block_height,
get_value_of_key,
get_keys,
)
from xian.rewards import (
distribute_rewards,
distribute_static_rewards,)
from xian.utils import (
encode_str,
decode_transaction_bytes,
unpack_transaction,
get_nanotime_from_block_time,
convert_binary_to_hex,
load_tendermint_config,
stringify_decimals,
load_genesis_data,
verify,
hash_list
)
from xian.storage import NonceStorage
from contracting.client import ContractingClient
from contracting.db.driver import (
ContractDriver,
)
from contracting.stdlib.bridge.decimal import ContractingDecimal
from contracting.compilation import parser
from xian.node_base import Node
# Logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
class Xian(BaseApplication):
def __init__(self):
try:
self.config = load_tendermint_config()
self.genesis = load_genesis_data()
except Exception as e:
logger.error(e)
raise SystemExit()
self.client = ContractingClient()
self.driver = ContractDriver()
self.nonce_storage = NonceStorage()
self.xian = Node(self.client, self.driver, self.nonce_storage)
self.validator_handler = ValidatorHandler(self)
self.current_block_meta: dict = None
self.fingerprint_hashes = []
self.chain_id = self.config.get("chain_id", None)
self.block_service_mode = self.config.get("block_service_mode", True)
if self.chain_id is None:
raise ValueError("No value set for 'chain_id' in Tendermint config")
if self.genesis.get("chain_id") != self.chain_id:
raise ValueError("Value of 'chain_id' in config.toml does not match value in Tendermint genesis.json")
if self.genesis.get("abci_genesis", None) is None:
raise ValueError("No value set for 'abci_genesis' in Tendermint genesis.json")
# current_block_meta :
# schema :
# {
# nanos: int
# height: int
# hash: str
# }
# set in begin_block
# used as environment for each tx in block
# unset at end_block / commit
# benchmark metrics
self.tx_count = 0
self.start_time = time.time()
self.enable_tx_fee = True
self.static_rewards = False
self.static_rewards_amount_foundation = 1
self.static_rewards_amount_validators = 1
self.current_block_rewards = {}
def info(self, req) -> ResponseInfo:
"""
Called every time the application starts
"""
r = ResponseInfo()
r.version = req.version
r.last_block_height = get_latest_block_height(self.driver)
r.last_block_app_hash = get_latest_block_hash(self.driver)
logger.debug(f"LAST_BLOCK_HEIGHT = {r.last_block_height}")
logger.debug(f"LAST_BLOCK_HASH = {r.last_block_app_hash}")
logger.debug(f"CHAIN_ID = {self.chain_id}")
logger.debug(f"BLOCK_SERVICE_MODE = {self.block_service_mode}")
logger.debug(f"BOOTED")
return r
def init_chain(self, req) -> ResponseInitChain:
"""Called the first time the application starts; when block_height is 0"""
abci_genesis_state = self.genesis["abci_genesis"]
asyncio.ensure_future(self.xian.store_genesis_block(abci_genesis_state))
return ResponseInitChain()
def check_tx(self, raw_tx) -> ResponseCheckTx:
"""
Validate the Tx before entry into the mempool
Checks the txs are submitted in order 1,2,3...
If not an order, a non-zero code is returned and the tx
will be dropped.
"""
try:
tx = decode_transaction_bytes(raw_tx)
self.xian.validate_transaction(tx)
sender, signature, payload = unpack_transaction(tx)
if not verify(vk=sender, msg=payload, signature=signature):
return ResponseCheckTx(code=ErrorCode, info="Invalid Signature")
payload = json.loads(payload)
if payload.get("chain_id") != self.chain_id:
return ResponseCheckTx(code=ErrorCode, info="Invalid Chain ID")
return ResponseCheckTx(code=OkCode)
except Exception as e:
logger.error(e)
return ResponseCheckTx(code=ErrorCode, info=f"ERROR: {e}")
def begin_block(self, req: RequestBeginBlock) -> ResponseBeginBlock:
"""
Called during the consensus process.
You can use this to do ``something`` for each new block.
The overall flow of the calls are:
begin_block()
for each tx:
deliver_tx(tx)
end_block()
commit()
"""
nanos = get_nanotime_from_block_time(req.header.time)
hash = convert_binary_to_hex(req.hash)
height = req.header.height
self.current_block_meta = {
"nanos": nanos,
"height": height,
"hash": hash,
}
self.fingerprint_hashes.append(hash)
return ResponseBeginBlock()
def deliver_tx(self, tx_raw) -> ResponseDeliverTx:
"""
Process each tx from the block & add to cached state.
"""
try:
tx = decode_transaction_bytes(tx_raw)
# Attach metadata to the transaction
tx["b_meta"] = self.current_block_meta
result = self.xian.tx_processor.process_tx(tx, enabled_fees=self.enable_tx_fee)
if self.enable_tx_fee:
self.current_block_rewards[tx['b_meta']['hash']] = {
"amount": result["stamp_rewards_amount"],
"contract": result["stamp_rewards_contract"]
}
self.xian.set_nonce(tx)
tx_hash = result["tx_result"]["hash"]
self.fingerprint_hashes.append(tx_hash)
parsed_tx_result = json.dumps(stringify_decimals(result["tx_result"]))
logger.debug(f"parsed tx result : {parsed_tx_result}")
return ResponseDeliverTx(
code=result["tx_result"]["status"],
data=encode_str(parsed_tx_result),
gas_used=result["stamp_rewards_amount"],
)
except Exception as err:
logger.error(f"DELIVER TX ERROR: {err}")
return ResponseDeliverTx(code=ErrorCode, info=f"ERROR: {err}")
def end_block(self, req: RequestEndBlock) -> ResponseEndBlock:
"""
Called at the end of processing the current block. If this is a stateful application
you can use the height from the request to record the last_block_height
"""
if self.static_rewards:
try:
distribute_static_rewards(
driver=self.driver,
foundation_reward=self.static_rewards_amount_foundation,
master_reward=self.static_rewards_amount_validators,
)
except Exception as e:
print(f"REWARD ERROR: {e}, No reward distributed for this block")
if self.current_block_rewards:
for tx_hash, reward in self.current_block_rewards.items():
try:
distribute_rewards(
stamp_rewards_amount=reward["amount"],
stamp_rewards_contract=reward["contract"],
driver=self.driver,
client=self.client,
)
except Exception as e:
print(f"REWARD ERROR: {e}, No reward distributed for {tx_hash}")
return ResponseEndBlock(validator_updates=self.validator_handler.build_validator_updates())
def commit(self) -> ResponseCommit:
"""
Called after ``end_block``. This should return a compact ``fingerprint``
of the current state of the application. This is usually the root hash
of a merkletree. The returned data is used as part of the consensus process.
Save all cached state from the block to filesystem DB
"""
# a hash of the previous block's app_hash + each of the tx hashes from this block.
fingerprint_hash = hash_list(self.fingerprint_hashes)
# commit block to filesystem db
set_latest_block_hash(fingerprint_hash, self.driver)
set_latest_block_height(self.current_block_meta["height"], self.driver)
self.driver.hard_apply(str(self.current_block_meta["nanos"]))
# unset current_block_meta & cleanup
self.current_block_meta = None
self.fingerprint_hashes = []
self.current_block_rewards = {}
gc.collect()
return ResponseCommit(data=fingerprint_hash)
def query(self, req) -> ResponseQuery:
"""
Query the application state
Request Ex. http://localhost:26657/abci_query?path="path"
(Yes you need to quote the path)
"""
result = None
type_of_data = "None"
key = ""
try:
request_path = req.path
path_parts = [part for part in request_path.split("/") if part]
# http://localhost:26657/abci_query?path="/get/currency.balances:c93dee52d7dc6cc43af44007c3b1dae5b730ccf18a9e6fb43521f8e4064561e6"
if path_parts and path_parts[0] == "get":
result = get_value_of_key(path_parts[1], self.driver)
key = path_parts[1]
# http://localhost:26657/abci_query?path="/keys/currency.balances" BLOCK SERVICE MODE ONLY
if self.block_service_mode:
if path_parts[0] == "keys":
result = get_keys(self.driver, path_parts[1])
# http://localhost:26657/abci_query?path="/health"
if path_parts[0] == "health":
result = "OK"
# http://localhost:26657/abci_query?path="/get_next_nonce/ddd326fddb5d1677595311f298b744a4e9f415b577ac179a6afbf38483dc0791"
if path_parts[0] == "get_next_nonce":
result = self.nonce_storage.get_next_nonce(sender=path_parts[1])
# http://localhost:26657/abci_query?path="/contract/con_some_contract"
if path_parts[0] == "contract":
self.client.raw_driver.clear_pending_state()
result = self.client.raw_driver.get_contract(path_parts[1])
# http://localhost:26657/abci_query?path="/contract_methods/con_some_contract"
if path_parts[0] == "contract_methods":
self.client.raw_driver.clear_pending_state()
contract_code = self.client.raw_driver.get_contract(path_parts[1])
funcs = parser.methods_for_contract(contract_code)
result = {"methods": funcs}
# http://localhost:26657/abci_query?path="/contract_methods/con_some_contract"
if path_parts[0] == "contract_methods":
self.client.raw_driver.clear_pending_state()
contract_code = self.client.raw_driver.get_contract(path_parts[1])
if contract_code is not None:
funcs = parser.methods_for_contract(contract_code)
result = {"methods": funcs}
# http://localhost:26657/abci_query?path="/contract_vars/con_some_contract"
if path_parts[0] == "contract_vars":
self.client.raw_driver.clear_pending_state()
contract_code = self.client.raw_driver.get_contract(path_parts[1])
if contract_code is not None:
result = parser.variables_for_contract(contract_code)
# http://localhost:26657/abci_query?path="/ping"
if path_parts[0] == "ping":
result = {'status': 'online'}
if result:
if isinstance(result, str):
v = encode_str(result)
type_of_data = "str"
elif isinstance(result, int):
v = encode_str(str(result))
type_of_data = "int"
elif isinstance(result, float) or isinstance(result, ContractingDecimal):
v = encode_str(str(result))
type_of_data = "decimal"
elif isinstance(result, dict) or isinstance(result, list):
v = encode_str(json.dumps(result))
type_of_data = "str"
else:
v = encode_str(str(result))
type_of_data = "str"
else:
# If no result, return a byte string representing None
v = b"\x00"
type_of_data = "None"
except Exception as e:
logger.error(f"QUERY ERROR: {e}")
return ResponseQuery(code=ErrorCode, log=f"QUERY ERROR")
return ResponseQuery(code=OkCode, value=v, info=type_of_data, key=encode_str(key))
def main():
app = ABCIServer(app=Xian())
app.run()
if __name__ == "__main__":
main()