Skip to content

Commit

Permalink
Merge pull request #30 from datachainlab/adr-001
Browse files Browse the repository at this point in the history
ADR-001 support

Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
  • Loading branch information
bluele authored Jan 30, 2024
2 parents abacd46 + 50e7cdd commit 1d2a2d4
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 48 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ethereum-ibc-relay-chain

This provides the ethereum chain module of [yui-relayer](https://github.com/hyperledger-labs/yui-relayer).

## Compatibility

This module is compatible with [ibc-solidity v0.3.23](https://github.com/hyperledger-labs/yui-ibc-solidity/releases/tag/v0.3.23).
85 changes: 80 additions & 5 deletions pkg/contract/ibchandler/ibchandler.go

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion pkg/relay/ethereum/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Chain struct {
ibcHandler *ibchandler.Ibchandler

signer Signer

// cache
connectionOpenedConfirmed bool
allowLCFunctions *AllowLCFunctions
}

var _ core.Chain = (*Chain)(nil)
Expand All @@ -68,14 +72,22 @@ func NewChain(config ChainConfig) (*Chain, error) {
if err != nil {
return nil, fmt.Errorf("failed to build signer: %v", err)
}
var alfs *AllowLCFunctions
if config.AllowLcFunctions != nil {
alfs, err = config.AllowLcFunctions.ToAllowLCFunctions()
if err != nil {
return nil, fmt.Errorf("failed to build allowLcFunctions: %v", err)
}
}
return &Chain{
config: config,
client: client,
chainID: id,

ibcHandler: ibcHandler,

signer: signer,
signer: signer,
allowLCFunctions: alfs,
}, nil
}

Expand Down Expand Up @@ -446,3 +458,28 @@ func (c *Chain) GetChainLogger() *log.RelayLogger {
chainID := c.Path().ChainID
return logger.WithChain(chainID)
}

func (c *Chain) confirmConnectionOpened(ctx context.Context) (bool, error) {
if c.connectionOpenedConfirmed {
return true, nil
}
if c.pathEnd.ConnectionID == "" {
return false, nil
}
latestHeight, err := c.LatestHeight()
if err != nil {
return false, err
}
// NOTE: err is nil if the connection not found
connRes, err := c.QueryConnection(
core.NewQueryContext(ctx, latestHeight),
)
if err != nil {
return false, err
}
if connRes.Connection.State != conntypes.OPEN {
return false, nil
}
c.connectionOpenedConfirmed = true
return true, nil
}
65 changes: 64 additions & 1 deletion pkg/relay/ethereum/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ethereum

import (
"encoding/hex"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -45,7 +46,11 @@ func (c ChainConfig) Validate() error {
} else if err := c.Signer.GetCachedValue().(SignerConfig).Validate(); err != nil {
errs = append(errs, fmt.Errorf("config attribute \"signer\" is invalid: %v", err))
}

if c.AllowLcFunctions != nil {
if err := c.AllowLcFunctions.ValidateBasic(); err != nil {
errs = append(errs, fmt.Errorf("config attribute \"allow_lc_functions\" is invalid: %v", err))
}
}
return errors.Join(errs...)
}

Expand All @@ -59,3 +64,61 @@ func (c ChainConfig) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
func (c ChainConfig) IBCAddress() common.Address {
return common.HexToAddress(c.IbcAddress)
}

func (alf AllowLCFunctionsConfig) ValidateBasic() error {
if !common.IsHexAddress(alf.LcAddress) {
return fmt.Errorf("invalid contract address: %s", alf.LcAddress)
} else if alf.AllowAll && len(alf.Selectors) > 0 {
return fmt.Errorf("allowAll is true and selectors is not empty")
} else if !alf.AllowAll && len(alf.Selectors) == 0 {
return fmt.Errorf("allowAll is false and selectors is empty")
}
return nil
}

// CONTRACT: alf.ValidateBasic() must be called before calling this method.
func (alf AllowLCFunctionsConfig) ToAllowLCFunctions() (*AllowLCFunctions, error) {
if alf.AllowAll {
return &AllowLCFunctions{
LCAddress: common.HexToAddress(alf.LcAddress),
AllowALL: true,
}, nil
}
selectors := make([][4]byte, len(alf.Selectors))
for i, s := range alf.Selectors {
bz, err := hex.DecodeString(strings.TrimPrefix(s, "0x"))
if err != nil {
return nil, fmt.Errorf("failed to decode selector: selector=%v err=%v", s, err)
}
if len(bz) != 4 {
return nil, fmt.Errorf("invalid selector: %s", s)
}
copy(selectors[i][:], bz)
}
return &AllowLCFunctions{
LCAddress: common.HexToAddress(alf.LcAddress),
AllowALL: false,
Selectors: selectors,
}, nil
}

type AllowLCFunctions struct {
LCAddress common.Address
AllowALL bool
Selectors [][4]byte
}

func (lcf AllowLCFunctions) IsAllowed(address common.Address, selector [4]byte) bool {
if lcf.LCAddress != address {
return false
}
if lcf.AllowALL {
return true
}
for _, s := range lcf.Selectors {
if s == selector {
return true
}
}
return false
}
Loading

0 comments on commit 1d2a2d4

Please sign in to comment.