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

l1 chain #1219

Merged
merged 14 commits into from
Oct 10, 2023
Merged

l1 chain #1219

merged 14 commits into from
Oct 10, 2023

Conversation

benny-conn
Copy link
Collaborator

Changes:

  • Added a new l1_chain column to wallets and nonces table.
  • Changed many queries to search by l1_chain
  • Changed WalletOverrides to live in persist instead of wire injection

Comment on lines 10 to 12
create index nonces_l1_chain_idx on nonces (address,chain,l1_chain);
create index wallets_l1_chain_idx on wallets (address,chain,l1_chain);
create unique index wallets_l1_chain_unique_idx on wallets (address,l1_chain) where deleted = false;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we need to remove any of the current indexes, specifically the unique ones on (address,chain)? I imagined we still would want them because we still need those two to be unique (despite them always being unique given we correctly use the l1_chain, e.g. there will be no case where two rows with (addr: 0x9, ch: BASE) and (addr:0x9, ch: BASE) getting through because they will always fail on (addr: 0x9, l1_ch: ETH) anyway). I imagine leaving them would not hurt us.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm in favor of leaving it for now! I think your point is spot-on: we shouldn't need it, assuming we're getting the L1 chain correct...but the wallets table is relatively small, the extra index doesn't cost us much, and I like the peace of mind in knowing that we can't accidentally end up in a situation that should be impossible.

Comment on lines -105 to -120
// Insert inserts a wallet by its address and chain
func (w *WalletRepository) Insert(ctx context.Context, chainAddress persist.ChainAddress, walletType persist.WalletType) (persist.DBID, error) {

_, err := w.insertStmt.ExecContext(ctx, persist.GenerateID(), 0, chainAddress.Address(), chainAddress.Chain(), walletType)
if err != nil {
return "", err
}

// rather than using the ID generated above, we must retrieve it because in the case of conflict the ID above would be inaccurate.
wa, err := w.GetByChainAddress(ctx, chainAddress)
if err != nil {
return "", err
}

return wa.ID, nil
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unused

Comment on lines -219 to -220
- column: '*.*.chain'
go_type: 'github.com/mikeydub/go-gallery/service/persist.Chain'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Duplicated above

Copy link
Contributor

@radazen radazen left a comment

Choose a reason for hiding this comment

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

Looks awesome, Benny! I really like how this turned out, and I think it's going to simplify a lot of the L1 chain issues we've been running into!

update wallets set l1_chain = 4 where chain = 4;

create index nonces_l1_chain_idx on nonces (address,chain,l1_chain);
create index wallets_l1_chain_idx on wallets (address,chain,l1_chain);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the non-unique indexes here would also benefit from where deleted = false

Comment on lines 10 to 12
create index nonces_l1_chain_idx on nonces (address,chain,l1_chain);
create index wallets_l1_chain_idx on wallets (address,chain,l1_chain);
create unique index wallets_l1_chain_unique_idx on wallets (address,l1_chain) where deleted = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm in favor of leaving it for now! I think your point is spot-on: we shouldn't need it, assuming we're getting the L1 chain correct...but the wallets table is relatively small, the extra index doesn't cost us much, and I like the peace of mind in knowing that we can't accidentally end up in a situation that should be impossible.

and w.id = any(u.wallets) and coalesce(nullif(c.owner_address, ''), nullif(c.creator_address, '')) = w.address
and c.chain = w.chain
and w.id = any(u.wallets) and coalesce(nullif(c.owner_address, ''), nullif(c.creator_address, '')) = w.address
and w.chain = any(@l1_chains::int[])
Copy link
Contributor

@radazen radazen Oct 10, 2023

Choose a reason for hiding this comment

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

Hmm, this makes me think we should add the l1_chain column to the contracts table, too. That way, we can say "give me contracts on any(@chains) where the user owns a wallet on the contract's L1 chain," which is (I think) the most correct way to phrase this query.

...but also, that would add some pretty significant scope to this PR, and I don't think it's necessary to add it for the sake of this one query. I think your current workaround is good here, but we should definitely add l1_chain to contracts in the near future!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, was thinking this as well when I had to make this little work around. If we do contracts, might as well do tokens too at some point...

@@ -22,6 +22,8 @@ type UserNonce struct {
LastUpdated time.Time `json:"last_updated"`
Value NullString `json:"value"`
Address Address `json:"address"`
Chain Chain `json:"chain"`
L1Chain Chain `json:"l1_chain"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a new type for L1Chain instead of repurposing Chain? IMHO we wouldn't want to accept an L1Chain anywhere we'd accept a regular Chain, or vice versa.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure!

checkNoErr(err)

getByIDStmt, err := db.PrepareContext(ctx, `SELECT ID,VERSION,CREATED_AT,LAST_UPDATED,ADDRESS,WALLET_TYPE,CHAIN FROM wallets WHERE ID = $1 AND DELETED = FALSE;`)
getByChainAddressStmt, err := db.PrepareContext(ctx, `SELECT ID,VERSION,CREATED_AT,LAST_UPDATED,ADDRESS,WALLET_TYPE,CHAIN,L1_CHAIN FROM wallets WHERE ADDRESS = $1 AND L1_CHAIN = $2 AND DELETED = FALSE;`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the concept of an L1ChainAddress that uses L1Chain instead of a regular chain? It's starting to feel that way to me! A query named getByChainAddress makes me think we'll be passing in a Chain, but we're actually using an L1Chain here, and it'd probably make the codebase clearer if we separated those concepts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For sure!

@benny-conn benny-conn merged commit 77bb22b into main Oct 10, 2023
6 checks passed
@benny-conn benny-conn deleted the benny/l1-chain branch October 10, 2023 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants