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

introduce authz tx msg command #48

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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.4+ | v0.6.4+ | 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
124 changes: 124 additions & 0 deletions cmd/opinitd/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

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

"cosmossdk.io/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/cosmos/cosmos-sdk/x/authz"

"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"

sdk "github.com/cosmos/cosmos-sdk/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)
}
79 changes: 59 additions & 20 deletions executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ To configure the Executor, fill in the values in the `~/.opinit/executor.json` f
}
```

### Oracle config
If the oracle is enabled, the `oracle_bridge_executor` field must be set. The oracle data is stored in the 0th tx of each L1 block. The bridge executor submits a `MsgUpdateOracle` containing the 0th Tx of l1 block to l2 when a block in l1 is created.

The `oracle_bridge_executor` must be an account that has received the authz grant from the executor. If it is not set, you can set the authz with the command below.
```bash
opinitd tx grant-oracle [oracle-account-address]
```

### Start height config examples

If the latest height stored in the db is not 0, start height config is ignored.
Expand Down Expand Up @@ -329,13 +337,18 @@ curl localhost:3000/status

```json
{
"bridge_id": 1,
"bridge_id": 0,
"host": {
"node": {
"last_block_height": 0,
"broadcaster": {
"pending_txs": 0,
"sequence": 0
"accounts_status": [
{
"address": "",
"sequence": 0
}
]
}
},
"last_proposed_output_index": 0,
Expand All @@ -346,7 +359,16 @@ curl localhost:3000/status
"last_block_height": 0,
"broadcaster": {
"pending_txs": 0,
"sequence": 0
"accounts_status": [
{
"address": "",
"sequence": 0
},
{
"address": "",
"sequence": 0
}
]
}
},
"last_updated_oracle_height": 0,
Expand All @@ -360,7 +382,7 @@ curl localhost:3000/status
},
"batch": {
"node": {
"last_block_height": 0,
"last_block_height": 0
},
"batch_info": {
"submitter": "",
Expand All @@ -374,7 +396,12 @@ curl localhost:3000/status
"da": {
"broadcaster": {
"pending_txs": 0,
"sequence": 0
"accounts_status": [
{
"address": "",
"sequence": 0
}
]
}
}
}
Expand All @@ -389,20 +416,32 @@ initiad tx ophost finalize-token-withdrawal ./withdrawal-info.json --gas= --gas-

```go
type QueryWithdrawalResponse struct {
// fields required to withdraw funds
BridgeId uint64 `json:"bridge_id"`
OutputIndex uint64 `json:"output_index"`
WithdrawalProofs [][]byte `json:"withdrawal_proofs"`
Sender string `json:"sender"`
Sequence uint64 `json:"sequence"`
Amount string `json:"amount"`
Version []byte `json:"version"`
StorageRoot []byte `json:"storage_root"`
LatestBlockHash []byte `json:"latest_block_hash"`

// extra info
BlockNumber int64 `json:"block_number"`
Receiver string `json:"receiver"`
WithdrawalHash []byte `json:"withdrawal_hash"`
Sequence uint64 `json:"sequence"`
To string `json:"to"`
From string `json:"from"`
Amount types.Coin `json:"amount"`
OutputIndex uint64 `json:"output_index"`
BridgeId uint64 `json:"bridge_id"`
WithdrawalProofs [][]byte `json:"withdrawal_proofs"`
Version []byte `json:"version"`
StorageRoot []byte `json:"storage_root"`
LastBlockHash []byte `json:"last_block_hash"`
}
```

```bash
curl localhost:3000/withdrawals/{address}
```
default options
- `limit`: 10
- `offset`: 0
- `order`: desc


```go
type QueryWithdrawalsResponse struct {
Withdrawals []QueryWithdrawalResponse `json:"withdrawals"`
Next uint64 `json:"next"`
Total uint64 `json:"total"`
}
```
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
10 changes: 10 additions & 0 deletions node/broadcaster/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 +106,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 +148,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"
)
Loading