Skip to content

Commit

Permalink
Apply optimizeRelay
Browse files Browse the repository at this point in the history
Signed-off-by: Dongri Jin <[email protected]>
  • Loading branch information
dongrie committed Nov 1, 2023
2 parents 093992c + 9e602df commit cc48cb2
Show file tree
Hide file tree
Showing 31 changed files with 1,119 additions and 259 deletions.
17 changes: 13 additions & 4 deletions chains/tendermint/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ func (c *Chain) waitForCommit(txHash string) (*coretypes.ResultTx, error) {
return retry.Unrecoverable(err)
}
}
// In a tendermint chain, when the latest height of the chain is N+1,
// proofs of states updated up to height N are available.
// In order to make the proof of the state updated by a tx available just after `sendMsgs`,
// `waitForCommit` must wait until the latest height is greater than the tx height.
if height, err := c.LatestHeight(); err != nil {
return fmt.Errorf("failed to obtain latest height: %v", err)
} else if height.GetRevisionHeight() <= uint64(resTx.Height) {
return fmt.Errorf("latest_height(%v) is less than or equal to tx_height(%v) yet", height, resTx.Height)
}
return nil
}, retry.Attempts(maxRetry), retry.Delay(retryInterval), rtyErr); err != nil {
return resTx, fmt.Errorf("failed to make sure that tx is committed: %v", err)
Expand Down Expand Up @@ -404,8 +413,8 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) {
var msgIDs []core.MsgID
for msgIndex := range msgs {
msgIDs = append(msgIDs, &MsgID{
txHash: res.TxHash,
msgIndex: uint32(msgIndex),
TxHash: res.TxHash,
MsgIndex: uint32(msgIndex),
})
}
return msgIDs, nil
Expand All @@ -418,7 +427,7 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) {
}

// find tx
resTx, err := c.waitForCommit(msgID.txHash)
resTx, err := c.waitForCommit(msgID.TxHash)
if err != nil {
return nil, fmt.Errorf("failed to query tx: %v", err)
}
Expand All @@ -445,7 +454,7 @@ func (c *Chain) GetMsgResult(id core.MsgID) (core.MsgResult, error) {
}

// parse the ABCI logs into core.MsgEventLog's
events, err := parseMsgEventLogs(abciLogs, msgID.msgIndex)
events, err := parseMsgEventLogs(abciLogs, msgID.MsgIndex)
if err != nil {
return nil, fmt.Errorf("failed to parse msg event log: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions chains/tendermint/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
(*core.ProverConfig)(nil),
&ProverConfig{},
)
registry.RegisterImplementations(
(*core.MsgID)(nil),
&MsgID{},
)
}
39 changes: 39 additions & 0 deletions chains/tendermint/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tendermint_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/hyperledger-labs/yui-relayer/chains/tendermint"
"github.com/hyperledger-labs/yui-relayer/core"
)

func TestCodec(t *testing.T) {
codec := codec.NewProtoCodec(types.NewInterfaceRegistry())
tendermint.RegisterInterfaces(codec.InterfaceRegistry())

orig := tendermint.MsgID{
TxHash: "hoge",
MsgIndex: 123,
}

bz, err := codec.MarshalInterface(core.MsgID(&orig))
if err != nil {
t.Fatalf("failed to marshal from tendermint.MsgID to Any: %v", err)
}

var msgID core.MsgID
if err := codec.UnmarshalInterface(bz, &msgID); err != nil {
t.Fatalf("failed to unmarshal from Any to core.MsgID: %v", err)
}

tmMsgID, ok := msgID.(*tendermint.MsgID)
if !ok {
t.Fatalf("unexpected MsgID instance type: %T", msgID)
}

if orig != *tmMsgID {
t.Fatalf("unmatched MsgID values: %v != %v", orig, *tmMsgID)
}
}
62 changes: 62 additions & 0 deletions chains/tendermint/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package tendermint

import (
"errors"
"fmt"
"strings"
"time"

"github.com/hyperledger-labs/yui-relayer/core"
)
Expand All @@ -14,6 +17,41 @@ func (c ChainConfig) Build() (core.Chain, error) {
}, nil
}

func (c ChainConfig) Validate() error {
isEmpty := func(s string) bool {
return strings.TrimSpace(s) == ""
}

var errs []error
if isEmpty(c.Key) {
errs = append(errs, fmt.Errorf("config attribute \"key\" is empty"))
}
if isEmpty(c.ChainId) {
errs = append(errs, fmt.Errorf("config attribute \"chain_id\" is empty"))
}
if isEmpty(c.RpcAddr) {
errs = append(errs, fmt.Errorf("config attribute \"rpc_addr\" is empty"))
}
if isEmpty(c.AccountPrefix) {
errs = append(errs, fmt.Errorf("config attribute \"account_prefix\" is empty"))
}
if c.GasAdjustment <= 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_adjustment\" is too small: %v", c.GasAdjustment))
}
if isEmpty(c.GasPrices) {
errs = append(errs, fmt.Errorf("config attribute \"gas_prices\" is empty"))
}
if c.AverageBlockTimeMsec == 0 {
errs = append(errs, fmt.Errorf("config attribute \"average_block_time_msec\" is zero"))
}
if c.MaxRetryForCommit == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_commit\" is zero"))
}

// errors.Join returns nil if len(errs) == 0
return errors.Join(errs...)
}

var _ core.ProverConfig = (*ProverConfig)(nil)

func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
Expand All @@ -23,3 +61,27 @@ func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
}
return NewProver(chain_, c), nil
}

func (c ProverConfig) Validate() error {
if _, err := time.ParseDuration(c.TrustingPeriod); err != nil {
return fmt.Errorf("config attribute \"trusting_period\" is invalid: %v", err)
}
if c.RefreshThresholdRate.Denominator == 0 {
return fmt.Errorf("config attribute \"refresh_threshold_rate.denominator\" must not be zero")
}
if c.RefreshThresholdRate.Numerator == 0 {
return fmt.Errorf("config attribute \"refresh_threshold_rate.numerator\" must not be zero")
}
if c.RefreshThresholdRate.Numerator > c.RefreshThresholdRate.Denominator {
return fmt.Errorf("config attribute \"refresh_threshold_rate\" must be less than or equal to 1.0: actual=%v/%v", c.RefreshThresholdRate.Numerator, c.RefreshThresholdRate.Denominator)
}
return nil
}

func (c ProverConfig) GetTrustingPeriod() time.Duration {
if d, err := time.ParseDuration(c.TrustingPeriod); err != nil {
panic(err)
} else {
return d
}
}
Loading

0 comments on commit cc48cb2

Please sign in to comment.