Skip to content

Commit

Permalink
fix(state): Integrate new Signer for concurrent tx submission
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jun 11, 2024
1 parent e40a109 commit 0308aea
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 159 deletions.
11 changes: 6 additions & 5 deletions blob/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"testing"
"time"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/go-header/store"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmrand "github.com/tendermint/tendermint/libs/rand"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/go-header/store"

"github.com/celestiaorg/celestia-node/blob/blobtest"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
Expand Down Expand Up @@ -425,8 +426,8 @@ func TestService_Get(t *testing.T) {
}

// TestService_GetAllWithoutPadding it retrieves all blobs under the given namespace:
// the amount of the blobs is known and equal to 5. Then it ensures that each blob has a correct index inside the eds
// by requesting share and comparing them.
// the amount of the blobs is known and equal to 5. Then it ensures that each blob has a correct
// index inside the eds by requesting share and comparing them.
func TestService_GetAllWithoutPadding(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
t.Cleanup(cancel)
Expand Down
6 changes: 1 addition & 5 deletions nodebuilder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ func ConstructModule(tp node.Type, network p2p.Network, cfg *Config, store Store
if err != nil {
return fx.Error(err)
}
signer, err := state.KeyringSigner(cfg.State, ks, network)
if err != nil {
return fx.Error(err)
}

baseComponents := fx.Options(
fx.Supply(tp),
fx.Supply(network),
fx.Supply(ks),
fx.Provide(p2p.BootstrappersFor),
fx.Provide(func(lc fx.Lifecycle) context.Context {
return fxutil.WithLifecycle(context.Background(), lc)
Expand All @@ -45,7 +42,6 @@ func ConstructModule(tp node.Type, network p2p.Network, cfg *Config, store Store
fx.Provide(store.Datastore),
fx.Provide(store.Keystore),
fx.Supply(node.StorePath(store.Path())),
fx.Supply(signer),
// modules provided by the node
p2p.ConstructModule(tp, &cfg.P2P),
state.ConstructModule(tp, &cfg.State, &cfg.Core),
Expand Down
19 changes: 14 additions & 5 deletions nodebuilder/state/core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package state

import (
apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"

libfraud "github.com/celestiaorg/go-fraud"
"github.com/celestiaorg/go-header/sync"

Expand All @@ -16,15 +17,23 @@ import (
// a celestia-core connection.
func coreAccessor(
corecfg core.Config,
signer *apptypes.KeyringSigner,
keyring keyring.Keyring,
keyname AccountName,
sync *sync.Syncer[*header.ExtendedHeader],
fraudServ libfraud.Service[*header.ExtendedHeader],
) (*state.CoreAccessor, Module, *modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader]) {
ca := state.NewCoreAccessor(signer, sync, corecfg.IP, corecfg.RPCPort, corecfg.GRPCPort)
) (
*state.CoreAccessor,
Module,
*modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader],
error,
) {
ca, err := state.NewCoreAccessor(keyring, string(keyname), sync, corecfg.IP, corecfg.RPCPort, corecfg.GRPCPort)

return ca, ca, &modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader]{
sBreaker := &modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader]{
Service: ca,
FraudType: byzantine.BadEncoding,
FraudServ: fraudServ,
}

return ca, ca, sBreaker, err
}
22 changes: 8 additions & 14 deletions nodebuilder/state/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,36 @@ package state
import (
kr "github.com/cosmos/cosmos-sdk/crypto/keyring"

apptypes "github.com/celestiaorg/celestia-app/x/blob/types"

"github.com/celestiaorg/celestia-node/libs/keystore"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

const DefaultAccountName = "my_celes_key"

// KeyringSigner constructs a new keyring signer.
// NOTE: we construct keyring signer before constructing node for easier UX
type AccountName string

// Keyring constructs a new keyring.
// NOTE: we construct keyring before constructing node for easier UX
// as having keyring-backend set to `file` prompts user for password.
func KeyringSigner(cfg Config, ks keystore.Keystore, net p2p.Network) (*apptypes.KeyringSigner, error) {
func Keyring(cfg Config, ks keystore.Keystore) (kr.Keyring, AccountName, error) {
ring := ks.Keyring()
var info *kr.Record
// if custom keyringAccName provided, find key for that name
if cfg.KeyringAccName != "" {
keyInfo, err := ring.Key(cfg.KeyringAccName)
if err != nil {
log.Errorw("failed to find key by given name", "keyring.accname", cfg.KeyringAccName)
return nil, err
return nil, "", err
}
info = keyInfo
} else {
// use default key
keyInfo, err := ring.Key(DefaultAccountName)
if err != nil {
log.Errorw("could not access key in keyring", "name", DefaultAccountName)
return nil, err
return nil, "", err
}
info = keyInfo
}
// construct signer using the default key found / generated above
signer := apptypes.NewKeyringSigner(ring, info.Name, string(net))
signerInfo := signer.GetSignerInfo()
log.Infow("constructed keyring signer", "backend", cfg.KeyringBackend, "path", ks.Path(),
"key name", signerInfo.Name, "chain-id", string(net))

return signer, nil
return ring, AccountName(info.Name), nil
}
6 changes: 5 additions & 1 deletion nodebuilder/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package state
import (
"context"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/libs/fxutil"
"github.com/celestiaorg/celestia-node/libs/keystore"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
modfraud "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
Expand All @@ -21,10 +23,12 @@ var log = logging.Logger("module/state")
func ConstructModule(tp node.Type, cfg *Config, coreCfg *core.Config) fx.Option {
// sanitize config values before constructing module
cfgErr := cfg.Validate()

baseComponents := fx.Options(
fx.Supply(*cfg),
fx.Error(cfgErr),
fx.Provide(func(ks keystore.Keystore) (keyring.Keyring, AccountName, error) {
return Keyring(*cfg, ks)
}),
fxutil.ProvideIf(coreCfg.IsEndpointConfigured(), fx.Annotate(
coreAccessor,
fx.OnStart(func(ctx context.Context,
Expand Down
14 changes: 10 additions & 4 deletions nodebuilder/state/opts.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package state

import (
kr "github.com/cosmos/cosmos-sdk/crypto/keyring"
"go.uber.org/fx"

"github.com/celestiaorg/celestia-app/x/blob/types"
"github.com/celestiaorg/celestia-node/libs/fxutil"
)

// WithKeyringSigner overrides the default keyring signer constructed
// WithKeyring overrides the default keyring constructed
// by the node.
func WithKeyringSigner(signer *types.KeyringSigner) fx.Option {
return fx.Replace(signer)
func WithKeyring(keyring kr.Keyring) fx.Option {
return fxutil.ReplaceAs(keyring, new(kr.Keyring))
}

// WithKeyName configures the signer to use the given key.
func WithKeyName(name AccountName) fx.Option {
return fx.Replace(name)
}
19 changes: 7 additions & 12 deletions nodebuilder/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/fx"

apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
libhead "github.com/celestiaorg/go-header"

"github.com/celestiaorg/celestia-node/core"
Expand All @@ -17,9 +16,10 @@ import (
"github.com/celestiaorg/celestia-node/libs/fxutil"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
)

const TestKeyringName = "test_celes"

// MockStore provides mock in memory Store for testing purposes.
func MockStore(t *testing.T, cfg *Config) Store {
t.Helper()
Expand Down Expand Up @@ -48,10 +48,13 @@ func TestNodeWithConfig(t *testing.T, tp node.Type, cfg *Config, opts ...fx.Opti
store := MockStore(t, cfg)
ks, err := store.Keystore()
require.NoError(t, err)
kr := ks.Keyring()
// create a key in the keystore to be used by the core accessor
_, _, err = kr.NewMnemonic(TestKeyringName, keyring.English, "", "", hd.Secp256k1)
require.NoError(t, err)
cfg.State.KeyringAccName = TestKeyringName

opts = append(opts,
// avoid writing keyring on disk
state.WithKeyringSigner(TestKeyringSigner(t, ks.Keyring())),
// temp dir for the eds store FIXME: Should be in mem
fx.Replace(node.StorePath(t.TempDir())),
// avoid requesting trustedPeer during initialization
Expand All @@ -71,11 +74,3 @@ func TestNodeWithConfig(t *testing.T, tp node.Type, cfg *Config, opts ...fx.Opti
require.NoError(t, err)
return nd
}

func TestKeyringSigner(t *testing.T, ring keyring.Keyring) *apptypes.KeyringSigner {
signer := apptypes.NewKeyringSigner(ring, "", string(p2p.Private))
_, _, err := signer.NewMnemonic("my_celes_key", keyring.English, "",
"", hd.Secp256k1)
require.NoError(t, err)
return signer
}
2 changes: 0 additions & 2 deletions nodebuilder/tests/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ func TestBlobModule(t *testing.T) {
blobs = append(blobs, blob)
}

require.NoError(t, err)
bridge := sw.NewBridgeNode()
require.NoError(t, bridge.Start(ctx))

addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(bridge.Host))
require.NoError(t, err)

Expand Down
5 changes: 2 additions & 3 deletions nodebuilder/tests/swamp/swamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"golang.org/x/exp/maps"

"github.com/celestiaorg/celestia-app/test/util/testnode"
apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
libhead "github.com/celestiaorg/go-header"

"github.com/celestiaorg/celestia-node/core"
Expand Down Expand Up @@ -258,9 +257,9 @@ func (s *Swamp) NewNodeWithStore(
store nodebuilder.Store,
options ...fx.Option,
) *nodebuilder.Node {
signer := apptypes.NewKeyringSigner(s.ClientContext.Keyring, s.Accounts[0], s.ClientContext.ChainID)
options = append(options,
state.WithKeyringSigner(signer),
state.WithKeyring(s.ClientContext.Keyring),
state.WithKeyName(state.AccountName(s.Accounts[0])),
)

switch tp {
Expand Down
Loading

0 comments on commit 0308aea

Please sign in to comment.