Skip to content

Commit

Permalink
Merge pull request #2 from MinterTeam/v0.1.0-dev
Browse files Browse the repository at this point in the history
V0.1.0
  • Loading branch information
danil-lashin authored Dec 9, 2021
2 parents 4051463 + 7e9bf0f commit 49fdd5c
Show file tree
Hide file tree
Showing 23 changed files with 717 additions and 173 deletions.
64 changes: 63 additions & 1 deletion auto-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
secp256k12 "github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/status-im/keycard-go/hexutils"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
Expand Down Expand Up @@ -193,7 +194,7 @@ func main() {
}

time.Sleep(time.Second * 10)
go runOrPanic("mhub-oracle --config=oracle-config.toml --cosmos-mnemonic=%s", cosmosMnemonic)
go runOrPanic("mhub-oracle --testnet --config=oracle-config.toml --cosmos-mnemonic=%s", cosmosMnemonic)
go runOrPanic("mhub-minter-connector --config=connector-config.toml --cosmos-mnemonic=%s --minter-private-key=%s --minter-multisig-addr=%s", cosmosMnemonic, ethPrivateKeyString, minterMultisig)
go runOrPanic("orchestrator --chain-id=ethereum --eth-fee-calculator-url=http://localhost:8840 --cosmos-phrase=%s --ethereum-key=%s --cosmos-grpc=%s --ethereum-rpc=%s --contract-address=%s --fees=%s --address-prefix=hub --metrics-listen=127.0.0.1:3000", cosmosMnemonic, ethPrivateKeyString, "http://localhost:9090", "http://localhost:8545", ethContract, denom)
go runOrPanic("orchestrator --chain-id=bsc --eth-fee-calculator-url=http://localhost:8840 --cosmos-phrase=%s --ethereum-key=%s --cosmos-grpc=%s --ethereum-rpc=%s --contract-address=%s --fees=%s --address-prefix=hub --metrics-listen=127.0.0.1:3001", cosmosMnemonic, ethPrivateKeyString, "http://localhost:9090", "http://localhost:8546", bscContract, denom)
Expand Down Expand Up @@ -241,6 +242,7 @@ func main() {
testMinterToEthereumTransfer(ctx)
testMinterToBscTransfer(ctx)
testColdStorageTransfer(ctx)
testVoteForTokenInfosUpdate(ctx)
testEthEthereumToMinterTransfer(ctx)
testEthMinterToEthereumTransfer(ctx)

Expand Down Expand Up @@ -283,7 +285,67 @@ func main() {

println("All tests are done")
}
func testVoteForTokenInfosUpdate(ctx *Context) {
ctx.TestsWg.Add(1)
addr, priv := cosmos.GetAccount(ctx.CosmosMnemonic)

initialDeposit := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(10000000)))

client := mhubtypes.NewQueryClient(ctx.CosmosConn)

response, err := client.TokenInfos(context.TODO(), &mhubtypes.TokenInfosRequest{})
if err != nil {
panic(err)
}

newInfos := &response.List

newInfos.TokenInfos = append(newInfos.TokenInfos, &mhubtypes.TokenInfo{
Id: 999,
Denom: "TEST",
ChainId: "minter",
ExternalTokenId: "123",
ExternalDecimals: 18,
Commission: bridgeCommission,
})

proposal := &govtypes.MsgSubmitProposal{}
proposal.SetInitialDeposit(initialDeposit)
proposal.SetProposer(addr)
if err := proposal.SetContent(&mhubtypes.TokenInfosChangeProposal{
NewInfos: newInfos,
}); err != nil {
panic(err)
}

cosmos.SendCosmosTx([]sdk.Msg{
proposal,
govtypes.NewMsgVote(addr, 4, govtypes.OptionYes),
}, addr, priv, ctx.CosmosConn, log.NewTMLogger(os.Stdout), true)

go func() {
startTime := time.Now()
for {
if time.Now().Sub(startTime).Seconds() > testTimeout.Seconds() {
panic("Timeout waiting for the token infos to update")
}

response, err := client.TokenInfos(context.TODO(), &mhubtypes.TokenInfosRequest{})
if err != nil {
panic(err)
}

if !assert.ObjectsAreEqual(&response.List, newInfos) {
time.Sleep(time.Second)
continue
}

println("SUCCESS: vote for new token list")
ctx.TestsWg.Done()
break
}
}()
}
func testColdStorageTransfer(ctx *Context) {
ctx.TestsWg.Add(1)
addr, priv := cosmos.GetAccount(ctx.CosmosMnemonic)
Expand Down
11 changes: 8 additions & 3 deletions module/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ var (
distrclient.ProposalHandler,
upgradeclient.ProposalHandler,
upgradeclient.CancelProposalHandler,
mhub2client.ProposalHandler,
mhub2client.ProposalColdStorageHandler,
mhub2client.ProposalTokensChangeHandler,
),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
Expand Down Expand Up @@ -400,7 +401,7 @@ func NewMhub2App(
app.bankKeeper,
app.slashingKeeper,
app.oracleKeeper,
sdk.NewIntFromUint64(1e18),
sdk.DefaultPowerReduction,
)

app.stakingKeeper = *stakingKeeper.SetHooks(
Expand All @@ -419,7 +420,7 @@ func NewMhub2App(
AddRoute(paramsproposal.RouterKey, params.NewParamChangeProposalHandler(app.paramsKeeper)).
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.upgradeKeeper)).
AddRoute(mhub2types.RouterKey, mhub2.NewColdStorageTransferProposalHandler(app.mhub2Keeper)).
AddRoute(mhub2types.RouterKey, mhub2.NewProposalsHandler(app.mhub2Keeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.ibcKeeper.ClientKeeper))

app.govKeeper = govkeeper.NewKeeper(
Expand Down Expand Up @@ -597,6 +598,10 @@ func NewMhub2App(
app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedTransferKeeper = scopedTransferKeeper

app.upgradeKeeper.SetUpgradeHandler("v0.1.0", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return nil, nil
})

return app
}

Expand Down
7 changes: 7 additions & 0 deletions module/proto/mhub2/v1/mhub2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ message ColdStorageTransferProposal {
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
}

message TokenInfosChangeProposal {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;

TokenInfos new_infos = 1;
}
56 changes: 56 additions & 0 deletions module/x/mhub2/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,59 @@ Where proposal.json contains:
},
}
}

func NewSubmitTokenInfosChangeProposalTxCmd() *cobra.Command {
return &cobra.Command{
Use: "token-infos-change [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a token infos change proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a token infos change proposal along with an initial deposit.
The proposal details must be supplied via a JSON file. For values that contains
objects, only non-empty fields will be updated.
Example:
$ %s tx gov submit-proposal token-infos-change <path/to/proposal.json> --from=<key_or_address>
Where proposal.json contains:
{
"new_token_infos": ...,
"deposit": "1000hub"
}
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := utils.ParseTokenInfosChangeProposalJSON(clientCtx.LegacyAmino, args[0])
if err != nil {
return err
}

from := clientCtx.GetFromAddress()
content := types.NewTokenInfosChangeProposal(
proposal.NewTokenInfos,
)

deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return err
}

msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}
if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
5 changes: 3 additions & 2 deletions module/x/mhub2/client/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import (
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
)

// ProposalHandler is the param change proposal handler.
var ProposalHandler = govclient.NewProposalHandler(cli.NewSubmitColdStorageTransferProposalTxCmd, rest.ProposalRESTHandler)
// ProposalColdStorageHandler is the param change proposal handler.
var ProposalColdStorageHandler = govclient.NewProposalHandler(cli.NewSubmitColdStorageTransferProposalTxCmd, rest.ColdStorageTransferProposalRESTHandler)
var ProposalTokensChangeHandler = govclient.NewProposalHandler(cli.NewSubmitTokenInfosChangeProposalTxCmd, rest.TokenInfosChangeProposalRESTHandler)
43 changes: 39 additions & 4 deletions module/x/mhub2/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,51 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

// ProposalRESTHandler returns a ProposalRESTHandler that exposes the param
// ColdStorageTransferProposalRESTHandler returns a ProposalRESTHandler that exposes the param
// change REST handler with a given sub-route.
func ProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
func ColdStorageTransferProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "cold_storage_transfer",
Handler: postProposalHandlerFn(clientCtx),
Handler: postProposalColdStorageTransferHandlerFn(clientCtx),
}
}

func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
func postProposalColdStorageTransferHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req utils.ColdStorageTransferProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

content := types.NewColdStorageTransferProposal(types.ChainID(req.ChainId), req.Amount)

msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, req.Proposer)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}

tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}

// TokenInfosChangeProposalRESTHandler returns a ProposalRESTHandler that exposes the param
// change REST handler with a given sub-route.
func TokenInfosChangeProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "token_infos_change",
Handler: postProposalTokenInfosChangeHandlerFn(clientCtx),
}
}

func postProposalTokenInfosChangeHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req utils.ColdStorageTransferProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
Expand Down
74 changes: 74 additions & 0 deletions module/x/mhub2/client/utils/token_infos_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package utils

import (
"io/ioutil"

"github.com/MinterTeam/mhub2/module/x/mhub2/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)

type (
// TokenInfosChangesJSON defines a slice of TokenInfosChangeJSON objects which can be
// converted to a slice of TokenInfosChange objects.
TokenInfosChangesJSON []TokenInfosChangeJSON

// TokenInfosChangeJSON defines a parameter change used in JSON input. This
// allows values to be specified in raw JSON instead of being string encoded.
TokenInfosChangeJSON struct {
NewTokenInfos *types.TokenInfos `json:"new_token_infos" yaml:"new_token_infos"`
}

// TokenInfosChangeProposalJSON defines a ParameterChangeProposal with a deposit used
// to parse parameter change proposals from a JSON file.
TokenInfosChangeProposalJSON struct {
NewTokenInfos *types.TokenInfos `json:"new_token_infos" yaml:"new_token_infos"`
Deposit string `json:"deposit" yaml:"deposit"`
}

// TokenInfosChangeProposalReq defines a parameter change proposal request body.
TokenInfosChangeProposalReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`

NewTokenInfos *types.TokenInfos `json:"new_token_infos" yaml:"new_token_infos"`
Proposer sdk.AccAddress `json:"proposer" yaml:"proposer"`
Deposit sdk.Coins `json:"deposit" yaml:"deposit"`
}
)

func NewTokenInfosChangeJSON(tokenInfos *types.TokenInfos) TokenInfosChangeJSON {
return TokenInfosChangeJSON{tokenInfos}
}

// ToTokenInfosChange converts a TokenInfosChangeJSON object to TokenInfosChange.
func (pcj TokenInfosChangeJSON) ToTokenInfosChange() types.TokenInfosChangeProposal {
return *types.NewTokenInfosChangeProposal(pcj.NewTokenInfos)
}

// ToTokenInfosChanges converts a slice of TokenInfosChangeJSON objects to a slice of
// TokenInfosChange.
func (pcj TokenInfosChangesJSON) ToTokenInfosChanges() []types.TokenInfosChangeProposal {
res := make([]types.TokenInfosChangeProposal, len(pcj))
for i, pc := range pcj {
res[i] = pc.ToTokenInfosChange()
}
return res
}

// ParseTokenInfosChangeProposalJSON reads and parses a TokenInfosChangeProposalJSON from
// file.
func ParseTokenInfosChangeProposalJSON(cdc *codec.LegacyAmino, proposalFile string) (TokenInfosChangeProposalJSON, error) {
proposal := TokenInfosChangeProposalJSON{}

contents, err := ioutil.ReadFile(proposalFile)
if err != nil {
return proposal, err
}

if err := cdc.UnmarshalJSON(contents, &proposal); err != nil {
return proposal, err
}

return proposal, nil
}
5 changes: 4 additions & 1 deletion module/x/mhub2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
}
}

func NewColdStorageTransferProposalHandler(k keeper.Keeper) govtypes.Handler {
func NewProposalsHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.ColdStorageTransferProposal:
return k.ColdStorageTransfer(ctx, c)
case *types.TokenInfosChangeProposal:
k.SetTokenInfos(ctx, c.NewInfos)
return nil

default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized proposal content type: %T", c)
Expand Down
2 changes: 1 addition & 1 deletion module/x/mhub2/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (k Keeper) batchTxExecuted(ctx sdk.Context, chainId types.ChainID, external

// pay val's commissions
if totalValCommission.IsPositive() {
valset := k.CurrentSignerSet(ctx, chainId)
valset := k.CurrentSignerSet(ctx, "minter")
var totalPower uint64
for _, val := range valset {
totalPower += val.Power
Expand Down
Loading

0 comments on commit 49fdd5c

Please sign in to comment.