Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emerald: import parsing from nexus #141

Merged
merged 7 commits into from
Oct 11, 2022
Merged

Emerald: import parsing from nexus #141

merged 7 commits into from
Oct 11, 2022

Conversation

pro-wh
Copy link
Collaborator

@pro-wh pro-wh commented Sep 26, 2022

import from https://github.com/oasisprotocol/oasis-nexus/pull/1

archival note: backed up at #644

@pro-wh pro-wh added the analysis-layer Analysis layer-related issues. label Sep 26, 2022
@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from 5b7321d to 06ef29e Compare September 26, 2022 22:31
@pro-wh
Copy link
Collaborator Author

pro-wh commented Sep 27, 2022

big, chunky todos

  1. improve testing to be as easy as it was for early days oasis-nexus
  2. file away items from analyzer/uncategorized into appropriate locations
  3. file away visitor handler logic into analyzer/modules
  4. unite block_extra and transaction_extra with existing tables
  5. spread small things like CloseOrLog around to rest of repo

@mitjat
Copy link
Contributor

mitjat commented Sep 30, 2022

Nice!

big, chunky todos

Do you intend to address these in a follow-up PR or in this one? I'm in favor of the former, but either way. Just so long as the scope of reviewing is clear.

@pro-wh
Copy link
Collaborator Author

pro-wh commented Sep 30, 2022 via email

@pro-wh
Copy link
Collaborator Author

pro-wh commented Sep 30, 2022

clerical note: moved to https://docs.google.com/document/d/1moqmGcHosdnVUHfYT6M4mpQbWsWEnmHIOpFAfcosZdY/edit#heading=h.imth7ku5x6is

here's my plan for integrating nexus db table/indexes:

db unification

  • oasis_3.emerald_rounds

    • block_extra.num_transactions will go into extra_data
    • block_extra.gas_used will go into extra_data
      • this will somewhat duplicate oasis_3.emerald_gas_used
    • block_extra.size will go into extra_data
    • unordered index added on block_hash
  • oasis_3.emerald_transactions

    • new table
      • indexer currently has separate tables for different transaction-related events
      • I believe we had always desired to have a table of transactions as well, just it had not been implemented
      • based on transaction_extra from nexus, although in indexer style, there is no chain_alias column, and Emerald gets its own table
    • height numeric
      • height is the round, but indexer calls it height
      • nexus used bigint for round. is it more expensive to index numeric columns?
    • tx_index integer
    • primary key (height, tx_index)
    • tx_hash text
      • nexus used char(64) for hashes. is it more expensive to index text columns for lookup by equality?
    • eth_hash text null
    • is there desire for an extra_data json column?
    • is there desire for a bytea of the actual transaction?
      • I see consensus oasis_3.transaction has body bytea, was this really only the body?
      • note that to
    • unordered index on tx_hash
    • unordered index on eth_hash
    • no connection with existing emerald_transfers, ... etc event-specific tables 🤔
  • oasis_3.emerald_transaction_signer

    • new table
      • justification: some interactions, notably deposit+withdraw results, need to be looked up by signer and nonce
      • based on transaction_signer from nexus
    • height numeric
    • tx_index integer
    • signer_index integer
    • primary key (height, tx_index, signer_index)
    • addr text
    • nonce integer
    • index on (addr, nonce)
  • oasis_3.emerald_related_transaction

    • new table
      • justification: for showing a list of related transactions for a given account
      • based on related_transaction from nexus
      • may alternatively be possible by querying many event tables and merging results?
      • this approach preserves transaction order though
      • this approach leaves out non-transaction events
    • account_address text
    • tx_height numeric
    • tx_index integer
    • index on (account_address, tx_height, tx_index)
  • oasis_1.address_preimage

    • new table
      • justification: determine ethereum address corresponding to oasis address, explorer UI will probably use only ethereum addresses where possible
      • justification: determine public key corresponding to oasis address, for registry lookups
      • namespace is oasis_1, referring to the earliest hardfork using compatible address derivation schemes
      • don't forsee hardforks ever to change the derivation scheme; shouldn't need to move to a new table unless it does
      • analysis of older hardforks can benefit from information obtained while analyzing newer hardforks
      • data collected is not isolated to Emerald runtime
    • address text primary key
    • context_identifier text
    • context_version integer
    • addr_data bytea

@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 1, 2022

oh I read that wrong, you prefer to address these in separate PRs? that may be fine, as long as we have an understanding that we'll be editing the 0011 migration instead of doing further migrations on top. it's just too rough right now imo

@mitjat
Copy link
Contributor

mitjat commented Oct 1, 2022

Oh that's a good writeup on table migrations 👌 . I copy-pasted it into https://docs.google.com/document/d/1moqmGcHosdnVUHfYT6M4mpQbWsWEnmHIOpFAfcosZdY/edit#heading=h.imth7ku5x6is (because github is not great for commenting on comments piecemeal), and added comments. Not 100% done yet but dinner calls; will wrap up probably Monday.

If you prefer to make all (or some of) the changes in this PR, that's fine with me also. I was thinking that submitting early is more for your benefit -- PRs that drag out forever and need to be rebased a thousand times are no fun. They're also no fun for reviewing; though in this particular case, probably the easiest thing for reviewing would be to first get in a baseline PR that changes the indexer where it should conform to nexus (I left some suggestions to that effect, mostly around data types; see if you agree), then submitting this PR neatly cleaned up. But I think you should prioritize your time/sanity with this one. It's small enough that it won't be too huge a pain to review no matter what.

Either way, don't worry about migrations. There are no clients for the indexer yet, so it's trivial to take it offline in prod and have it reindex everything. Just check let the indexer team know before you nuke the db so we all expect the outage. Also, I can help with all ops-related stuff like this.

@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 3, 2022

ok let's do this in separate PRs

@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from 06ef29e to a4e7754 Compare October 3, 2022 19:42
@pro-wh pro-wh changed the title emerald work area emerald tx parsing import Oct 3, 2022
BEGIN;

CREATE TABLE block_extra (
chain_alias VARCHAR(32),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this indentation

Copy link
Contributor

@mitjat mitjat Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdness indeed.

Assuming you used an autoformatter and you're not happy with it: I looked around and tested a few; sqlfmt by Cockroach Labs looks best. See writeup, demo (increase max width with non-obvious GUI slider), binary.

I don't feel motivated enough to integrate it into our CI, but it's where we should head eventually, probably.
I sunk a good chunk of time into this before realizing that contrary to their docs, they can ingest only some postgres-style SQL, and they cannot emit it. For example, TEXT is consistently changed to STRING.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't have to worry about this in this PR, gonna address in #147

@aefhm
Copy link
Contributor

aefhm commented Oct 4, 2022

table migrations

We did agree (verbally I believe) to squash current migrations.

@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from a4e7754 to 6461517 Compare October 7, 2022 23:21
@pro-wh pro-wh changed the title emerald tx parsing import Emerald: import parsing from nexus Oct 7, 2022
@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 7, 2022

reviewers, can we merge this?

@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from 6461517 to 44dcdae Compare October 7, 2022 23:26
Copy link
Contributor

@mitjat mitjat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for letting this one sit unattended; I thought it was blocked on #147, not the other way around.

// TODO(ennsharma): Process Emerald transaction data in accordance with
// https://github.com/oasisprotocol/emerald-web3-gateway/blob/main/indexer/utils.go#L225
var rtid ocCommon.Namespace
if err := rtid.UnmarshalHex("000000000000000000000000000000000000000000000000e2eaa99fc008f87f"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extract to top-level constant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing this in #158

if err := rtid.UnmarshalHex("000000000000000000000000000000000000000000000000e2eaa99fc008f87f"); err != nil {
return err
}
sigContext := signature.DeriveChainContext(rtid, "b11b369e0da5bb230b220127f5e7b242d385ef8c6f54906243f30af63c815535")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fetch this from the node instead of hardcoding? This same emerald parser will run for emerald v1 (or whatever the current version is) on oasis-4.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unhardcoded in #158

analyzer/emerald/incoming.go Show resolved Hide resolved
tx, err := common.VerifyUtx(sigContext, &txr.Tx)
if err != nil {
err = fmt.Errorf("tx %d: %w", i, err)
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pull in and use the logger, like other modules do. I'm fine to do it in a follow-up PR, though in that case let's create a doc/issue/whatever where we can track those "to do later" tasks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do this in #158

blockData.NumTransactions = len(txrs)
blockData.TransactionData = make([]*BlockTransactionData, 0, len(txrs))
blockData.AddressPreimages = map[string]*AddressPreimageData{}
for i, txr := range txrs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: i is good for a short-lived local variable, but this one lives over ~200 lines. Let's call it tx_idx or something that still makes sense (and is not in danger of being shadowed) 100 lines lower.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do this in #158

analyzer/emerald/incoming.go Show resolved Hide resolved
analyzer/emerald/incoming.go Show resolved Hide resolved
func decodeExpectCall(
t *testing.T,
raw string,
expectedChainID uint64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The high number of args makes tests hard to read. Can we introduce a type Call struct {...}, then having expected Call here and Call { ChainId: ..., To:, ..., ... } in tests?

Same for decodeExpectCreate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya that'd be good. let's do that in #158

@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 10, 2022

let's make most changes in #158 or later. that PR will be moving things around, so it would be more work to adjust things here and rebase #158

@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from bb2ee02 to 540f441 Compare October 10, 2022 22:59
Copy link
Contributor

@mitjat mitjat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for responding to all the comments! I left the threads unresolved for easier tracking, but I see you've nicely documented them in the description of #158 already, so do with the threads here as you please :)

analyzer/emerald/incoming.go Show resolved Hide resolved
@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from 540f441 to e1b4d3a Compare October 11, 2022 18:20
@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 11, 2022

Error: cannot use batch (variable of type *storage.QueryBatch) as *pgx.Batch value in argument to emitRoundBatch (typecheck)

ugh what's going on in main

@pro-wh pro-wh force-pushed the pro-wh/feature/emerald branch from e1b4d3a to 209444c Compare October 11, 2022 18:28
@pro-wh
Copy link
Collaborator Author

pro-wh commented Oct 11, 2022

#154 changed the batch type

@pro-wh pro-wh merged commit 33948ea into main Oct 11, 2022
@pro-wh pro-wh deleted the pro-wh/feature/emerald branch October 11, 2022 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analysis-layer Analysis layer-related issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants