-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aerial: Initial offline table based gas strategy (#107)
- Loading branch information
1 parent
3cb23c5
commit 345c9d4
Showing
7 changed files
with
224 additions
and
29 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
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,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
from abc import ABC, abstractmethod | ||
from typing import Dict, Optional | ||
|
||
from cosmpy.aerial.tx import Transaction | ||
|
||
|
||
class GasStrategy(ABC): | ||
@abstractmethod | ||
def estimate_gas(self, tx: Transaction) -> int: | ||
pass # pragma: no cover | ||
|
||
@abstractmethod | ||
def block_gas_limit(self) -> int: | ||
pass # pragma: no cover | ||
|
||
|
||
class OfflineMessageTableStrategy(GasStrategy): | ||
DEFAULT_FALLBACK_GAS_LIMIT = 400_000 | ||
DEFAULT_BLOCK_LIMIT = 2_000_000 | ||
|
||
@staticmethod | ||
def default_table() -> "OfflineMessageTableStrategy": | ||
strategy = OfflineMessageTableStrategy() | ||
strategy.update_entry("cosmos.bank.v1beta1.MsgSend", 100_000) | ||
strategy.update_entry("cosmwasm.wasm.v1.MsgStoreCode", 2_000_000) | ||
strategy.update_entry("cosmwasm.wasm.v1.MsgInstantiateContract", 250_000) | ||
strategy.update_entry("cosmwasm.wasm.v1.MsgExecuteContract", 400_000) | ||
return strategy | ||
|
||
def __init__( | ||
self, | ||
fallback_gas_limit: Optional[int] = None, | ||
block_limit: Optional[int] = None, | ||
): | ||
self._table: Dict[str, int] = {} | ||
self._block_limit = block_limit or self.DEFAULT_BLOCK_LIMIT | ||
self._fallback_gas_limit = fallback_gas_limit or self.DEFAULT_FALLBACK_GAS_LIMIT | ||
|
||
def update_entry(self, transaction_type: str, gas_limit: int): | ||
self._table[str(transaction_type)] = int(gas_limit) | ||
|
||
def estimate_gas(self, tx: Transaction) -> int: | ||
gas_estimate = 0 | ||
for msg in tx.msgs: | ||
gas_estimate += self._table.get( | ||
msg.DESCRIPTOR.full_name, self._fallback_gas_limit | ||
) | ||
return min(self.block_gas_limit(), gas_estimate) | ||
|
||
def block_gas_limit(self) -> int: | ||
return self._block_limit |
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,48 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
echo Running black-check ... | ||
tox -e black-check | ||
echo Running black-check ... complete | ||
|
||
echo Running isort-check ... | ||
tox -e isort-check | ||
echo Running isort-check ... complete | ||
|
||
echo Running flake8 ... | ||
tox -e flake8 | ||
echo Running flake8 ... complete | ||
|
||
echo Running vulture ... | ||
tox -e vulture | ||
echo Running vulture ... complete | ||
|
||
echo Running bandit ... | ||
tox -e bandit | ||
echo Running bandit ... complete | ||
|
||
echo Running safety ... | ||
tox -e safety | ||
echo Running safety ... complete | ||
|
||
echo Running mypy ... | ||
tox -e mypy | ||
echo Running mypy ... complete | ||
|
||
echo Running liccheck ... | ||
tox -e liccheck | ||
echo Running liccheck ... complete | ||
|
||
echo Running copyright-check ... | ||
tox -e copyright-check | ||
echo Running copyright-check ... complete | ||
|
||
echo Running test ... | ||
tox -e test | ||
echo Running test ... complete | ||
|
||
echo Running coverage-report ... | ||
tox -e coverage-report | ||
echo Running coverage-report ... complete | ||
|
||
echo 'Great Success - all done!' |
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,8 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
echo Running Black... | ||
tox -e black | ||
|
||
echo Running Black... | ||
tox -e isort |
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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
import pytest | ||
|
||
from cosmpy.aerial.gas import GasStrategy, OfflineMessageTableStrategy | ||
from cosmpy.aerial.tx import Transaction | ||
from cosmpy.protos.cosmos.bank.v1beta1.tx_pb2 import MsgSend | ||
from cosmpy.protos.cosmwasm.wasm.v1.tx_pb2 import ( | ||
MsgExecuteContract, | ||
MsgInstantiateContract, | ||
MsgStoreCode, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_msgs,expected_gas_estimate", | ||
[ | ||
([MsgSend()], 100_000), | ||
([MsgStoreCode()], 2_000_000), | ||
([MsgInstantiateContract()], 250_000), | ||
([MsgExecuteContract()], 400_000), | ||
([MsgSend(), MsgSend()], 200_000), | ||
([MsgSend(), MsgStoreCode()], 2_000_000), # hits block limit | ||
([MsgSend(), MsgInstantiateContract()], 350_000), | ||
([MsgInstantiateContract(), MsgExecuteContract()], 650_000), | ||
], | ||
) | ||
def test_table_gas_estimation(input_msgs, expected_gas_estimate): | ||
# build up the TX | ||
tx = Transaction() | ||
for input_msg in input_msgs: | ||
tx.add_message(input_msg) | ||
|
||
# estimate the gas for the this test transaction | ||
strategy: GasStrategy = OfflineMessageTableStrategy.default_table() | ||
gas_estimate = strategy.estimate_gas(tx) | ||
|
||
assert gas_estimate == expected_gas_estimate |