Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dongrie committed Oct 24, 2023
1 parent 4e913ba commit 4b9ff96
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 16 deletions.
5 changes: 4 additions & 1 deletion core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) erro
continue
}

chanSteps.Send(src, dst)
srcMsgIDs, dstMsgIDs := chanSteps.Send(src, dst)
if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, "channel"); err != nil {
return err
}

switch {
// In the case of success and this being the last transaction
Expand Down
6 changes: 5 additions & 1 deletion core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ func CreateClients(src, dst *ProvableChain) error {
// Send msgs to both chains
if clients.Ready() {
// TODO: Add retry here for out of gas or other errors
if clients.Send(src, dst); clients.Success() {
srcMsgIDs, dstMsgIDs := clients.Send(src, dst)
if clients.Success() {
logger.Info(
"★ Clients created",
)
}
if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, "client"); err != nil {
return err
}
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func CreateConnection(src, dst *ProvableChain, to time.Duration) error {
continue
}

connSteps.Send(src, dst)
srcMsgIDs, dstMsgIDs := connSteps.Send(src, dst)
if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, "connection"); err != nil {
return err
}

switch {
// In the case of success and this being the last transaction
Expand Down
9 changes: 9 additions & 0 deletions core/ics24.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import (

// Vclient validates the client identifier in the path
func (pe *PathEnd) Vclient() error {
if pe.ClientID == "" {
return nil
}
return host.ClientIdentifierValidator(pe.ClientID)
}

// Vconn validates the connection identifier in the path
func (pe *PathEnd) Vconn() error {
if pe.ConnectionID == "" {
return nil
}
return host.ConnectionIdentifierValidator(pe.ConnectionID)
}

// Vchan validates the channel identifier in the path
func (pe *PathEnd) Vchan() error {
if pe.ChannelID == "" {
return nil
}
return host.ChannelIdentifierValidator(pe.ChannelID)
}

Expand Down
119 changes: 119 additions & 0 deletions core/pathConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package core

import (
"encoding/json"
"fmt"
"os"

retry "github.com/avast/retry-go"
)

type Config struct {
Global GlobalConfig `yaml:"global" json:"global"`
Chains []ChainProverConfig `yaml:"chains" json:"chains"`
Paths Paths `yaml:"paths" json:"paths"`
}

type GlobalConfig struct {
Timeout string `yaml:"timeout" json:"timeout"`
LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"`
LoggerConfig LoggerConfig `yaml:"logger" json:"logger"`
}

type LoggerConfig struct {
Level string `yaml:"level" json:"level"`
Format string `yaml:"format" json:"format"`
Output string `yaml:"output" json:"output"`
}

func SyncChainConfigFromEvents(msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, key string) error {
if err := ProcessPathMsgIDs(msgIDsSrc, src, key); err != nil {
return err
}
if err := ProcessPathMsgIDs(msgIDsDst, dst, key); err != nil {
return err
}
return nil
}

func ProcessPathMsgIDs(msgIDs []MsgID, chain *ProvableChain, key string) error {
for _, msgID := range msgIDs {
msgRes, err := chain.Chain.GetMsgResult(msgID)
if err != nil {
return retry.Unrecoverable(fmt.Errorf("failed to get message result: %v", err))
} else if ok, failureReason := msgRes.Status(); !ok {
return retry.Unrecoverable(fmt.Errorf("msg(id=%v) execution failed: %v", msgID, failureReason))
}
for _, event := range msgRes.Events() {
switch key {
case "client":
if clientIdentifier, ok := event.(*EventGenerateClientIdentifier); ok {
id := clientIdentifier.ID
if err := UpdateConfigID(chain, id, key); err != nil {
return err
}
chain.Chain.Path().ClientID = id
}
case "connection":
if connectionIdentifier, ok := event.(*EventGenerateConnectionIdentifier); ok {
id := connectionIdentifier.ID
if err := UpdateConfigID(chain, id, key); err != nil {
return err
}
chain.Chain.Path().ConnectionID = id
}
case "channel":
if channelIdentifier, ok := event.(*EventGenerateChannelIdentifier); ok {
id := channelIdentifier.ID
if err := UpdateConfigID(chain, id, key); err != nil {
return err
}
chain.Chain.Path().ChannelID = id
}
}
}
}
return nil
}

func UpdateConfigID(chain *ProvableChain, ID, key string) error {
configFile := "/Users/dongri.jin/.yui-relayer/config/config.yaml"
data, err := os.ReadFile(configFile)
if err != nil {
return err
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return err
}
configPath, err := config.Paths.Get("ibc01")
if err != nil {
return err
}
var pathEnd *PathEnd
if chain.Path().ChainID == configPath.Src.ChainID {
pathEnd = configPath.Src
}
if chain.Path().ChainID == configPath.Dst.ChainID {
pathEnd = configPath.Dst
}
if pathEnd == nil {
return fmt.Errorf("pathEnd is nil")
}
switch key {
case "client":
pathEnd.ClientID = ID
case "connection":
pathEnd.ConnectionID = ID
case "channel":
pathEnd.ChannelID = ID
}
configData, err := json.Marshal(config)
if err != nil {
return err
}
if err := os.WriteFile(configFile, configData, 0666); err != nil {
return err
}
return nil
}
30 changes: 23 additions & 7 deletions core/relayMsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *RelayMsgs) IsMaxTx(msgLen, txSize uint64) bool {

// Send sends the messages with appropriate output
// TODO: Parallelize? Maybe?
func (r *RelayMsgs) Send(src, dst Chain) {
func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) {
logger := GetChannelPairLogger(src, dst)
//nolint:prealloc // can not be pre allocated
var (
Expand All @@ -57,6 +57,8 @@ func (r *RelayMsgs) Send(src, dst Chain) {

r.Succeeded = true

srcMsgIDs := []MsgID{}
dstMsgIDs := []MsgID{}
// submit batches of relay transactions
for _, msg := range r.Src {
bz, err := proto.Marshal(msg)
Expand All @@ -70,7 +72,9 @@ func (r *RelayMsgs) Send(src, dst Chain) {

if r.IsMaxTx(msgLen, txSize) {
// Submit the transactions to src chain and update its status
r.Succeeded = r.Succeeded && SendCheckMsgs(src, msgs)
msgIDs, err := SendMsgs(src, msgs)
r.Succeeded = r.Succeeded && err == nil
srcMsgIDs = append(srcMsgIDs, msgIDs...)

// clear the current batch and reset variables
msgLen, txSize = 1, uint64(len(bz))
Expand All @@ -80,8 +84,12 @@ func (r *RelayMsgs) Send(src, dst Chain) {
}

// submit leftover msgs
if len(msgs) > 0 && !SendCheckMsgs(src, msgs) {
r.Succeeded = false
if len(msgs) > 0 {
msgIDs, err := SendMsgs(src, msgs)
if err != nil {
r.Succeeded = false
}
srcMsgIDs = append(srcMsgIDs, msgIDs...)
}

// reset variables
Expand All @@ -100,7 +108,9 @@ func (r *RelayMsgs) Send(src, dst Chain) {

if r.IsMaxTx(msgLen, txSize) {
// Submit the transaction to dst chain and update its status
r.Succeeded = r.Succeeded && SendCheckMsgs(dst, msgs)
msgIDs, err := SendMsgs(dst, msgs)
r.Succeeded = r.Succeeded && err == nil
dstMsgIDs = append(dstMsgIDs, msgIDs...)

// clear the current batch and reset variables
msgLen, txSize = 1, uint64(len(bz))
Expand All @@ -110,7 +120,13 @@ func (r *RelayMsgs) Send(src, dst Chain) {
}

// submit leftover msgs
if len(msgs) > 0 && !SendCheckMsgs(dst, msgs) {
r.Succeeded = false
if len(msgs) > 0 {
msgIDs, err := SendMsgs(dst, msgs)
if err != nil {
r.Succeeded = false
}
dstMsgIDs = append(dstMsgIDs, msgIDs...)
}

return srcMsgIDs, dstMsgIDs
}
9 changes: 9 additions & 0 deletions core/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func SendCheckMsgs(chain Chain, msgs []types.Msg) bool {
return true
}

func SendMsgs(chain Chain, msgs []types.Msg) ([]MsgID, error) {
msgIDs, err := chain.SendMsgs(msgs)
if err != nil {
GetChainLogger(chain).Error("failed to send msgs", err, "msgs", msgs)
return nil, err
}
return msgIDs, nil
}

// GetFinalizedMsgResult is an utility function that waits for the finalization of the message execution and then returns the result.
func GetFinalizedMsgResult(chain ProvableChain, msgID MsgID) (MsgResult, error) {
var msgRes MsgResult
Expand Down
12 changes: 6 additions & 6 deletions tests/cases/tmmock2tmmock/configs/path.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"src": {
"chain-id": "ibc0",
"client-id": "mock-client-0",
"connection-id": "connection-0",
"channel-id": "channel-0",
"client-id": "",
"connection-id": "",
"channel-id": "",
"port-id": "transfer",
"order": "unordered",
"version": "ics20-1"
},
"dst": {
"chain-id": "ibc1",
"client-id": "mock-client-0",
"connection-id": "connection-0",
"channel-id": "channel-0",
"client-id": "",
"connection-id": "",
"channel-id": "",
"port-id": "transfer",
"order": "unordered",
"version": "ics20-1"
Expand Down

0 comments on commit 4b9ff96

Please sign in to comment.