Skip to content

Commit

Permalink
read bitcoin rpc params
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Nov 20, 2023
1 parent e4e7dc9 commit 1941427
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func NewEthermintApp(
appCodec,
keys[bitcoincommitertypes.MemStoreKey],
app.GetSubspace(bitcoincommitertypes.ModuleName),
homePath,
)

/**** Module Options ****/
Expand Down
1 change: 1 addition & 0 deletions testutil/keeper/bitcoincommiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func BitcoincommiterKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
cdc,
memStoreKey,
paramsSubspace,
"",
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
34 changes: 34 additions & 0 deletions x/bitcoincommiter/keeper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package keeper

import (
"log"
"os"

"gopkg.in/yaml.v3"
)

type BITCOINConfig struct {
// NetworkName defines the bitcoin network name
NetworkName string `yaml:"network-name"`
RPCHost string `yaml:"rpc-host"`
RPCPort string `yaml:"rpc-port"`
RPCUser string `yaml:"rpc-user"`
RPCPass string `yaml:"rpc-pass"`
WalletName string `yaml:"wallet-name"`
// Destination defines the taproot transaction destination address
Destination string `yaml:"destination"`
}

const (
BitcoinRPCConfigFileName = "bitcoin.yaml"
)

func DefaultBITCOINConfig(homePath string) (*BITCOINConfig, error) {
data, err := os.ReadFile(homePath + "/" + BitcoinRPCConfigFileName)
if err != nil {
log.Fatalf("can not read rpc config file: %v", err)
}
config := BITCOINConfig{}
err = yaml.Unmarshal(data, &config)
return &config, err
}
19 changes: 19 additions & 0 deletions x/bitcoincommiter/keeper/connfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper_test

import (
"testing"

"github.com/evmos/ethermint/x/bitcoincommiter/keeper"
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
config, _ := keeper.DefaultBITCOINConfig("../test/source")
require.Equal(t, "signet", config.NetworkName)
require.Equal(t, "localhost", config.RPCHost)
require.Equal(t, "8332", config.RPCPort)
require.Equal(t, "b2node", config.RPCUser)
require.Equal(t, "b2node", config.RPCPass)
require.Equal(t, "b2node", config.WalletName)
require.Equal(t, "tb1qgm39cu009lyvq93afx47pp4h9wxq5x92lxxgnz", config.Destination)
}
12 changes: 5 additions & 7 deletions x/bitcoincommiter/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/evmos/ethermint/x/bitcoincommiter/types"
"github.com/tendermint/tendermint/libs/log"
)

type (
Keeper struct {
cdc codec.BinaryCodec
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
config *BITCOINConfig
}
)

func NewKeeper(
cdc codec.BinaryCodec,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
homePath string,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

bitcoinConfig, _ := DefaultBITCOINConfig(homePath)

Check warning

Code scanning / gosec

Returned error is not propagated up the stack. Warning

Returned error is not propagated up the stack.
return &Keeper{
cdc: cdc,
memKey: memKey,
paramstore: ps,
config: bitcoinConfig,
}
}

Expand Down
31 changes: 31 additions & 0 deletions x/bitcoincommiter/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
"testing"

codecc "github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
bitcoincommitertypes "github.com/evmos/ethermint/x/bitcoincommiter/types"
"github.com/stretchr/testify/require"
)

func TestInitKeeper(t *testing.T) {
amino := codecc.NewLegacyAmino()
interfaceRegistry := codectypes.NewInterfaceRegistry()
marshaler := codecc.NewProtoCodec(interfaceRegistry)
key := sdk.NewKVStoreKey("key")
tkey := sdk.NewTransientStoreKey("tkey")
paramsKeeper := paramskeeper.NewKeeper(marshaler, amino, key, tkey)
space := paramsKeeper.Subspace(bitcoincommitertypes.ModuleName)
commiterKey := sdk.NewKVStoreKey(bitcoincommitertypes.ModuleName)
keeper := NewKeeper(marshaler, commiterKey, space, "../test/source")
require.Equal(t, "signet", keeper.config.NetworkName)
require.Equal(t, "localhost", keeper.config.RPCHost)
require.Equal(t, "8332", keeper.config.RPCPort)
require.Equal(t, "b2node", keeper.config.RPCUser)
require.Equal(t, "b2node", keeper.config.RPCPass)
require.Equal(t, "b2node", keeper.config.WalletName)
require.Equal(t, "tb1qgm39cu009lyvq93afx47pp4h9wxq5x92lxxgnz", keeper.config.Destination)
}
7 changes: 7 additions & 0 deletions x/bitcoincommiter/test/source/bitcoin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
network-name: signet
rpc-host: localhost
rpc-port: 8332
rpc-user: b2node
rpc-pass: b2node
wallet-name: b2node
destination: tb1qgm39cu009lyvq93afx47pp4h9wxq5x92lxxgnz

0 comments on commit 1941427

Please sign in to comment.