Skip to content

Commit

Permalink
fix: find proofContext change heights from last client state to lates…
Browse files Browse the repository at this point in the history
…t block
  • Loading branch information
viveksharmapoudel committed Sep 18, 2023
1 parent 2098511 commit c310e79
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 5 deletions.
4 changes: 4 additions & 0 deletions relayer/chains/cosmos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,3 +1191,7 @@ func (ap *CosmosProvider) QueryMissingPacketReceipts(ctx context.Context, latest
func (ap *CosmosProvider) QueryNextSeqSend(ctx context.Context, height int64, channelid, portid string) (seq uint64, err error) {
panic("QueryNextSeqSend not implemented")

Check warning on line 1192 in relayer/chains/cosmos/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/cosmos/query.go#L1191-L1192

Added lines #L1191 - L1192 were not covered by tests
}

func (ap *CosmosProvider) QueryProofContextChangeHeights(ctx context.Context, counterpartyClientHeight uint64, height uint64) ([]uint64, error) {
panic("QueryProofContextChangeHeights not implemented")

Check warning on line 1196 in relayer/chains/cosmos/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/cosmos/query.go#L1195-L1196

Added lines #L1195 - L1196 were not covered by tests
}
20 changes: 20 additions & 0 deletions relayer/chains/icon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type IClient interface {
GetLastBlock() (*types.Block, error)
GetBlockHeaderByHeight(height int64) (*types.BlockHeader, error)
GetValidatorsByHash(hash common.HexHash) ([]common.Address, error)
GetPrepTerm() (*types.PrepTerm, error)
}

type Client struct {
Expand Down Expand Up @@ -622,6 +623,25 @@ func (c *Client) EstimateStep(param *types.TransactionParamForEstimate) (*types.
return &result, nil
}

func (c *Client) GetPrepTerm() (*types.PrepTerm, error) {

param := types.CallParam{
FromAddress: types.Address(fmt.Sprintf("hx%s", strings.Repeat("0", 40))),
ToAddress: types.Address(genesisContract),
DataType: "call",
Data: &types.CallData{
Method: MethodGetPrepTerm,
Params: map[string]string{},
},
}

var op types.PrepTerm
if err := c.Call(&param, &op); err != nil {
return nil, err
}
return &op, nil

Check warning on line 642 in relayer/chains/icon/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/client.go#L626-L642

Added lines #L626 - L642 were not covered by tests
}

func NewClient(uri string, l *zap.Logger) *Client {
//TODO options {MaxRetrySendTx, MaxRetryGetResult, MaxIdleConnsPerHost, Debug, Dump}
tr := &http.Transport{MaxIdleConnsPerHost: 1000}
Expand Down
3 changes: 3 additions & 0 deletions relayer/chains/icon/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ const (
MethodGetMissingPacketReceipts = "getMissingPacketReceipts"
MethodGetPacketHeights = "getPacketHeights"
MethodGetAckHeights = "getAckHeights"

//
MethodGetPrepTerm = "getPRepTerm"
)
5 changes: 2 additions & 3 deletions relayer/chains/icon/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ func (pp *IconProviderConfig) NewProvider(log *zap.Logger, homepath string, debu
return nil, err
}

codec := MakeCodec(ModuleBasics, []string{})

return &IconProvider{
log: log.With(zap.String("chain_id", pp.ChainID)),
client: NewClient(pp.getRPCAddr(), log),
PCfg: pp,
StartHeight: uint64(pp.StartHeight),
codec: codec,
codec: MakeCodec(ModuleBasics, []string{}),
}, nil

}

func (pp IconProviderConfig) getRPCAddr() string {
Expand Down
64 changes: 62 additions & 2 deletions relayer/chains/icon/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import (
var _ provider.QueryProvider = &IconProvider{}

const (
epoch = 24 * 3600 * 1000
sequenceLimit = 2
epoch = 24 * 3600 * 1000
sequenceLimit = 2
genesisContract = "cx0000000000000000000000000000000000000000"
)

type CallParamOption func(*types.CallParam)
Expand Down Expand Up @@ -1015,3 +1016,62 @@ func (icp *IconProvider) HexStringToProtoUnmarshal(encoded string, v proto.Messa
return inputBytes, nil

}

func (ip *IconProvider) GetProofContextChangePeriod() (uint64, error) {
// assigning termPeriod
prep, err := ip.client.GetPrepTerm()
if err != nil {
return 0, fmt.Errorf("fail to get prepterm: %v", err)
}

Check warning on line 1025 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1020-L1025

Added lines #L1020 - L1025 were not covered by tests

decentralized, err := prep.IsDecentralized.Value()
if err != nil {
return 0, err
}

Check warning on line 1030 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1027-L1030

Added lines #L1027 - L1030 were not covered by tests

// storing prep-term term only if decentralized
if decentralized == 1 {
period, err := prep.Period.Value()
if err != nil {
return 0, err
}
return uint64(period), nil

Check warning on line 1038 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1033-L1038

Added lines #L1033 - L1038 were not covered by tests

}
return 0, nil

Check warning on line 1041 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1041

Added line #L1041 was not covered by tests
}

func (ip *IconProvider) QueryProofContextChangeHeights(ctx context.Context, counterpartyClientHeight uint64, latestHeight uint64) ([]uint64, error) {
heights := make([]uint64, 0)
// querying prepterm

period, err := ip.GetProofContextChangePeriod()
if err != nil {
return heights, err
}

Check warning on line 1051 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1044-L1051

Added lines #L1044 - L1051 were not covered by tests

// 0 suggest that proof context period is not set
if period == 0 {
return heights, nil
}

Check warning on line 1056 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1054-L1056

Added lines #L1054 - L1056 were not covered by tests

// hasn't reached until the latest block
if counterpartyClientHeight+period > latestHeight {
return heights, nil
}

Check warning on line 1061 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1059-L1061

Added lines #L1059 - L1061 were not covered by tests

lastPeriodChangeHeight := counterpartyClientHeight - counterpartyClientHeight%period

for lastPeriodChangeHeight < latestHeight {
lastPeriodChangeHeight += period
btpblock, err := ip.GetBtpHeader(int64(lastPeriodChangeHeight + 1))
if err != nil {
continue

Check warning on line 1069 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1063-L1069

Added lines #L1063 - L1069 were not covered by tests
}
if btpblock != nil {
heights = append(heights, btpblock.MainHeight)
}

Check warning on line 1073 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1071-L1073

Added lines #L1071 - L1073 were not covered by tests
}

return heights, nil

Check warning on line 1076 in relayer/chains/icon/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/icon/query.go#L1076

Added line #L1076 was not covered by tests
}
20 changes: 20 additions & 0 deletions relayer/chains/icon/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,23 @@ func (h *NetworkTypeSection) Encode() []byte {
func (h *NetworkTypeSection) Hash() []byte {
return relayer_common.Sha3keccak256(h.Encode())
}

type PrepTerm struct {
BlockHeight HexInt
bondRequirement HexInt
EndBlockHeight HexInt
IissVersion HexInt
Irep HexInt
IsDecentralized HexInt
MainPRepCount HexInt
Period HexInt
Preps interface{} //dont need right now
Revision HexInt
RewardFund interface{} //dont need right now
Rrep HexInt
Sequence HexInt
StartBlockHeight HexInt
TotalDelegated HexInt
TotalPower HexInt
TotalSupply HexInt
}
3 changes: 3 additions & 0 deletions relayer/chains/penumbra/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,6 @@ func (ap *PenumbraProvider) QueryMissingPacketReceipts(ctx context.Context, late
func (ap *PenumbraProvider) QueryNextSeqSend(ctx context.Context, height int64, channelid, portid string) (seq uint64, err error) {
panic("QueryNextSeqSend not implemented")
}
func (ap *PenumbraProvider) QueryProofContextChangeHeights(ctx context.Context, counterpartyClientHeight uint64, height uint64) ([]uint64, error) {
panic("QueryProofContextChangeHeights not implemented")
}
4 changes: 4 additions & 0 deletions relayer/chains/wasm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,7 @@ func (ap *WasmProvider) QueryNextSeqSend(ctx context.Context, height int64, chan
}
return response, nil

Check warning on line 1032 in relayer/chains/wasm/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/query.go#L1021-L1032

Added lines #L1021 - L1032 were not covered by tests
}

func (ap *WasmProvider) QueryProofContextChangeHeights(ctx context.Context, counterpartyClientHeight uint64, height uint64) ([]uint64, error) {
panic("QueryProofContextChangeHeights not implemented")

Check warning on line 1036 in relayer/chains/wasm/query.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/query.go#L1035-L1036

Added lines #L1035 - L1036 were not covered by tests
}
14 changes: 14 additions & 0 deletions relayer/processor/path_end_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ func (pathEnd *pathEndRuntime) mergeCacheData(ctx context.Context, cancel func()
pathEnd.latestHeader = d.LatestHeader
pathEnd.clientState = d.ClientState

// from the last clientState update to latest header
// add all the proof context change height in btp
if d.IsGenesis && pathEnd.chainProvider.Type() == common.IconModule {

heights, _ := pathEnd.chainProvider.QueryProofContextChangeHeights(ctx,
counterParty.clientState.ConsensusHeight.RevisionHeight,
d.LatestHeader.Height())

for _, h := range heights {
pathEnd.BTPHeightQueue.Enqueue(h)

}

Check warning on line 405 in relayer/processor/path_end_runtime.go

View check run for this annotation

Codecov / codecov/patch

relayer/processor/path_end_runtime.go#L394-L405

Added lines #L394 - L405 were not covered by tests
}

if pathEnd.chainProvider.Type() == common.IconModule && d.LatestHeader.IsCompleteBlock() {
pathEnd.BTPHeightQueue.Enqueue(d.LatestHeader.Height())

Check warning on line 409 in relayer/processor/path_end_runtime.go

View check run for this annotation

Codecov / codecov/patch

relayer/processor/path_end_runtime.go#L409

Added line #L409 was not covered by tests
}
Expand Down
3 changes: 3 additions & 0 deletions relayer/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ type QueryProvider interface {
// ics 20 - transfer
QueryDenomTrace(ctx context.Context, denom string) (*transfertypes.DenomTrace, error)
QueryDenomTraces(ctx context.Context, offset, limit uint64, height int64) ([]transfertypes.DenomTrace, error)

// For BTP
QueryProofContextChangeHeights(ctx context.Context, counterpartyClientHeight uint64, height uint64) ([]uint64, error)
}

type RelayPacket interface {
Expand Down

0 comments on commit c310e79

Please sign in to comment.