-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #5136: Refactor CLIContext to allow multi-chain verifiers
- Loading branch information
1 parent
532ea18
commit f84d9fa
Showing
4 changed files
with
129 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package context | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmlite "github.com/tendermint/tendermint/lite" | ||
tmliteproxy "github.com/tendermint/tendermint/lite/proxy" | ||
rpcclient "github.com/tendermint/tendermint/rpc/client" | ||
) | ||
|
||
const ( | ||
verifierDir = ".lite_verifier" | ||
|
||
// DefaultVerifierCacheSize defines the default Tendermint cache size. | ||
DefaultVerifierCacheSize = 10 | ||
) | ||
|
||
// CreateVerifier returns a Tendermint verifier from a CLIContext object and | ||
// cache size. An error is returned if the CLIContext is missing required values | ||
// or if the verifier could not be created. A CLIContext must at the very least | ||
// have the chain ID and home directory set. If the CLIContext has TrustNode | ||
// enabled, no verifier will be created. | ||
func CreateVerifier(ctx CLIContext, cacheSize int) (tmlite.Verifier, error) { | ||
if ctx.TrustNode { | ||
return nil, nil | ||
} | ||
|
||
switch { | ||
case ctx.ChainID == "": | ||
return nil, errors.New("must provide a valid chain ID to create verifier") | ||
|
||
case ctx.HomeDir == "": | ||
return nil, errors.New("must provide a valid home directory to create verifier") | ||
|
||
case ctx.Client == nil && ctx.NodeURI == "": | ||
return nil, errors.New("must provide a valid RPC client or RPC URI to create verifier") | ||
} | ||
|
||
// create an RPC client based off of the RPC URI if no RPC client exists | ||
client := ctx.Client | ||
if client == nil { | ||
client = rpcclient.NewHTTP(ctx.NodeURI, "/websocket") | ||
} | ||
|
||
return tmliteproxy.NewVerifier( | ||
ctx.ChainID, filepath.Join(ctx.HomeDir, ctx.ChainID, verifierDir), | ||
client, log.NewNopLogger(), cacheSize, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package context_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateVerifier(t *testing.T) { | ||
tmpDir, err := ioutil.TempDir("", "example") | ||
require.NoError(t, err) | ||
|
||
testCases := []struct { | ||
name string | ||
ctx context.CLIContext | ||
expectErr bool | ||
}{ | ||
{"no chain ID", context.CLIContext{}, true}, | ||
{"no home directory", context.CLIContext{}.WithChainID("test"), true}, | ||
{"no client or RPC URI", context.CLIContext{HomeDir: tmpDir}.WithChainID("test"), true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
verifier, err := context.CreateVerifier(tc.ctx, context.DefaultVerifierCacheSize) | ||
require.Equal(t, tc.expectErr, err != nil, err) | ||
|
||
if !tc.expectErr { | ||
require.NotNil(t, verifier) | ||
} | ||
}) | ||
} | ||
} |