-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: benchmark don't support batch tx
Solution: - add batch mode
- Loading branch information
Showing
7 changed files
with
216 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import base64 | ||
import itertools | ||
import json | ||
import multiprocessing | ||
import os | ||
import tempfile | ||
from collections import namedtuple | ||
|
||
from eth_account import Account | ||
from eth_account._utils.legacy_transactions import Transaction, vrs_from | ||
from eth_account._utils.signing import hash_of_signed_transaction | ||
from hexbytes import HexBytes | ||
|
||
from .utils import DEFAULT_DENOM, split, split_batch | ||
|
||
|
||
def build_tx(raw: HexBytes): | ||
tx = Transaction.from_bytes(raw) | ||
msg_hash = hash_of_signed_transaction(tx) | ||
vrs = vrs_from(tx) | ||
sender = HexBytes(Account._recover_hash(msg_hash, vrs=vrs)) | ||
msg = { | ||
"@type": "/ethermint.evm.v1.MsgEthereumTx", | ||
"data": None, | ||
"size": 0, | ||
"deprecated_hash": "", | ||
"deprecated_from": "", | ||
"from": base64.b64encode(sender).decode(), | ||
"raw": raw.hex(), | ||
} | ||
fee = tx.gas * tx.gasPrice | ||
return { | ||
"body": { | ||
"messages": [msg], | ||
"memo": "", | ||
"timeout_height": "0", | ||
"extension_options": [ | ||
{"@type": "/ethermint.evm.v1.ExtensionOptionsEthereumTx"} | ||
], | ||
"non_critical_extension_options": [], | ||
}, | ||
"auth_info": { | ||
"signer_infos": [], | ||
"fee": { | ||
"amount": [{"denom": DEFAULT_DENOM, "amount": str(fee)}], | ||
"gas_limit": str(tx.gas), | ||
"payer": "", | ||
"granter": "", | ||
}, | ||
"tip": None, | ||
}, | ||
"signatures": [], | ||
} | ||
|
||
|
||
def build_batch_tx(signed_txs: [str]): | ||
"return cosmos batch tx and eth tx hashes" | ||
tmp_txs = [build_tx(HexBytes(signed)) for signed in signed_txs] | ||
|
||
msgs = [tx["body"]["messages"][0] for tx in tmp_txs] | ||
fee = sum(int(tx["auth_info"]["fee"]["amount"][0]["amount"]) for tx in tmp_txs) | ||
gas_limit = sum(int(tx["auth_info"]["fee"]["gas_limit"]) for tx in tmp_txs) | ||
|
||
return { | ||
"body": { | ||
"messages": msgs, | ||
"memo": "", | ||
"timeout_height": "0", | ||
"extension_options": [ | ||
{"@type": "/ethermint.evm.v1.ExtensionOptionsEthereumTx"} | ||
], | ||
"non_critical_extension_options": [], | ||
}, | ||
"auth_info": { | ||
"signer_infos": [], | ||
"fee": { | ||
"amount": [{"denom": DEFAULT_DENOM, "amount": str(fee)}], | ||
"gas_limit": str(gas_limit), | ||
"payer": "", | ||
"granter": "", | ||
}, | ||
}, | ||
"signatures": [], | ||
} | ||
|
||
|
||
Batch = namedtuple("Batch", ["start", "end", "raw"]) | ||
Job = namedtuple("Job", ["batches", "cli"]) | ||
|
||
|
||
def _do_job(job: Job): | ||
txs = [] | ||
for batch in job.batches: | ||
tx = build_batch_tx(batch.raw) | ||
with tempfile.NamedTemporaryFile("w") as fp: | ||
fp.write(json.dumps(tx)) | ||
fp.flush() | ||
encoded = job.cli("tx", "encode", fp.name).strip() | ||
txs.append(encoded) | ||
print("batch built:", batch.start, batch.end, len(encoded)) | ||
return txs | ||
|
||
|
||
def build_batch_txs(cli, raw: [str], batch) -> [str]: | ||
batches = split_batch(len(raw), batch) | ||
chunks = split(len(batches), os.cpu_count()) | ||
jobs = [ | ||
Job([Batch(b, e, raw[b:e]) for b, e in batches[begin:end]], cli) | ||
for begin, end in chunks | ||
] | ||
txs = [] | ||
with multiprocessing.Pool() as pool: | ||
txs = pool.map(_do_job, jobs) | ||
return list(itertools.chain(*txs)) | ||
|
||
|
||
if __name__ == "__main__": | ||
from .cli import ChainCommand | ||
|
||
raw = [ | ||
"0xf86580843b9aca008252089410000000000000000000000000000000000000000180820636a08acf3003dc8df7e81c2ee5c3e97125f526de84414fef21ea428accf83457a7a5a01dd8f73270b4152f6fd513d12703b7f45480190b4795d39f6d7626a396e7c032", # noqa | ||
"0xf86580843b9aca008252089410000000000000000000000000000000000000000180820636a071d777ab815c11af0000b86333498c74dc0dfda45348869f5d11628932c7e1cca02b23fa2620796944df821ba6531eba1affa569d52389b9f799118d5a32a64f4b", # noqa | ||
"0xf86580843b9aca008252089410000000000000000000000000000000000000000180820636a05dd5f6bfe94f2de23cc15c1217080b13b53159d73ee71e081639671dc3ce7ea6a02693d3fc4ed1578b2cbdc6e318755bc425ec23a6dcba3856b2e20729f58f2378", # noqa | ||
] | ||
txs = [HexBytes(tx) for tx in raw] | ||
for tx in build_batch_txs(ChainCommand("cronosd"), txs, 2): | ||
print(tx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters