Skip to content

Commit

Permalink
Merge pull request #12 from datachainlab/fix-conn-open-once
Browse files Browse the repository at this point in the history
Fix to initialize grpc connection in prover constructor

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Dec 18, 2023
2 parents b910adf + 456b71b commit 850951c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
6 changes: 1 addition & 5 deletions relay/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package relay

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/datachainlab/lcp-go/relay/elc"
"github.com/datachainlab/lcp-go/relay/enclave"
"google.golang.org/grpc"
Expand All @@ -14,16 +13,13 @@ type (
)

type LCPServiceClient struct {
codec codec.ProtoCodecMarshaler

ELCMsgClient
ELCQueryClient
EnclaveQueryClient
}

func NewLCPServiceClient(conn *grpc.ClientConn, codec codec.ProtoCodecMarshaler) LCPServiceClient {
func NewLCPServiceClient(conn *grpc.ClientConn) LCPServiceClient {
return LCPServiceClient{
codec: codec,
ELCMsgClient: elc.NewMsgClient(conn),
ELCQueryClient: elc.NewQueryClient(conn),
EnclaveQueryClient: enclave.NewQueryClient(conn),
Expand Down
4 changes: 0 additions & 4 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ func checkMsgStatus(verifier core.FinalityAwareChain, msgID core.MsgID) (bool, b

// if returns true, query new key and register key and set it to memory
func (pr *Prover) loadEKIAndCheckUpdateNeeded(ctx context.Context, verifier core.FinalityAwareChain) (bool, error) {
if err := pr.initServiceClient(); err != nil {
return false, err
}

now := time.Now()

// no active enclave key in memory
Expand Down
24 changes: 8 additions & 16 deletions relay/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,19 @@ var (
)

func NewProver(config ProverConfig, originChain core.Chain, originProver core.Prover) (*Prover, error) {
return &Prover{config: config, originChain: originChain, originProver: originProver}, nil
}

func (pr *Prover) GetOriginProver() core.Prover {
return pr.originProver
}

func (pr *Prover) initServiceClient() error {
conn, err := grpc.Dial(
pr.config.LcpServiceAddress,
config.LcpServiceAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
return err
return nil, err
}
pr.lcpServiceClient = NewLCPServiceClient(conn, pr.codec)
return nil
return &Prover{config: config, originChain: originChain, originProver: originProver, lcpServiceClient: NewLCPServiceClient(conn)}, nil
}

func (pr *Prover) GetOriginProver() core.Prover {
return pr.originProver
}

// Init initializes the chain
Expand Down Expand Up @@ -90,7 +85,7 @@ func (pr *Prover) SetRelayInfo(path *core.PathEnd, counterparty *core.ProvableCh

// SetupForRelay performs chain-specific setup before starting the relay
func (pr *Prover) SetupForRelay(ctx context.Context) error {
return pr.initServiceClient()
return nil
}

// GetChainID returns the chain ID
Expand All @@ -102,9 +97,6 @@ func (pr *Prover) GetChainID() string {
// These states will be submitted to the counterparty chain as MsgCreateClient.
// If `height` is nil, the latest finalized height is selected automatically.
func (pr *Prover) CreateInitialLightClientState(height exported.Height) (exported.ClientState, exported.ConsensusState, error) {
if err := pr.initServiceClient(); err != nil {
return nil, nil, err
}
// NOTE: Query the LCP for available keys, but no need to register it into on-chain here
tmpEKI, err := pr.selectNewEnclaveKey(context.TODO())
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions relay/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

func (pr *Prover) restoreELCState(ctx context.Context, counterparty core.FinalityAwareChain, height uint64) error {
if err := pr.initServiceClient(); err != nil {
return err
}

// ensure the client does not exist in the LCP service
_, err := pr.lcpServiceClient.Client(ctx, &elc.QueryClientRequest{
Expand Down

0 comments on commit 850951c

Please sign in to comment.