Skip to content

Commit

Permalink
fix: compass attestation with ERC20 tokens (#1253)
Browse files Browse the repository at this point in the history
# Related Github tickets

- Closes #1953

# Background

When some chain has ERC20 tokens deployed, compass upgrades would be
stuck on chains that do not have tokens. Now we only query deployed
tokens for the specific chain currently being iterated.

NOTE: After deploying this change, we have to manually remove any stuck
compass upgrade. It should redeploy automatically afterwards.

# Testing completed

- [x] test coverage exists or has been added/updated
- [x] tested in a private testnet

# Breaking changes

- [x] I have checked my code for breaking changes
- [x] If there are breaking changes, there is a supporting migration.
  • Loading branch information
maharifu authored Aug 9, 2024
1 parent 4b2ebb4 commit f0d54a2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
5 changes: 2 additions & 3 deletions x/evm/keeper/attest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ var _ = g.Describe("attest router", func() {

g.When("target chain has no deployed ERC20 tokens", func() {
g.BeforeEach(func() {
gk.On("CastAllERC20ToDenoms", mock.Anything).Return(nil, nil)
gk.On("CastChainERC20ToDenoms", mock.Anything, mock.Anything).Return(nil, nil)
})
g.It("removes deployment", func() {
setupChainSupport()
Expand All @@ -590,10 +590,9 @@ var _ = g.Describe("attest router", func() {

g.When("target chain has active ERC20 tokens deployed", func() {
g.BeforeEach(func() {
gk.On("CastAllERC20ToDenoms", mock.Anything).Return([]types.ERC20Record{
gk.On("CastChainERC20ToDenoms", mock.Anything, newChain.ChainReferenceID).Return([]types.ERC20Record{
record{"denom", "address1", newChain.ChainReferenceID},
record{"denom2", "address2", newChain.ChainReferenceID},
record{"denom3", "address3", "unknown-chain"},
}, nil)
})
g.It("updates deployment", func() {
Expand Down
10 changes: 9 additions & 1 deletion x/evm/keeper/attest_upload_smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func (a *uploadSmartContractAttester) attest(ctx sdk.Context, evidence *types.Tx
return err
}

records, err := a.k.Skyway.CastAllERC20ToDenoms(ctx)
// Get the ERC20 tokens just for this chain
records, err := a.k.Skyway.CastChainERC20ToDenoms(ctx, a.chainReferenceID)
if err != nil {
a.logger.WithError(err).Error("Failed to extract ERC20 records.")
return err
Expand Down Expand Up @@ -212,6 +213,13 @@ func (a *uploadSmartContractAttester) startTokenRelink(
})
}

// This shouldn't be needed anymore, since we query the ERC20 tokens for
// this specific chain. However, just to make double sure, we check the
// transfers. If there's none, just set the contract to active.
if len(transfers) == 0 {
return a.k.SetSmartContractAsActive(ctx, smartContractID, a.chainReferenceID)
}

deployment.Erc20Transfers = transfers
if err := a.k.updateSmartContractDeployment(ctx, smartContractID, a.chainReferenceID, deployment); err != nil {
a.logger.WithError(err).Error("Failed to update smart contract deployment")
Expand Down
1 change: 1 addition & 0 deletions x/evm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ERC20Record interface {
type SkywayKeeper interface {
GetLastObservedSkywayNonce(ctx context.Context, chainReferenceID string) (uint64, error)
CastAllERC20ToDenoms(ctx context.Context) ([]ERC20Record, error)
CastChainERC20ToDenoms(ctx context.Context, chainReferenceID string) ([]ERC20Record, error)
}

//go:generate mockery --name=MetrixKeeper
Expand Down
33 changes: 31 additions & 2 deletions x/evm/types/mocks/SkywayKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions x/skyway/keeper/cosmos-originated.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ func (k Keeper) CastAllERC20ToDenoms(ctx context.Context) ([]evmtypes.ERC20Recor
return cast, nil
}

func (k Keeper) CastChainERC20ToDenoms(
ctx context.Context,
chainReferenceID string,
) ([]evmtypes.ERC20Record, error) {
all, err := k.GetAllERC20ToDenoms(ctx)
if err != nil {
return nil, err
}

cast := make([]evmtypes.ERC20Record, 0, len(all))
for _, v := range all {
if v.GetChainReferenceId() == chainReferenceID {
cast = append(cast, v)
}
}

return cast, nil
}

func (k Keeper) setDenomToERC20(ctx context.Context, chainReferenceId, denom string, tokenContract types.EthAddress) error {
store := k.GetStore(ctx, types.StoreModulePrefix)

Expand Down

0 comments on commit f0d54a2

Please sign in to comment.