-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,342 additions
and
392 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,63 @@ | ||
package clientcontroller | ||
|
||
type CommitPublicRandomnessMsg struct { | ||
CommitPublicRandomness CommitPublicRandomnessMsgParams `json:"commit_public_randomness"` | ||
} | ||
|
||
type CommitPublicRandomnessMsgParams struct { | ||
FpPubkeyHex string `json:"fp_pubkey_hex"` | ||
StartHeight uint64 `json:"start_height"` | ||
NumPubRand uint64 `json:"num_pub_rand"` | ||
Commitment []byte `json:"commitment"` | ||
Signature []byte `json:"signature"` | ||
} | ||
|
||
// TODO: need to update based on contract implementation | ||
type CommitPublicRandomnessResponse struct { | ||
Result bool `json:"result"` | ||
} | ||
|
||
type SubmitFinalitySignatureMsg struct { | ||
SubmitFinalitySignature SubmitFinalitySignatureMsgParams `json:"submit_finality_signature"` | ||
} | ||
|
||
type SubmitFinalitySignatureMsgParams struct { | ||
FpPubkeyHex string `json:"fp_pubkey_hex"` | ||
Height uint64 `json:"height"` | ||
PubRand []byte `json:"pub_rand"` | ||
Proof Proof `json:"proof"` | ||
BlockHash []byte `json:"block_hash"` | ||
Signature []byte `json:"signature"` | ||
} | ||
|
||
// TODO: need to update based on contract implementation | ||
type SubmitFinalitySignatureResponse struct { | ||
Result bool `json:"result"` | ||
} | ||
|
||
type QueryMsg struct { | ||
Config *Config `json:"config,omitempty"` | ||
FirstPubRandCommit *PubRandCommit `json:"first_pub_rand_commit,omitempty"` | ||
LastPubRandCommit *PubRandCommit `json:"last_pub_rand_commit,omitempty"` | ||
} | ||
|
||
type Config struct{} | ||
|
||
type PubRandCommit struct { | ||
BtcPkHex string `json:"btc_pk_hex"` | ||
} | ||
|
||
type ConfigResponse struct { | ||
ConsumerId string `json:"consumer_id"` | ||
ActivatedHeight uint64 `json:"activated_height"` | ||
} | ||
|
||
// FIXME: Remove this ancillary struct. | ||
// Only required because the e2e tests are using a zero index, which is removed by the `json:"omitempty"` annotation in | ||
// the original cmtcrypto Proof | ||
type Proof struct { | ||
Total uint64 `json:"total"` | ||
Index uint64 `json:"index"` | ||
LeafHash []byte `json:"leaf_hash"` | ||
Aunts [][]byte `json:"aunts"` | ||
} |
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,98 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
wasmdparams "github.com/CosmWasm/wasmd/app/params" | ||
"github.com/Manta-Network/manta-fp/cosmwasmclient/config" | ||
"github.com/Manta-Network/manta-fp/cosmwasmclient/query" | ||
rpchttp "github.com/cometbft/cometbft/rpc/client/http" | ||
"github.com/cosmos/relayer/v2/relayer/chains/cosmos" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Client struct { | ||
mu sync.Mutex | ||
*query.QueryClient | ||
|
||
provider *cosmos.CosmosProvider | ||
timeout time.Duration | ||
logger *zap.Logger | ||
cfg *config.CosmwasmConfig | ||
} | ||
|
||
func New(cfg *config.CosmwasmConfig, chainName string, encodingCfg wasmdparams.EncodingConfig, logger *zap.Logger) (*Client, error) { | ||
var ( | ||
zapLogger *zap.Logger | ||
err error | ||
) | ||
|
||
// ensure cfg is valid | ||
if err := cfg.Validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
// use the existing logger or create a new one if not given | ||
zapLogger = logger | ||
if zapLogger == nil { | ||
zapLogger, err = newRootLogger("console", true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
provider, err := cfg.ToCosmosProviderConfig().NewProvider( | ||
zapLogger, | ||
"", // TODO: set home path | ||
true, | ||
chainName, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cp := provider.(*cosmos.CosmosProvider) | ||
cp.PCfg.KeyDirectory = cfg.KeyDirectory | ||
cp.Cdc = cosmos.Codec{ | ||
InterfaceRegistry: encodingCfg.InterfaceRegistry, | ||
Marshaler: encodingCfg.Codec, | ||
TxConfig: encodingCfg.TxConfig, | ||
Amino: encodingCfg.Amino, | ||
} | ||
|
||
// initialise Cosmos provider | ||
// NOTE: this will create a RPC client. The RPC client will be used for | ||
// submitting txs and making ad hoc queries. It won't create WebSocket | ||
// connection with wasmd node | ||
err = cp.Init(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// create a queryClient so that the Client inherits all query functions | ||
// TODO: merge this RPC client with the one in `cp` after Cosmos side | ||
// finishes the migration to new RPC client | ||
// see https://github.com/strangelove-ventures/cometbft-client | ||
c, err := rpchttp.NewWithTimeout(cp.PCfg.RPCAddr, "/websocket", uint(cfg.Timeout.Seconds())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
queryClient, err := query.NewWithClient(c, cfg.Timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
QueryClient: queryClient, | ||
provider: cp, | ||
timeout: cfg.Timeout, | ||
logger: zapLogger, | ||
cfg: cfg, | ||
}, nil | ||
} | ||
|
||
func (c *Client) GetConfig() *config.CosmwasmConfig { | ||
return c.cfg | ||
} |
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,80 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
wasmdtypes "github.com/CosmWasm/wasmd/x/wasm/types" | ||
sdkquery "github.com/cosmos/cosmos-sdk/types/query" | ||
) | ||
|
||
func (cwClient *Client) StoreWasmCode(wasmFile string) error { | ||
wasmCode, err := os.ReadFile(wasmFile) | ||
if err != nil { | ||
return err | ||
} | ||
if strings.HasSuffix(wasmFile, "wasm") { // compress for gas limit | ||
var buf bytes.Buffer | ||
gz := gzip.NewWriter(&buf) | ||
_, err = gz.Write(wasmCode) | ||
if err != nil { | ||
return err | ||
} | ||
err = gz.Close() | ||
if err != nil { | ||
return err | ||
} | ||
wasmCode = buf.Bytes() | ||
} | ||
|
||
storeMsg := &wasmdtypes.MsgStoreCode{ | ||
Sender: cwClient.MustGetAddr(), | ||
WASMByteCode: wasmCode, | ||
} | ||
_, err = cwClient.ReliablySendMsg(context.Background(), storeMsg, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cwClient *Client) InstantiateContract(codeID uint64, initMsg []byte) error { | ||
instantiateMsg := &wasmdtypes.MsgInstantiateContract{ | ||
Sender: cwClient.MustGetAddr(), | ||
Admin: cwClient.MustGetAddr(), | ||
CodeID: codeID, | ||
Label: "cw", | ||
Msg: initMsg, | ||
Funds: nil, | ||
} | ||
|
||
_, err := cwClient.ReliablySendMsg(context.Background(), instantiateMsg, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// returns the latest wasm code id. | ||
func (cwClient *Client) GetLatestCodeId() (uint64, error) { | ||
pagination := &sdkquery.PageRequest{ | ||
Limit: 1, | ||
Reverse: true, | ||
} | ||
resp, err := cwClient.ListCodes(pagination) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if len(resp.CodeInfos) == 0 { | ||
return 0, fmt.Errorf("no codes found") | ||
} | ||
|
||
return resp.CodeInfos[0].CodeID, nil | ||
} |
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,49 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/juju/fslock" | ||
) | ||
|
||
func (c *Client) GetAddr() (string, error) { | ||
return c.provider.Address() | ||
} | ||
|
||
func (c *Client) MustGetAddr() string { | ||
addr, err := c.provider.Address() | ||
if err != nil { | ||
panic(fmt.Errorf("failed to get signer: %v", err)) | ||
} | ||
return addr | ||
} | ||
|
||
func (c *Client) GetKeyring() keyring.Keyring { | ||
return c.provider.Keybase | ||
} | ||
|
||
// accessKeyWithLock triggers a function that access key ring while acquiring | ||
// the file system lock, in order to remain thread-safe when multiple concurrent | ||
// relayers are running on the same machine and accessing the same keyring | ||
// adapted from | ||
// https://github.com/babylonchain/babylon-relayer/blob/f962d0940832a8f84f747c5d9cbc67bc1b156386/bbnrelayer/utils.go#L212 | ||
func (c *Client) accessKeyWithLock(accessFunc func()) error { | ||
// use lock file to guard concurrent access to the keyring | ||
lockFilePath := path.Join(c.provider.PCfg.KeyDirectory, "keys.lock") | ||
lock := fslock.New(lockFilePath) | ||
if err := lock.Lock(); err != nil { | ||
return fmt.Errorf("failed to acquire file system lock (%s): %w", lockFilePath, err) | ||
} | ||
|
||
// trigger function that access keyring | ||
accessFunc() | ||
|
||
// unlock and release access | ||
if err := lock.Unlock(); err != nil { | ||
return fmt.Errorf("error unlocking file system lock (%s), please manually delete", lockFilePath) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.