Skip to content

Commit

Permalink
feat: add abi column to deployments (#324)
Browse files Browse the repository at this point in the history
* add abi column to deployments

* change get_deployments() to return a generator which loads Deployments lazily
this is a perf change; fetching 10000 deployments costs about 350ms, so give the user
the ability to fetch Deployments lazily.
  • Loading branch information
charles-cooper authored Oct 5, 2024
1 parent 73447e1 commit 651ed33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
26 changes: 20 additions & 6 deletions boa/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid
from dataclasses import asdict, dataclass, field, fields
from pathlib import Path
from typing import Any, Optional
from typing import Any, Generator, Optional

from boa.util.abi import Address
from boa.util.open_ctx import Open
Expand Down Expand Up @@ -39,6 +39,7 @@ class Deployment:
tx_dict: dict # raw tx fields
receipt_dict: dict # raw receipt fields
source_code: Optional[Any] # optional source code or bundle
abi: Optional[Any]
session_id: str = field(default_factory=get_session_id)
deployment_id: Optional[int] = None # the db-assigned id - primary key

Expand All @@ -49,6 +50,8 @@ def sql_values(self):
ret["receipt_dict"] = json.dumps(ret["receipt_dict"])
if ret["source_code"] is not None:
ret["source_code"] = json.dumps(ret["source_code"])
if ret["abi"] is not None:
ret["abi"] = json.dumps(ret["abi"])
return ret

def to_dict(self):
Expand All @@ -75,6 +78,8 @@ def from_sql_tuple(cls, values):
ret["receipt_dict"] = json.loads(ret["receipt_dict"])
if ret["source_code"] is not None:
ret["source_code"] = json.loads(ret["source_code"])
if ret["abi"] is not None:
ret["abi"] = json.loads(ret["abi"])
return cls(**ret)


Expand All @@ -91,7 +96,8 @@ def from_sql_tuple(cls, values):
broadcast_ts real,
tx_dict text,
receipt_dict text,
source_code text
source_code text,
abi text
);
"""

Expand Down Expand Up @@ -123,15 +129,23 @@ def insert_deployment(self, deployment: Deployment):

def _get_deployments_from_sql(self, sql_query: str, parameters=(), /):
cur = self.db.execute(sql_query, parameters)
ret = [Deployment.from_sql_tuple(item) for item in cur.fetchall()]
return ret
return (Deployment.from_sql_tuple(item) for item in cur)

def _get_fieldnames_str(self) -> str:
return ",".join(field.name for field in fields(Deployment))

def get_deployments(self) -> list[Deployment]:
def get_deployments(self) -> Generator[Deployment, None, None]:
"""
Return all the deployments from the database. Returns an iterator
which can be converted to a list with `list(db.get_deployments())`.
Returns the deployments ordered by most recent first, i.e. using
an `ORDER BY deployment_id DESC` clause.
"""
fieldnames = self._get_fieldnames_str()
return self._get_deployments_from_sql(f"SELECT {fieldnames} FROM deployments")
return self._get_deployments_from_sql(
f"SELECT {fieldnames} FROM deployments order by deployment_id desc"
)


_db: Optional[DeploymentsDB] = None
Expand Down
2 changes: 2 additions & 0 deletions boa/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ def deploy(
msg += f" trace:\n```\n{e}\n```\nContinuing.\n"
warnings.warn(msg, stacklevel=2)
source_bundle = None
abi = getattr(contract, "abi", None)

deployment_data = Deployment(
create_address,
Expand All @@ -425,6 +426,7 @@ def deploy(
txdata,
receipt,
source_bundle,
abi,
)
deployments_db.insert_deployment(deployment_data)

Expand Down
3 changes: 2 additions & 1 deletion tests/integration/network/anvil/test_network_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_deployment_db():
contract = boa.loads(code, arg)

# test get_deployments()
deployment = db.get_deployments()[-1]
deployment = next(db.get_deployments())

initcode = contract.compiler_data.bytecode + arg.to_bytes(32, "big")

Expand All @@ -91,6 +91,7 @@ def test_deployment_db():
assert deployment.deployer == boa.env.eoa
assert deployment.rpc == boa.env._rpc.name
assert deployment.source_code == contract.deployer.solc_json
assert deployment.abi == contract.abi

# some sanity checks on tx_dict and rx_dict fields
assert to_bytes(deployment.tx_dict["data"]) == initcode
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/network/sepolia/test_sepolia_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_deployment_db():
contract = boa.loads(code, arg)

# test get_deployments()
deployment = db.get_deployments()[-1]
deployment = next(db.get_deployments())

initcode = contract.compiler_data.bytecode + arg.to_bytes(32, "big")

Expand Down

0 comments on commit 651ed33

Please sign in to comment.