Skip to content

Commit

Permalink
feat: add Skyway CW bindings (#1325)
Browse files Browse the repository at this point in the history
# Related Github tickets

- VolumeFi#2442
- VolumeFi#2468

# Background

This change refactors a large amount of the existing CW integration,
fixes a bug introduced with the last update & introduces bindings for
the Skyway module.

# 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
byte-bandit authored Jan 9, 2025
1 parent 65d95c2 commit 017da08
Show file tree
Hide file tree
Showing 31 changed files with 1,398 additions and 491 deletions.
67 changes: 57 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import (
chainparams "github.com/palomachain/paloma/v2/app/params"
xchain "github.com/palomachain/paloma/v2/internal/x-chain"
"github.com/palomachain/paloma/v2/util/libcons"
"github.com/palomachain/paloma/v2/util/libwasm"
consensusmodule "github.com/palomachain/paloma/v2/x/consensus"
consensusmodulekeeper "github.com/palomachain/paloma/v2/x/consensus/keeper"
consensusmoduletypes "github.com/palomachain/paloma/v2/x/consensus/types"
Expand All @@ -143,6 +144,7 @@ import (
schedulermodulekeeper "github.com/palomachain/paloma/v2/x/scheduler/keeper"
schedulermoduletypes "github.com/palomachain/paloma/v2/x/scheduler/types"
skywaymodule "github.com/palomachain/paloma/v2/x/skyway"
skywaybindings "github.com/palomachain/paloma/v2/x/skyway/bindings"
skywayclient "github.com/palomachain/paloma/v2/x/skyway/client"
skywaymodulekeeper "github.com/palomachain/paloma/v2/x/skyway/keeper"
skywaymoduletypes "github.com/palomachain/paloma/v2/x/skyway/types"
Expand Down Expand Up @@ -742,20 +744,33 @@ func New(
"cosmwasm_2_0",
}

opts := []wasmkeeper.Option{
wasmkeeper.WithMessageHandlerDecorator(func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
return wasmkeeper.NewMessageHandlerChain(
old,
app.SchedulerKeeper.ExecuteWasmJobEventListener(),
)
}),
}
bbk, ok := app.BankKeeper.(bankkeeper.BaseKeeper)
if !ok {
panic("bankkeeper is not a BaseKeeper")
}
opts = append(opts, tokenfactorybindings.RegisterCustomPlugins(&bbk, &app.TokenFactoryKeeper)...)
opts = append(opts, schedulerbindings.RegisterCustomPlugins(&app.SchedulerKeeper)...)
messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator(
buildWasmMessageDecorator(
logger,
&app.SchedulerKeeper,
&app.SkywayKeeper,
&bbk,
&app.TokenFactoryKeeper,
),
)
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: buildWasmQuerier(
&app.SchedulerKeeper,
&app.SkywayKeeper,
&bbk,
&app.TokenFactoryKeeper,
),
})

opts := []wasmkeeper.Option{
messengerDecoratorOpt,
queryPluginOpt,
}

app.wasmKeeper = wasmkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[wasmtypes.StoreKey]),
Expand Down Expand Up @@ -1311,3 +1326,35 @@ func BlockedAddresses() map[string]bool {
func (a *App) Configurator() module.Configurator {
return a.configurator
}

func buildWasmMessageDecorator(
log log.Logger,
scheduler *schedulermodulekeeper.Keeper,
skyway *skywaymodulekeeper.Keeper,
bank *bankkeeper.BaseKeeper,
tokenfactory *tokenfactorymodulekeeper.Keeper,
) func(wasmkeeper.Messenger) wasmkeeper.Messenger {
srv := schedulermodulekeeper.NewMsgServerImpl(scheduler)
skwSrv := skywaymodulekeeper.NewMsgServerImpl(*skyway)

return libwasm.NewMessenger(
log,
schedulerbindings.NewLegacyMessenger(scheduler),
schedulerbindings.NewMessenger(scheduler, srv),
skywaybindings.NewMessenger(skwSrv),
tokenfactorybindings.NewMessenger(bank, tokenfactory),
)
}

func buildWasmQuerier(
scheduler schedulerbindings.Schedulerkeeper,
skyway skywaybindings.SkywayKeeper,
b *bankkeeper.BaseKeeper,
tfk *tokenfactorymodulekeeper.Keeper,
) wasmkeeper.CustomQuerier {
return libwasm.NewQuerier(
schedulerbindings.NewQueryPlugin(scheduler),
skywaybindings.NewQueryPlugin(skyway),
tokenfactorybindings.NewQueryPlugin(b, tfk),
)
}
133 changes: 0 additions & 133 deletions tests/integration/scheduler/keeper/wasm_handler_test.go

This file was deleted.

34 changes: 34 additions & 0 deletions util/libeth/libeth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package libeth

import (
"encoding/hex"
"fmt"

"github.com/ethereum/go-ethereum/common"
)

// Validates the input string as an Ethereum Address
// Addresses must not be empty, have 42 character length, start with 0x and have 40 remaining characters in [0-9a-fA-F]
func ValidateEthAddress(address string) error {
if address == "" {
return fmt.Errorf("empty")
}

if has0xPrefix(address) {
address = address[2:]
}

if _, err := hex.DecodeString(address); err != nil {
return fmt.Errorf("invalid hex with error: %s", err)
}

if !common.IsHexAddress(address) {
return fmt.Errorf("address(%s) doesn't pass format validation", address)
}

return nil
}

func has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
88 changes: 88 additions & 0 deletions util/libeth/libeth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package libeth

import (
"testing"
)

func TestValidateEthAddress(t *testing.T) {
tests := []struct {
name string
address string
wantErr bool
}{
{
name: "Valid address with 0x prefix",
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
wantErr: false,
},
{
name: "Valid address without 0x prefix",
address: "742d35Cc6634C0532925a3b844Bc454e4438f44e",
wantErr: false,
},
{
name: "Empty address",
address: "",
wantErr: true,
},
{
name: "Invalid hex characters",
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44g",
wantErr: true,
},
{
name: "Too short address",
address: "0x742d35",
wantErr: true,
},
{
name: "Too long address",
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e11",
wantErr: true,
},
{
name: "Invalid prefix",
address: "1x742d35Cc6634C0532925a3b844Bc454e4438f44e",
wantErr: true,
},
{
name: "Only 0x prefix",
address: "0x",
wantErr: true,
},
{
name: "Case-insensitive 0X prefix",
address: "0X742d35Cc6634C0532925a3b844Bc454e4438f44e",
wantErr: false,
},
{
name: "Mixed case address",
address: "0x1DeaDbeeF634C0532925a3b844Bc454e4438f44e",
wantErr: false,
},
{
name: "All lowercase address",
address: "0x1deadbeef634c0532925a3b844bc454e4438f44e",
wantErr: false,
},
{
name: "All uppercase address",
address: "0x1DEADBEEF634C0532925A3B844BC454E4438F44E",
wantErr: false,
},
{
name: "Special characters in address",
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f4$e",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateEthAddress(tt.address)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateEthAddress() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading

0 comments on commit 017da08

Please sign in to comment.