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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos hub plugin and refactor for plugins module loading #653

Merged
merged 10 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
cosmossdk.io/api v0.4.1
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba
cosmossdk.io/core v0.6.2-0.20230323161322-ccd8d40119e4
cosmossdk.io/core v0.7.0
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/log v1.1.0
cosmossdk.io/math v1.0.0
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
cosmossdk.io/api v0.4.1
cosmossdk.io/core v0.6.2-0.20230323161322-ccd8d40119e4
cosmossdk.io/core v0.7.0
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/errors v1.0.0-beta.7.0.20230429155654-3ee8242364e4
cosmossdk.io/log v1.1.0
Expand Down
4 changes: 3 additions & 1 deletion tools/rosetta/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/rosetta
/rosetta

*.so
12 changes: 5 additions & 7 deletions tools/rosetta/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
all: build

rosetta:
go build -mod=readonly - ./cmd/rosetta

build:
go build ./cmd/rosetta.go
go build -mod=readonly -buildmode=plugin ./cmd/rosetta

test:
go test -mod=readonly -race ./...

kek:
cd plugins/osmosis && go mod tidy && make plugin
go run cmd/rosetta/main.go --blockchain "osmosis" --denom-to-suggest "osmo" --network "osmo-test-4" --tendermint "tcp://127.0.0.1:26657" --grpc "lola.wk.zondax.ch:9090"
build:
cd plugins/default && make plugin
cd plugins/osmosis && make plugin
go build -mod=readonly -buildmode=plugin ./cmd/rosetta

.PHONY: all build rosetta test
8 changes: 8 additions & 0 deletions tools/rosetta/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ go install cosmossdk.io/tools/rosetta/cmd/rosetta

Alternatively, for building from source, simply run `make rosetta`. The binary will be located in `tools/rosetta`.

## Plugins

To use Rosetta standalone over any chain, it is possible to easily achieve it by setting up required prefixes and registering zone specific interfaces through plugins.

Each plugin is a minimalist implementation of `InitZone` and `RegisterInterfaces` which allow Rosetta to parse chain specific data. There is an example for cosmos-hub chain under `plugins/default/` folder

The plugin is selected through the cli `--blockchain` flag

## Extensions

There are two ways in which you can customize and extend the implementation with your custom settings.
Expand Down
18 changes: 4 additions & 14 deletions tools/rosetta/cmd/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@ func RosettaCommand(ir codectypes.InterfaceRegistry, cdc codec.Codec) *cobra.Com
}
conf.WithCodec(ir, protoCodec)

// load module
// 1. open the so file to load the symbols
plug, err := plugin.Open("./plugins/osmosis/osmosis.so")
err = rosetta.LoadPlugin(ir, conf.Blockchain)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// 2. look up a symbol (an exported function or variable)
initZone, err := plug.Lookup("InitZone")
if err != nil {
fmt.Println(err)
os.Exit(1)
fmt.Printf("[Rosetta]- Error while loading the plugin: %s", err.Error())
return err
}
initZone.(func())()


rosettaSrv, err := rosetta.ServerFromConfig(conf)
if err != nil {
fmt.Printf("[Rosetta]- Error while creating server: %s", err.Error())
Expand Down
4 changes: 0 additions & 4 deletions tools/rosetta/cmd/rosetta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"os"

"cosmossdk.io/tools/rosetta"

"cosmossdk.io/log"
rosettaCmd "cosmossdk.io/tools/rosetta/cmd"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -18,8 +16,6 @@ func main() {
cdc = codec.NewProtoCodec(interfaceRegistry)
)

rosetta.RegisterInterfaces(interfaceRegistry)

if err := rosettaCmd.RosettaCommand(interfaceRegistry, cdc).Execute(); err != nil {
logger.Error("failed to run rosetta", "error", err)
os.Exit(1)
Expand Down
16 changes: 0 additions & 16 deletions tools/rosetta/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/types"
bankcodec "github.com/cosmos/cosmos-sdk/x/bank/types"

ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
// "github.com/cosmos/ibc-go/v7/modules/core/exported"
ibcLightClient "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"

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

// MakeCodec generates the codec required to interact
Expand All @@ -27,12 +20,3 @@ func MakeCodec() (*codec.ProtoCodec, codectypes.InterfaceRegistry) {

return cdc, ir
}

// RegisterInterfaces registers rosetta related implementations and interfaces.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
ibcclienttypes.RegisterInterfaces(registry)
ibcLightClient.RegisterInterfaces(registry)
sdk.RegisterInterfaces(registry)
txtypes.RegisterInterfaces(registry)
cryptocodec.RegisterInterfaces(registry)
}
4 changes: 2 additions & 2 deletions tools/rosetta/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ require (
)

require (
cosmossdk.io/api v0.3.1 // indirect
cosmossdk.io/core v0.6.1 // indirect
cosmossdk.io/api v0.4.1 // indirect
cosmossdk.io/core v0.7.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7.0.20230429155654-3ee8242364e4 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
Expand Down
41 changes: 41 additions & 0 deletions tools/rosetta/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rosetta

import (
"fmt"
"os"
"plugin"

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

func LoadPlugin(ir codectypes.InterfaceRegistry, blockchain string) (err error) {
pluginPathMain := fmt.Sprintf("./plugins/%s/main.so", blockchain)
if _, err := os.Stat(pluginPathMain); os.IsNotExist(err) {
fmt.Printf("Plugin file '%s' does not exist, loading default plugin 'cosmos-hub'.\n", pluginPathMain)
pluginPathMain = fmt.Sprintf("./plugins/%s/main.so", "default")
} else if os.IsExist(err) {
return err
}

// load module
plug, err := plugin.Open(pluginPathMain)
if err != nil {
fmt.Println("There was an error while opening the plugin...", err)
return err
}

initZone, err := plug.Lookup("InitZone")
if err != nil {
fmt.Println("There was an error while initializing the zone.", err)
return err
}
initZone.(func())()

registerInterfaces, err := plug.Lookup("RegisterInterfaces")
if err != nil {
fmt.Println("There was an error while registering interfaces...", err)
return err
}
registerInterfaces.(func(codectypes.InterfaceRegistry))(ir)
return err
}
4 changes: 4 additions & 0 deletions tools/rosetta/plugins/default/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/make -f

plugin:
go build -buildmode=plugin -o main.so main.go
28 changes: 28 additions & 0 deletions tools/rosetta/plugins/default/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"

ibcLightClient "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"

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

func InitZone() {
config := sdk.GetConfig()
prefix := "cosmos"
config.SetBech32PrefixForAccount(prefix, prefix+"pub")
}

func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
ibcclienttypes.RegisterInterfaces(registry)
ibcLightClient.RegisterInterfaces(registry)
sdk.RegisterInterfaces(registry)
txtypes.RegisterInterfaces(registry)
cryptocodec.RegisterInterfaces(registry)
}
2 changes: 1 addition & 1 deletion tools/rosetta/plugins/osmosis/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/make -f

plugin:
go build -buildmode=plugin -o osmosis.so osmosis.go
go build -buildmode=plugin -o main.so main.go
58 changes: 56 additions & 2 deletions tools/rosetta/plugins/osmosis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,117 @@ module osmosis
go 1.20

require (
github.com/CosmWasm/wasmd v0.40.0-rc.2
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/ibc-go/v7 v7.0.0
github.com/cosmos/ibc-go/v7 v7.0.1
)

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
cosmossdk.io/api v0.4.1 // indirect
cosmossdk.io/core v0.7.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7.0.20230429155654-3ee8242364e4 // indirect
cosmossdk.io/math v1.0.0 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/CosmWasm/wasmvm v1.2.3 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/cometbft/cometbft v0.37.1 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ics23/go v0.9.1-0.20221207100636-b1abd8678aab // indirect
github.com/cosmos/ledger-cosmos-go v0.12.1 // indirect
github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect
github.com/creachadair/taskgroup v0.4.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20221215004737-a150e88a970d // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -81,7 +122,9 @@ require (
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.43.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rakyll/statik v0.1.7 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand All @@ -95,23 +138,34 @@ require (
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
pgregory.net/rapid v0.5.7 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace cosmossdk.io/api => cosmossdk.io/api v0.3.1
replace (
// github.com/CosmWasm/wasmd => github.com/osmosis-labs/wasmd v0.31.0-osmo-v16
cosmossdk.io/api => cosmossdk.io/api v0.3.1
github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
)
Loading
Loading