#Need help with contract storage while running a local node.

11 messages · Page 1 of 1 (latest)

oak carbon
#

I am working with these two tables:

core=> \d contractcode;
               Table "public.contractcode"
    Column    |  Type   | Collation | Nullable | Default 
--------------+---------+-----------+----------+---------
 hash         | text    | C         | not null | 
 ledgerentry  | text    | C         | not null | 
 lastmodified | integer |           | not null | 
Indexes:
    "contractcode_pkey" PRIMARY KEY, btree (hash)

core=> \d contractdata;
               Table "public.contractdata"
    Column    |  Type   | Collation | Nullable | Default 
--------------+---------+-----------+----------+---------
 contractid   | text    | C         | not null | 
 key          | text    | C         | not null | 
 type         | integer |           | not null | 
 ledgerentry  | text    | C         | not null | 
 lastmodified | integer |           | not null | 
Indexes:
    "contractdata_pkey" PRIMARY KEY, btree (contractid, key, type)

When I deploy a contract, an entry is added to both tables.
I can use contactdata.ledgerentry to get the address which is fine.
The problem am facing is how to get the adjacent code for the contact (ie from contractcode)
On the database I have something like this:

core=> select hash from contractcode ;
                     hash                     
----------------------------------------------
 JsyywvEb0yYC+GPWoA25bkrPRGNXBm1wWnEImDUGyuQ=

When I decode contactdata.legderentry, I will get a hash like 26ccb2c2f11bd32602f863d6a00db96e4acf446357066d705a7108983506cae4
I am unable to find the connection which I am assuming it exists.

Give a contract address is there a way to get the contract code using sql queries?

unreal ermine
#

if you're going to build your production logic based on SQL DB, please don't - it's an implementation detail and might be gone completely in some of the future releases. if you're just exploring the data, the contract implementation itself is in the ledgerentry column of contractcode table

#

so contractdata has a hash that is a key in contractcode table

oak carbon
#

@unreal ermine You mean the contractid?

unreal ermine
#

no, I mean that public.contractdata has a reference to a row in public.contractcode table, specifically SCV_LEDGER_KEY_CONTRACT_INSTANCE key will contain it for any given contractid.

oak carbon
#

But there is no direct key? I have to extract every contract in public.contractcode to get the underlying id, then match?

unreal ermine
#

you first lookup the row in contractdata (using contractid + key columns), then use the hash from ledgerentry to find the row in contractcode. but really, what exactly are you trying to achieve?

oak carbon
#

I have a scenario where I am ingesting contracts from the core db.
I was hoping to be able to introspect the wasm code for invoke-able methods

oak carbon
#

I was able to work it out from your direction @unreal ermine Thanks.

unreal ermine
#

just want to emphasize again, that core db is not the intended way to lookup data - if you want to build something real, you'd need to ingest core metadata instead (and potentially store it in your own DB)

oak carbon
#

I am part of a team working on a soroban indexer called Quasar.
Currently we are running a custom node and connecting to the postgres core database.
We are ingesting new ledger/account/ entries and then storing them in our own database to serve via a GraphQL API.
I am assuming that should be ok from your recommendations?