Skip to content

Commit

Permalink
introduce authz tx msg command
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Nov 19, 2024
1 parent 3ec9bfd commit 8ea22b1
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To ensure compatibility with the node version, check the following versions:

| L1 Node | MiniMove | MiniWasm | MiniEVM |
| ------- | -------- | -------- | ------- |
| v0.5.3+ | v0.5.3+ | v0.5.2+ | v0.5.2+ |
| v0.6.1+ | v0.6.3+ | v0.6.3+ | v0.6.6+ |

### Build and Configure

Expand Down
1 change: 1 addition & 0 deletions cmd/opinitd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewRootCmd() *cobra.Command {
resetHeightsCmd(ctx),
resetHeightCmd(ctx),
migration015Cmd(ctx),
txCmd(ctx),
version.NewVersionCommand(),
)
return rootCmd
Expand Down
122 changes: 122 additions & 0 deletions cmd/opinitd/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"context"
"encoding/json"
"fmt"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/initia-labs/opinit-bots/bot"
bottypes "github.com/initia-labs/opinit-bots/bot/types"
executortypes "github.com/initia-labs/opinit-bots/executor/types"
"github.com/initia-labs/opinit-bots/keys"
"github.com/initia-labs/opinit-bots/node/broadcaster"
broadcastertypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
"github.com/initia-labs/opinit-bots/node/rpcclient"
"github.com/initia-labs/opinit-bots/provider/child"
"github.com/initia-labs/opinit-bots/types"
)

// txCmd represents the tx command
func txCmd(ctx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "send a transaction",
}

cmd.AddCommand(
txGrantOracleCmd(ctx),
)
return cmd
}

func txGrantOracleCmd(baseCtx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "grant-oracle [oracle-account-address]",
Args: cobra.ExactArgs(1),
Short: "Grant oracle permission to the given account",
Long: `Grant oracle permission to the given account on L2 chain`,
RunE: func(cmd *cobra.Command, args []string) error {
cmdCtx, botDone := context.WithCancel(cmd.Context())
gracefulShutdown(botDone)

errGrp, ctx := errgroup.WithContext(cmdCtx)
ctx = types.WithErrGrp(ctx, errGrp)

account, err := l2BroadcasterAccount(baseCtx, cmd)
if err != nil {
return err
}
err = account.Load(ctx)
if err != nil {
return err
}

oracleAddress, err := keys.DecodeBech32AccAddr(args[0], account.Bech32Prefix())
if err != nil {
return err
}

grantMsg, err := authz.NewMsgGrant(account.GetAddress(), oracleAddress, authz.NewGenericAuthorization(types.MsgUpdateOracleTypeUrl), nil)
if err != nil {
return err
}

txBytes, _, err := account.BuildTxWithMessages(ctx, []sdk.Msg{grantMsg})
if err != nil {
return errors.Wrapf(err, "simulation failed")
}

res, err := account.BroadcastTxSync(ctx, txBytes)
if err != nil {
// TODO: handle error, may repeat sending tx
return fmt.Errorf("broadcast txs: %w", err)
}
bz, err := json.Marshal(res)
if err != nil {
return err
}
fmt.Println(string(bz))
return nil
},
}

cmd = configFlag(baseCtx.v, cmd)
return cmd
}

func l2BroadcasterAccount(ctx *cmdContext, cmd *cobra.Command) (*broadcaster.BroadcasterAccount, error) {
configPath, err := getConfigPath(cmd, ctx.homePath, string(bottypes.BotTypeExecutor))
if err != nil {
return nil, err
}

cfg := &executortypes.Config{}
err = bot.LoadJsonConfig(configPath, cfg)
if err != nil {
return nil, err
}

l2Config := cfg.L2NodeConfig(ctx.homePath)
broadcasterConfig := l2Config.BroadcasterConfig
cdc, txConfig, err := child.GetCodec(broadcasterConfig.Bech32Prefix)
if err != nil {
return nil, err
}

rpcClient, err := rpcclient.NewRPCClient(cdc, l2Config.RPC)
if err != nil {
return nil, err
}

keyringConfig := broadcastertypes.KeyringConfig{
Name: cfg.BridgeExecutor,
}

return broadcaster.NewBroadcasterAccount(*broadcasterConfig, cdc, txConfig, rpcClient, keyringConfig)
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
cosmossdk.io/core v0.11.1
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
cosmossdk.io/x/tx v0.13.4
cosmossdk.io/x/tx v0.13.5
github.com/celestiaorg/go-square/v2 v2.0.0
github.com/cometbft/cometbft v0.38.12
github.com/cosmos/cosmos-sdk v0.50.9
github.com/cosmos/cosmos-sdk v0.50.10
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.7.0
github.com/gofiber/fiber/v2 v2.52.5
github.com/initia-labs/OPinit v0.5.7
github.com/initia-labs/OPinit v0.6.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down Expand Up @@ -112,7 +112,7 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/initia-labs/OPinit/api v0.5.1 // indirect
github.com/initia-labs/OPinit/api v0.6.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ cosmossdk.io/x/evidence v0.1.1 h1:Ks+BLTa3uftFpElLTDp9L76t2b58htjVbSZ86aoK/E4=
cosmossdk.io/x/evidence v0.1.1/go.mod h1:OoDsWlbtuyqS70LY51aX8FBTvguQqvFrt78qL7UzeNc=
cosmossdk.io/x/feegrant v0.1.1 h1:EKFWOeo/pup0yF0svDisWWKAA9Zags6Zd0P3nRvVvw8=
cosmossdk.io/x/feegrant v0.1.1/go.mod h1:2GjVVxX6G2fta8LWj7pC/ytHjryA6MHAJroBWHFNiEQ=
cosmossdk.io/x/tx v0.13.4 h1:Eg0PbJgeO0gM8p5wx6xa0fKR7hIV6+8lC56UrsvSo0Y=
cosmossdk.io/x/tx v0.13.4/go.mod h1:BkFqrnGGgW50Y6cwTy+JvgAhiffbGEKW6KF9ufcDpvk=
cosmossdk.io/x/tx v0.13.5 h1:FdnU+MdmFWn1pTsbfU0OCf2u6mJ8cqc1H4OMG418MLw=
cosmossdk.io/x/tx v0.13.5/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w=
cosmossdk.io/x/upgrade v0.1.4 h1:/BWJim24QHoXde8Bc64/2BSEB6W4eTydq0X/2f8+g38=
cosmossdk.io/x/upgrade v0.1.4/go.mod h1:9v0Aj+fs97O+Ztw+tG3/tp5JSlrmT7IcFhAebQHmOPo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down Expand Up @@ -940,8 +940,8 @@ github.com/cosmos/cosmos-db v1.0.2 h1:hwMjozuY1OlJs/uh6vddqnk9j7VamLv+0DBlbEXbAK
github.com/cosmos/cosmos-db v1.0.2/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/cosmos-sdk v0.50.9 h1:gt2usjz0H0qW6KwAxWw7ZJ3XU8uDwmhN+hYG3nTLeSg=
github.com/cosmos/cosmos-sdk v0.50.9/go.mod h1:TMH6wpoYBcg7Cp5BEg8fneLr+8XloNQkf2MRNF9V6JE=
github.com/cosmos/cosmos-sdk v0.50.10 h1:zXfeu/z653tWZARr/jESzAEiCUYjgJwwG4ytnYWMoDM=
github.com/cosmos/cosmos-sdk v0.50.10/go.mod h1:6Eesrx3ZE7vxBZWpK++30H+Uc7Q4ahQWCL7JKU/LEdU=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down Expand Up @@ -1327,10 +1327,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.5.7 h1:25G0o2VXcO4EXzh9afdTdsXl1PTdYG9VMUx4efmWCuM=
github.com/initia-labs/OPinit v0.5.7/go.mod h1:lx1amLMszculwPu8ln+btJno38UV28fd2nP7XC88ZeE=
github.com/initia-labs/OPinit/api v0.5.1 h1:zwyJf7HtKJCKvLJ1R9PjVfJO1L+d/jKoeFyT7WTLHFI=
github.com/initia-labs/OPinit/api v0.5.1/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/initia-labs/OPinit v0.6.1 h1:G9ebeYeqPlV9Z2s3JdSWfwQAUgIM+nhkcA8xSJUMR7M=
github.com/initia-labs/OPinit v0.6.1/go.mod h1:gDpCh4Zx94mihwgzP/PLav8eVHLroZBu3dFyzCy8iIs=
github.com/initia-labs/OPinit/api v0.6.0 h1:Q3hDHpTd9EqlDfY/OryCKIwuXYWJxGJdGfJicV1RjL4=
github.com/initia-labs/OPinit/api v0.6.0/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
9 changes: 9 additions & 0 deletions node/broadcaster/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/initia-labs/opinit-bots/node/rpcclient"
"github.com/initia-labs/opinit-bots/txutils"

ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -104,6 +105,10 @@ func (b BroadcasterAccount) GetAddressString() string {
return b.addressString
}

func (b BroadcasterAccount) Bech32Prefix() string {
return b.cfg.Bech32Prefix
}

func (b *BroadcasterAccount) Load(ctx context.Context) error {
account, err := b.GetAccount(b.getClientCtx(ctx), b.address)
if err != nil {
Expand Down Expand Up @@ -142,6 +147,10 @@ func (b *BroadcasterAccount) UpdateSequence(sequence uint64) {
b.txf = b.txf.WithSequence(sequence)
}

func (b BroadcasterAccount) BroadcastTxSync(ctx context.Context, txBytes []byte) (*ctypes.ResultBroadcastTx, error) {
return b.rpcClient.BroadcastTxSync(ctx, txBytes)
}

// BuildSimTx creates an unsigned tx with an empty single signature and returns
// the encoded transaction or an error if the unsigned transaction cannot be built.
func (b BroadcasterAccount) buildSimTx(msgs ...sdk.Msg) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion provider/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (b *BaseChild) Initialize(
GRANTLOOP:
for _, grant := range grants {
if grant.Authorization.TypeUrl != "/cosmos.authz.v1beta1.GenericAuthorization" ||
!bytes.Contains(grant.Authorization.Value, []byte("/opinit.opchild.v1.MsgUpdateOracle")) {
!bytes.Contains(grant.Authorization.Value, []byte(types.MsgUpdateOracleTypeUrl)) {
continue
}

Expand Down
2 changes: 2 additions & 0 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ const (

DAHostName = "da_host"
DACelestiaName = "da_celestia"

MsgUpdateOracleTypeUrl = "/opinit.opchild.v1.MsgUpdateOracle"
)

0 comments on commit 8ea22b1

Please sign in to comment.