Skip to content

Commit

Permalink
added client field for light node
Browse files Browse the repository at this point in the history
  • Loading branch information
chandiniv1 committed Nov 22, 2023
1 parent d2001ba commit 4de74ac
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
3 changes: 3 additions & 0 deletions node/full_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func NewFullClient(node *FullNode) *FullClient {
}

func (n *FullNode) GetClient() rpcclient.Client {
if n.client == nil {
n.client = NewFullClient(n)
}
return n.client
}

Expand Down
22 changes: 16 additions & 6 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"
"time"

rpcclient "github.com/cometbft/cometbft/rpc/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -79,7 +80,8 @@ func getRPC(t *testing.T) (*mocks.Application, *FullClient) {
key, _, _ := crypto.GenerateEd25519Key(crand.Reader)
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
ctx := context.Background()
node, err := newFullNode(ctx, config.NodeConfig{DALayer: "mock"}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
var client rpcclient.Client
node, err := newFullNode(ctx, config.NodeConfig{DALayer: "mock"}, client, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
require.NoError(err)
require.NotNil(node)

Expand Down Expand Up @@ -177,7 +179,8 @@ func TestGenesisChunked(t *testing.T) {
mockApp.On(InitChain, mock.Anything).Return(abci.ResponseInitChain{})
privKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
n, _ := newFullNode(context.Background(), config.NodeConfig{DALayer: "mock"}, privKey, signingKey, proxy.NewLocalClientCreator(mockApp), genDoc, test.NewFileLogger(t))
var client rpcclient.Client
n, _ := newFullNode(context.Background(), config.NodeConfig{DALayer: "mock"}, client, privKey, signingKey, proxy.NewLocalClientCreator(mockApp), genDoc, test.NewFileLogger(t))

rpc := NewFullClient(n)

Expand Down Expand Up @@ -485,12 +488,14 @@ func TestTx(t *testing.T) {
genesisValidators, signingKey := getGenesisValidatorSetWithSigner()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var client rpcclient.Client
node, err := newFullNode(ctx, config.NodeConfig{
DALayer: "mock",
Aggregator: true,
BlockManagerConfig: config.BlockManagerConfig{
BlockTime: 1 * time.Second, // blocks must be at least 1 sec apart for adjacent headers to get verified correctly
}},
client,
key, signingKey, proxy.NewLocalClientCreator(mockApp),
&cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators},
test.NewFileLogger(t))
Expand Down Expand Up @@ -744,6 +749,7 @@ func createGenesisValidators(t *testing.T, numNodes int, appCreator func(require
}
signingKey, err := GetNodeKey(nodeKey)
require.NoError(err)
var client rpcclient.Client
nodes[i], err = newFullNode(
ctx,
config.NodeConfig{
Expand All @@ -754,6 +760,7 @@ func createGenesisValidators(t *testing.T, numNodes int, appCreator func(require
DABlockTime: 100 * time.Millisecond,
},
},
client,
signingKey,
signingKey,
proxy.NewLocalClientCreator(apps[i]),
Expand Down Expand Up @@ -912,6 +919,7 @@ func TestMempool2Nodes(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var client rpcclient.Client
// make node1 an aggregator, so that node2 can start gracefully
node1, err := newFullNode(ctx, config.NodeConfig{
Aggregator: true,
Expand All @@ -922,17 +930,16 @@ func TestMempool2Nodes(t *testing.T) {
BlockManagerConfig: config.BlockManagerConfig{
BlockTime: 1 * time.Second,
},
}, key1, signingKey1, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
}, client, key1, signingKey1, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
require.NoError(err)
require.NotNil(node1)

node2, err := newFullNode(ctx, config.NodeConfig{
DALayer: "mock",
P2P: config.P2PConfig{
ListenAddress: "/ip4/127.0.0.1/tcp/9002",
Seeds: "/ip4/127.0.0.1/tcp/9001/p2p/" + id1.Pretty(),
},
}, key2, signingKey2, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
}, client, key2, signingKey2, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test"}, test.NewFileLogger(t))
require.NoError(err)
require.NotNil(node1)

Expand Down Expand Up @@ -1006,7 +1013,7 @@ func TestStatus(t *testing.T) {
Name: "one",
}
}

var client rpcclient.Client
node, err := newFullNode(
context.Background(),
config.NodeConfig{
Expand All @@ -1019,6 +1026,7 @@ func TestStatus(t *testing.T) {
BlockTime: 10 * time.Millisecond,
},
},
client,
key,
signingKey,
proxy.NewLocalClientCreator(app),
Expand Down Expand Up @@ -1123,12 +1131,14 @@ func TestFutureGenesisTime(t *testing.T) {
genesisTime := time.Now().Local().Add(time.Second * time.Duration(1))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var client rpcclient.Client
node, err := newFullNode(ctx, config.NodeConfig{
DALayer: "mock",
Aggregator: true,
BlockManagerConfig: config.BlockManagerConfig{
BlockTime: 200 * time.Millisecond,
}},
client,
key, signingKey,
proxy.NewLocalClientCreator(mockApp),
&cmtypes.GenesisDoc{
Expand Down
14 changes: 9 additions & 5 deletions node/full_node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/proxy"
rpcclient "github.com/cometbft/cometbft/rpc/client"
cmtypes "github.com/cometbft/cometbft/types"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -52,7 +52,8 @@ func TestAggregatorMode(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
node, err := newFullNode(ctx, config.NodeConfig{DALayer: "mock", Aggregator: true, BlockManagerConfig: blockManagerConfig}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
var client rpcclient.Client
node, err := newFullNode(ctx, config.NodeConfig{DALayer: "mock", Aggregator: true, BlockManagerConfig: blockManagerConfig}, client, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
require.NoError(err)
require.NotNil(node)

Expand Down Expand Up @@ -156,12 +157,13 @@ func TestLazyAggregator(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var client rpcclient.Client
node, err := NewNode(ctx, config.NodeConfig{
DALayer: "mock",
Aggregator: true,
BlockManagerConfig: blockManagerConfig,
LazyAggregator: true,
}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
}, key, signingKey, client, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
assert.False(node.IsRunning())
assert.NoError(err)

Expand All @@ -173,7 +175,7 @@ func TestLazyAggregator(t *testing.T) {

require.NoError(waitForFirstBlock(node.(*FullNode), Header))

client := node.GetClient()
client = node.GetClient()

_, err = client.BroadcastTxCommit(context.Background(), []byte{0, 0, 0, 1})
assert.NoError(err)
Expand Down Expand Up @@ -643,6 +645,7 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys
genesis := &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}
// TODO: need to investigate why this needs to be done for light nodes
genesis.InitialHeight = 1
var client rpcclient.Client
node, err := NewNode(
ctx,
config.NodeConfig{
Expand All @@ -654,6 +657,7 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys
},
keys[n],
signingKey,
client,
proxy.NewLocalClientCreator(app),
genesis,
test.NewFileLoggerCustom(t, test.TempLogFileName(t, fmt.Sprintf("node%v", n))).With("node", n))
Expand Down
15 changes: 10 additions & 5 deletions node/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var _ Node = &LightNode{}
type LightNode struct {
service.BaseService

P2P *p2p.Client
P2P *p2p.Client
client rpcclient.Client

proxyApp proxy.AppConns

Expand All @@ -35,13 +36,17 @@ type LightNode struct {
}

func (ln *LightNode) GetClient() rpcclient.Client {
return NewLightClient(ln)
if ln.client == nil {
ln.client = NewLightClient(ln)
}
return ln.client
}

func newLightNode(
ctx context.Context,
conf config.NodeConfig,
p2pKey crypto.PrivKey,
client rpcclient.Client,
clientCreator proxy.ClientCreator,
genesis *cmtypes.GenesisDoc,
logger log.Logger,
Expand All @@ -57,20 +62,20 @@ func newLightNode(
if err != nil {
return nil, err
}
client, err := p2p.NewClient(conf.P2P, p2pKey, genesis.ChainID, datastore, logger.With("module", "p2p"))
p2pClient, err := p2p.NewClient(conf.P2P, p2pKey, genesis.ChainID, datastore, logger.With("module", "p2p"))
if err != nil {
return nil, err
}

headerSyncService, err := block.NewHeaderSynceService(ctx, datastore, conf, genesis, client, logger.With("module", "HeaderSyncService"))
headerSyncService, err := block.NewHeaderSynceService(ctx, datastore, conf, genesis, p2pClient, logger.With("module", "HeaderSyncService"))
if err != nil {
return nil, fmt.Errorf("error while initializing HeaderSyncService: %w", err)
}

ctx, cancel := context.WithCancel(ctx)

node := &LightNode{
P2P: client,
P2P: p2pClient,
proxyApp: proxyApp,
hSyncService: headerSyncService,
cancel: cancel,
Expand Down
3 changes: 3 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewNode(
conf config.NodeConfig,
p2pKey crypto.PrivKey,
signingKey crypto.PrivKey,
client rpcclient.Client,
appClient proxy.ClientCreator,
genesis *cmtypes.GenesisDoc,
logger log.Logger,
Expand All @@ -36,6 +37,7 @@ func NewNode(
return newFullNode(
ctx,
conf,
client,
p2pKey,
signingKey,
appClient,
Expand All @@ -47,6 +49,7 @@ func NewNode(
ctx,
conf,
p2pKey,
client,
appClient,
genesis,
logger,
Expand Down
9 changes: 5 additions & 4 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"testing"

proxy "github.com/cometbft/cometbft/proxy"
rpcclient "github.com/cometbft/cometbft/rpc/client"
cmtypes "github.com/cometbft/cometbft/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/rollkit/rollkit/config"
test "github.com/rollkit/rollkit/test/log"
"github.com/rollkit/rollkit/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// cleanUpNode stops the node and checks if it is running
Expand Down Expand Up @@ -54,10 +54,11 @@ func newTestNode(ctx context.Context, t *testing.T, nodeType string) (Node, erro
default:
panic(fmt.Sprint("invalid node type", nodeType))
}
var client rpcclient.Client
app := setupMockApplication()
key, signingKey := generateSingleKey(), generateSingleKey()
logger := test.NewFileLogger(t)
return NewNode(ctx, config, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: types.TestChainID}, logger)
return NewNode(ctx, config, key, signingKey, client, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: types.TestChainID}, logger)
}

// setupTestNode sets up a test node
Expand Down
3 changes: 2 additions & 1 deletion rpc/json/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func getRPC(t *testing.T) (*mocks.Application, rpcclient.Client) {
genesisValidators := []cmtypes.GenesisValidator{
{Address: pubKey.Address(), PubKey: pubKey, Power: int64(100), Name: "gen #1"},
}
n, err := node.NewNode(context.Background(), config.NodeConfig{Aggregator: true, DALayer: "mock", BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
var client rpcclient.Client
n, err := node.NewNode(context.Background(), config.NodeConfig{Aggregator: true, DALayer: "mock", BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false}, key, signingKey, client, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
require.NoError(err)
require.NotNil(n)

Expand Down

0 comments on commit 4de74ac

Please sign in to comment.