Skip to content

Commit

Permalink
Temporary command to upload smart contracts (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni authored Jun 23, 2022
1 parent 51803cb commit fa9599d
Show file tree
Hide file tree
Showing 20 changed files with 957 additions and 93 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ require (
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/gateway v1.1.0 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.5.8 // indirect
Expand All @@ -72,6 +74,7 @@ require (
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.10.3 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -740,6 +741,8 @@ github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -907,6 +910,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqC
github.com/grpc-ecosystem/grpc-gateway v1.14.7/go.mod h1:oYZKL012gGh6LMyg/xA7Q2yq6j8bu0wa+9w14EEthWU=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.10.3 h1:BGNSrTRW4rwfhJiFwvwF4XQ0Y72Jj9YEgxVrtovbD5o=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.10.3/go.mod h1:VHn7KgNsRriXa4mcgtkpR00OXyQY6g67JWMvn+R27A4=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
Expand Down
2 changes: 1 addition & 1 deletion proto/evm/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ service Query {
}
// Queries a list of GetValsetByID items.
rpc GetValsetByID(QueryGetValsetByIDRequest) returns (QueryGetValsetByIDResponse) {
option (google.api.http).get = "/palomachain/paloma/evm/get_valset_by_id/{valsettID}";
option (google.api.http).get = "/palomachain/paloma/evm/get_valset_by_id/{valsetID}";
}

// this line is used by starport scaffolding # 2
Expand Down
2 changes: 2 additions & 0 deletions proto/evm/turnstone.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ message UpdateValset {

message UploadSmartContract {
bytes bytecode = 1;
bytes abi = 2;
bytes constructorInput = 3;
}


Expand Down
12 changes: 12 additions & 0 deletions proto/evm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ option go_package = "github.com/palomachain/paloma/x/evm/types";
// Msg defines the Msg service.
service Msg {
rpc SubmitNewJob(MsgSubmitNewJob) returns (MsgSubmitNewJobResponse);
rpc UploadNewSmartContractTemp(MsgUploadNewSmartContractTemp) returns (MsgUploadNewSmartContractTempResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}

Expand All @@ -26,4 +27,15 @@ message MsgSubmitNewJob {
message MsgSubmitNewJobResponse {
}

message MsgUploadNewSmartContractTemp {
string creator = 1;
string abi = 2;
string bytecode = 3;
string constructorInput = 4;
string chainID = 5;
}

message MsgUploadNewSmartContractTempResponse {
}

// this line is used by starport scaffolding # proto/tx/message
3 changes: 2 additions & 1 deletion x/evm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdSubmitNewJob())
// this line is used by starport scaffolding # 1
cmd.AddCommand(CmdUploadNewSmartContractTemp())
// this line is used by starport scaffolding # 1

return cmd
}
49 changes: 49 additions & 0 deletions x/evm/client/cli/tx_upload_new_smart_contract_temp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cli

import (
"strconv"

"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/palomachain/paloma/x/evm/types"
)

var _ = strconv.Itoa(0)

func CmdUploadNewSmartContractTemp() *cobra.Command {
cmd := &cobra.Command{
Use: "upload-new-smart-contract-temp [abi] [bytecode] [constructor-input] [chain-id]",
Short: "Broadcast message UploadNewSmartContractTemp",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argAbi := args[0]
argBytecode := args[1]
argConstructorInput := args[2]
argChainID := args[3]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgUploadNewSmartContractTemp(
clientCtx.GetFromAddress().String(),
argAbi,
argBytecode,
argConstructorInput,
argChainID,

)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
5 changes: 4 additions & 1 deletion x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgSubmitNewJob:
res, err := msgServer.SubmitNewJob(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
// this line is used by starport scaffolding # 1
case *types.MsgUploadNewSmartContractTemp:
res, err := msgServer.UploadNewSmartContractTemp(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
// this line is used by starport scaffolding # 1
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
14 changes: 13 additions & 1 deletion x/evm/keeper/grpc_query_get_valset_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/palomachain/paloma/x/evm/types"
valsettypes "github.com/palomachain/paloma/x/valset/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// GetValsetByID returns the valset given chain id and valset id. if the valset
// id is non-pozitive then it returns the latest valset existing.
func (k Keeper) GetValsetByID(goCtx context.Context, req *types.QueryGetValsetByIDRequest) (*types.QueryGetValsetByIDResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

snapshot, err := k.Valset.FindSnapshotByID(ctx, req.GetValsetID())
var snapshot *valsettypes.Snapshot
var err error

if req.GetValsetID() > 0 {
snapshot, err = k.Valset.FindSnapshotByID(ctx, req.GetValsetID())
} else {
snapshot, err = k.Valset.GetCurrentSnapshot(ctx)
}

if err != nil {
return nil, err
}

valset := transformSnapshotToTurnstoneValset(snapshot, req.GetChainID())

return &types.QueryGetValsetByIDResponse{
Expand Down
21 changes: 21 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ func (k Keeper) AddSmartContractExecutionToConsensus(
)
}

func (k Keeper) addUploadSmartContractToConsensus(
ctx sdk.Context,
chainID string,
upload *types.UploadSmartContract,
) error {
return k.ConsensusKeeper.PutMessageForSigning(
ctx,
consensustypes.Queue(
ConsensusTurnstoneMessage,
consensustypes.ChainTypeEVM,
chainID,
),
&types.Message{
ChainID: chainID,
Action: &types.Message_UploadSmartContract{
UploadSmartContract: upload,
},
},
)
}

// {"target_contract_info":{"method":"foo","chain_id":"abc","compass_id":"abc","contract_address":"0xabc","smart_contract_abi":"abc"},"paloma_address":"paloma1sp6yeu2cdemlh0jpterpe3as9mvx36ck6ys0ce","eth_address":[0,0,0,0,0,0,0,0,0,0,0,0,22,248,182,92,183,148,210,0,134,193,229,48,158,88,192,76,57,198,237,233]}
type executeEVMFromCosmWasm struct {
TargetContractInfo struct {
Expand Down
7 changes: 0 additions & 7 deletions x/evm/keeper/msg_server_submit_new_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ import (
func (k msgServer) SubmitNewJob(goCtx context.Context, msg *types.MsgSubmitNewJob) (*types.MsgSubmitNewJobResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: check if ValidateBasic is being called somewhere in the Cosmos SDK
// and if this is redundant call.
// It should be caled on the "server" side as well as the client side of things.
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

err := k.AddSmartContractExecutionToConsensus(
ctx,
msg.GetChainID(),
Expand Down
25 changes: 25 additions & 0 deletions x/evm/keeper/msg_server_upload_new_smart_contract_temp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/palomachain/paloma/x/evm/types"
)

func (k msgServer) UploadNewSmartContractTemp(goCtx context.Context, msg *types.MsgUploadNewSmartContractTemp) (*types.MsgUploadNewSmartContractTempResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := k.addUploadSmartContractToConsensus(ctx, msg.GetChainID(), &types.UploadSmartContract{
Bytecode: common.FromHex(msg.GetBytecode()),
Abi: []byte(msg.GetAbi()),
ConstructorInput: common.FromHex(msg.GetConstructorInput()),
})

if err != nil {
return nil, err
}

return &types.MsgUploadNewSmartContractTempResponse{}, nil
}
8 changes: 6 additions & 2 deletions x/evm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSubmitNewJob{}, "evm/SubmitNewJob", nil)
// this line is used by starport scaffolding # 2
cdc.RegisterConcrete(&MsgUploadNewSmartContractTemp{}, "evm/UploadNewSmartContractTemp", nil)
// this line is used by starport scaffolding # 2
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
Expand All @@ -21,7 +22,10 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&ArbitrarySmartContractCall{},
&Message{},
)
// this line is used by starport scaffolding # 3
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgUploadNewSmartContractTemp{},
)
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
Expand Down
1 change: 1 addition & 0 deletions x/evm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type ConsensusKeeper interface {

type ValsetKeeper interface {
FindSnapshotByID(ctx sdk.Context, id uint64) (*valsettypes.Snapshot, error)
GetCurrentSnapshot(ctx sdk.Context) (*valsettypes.Snapshot, error)
}
50 changes: 50 additions & 0 deletions x/evm/types/message_upload_new_smart_contract_temp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgUploadNewSmartContractTemp = "upload_new_smart_contract_temp"

var _ sdk.Msg = &MsgUploadNewSmartContractTemp{}

func NewMsgUploadNewSmartContractTemp(creator string, abi string, bytecode string, constructorInput string, chainID string) *MsgUploadNewSmartContractTemp {
return &MsgUploadNewSmartContractTemp{
Creator: creator,
Abi: abi,
Bytecode: bytecode,
ConstructorInput: constructorInput,
ChainID: chainID,
}
}

func (msg *MsgUploadNewSmartContractTemp) Route() string {
return RouterKey
}

func (msg *MsgUploadNewSmartContractTemp) Type() string {
return TypeMsgUploadNewSmartContractTemp
}

func (msg *MsgUploadNewSmartContractTemp) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}

func (msg *MsgUploadNewSmartContractTemp) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgUploadNewSmartContractTemp) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
return nil
}

40 changes: 40 additions & 0 deletions x/evm/types/message_upload_new_smart_contract_temp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/palomachain/paloma/testutil/sample"
)

func TestMsgUploadNewSmartContractTemp_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgUploadNewSmartContractTemp
err error
}{
{
name: "invalid address",
msg: MsgUploadNewSmartContractTemp{
Creator: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgUploadNewSmartContractTemp{
Creator: sample.AccAddress(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}
Loading

0 comments on commit fa9599d

Please sign in to comment.