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

feat(erc20): add indexes at genesis state to track active token pairs #68

Merged
merged 10 commits into from
Jul 18, 2024

Conversation

zsystm
Copy link

@zsystm zsystm commented Jul 16, 2024

Description

Naming Change

The following maps exist for the purpose of indexing to find TokenPair. I believe the new names better reflect their purpose:

  • DenomMap --> TokenPairDenomIndex
  • ERC20Map --> TokenPairERC20AddressIndex

Aligned the following method names with their original intent and actual actions:

  • GetDenomMap --> GetTokenPairIdByDenom
  • GetERC20Map --> GetTokenPairIdByERC20Addr

Added indexes as state

Problem

  • The original InitGenesis logic iterates over all existing TokenPair to set denomIndex(= denomMap) and erc20AddressIndex(= erc20Map).
  • The problem arises when multiple token pairs exist for same denom. The iteration order is based on bytes, not the actual registration time.
  • This means the logic might not correctly identify the most recently registered TokenPair.
  • For example, the denomIndex for ibc/13B6057538B93225F6EBACCB64574C49B2C1568C5AE6CCFE0A039D7DAC02BF29 ends up with the TokenPair associated with 0x2C68D1d6aB986Ff4640b51e1F14C716a076E44C4, which is incorrect.
  • The correct TokenPair should be the one associated with 0xD32eB974468ed767338533842D2D4Cc90B9BAb46, as it was registered later and is currently active.

Solution

  • Added indexes at genesis state to track active token pair. Now we can track the latest(= active) one if there were multiple token pairs at the same denom.

Notes

  • Actual logic fix will be handled as another PR.

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • included the necessary unit and integration tests
  • reviewed "Files changed" and left comments if necessary

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@zsystm zsystm requested review from dongsam, dudong2 and poorphd July 16, 2024 09:26
@zsystm zsystm self-assigned this Jul 16, 2024
},
}

for _, tc := range testCases {

// TODO: fix duplicate problem and uncomment this line
//suite.Nil(tc.genesisState.Validate(), "genesis state should be valid")
Copy link

Choose a reason for hiding this comment

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

no need to uncomment?

Copy link
Author

Choose a reason for hiding this comment

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

@dudong2
I removed it. Thanks.

@zsystm zsystm force-pushed the patch/non-deterministic-denommap branch from 5b34645 to acae2d0 Compare July 16, 2024 11:03
@zsystm zsystm marked this pull request as draft July 17, 2024 05:56
@zsystm zsystm marked this pull request as ready for review July 17, 2024 06:11
@zsystm zsystm requested a review from dudong2 July 17, 2024 06:22
@zsystm zsystm force-pushed the patch/non-deterministic-denommap branch from 0927856 to bb1990b Compare July 17, 2024 07:45
zsystm added 9 commits July 17, 2024 16:45
- multiple token pairs can exists at the same denom
- but only one active token pair can exist by index
- so index must be handled as genesis state. without it, we cannot track what is the active token pair from multiple token pairs for same denom
problem
- one token pair has two indexes
  - erc20 index
  - denom index
- so, set token pair per indexes and delete keys is wrong
  - when iterate erc20 indexes, id is already gone. so it will set empty token pair multiple times

solution
- set token pair once when iterate token pairs
- currently we already have multiple token pairs for same denom which is not allowed for validation genesis

- we have not decided yet for handling this.
Regardless of whether it’s OWNER_EXTERNAL or OWNER_MODULE, both indexes are being managed.
@tkkwon1998
Copy link

lgtm!

Copy link
Member

@dongsam dongsam left a comment

Choose a reason for hiding this comment

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

LGTM, apart from the chore suggestion on the comment

x/erc20/genesis_test.go Outdated Show resolved Hide resolved
Copy link

@dudong2 dudong2 left a comment

Choose a reason for hiding this comment

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

lgtm

@zsystm zsystm merged commit 54b2ea3 into v8/develop Jul 18, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants