Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arweave message #6 #21

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import (
sequencermodule "github.com/warp-contracts/sequencer/x/sequencer"
sequencermodulekeeper "github.com/warp-contracts/sequencer/x/sequencer/keeper"
sequencermoduletypes "github.com/warp-contracts/sequencer/x/sequencer/types"
arweaveapi "github.com/warp-contracts/sequencer/x/sequencer/api"
// this line is used by starport scaffolding # stargate/app/moduleImport

appparams "github.com/warp-contracts/sequencer/app/params"
Expand Down Expand Up @@ -841,6 +842,9 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig

// register app's OpenAPI routes.
docs.RegisterOpenAPIService(Name, apiSvr.Router)

// Register the route for sending Arweave transactions as JSON
arweaveapi.RegisterArweaveAPIRoute(clientCtx, apiSvr.Router)
}

// RegisterTxService implements the Application.RegisterTxService method.
Expand Down
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ faucet:
validators:
- name: alice
bonded: 100000000warp
genesis:
app_state:
staking:
params:
bond_denom: "warp"
9 changes: 9 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71434,6 +71434,8 @@ definitions:
description: |-
Version defines the versioning scheme used to negotiate the IBC verison in
the connection handshake.
warpcontracts.sequencer.sequencer.MsgArweaveResponse:
type: object
warpcontracts.sequencer.sequencer.Params:
type: object
description: Params defines the parameters for the module.
Expand All @@ -71444,3 +71446,10 @@ definitions:
description: params holds all the parameters of this module.
type: object
description: QueryParamsResponse is response type for the Query/Params RPC method.
warpcontracts.sequencer.sequencer.Tag:
type: object
properties:
name:
type: string
value:
type: string
28 changes: 27 additions & 1 deletion proto/sequencer/sequencer/tx.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
syntax = "proto3";

package warpcontracts.sequencer.sequencer;

option go_package = "github.com/warp-contracts/sequencer/x/sequencer/types";

// Msg defines the Msg service.
service Msg {}
service Msg {
rpc Arweave (MsgArweave) returns (MsgArweaveResponse);
}
message Tag {
string name = 1;
string value = 2;
}

message MsgArweave {
string creator = 1;
int32 format = 2;
string id = 3;
string lastTx = 4;
string owner = 5;
repeated Tag tags = 6;
string target = 7;
string quantity = 8;
string dataRoot = 9;
string dataSize = 10;
string data = 11;
string reward = 12;
string signature = 13;
}

message MsgArweaveResponse {}

62 changes: 62 additions & 0 deletions x/sequencer/api/arweave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api

import (
"encoding/json"
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/warp-contracts/sequencer/x/sequencer/types"
)

type arweaveHandler struct {
ctx client.Context
}

// The endpoint that accepts the Arweave transaction in the form of JSON,
// wraps it with a Cosmos transaction and broadcasts it to the network.
func RegisterArweaveAPIRoute(clientCtx client.Context, router *mux.Router) {
router.Handle("/arweave", arweaveHandler{ctx: clientCtx}).Methods("POST")
szynwelski marked this conversation as resolved.
Show resolved Hide resolved
}

func (h arweaveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
szynwelski marked this conversation as resolved.
Show resolved Hide resolved
// parse JSON
var msg types.MsgArweave
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&msg)
if err != nil {
badRequest(w, err.Error())
return
}

// wrap Arweave message with Cosmos transaction
txBytes, err := createTxWithArweaveMsg(h.ctx, msg)
if err != nil {
badRequest(w, err.Error())
return
}

// broadcast transaction
response, err := h.ctx.BroadcastTxSync(txBytes)
if err != nil {
badRequest(w, err.Error())
szynwelski marked this conversation as resolved.
Show resolved Hide resolved
return
}
if response.Code != 0 {
badRequest(w, response.RawLog)
return
}

w.Write([]byte(response.TxHash))
}

func badRequest(w http.ResponseWriter, errorMessage string) {
http.Error(w, errorMessage, http.StatusBadRequest)
}

func createTxWithArweaveMsg(ctx client.Context, msg types.MsgArweave) ([]byte, error) {
txBuilder := ctx.TxConfig.NewTxBuilder()
txBuilder.SetMsgs(&msg)
return ctx.TxConfig.TxEncoder()(txBuilder.GetTx())
}
1 change: 1 addition & 0 deletions x/sequencer/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

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

return cmd
Expand Down
67 changes: 67 additions & 0 deletions x/sequencer/client/cli/tx_arweave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
"github.com/warp-contracts/sequencer/x/sequencer/types"
)

var _ = strconv.Itoa(0)

func CmdArweave() *cobra.Command {
cmd := &cobra.Command{
Use: "arweave [format] [id] [last-tx] [owner] [tags] [target] [quantity] [data-root] [data-size] [data] [reward] [signature]",
Short: "Broadcast message arweave",
Args: cobra.ExactArgs(12),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argFormat := args[0]
argId := args[1]
argLastTx := args[2]
argOwner := args[3]
argTags := args[4]
argTarget := args[5]
argQuantity := args[6]
argDataRoot := args[7]
argDataSize := args[8]
argData := args[9]
argReward := args[10]
argSignature := args[11]

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

msg, err := types.NewMsgArweave(
clientCtx.GetFromAddress().String(),
argFormat,
argId,
argLastTx,
argOwner,
argTags,
argTarget,
argQuantity,
argDataRoot,
argDataSize,
argData,
argReward,
argSignature,
)
if err != nil {
return err
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/sequencer/keeper/msg_server_arweave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/warp-contracts/sequencer/x/sequencer/types"
)

func (k msgServer) Arweave(goCtx context.Context, msg *types.MsgArweave) (*types.MsgArweaveResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
szynwelski marked this conversation as resolved.
Show resolved Hide resolved
_ = ctx

return &types.MsgArweaveResponse{}, nil
}
17 changes: 16 additions & 1 deletion x/sequencer/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ var (
)

const (
// this line is used by starport scaffolding # simapp/module/const
opWeightMsgArweave = "op_weight_msg_arweave"
// TODO: Determine the simulation weight value
szynwelski marked this conversation as resolved.
Show resolved Hide resolved
defaultWeightMsgArweave int = 100

// this line is used by starport scaffolding # simapp/module/const
)

// GenerateGenesisState creates a randomized GenState of the module
Expand Down Expand Up @@ -58,6 +62,17 @@ func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgArweave int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgArweave, &weightMsgArweave, nil,
func(_ *rand.Rand) {
weightMsgArweave = defaultWeightMsgArweave
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgArweave,
sequencersimulation.SimulateMsgArweave(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand Down
29 changes: 29 additions & 0 deletions x/sequencer/simulation/arweave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/warp-contracts/sequencer/x/sequencer/keeper"
"github.com/warp-contracts/sequencer/x/sequencer/types"
)

func SimulateMsgArweave(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := &types.MsgArweave{
Creator: simAccount.Address.String(),
}

// TODO: Handling the Arweave simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "Arweave simulation not implemented"), nil, nil
}
}
6 changes: 5 additions & 1 deletion x/sequencer/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
// this line is used by starport scaffolding # 1
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgArweave{}, "sequencer/Arweave", nil)
// this line is used by starport scaffolding # 2
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgArweave{},
)
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
Loading