Skip to content

Commit

Permalink
Ensure IBC marker has matching supply (#1708)
Browse files Browse the repository at this point in the history
* add supply adjustment on marker for ibc coins that previous existed before marker

* remove exchange item

* remove unneeded func

* refactor method nameing and add more to method documentation

* add changelog entry

* fix lint

* refactor method again
  • Loading branch information
nullpointer0x00 authored Oct 13, 2023
1 parent 05bbce3 commit df2d286
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Bump cometbft to v0.34.29 (from v0.34.28) [PR 1649](https://github.com/provenance-io/provenance/pull/1649).
* Add genesis/init for Marker module send deny list addresses. [#1660](https://github.com/provenance-io/provenance/issues/1660)
* Add automatic changelog entries for dependabot. [#1674](https://github.com/provenance-io/provenance/issues/1674)

* Ensure IBC marker has matching supply [#1706](https://github.com/provenance-io/provenance/issues/1706).
### Bug Fixes

* Fix ibcnet relayer creating multiple connections on restart [#1620](https://github.com/provenance-io/provenance/issues/1620).
Expand Down
7 changes: 7 additions & 0 deletions x/ibchooks/marker_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (h MarkerHooks) createNewIbcMarker(ctx sdktypes.Context, data transfertypes
allowForceTransfer,
[]string{},
)
existingSupply := h.getExistingSupply(ctx, marker)
_ = marker.SetSupply(marker.GetSupply().Add(existingSupply))
if err = ResetMarkerAccessGrants(transferAuthAddrs, marker); err != nil {
return err
}
Expand All @@ -105,6 +107,11 @@ func (h MarkerHooks) createNewIbcMarker(ctx sdktypes.Context, data transfertypes
return h.addDenomMetaData(ctx, packet, ibcKeeper, ibcDenom, data)
}

// getExistingSupply returns current supply coin, if coin does not exist amount will be 0
func (h MarkerHooks) getExistingSupply(ctx sdktypes.Context, marker *markertypes.MarkerAccount) sdktypes.Coin {
return sdktypes.NewCoin(marker.Denom, h.MarkerKeeper.CurrentCirculation(ctx, marker))
}

// addDenomMetaData adds denom metadata for ibc token
func (h MarkerHooks) addDenomMetaData(ctx sdktypes.Context, packet exported.PacketI, ibcKeeper *ibckeeper.Keeper, ibcDenom string, data transfertypes.FungibleTokenPacketData) error {
chainID := h.GetChainID(ctx, packet, ibcKeeper)
Expand Down
15 changes: 15 additions & 0 deletions x/ibchooks/marker_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (suite *MarkerHooksTestSuite) makeMockPacket(denom, receiver, memo string,
}

func (suite *MarkerHooksTestSuite) TestAddUpdateMarker() {
suite.chainA.GetProvenanceApp().BankKeeper.MintCoins(suite.chainA.GetContext(), markertypes.CoinPoolName, sdk.NewCoins(sdk.NewInt64Coin("ibc/F7466BCD642C14163B3E67D5B4401FD2B77271C3225FDE0C57ADD61B8046253D", 100)))
address1 := sdk.AccAddress("address1")
address2 := sdk.AccAddress("address2")
markerHooks := ibchooks.NewMarkerHooks(&suite.chainA.GetProvenanceApp().MarkerKeeper)
Expand All @@ -92,27 +93,31 @@ func (suite *MarkerHooksTestSuite) TestAddUpdateMarker() {
expErr string
expIbcDenom string
expTransAuths []sdk.AccAddress
expSupply sdk.Coin
}{
{
name: "successfully process with empty memo",
denom: "fiftyfivehamburgers",
memo: "",
expErr: "",
expIbcDenom: "ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E",
expSupply: sdk.NewInt64Coin("ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E", 1),
},
{
name: "successfully process with non json memo",
denom: "fiftyfivehamburgers",
memo: "55 burger 55 fries...",
expErr: "",
expIbcDenom: "ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E",
expSupply: sdk.NewInt64Coin("ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E", 1),
},
{
name: "successfully process with non json marker part memo",
denom: "fiftyfivehamburgers",
memo: `{"marker":{random},"wasm":{"contract":"%1234","msg":{"echo":{"msg":"test"}}}}`,
expErr: "",
expIbcDenom: "ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E",
expSupply: sdk.NewInt64Coin("ibc/F3F4565153F3DD64470F075D6D6B1CB183F06EB55B287CCD0D3506277A03DE8E", 1),
},
{
name: "successfully process with transfer auths",
Expand All @@ -121,13 +126,22 @@ func (suite *MarkerHooksTestSuite) TestAddUpdateMarker() {
expErr: "",
expIbcDenom: "ibc/1B3A5773661E8A6B9F6BB407979B5933C2FA792DF24ED2A40B028C90277B0C22",
expTransAuths: []sdk.AccAddress{address1, address2},
expSupply: sdk.NewInt64Coin("ibc/1B3A5773661E8A6B9F6BB407979B5933C2FA792DF24ED2A40B028C90277B0C22", 1),
},
{
name: "fail invalid json",
denom: "fiftyfivetacos",
memo: fmt.Sprintf(`{"marker":{"transfer-auths":"%s"}}`, address1.String()),
expErr: "json: cannot unmarshal string into Go struct field MarkerPayload.transfer-auths of type []string",
},
{
name: "successfully process with ibc denom that existed before marker correctly adjust supply",
denom: "ibcdenombeforemiddleware",
memo: "",
expErr: "",
expIbcDenom: "ibc/F7466BCD642C14163B3E67D5B4401FD2B77271C3225FDE0C57ADD61B8046253D",
expSupply: sdk.NewInt64Coin("ibc/F7466BCD642C14163B3E67D5B4401FD2B77271C3225FDE0C57ADD61B8046253D", 101),
},
}
for _, tc := range testCases {
suite.T().Run(tc.name, func(t *testing.T) {
Expand All @@ -140,6 +154,7 @@ func (suite *MarkerHooksTestSuite) TestAddUpdateMarker() {
marker, err := suite.chainA.GetProvenanceApp().MarkerKeeper.GetMarkerByDenom(suite.chainA.GetContext(), tc.expIbcDenom)
require.NoError(t, err, "GetMarkerByDenom should find "+tc.expErr)
assert.Equal(t, tc.expIbcDenom, marker.GetDenom(), "Marker Denom should be ibc denom")
assert.Equal(t, tc.expSupply, marker.GetSupply(), "Marker Supply should match expected")
metadata, found := suite.chainA.GetProvenanceApp().BankKeeper.GetDenomMetaData(suite.chainA.GetContext(), tc.expIbcDenom)
require.True(t, found, "GetDenomMetaData() not found for "+tc.expErr)
assert.Equal(t, marker.GetDenom(), metadata.Base, "Metadata Base should equal marker denom")
Expand Down

0 comments on commit df2d286

Please sign in to comment.