Skip to content

Commit

Permalink
Merge pull request #292 from terra-money/feat/v2.11/alliance-wasm-bin…
Browse files Browse the repository at this point in the history
…dings

feat: added alliance bindings
  • Loading branch information
emidev98 authored Apr 11, 2024
2 parents b652921 + dd2bbfa commit 81f8d84
Show file tree
Hide file tree
Showing 20 changed files with 1,649 additions and 42 deletions.
7 changes: 4 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/terra-money/core/v2/app/custom_queriers"
"github.com/terra-money/core/v2/app/keepers"
"github.com/terra-money/core/v2/app/post"
"github.com/terra-money/core/v2/app/rpc"
tokenfactorybindings "github.com/terra-money/core/v2/x/tokenfactory/bindings"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -478,9 +478,10 @@ func (app *TerraApp) GetWasmOpts(appOpts servertypes.AppOptions) []wasmkeeper.Op
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
}

wasmOpts = append(wasmOpts, tokenfactorybindings.RegisterCustomPlugins(
wasmOpts = append(wasmOpts, custom_queriers.RegisterCustomPlugins(
&app.Keepers.BankKeeper.BaseKeeper,
&app.Keepers.TokenFactoryKeeper)...,
&app.Keepers.TokenFactoryKeeper,
&app.Keepers.AllianceKeeper)...,
)

return wasmOpts
Expand Down
52 changes: 52 additions & 0 deletions app/custom_queriers/custom_queriers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package custom_queriers

import (
"encoding/json"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
tokenfactorybindings "github.com/terra-money/core/v2/x/tokenfactory/bindings"
tokenfactorykeeper "github.com/terra-money/core/v2/x/tokenfactory/keeper"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type Querier func(ctx sdk.Context, request json.RawMessage) ([]byte, error)

func CustomQueriers(queriers ...Querier) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
for _, querier := range queriers {
res, err := querier(ctx, request)
if err == nil || !strings.Contains(err.Error(), "unknown query") {
return res, err
}
}
return nil, fmt.Errorf("unknown query")
}
}

func RegisterCustomPlugins(
bank *bankkeeper.BaseKeeper,
tokenFactory *tokenfactorykeeper.Keeper,
allianceKeeper *alliancekeeper.Keeper,
) []wasmkeeper.Option {
tfQuerier := tokenfactorybindings.CustomQuerier(tokenfactorybindings.NewQueryPlugin(bank, tokenFactory))
allianceQuerier := alliancebindings.CustomQuerier(alliancebindings.NewAllianceQueryPlugin(allianceKeeper))
queriers := CustomQueriers(tfQuerier, allianceQuerier)

queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: queriers,
})
messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator(
tokenfactorybindings.CustomMessageDecorator(bank, tokenFactory),
)

return []wasm.Option{
queryPluginOpt,
messengerDecoratorOpt,
}
}
109 changes: 109 additions & 0 deletions app/custom_queriers/custom_queriers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package custom_queriers

import (
"encoding/json"
"fmt"
"runtime"
"testing"

"github.com/stretchr/testify/require"
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings"
"github.com/terra-money/alliance/x/alliance/bindings/types"
"github.com/terra-money/core/v2/x/tokenfactory/bindings"
types2 "github.com/terra-money/core/v2/x/tokenfactory/bindings/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func AlwaysErrorQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return nil, fmt.Errorf("always error")
}

func AlwaysUnknownQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return nil, fmt.Errorf("unknown query")
}

func AlwaysGoodQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return []byte("good"), nil
}

func TestCustomQueriers(t *testing.T) {
querier := CustomQueriers(AlwaysUnknownQuerier, AlwaysErrorQuerier, AlwaysGoodQuerier)
_, err := querier(sdk.Context{}, nil)
require.ErrorContainsf(t, err, "always error", "")

querier = CustomQueriers(AlwaysUnknownQuerier, AlwaysGoodQuerier, AlwaysErrorQuerier)
_, err = querier(sdk.Context{}, nil)
require.NoError(t, err)
}

func TestWithTfAndAllianceButCallAlliance(t *testing.T) {
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{})
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{})
querier := CustomQueriers(tfQuerier, allianceQuerier)

query := types.AllianceQuery{
Alliance: &types.Alliance{Denom: "123"},
}
bz, err := json.Marshal(query)
require.NoError(t, err)

defer func() {
if r := recover(); r != nil {
stack := make([]byte, 1024)
runtime.Stack(stack, false)
// We make sure alliance is called here
require.Containsf(t, string(stack), "GetAlliance", "")
}
}()

// We call querier but it will panic because we don't have a keeper
_, err = querier(sdk.Context{}, bz)
require.Fail(t, "should panic")
}

func TestWithTfAndAllianceButCallTf(t *testing.T) {
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{})
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{})
querier := CustomQueriers(tfQuerier, allianceQuerier)

query := types2.TokenFactoryQuery{
Token: &types2.TokenQuery{
Params: &types2.GetParams{},
},
}
bz, err := json.Marshal(query)
require.NoError(t, err)

defer func() {
if r := recover(); r != nil {
stack := make([]byte, 1024)
runtime.Stack(stack, false)
// We make sure tf is called here
require.Containsf(t, string(stack), "GetParams", "")
}
}()

// We call querier but it will panic because we don't have a keeper
_, err = querier(sdk.Context{}, bz)
require.Fail(t, "should panic")
}

func TestWithTfAndAllianceButRandomCall(t *testing.T) {
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{})
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{})
querier := CustomQueriers(tfQuerier, allianceQuerier)

query := sdk.NewCoin("denom", sdk.NewInt(1))
bz, err := json.Marshal(query)
require.NoError(t, err)

// We call querier but it will panic because we don't have a keeper
_, err = querier(sdk.Context{}, bz)
require.Error(t, err)
}

func TestRegisterCustomPlugins(t *testing.T) {
options := RegisterCustomPlugins(nil, nil, nil)
require.Len(t, options, 2)
}
7 changes: 4 additions & 3 deletions app/upgrades/v2.11/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package v2_11

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
custombankkeeper "github.com/terra-money/core/v2/x/bank/keeper"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

type EscrowUpdate struct {
Expand Down
1 change: 1 addition & 0 deletions cmd/terrad/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"cosmossdk.io/math"

"cosmossdk.io/simapp"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/terra-money/alliance v0.3.5
github.com/terra-money/alliance v0.3.6
go.uber.org/mock v0.3.0
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0
google.golang.org/grpc v1.60.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/terra-money/alliance v0.3.5 h1:7bLlw9ZUNaFFxGYiKyJKB7x2SV2+6lVvMYbmBm3OORU=
github.com/terra-money/alliance v0.3.5/go.mod h1:HDiUexeXRUkLkLRw5jLQcHuVt1Sx43HfyVl0kfwW3JM=
github.com/terra-money/alliance v0.3.6 h1:FWfix+mKcCrXvdk29MgfXGj0JThOsBxzK81OiSjUMQc=
github.com/terra-money/alliance v0.3.6/go.mod h1:gyenuDQEwyN6mfiOEkaRBaokgk9ryBeU3eCAiZpVKZg=
github.com/terra-money/cosmos-sdk v0.47.10-terra.0 h1:vpod9zXeBp8S8JfP0++YzwCvCEMkJnz3qRmz0pciEQI=
github.com/terra-money/cosmos-sdk v0.47.10-terra.0/go.mod h1:UWpgWkhcsBIATS68uUC0del7IiBN4hPv/vqg8Zz23uw=
github.com/terra-money/ibc-go/v7 v7.3.1-terra.0 h1:CF+iicqyI4BJsW2zjUrUrTxRRrPWFZC30VqvlRyVl28=
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/src/contracts/custom-queries/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
2 changes: 2 additions & 0 deletions integration-tests/src/contracts/custom-queries/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scripts
target
Loading

0 comments on commit 81f8d84

Please sign in to comment.