Skip to content

Commit

Permalink
make sdk initialization only once
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Aug 3, 2023
1 parent 2557d57 commit c24b265
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
8 changes: 8 additions & 0 deletions pkg/cosmos/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package params

import (
"fmt"
"sync"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -40,7 +41,14 @@ func makeEncodingConfig() encodingConfig {
// TODO: import as params.MakeEncoding config
var config = makeEncodingConfig()

var initOnce sync.Once

// Initialize the cosmos sdk at most one time
func InitCosmosSdk(bech32Prefix, token string) {
initOnce.Do(func() { initCosmosSdk(bech32Prefix, token) })
}

func initCosmosSdk(bech32Prefix, token string) {
// copied from wasmd https://github.com/CosmWasm/wasmd/blob/88e01a98ab8a87b98dc26c03715e6aef5c92781b/app/app.go#L163-L174
// NOTE: Bech32 is configured globally, blocked on https://github.com/cosmos/cosmos-sdk/issues/13140
var (
Expand Down
24 changes: 24 additions & 0 deletions pkg/cosmos/params/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package params

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)

func TestInitCosmosSdk(t *testing.T) {
// sdk initialized only once
assert.NotPanics(t, func() { InitCosmosSdk("wasm", "atom") })
assert.NotPanics(t, func() { InitCosmosSdk("notwasm", "cosmos") })
// calling the internal implementation panics when called a second time
assert.Panics(t, func() { initCosmosSdk("wasm", "cosmos") })

// first call to Init wins
sdkConfig := sdk.GetConfig()
assert.Equal(t, sdkConfig.GetBech32AccountAddrPrefix(), "wasm")
_, ok := sdk.GetDenomUnit("atom")
assert.True(t, ok)
_, ok = sdk.GetDenomUnit("cosmos")
assert.False(t, ok)
}
17 changes: 4 additions & 13 deletions pkg/cosmos/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"sync"

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

Expand All @@ -25,8 +24,6 @@ func (e *ErrMsgUnsupported) Error() string {
return fmt.Sprintf("unsupported message type %T: %s", e.Msg, e.Msg)
}

var initCosmosOnce sync.Once

var _ relaytypes.Relayer = &Relayer{}

type Relayer struct {
Expand Down Expand Up @@ -57,18 +54,12 @@ func (r *Relayer) Start(context.Context) error {
return errors.New("Cosmos unavailable")
}

// initializing the SDK only once enables callers to instantiate
// mulitple relayers. needed for BCF-2317. Note that this is buried in
// Start to prevent conflicts with other tests
// TODO(BCI-915): Make this configurable and relayer-specific
// when doing so, core side will have to run each relayer in a LOOPP
initCosmosOnce.Do(
func() {
params.InitCosmosSdk(
/* bech32Prefix= */ "wasm",
/* token= */ "atom",
)
})
params.InitCosmosSdk(
/* bech32Prefix= */ "wasm",
/* token= */ "atom",
)

return nil
}
Expand Down

0 comments on commit c24b265

Please sign in to comment.