Skip to content

Commit

Permalink
relayer: Create Light Blocks (#7)
Browse files Browse the repository at this point in the history
* Update blockchian.go

* Update rpc.go

* Update handlers.go

* Update handlers.go

* Update handlers.go

* Update rpc.go

* Update handlers.go

* Update rpc.go

* Update handlers.go

* Update handlers.go

* Update handlers.go

* Update handlers.go

* Update connection.go

* added gofmt

* Revert "added gofmt"

This reverts commit 880746f.

* fixed imports issue

* added gofmt

* added gofmt

---------

Co-authored-by: shivnative <[email protected]>
  • Loading branch information
shivjetwal and shivnative authored Oct 3, 2024
1 parent f93d6d3 commit 58e94ce
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/rly-pera/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func CmdStart() *cobra.Command {
},
}

cmd.Flags().Int(FlagMinimumBlockHeight, 1, fmt.Sprintf("%s=100 to start relaying from block 100", FlagMinimumBlockHeight))
cmd.Flags().Int(FlagMinimumBlockHeight, 1, fmt.Sprintf(
"%s=100 to start relaying from block 100", FlagMinimumBlockHeight))
return cmd
}

Expand Down
4 changes: 2 additions & 2 deletions native/blockchain/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func NewConn(rpc, gRPC string) (*Conn, error) {
}

// Create a connection to the gRPC server.
grpcConn, err := grpc.Dial(
grpcConn, err := grpc.NewClient(
gRPC, // your gRPC server address.
grpc.WithTransportCredentials(insecure.NewCredentials()), // The Cosmos SDK doesn't support any transport security mechanism.
grpc.WithTransportCredentials(insecure.NewCredentials()), // The Cosmos SDK doesn't support any transport.
// This instantiates a general gRPC codec which handles proto bytes. We pass in a nil interface registry
// if the request/response types contain interface instead of 'nil' you should pass the application specific codec.
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec())),
Expand Down
20 changes: 17 additions & 3 deletions native/blockchain/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
types "github.com/cometbft/cometbft/rpc/jsonrpc/types"
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
prov "github.com/cometbft/cometbft/light/provider/http"
provtypes "github.com/cometbft/cometbft/light/provider"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"

Expand Down Expand Up @@ -135,7 +137,7 @@ func (b *chainRPC) ChainHeader() (string, uint64, error) {
func (b *chainRPC) makeRPCRequest(req any, responseStruct any) error {
reqBytes, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("error marshalling request: %w", err)
return fmt.Errorf("error marshaling request: %w", err)
}

resp, err := b.conn.httpClient.Post(b.conn.AddrRPC, "application/json", bytes.NewReader(reqBytes))
Expand All @@ -158,8 +160,11 @@ func (b *chainRPC) Block(ctx context.Context, height int64) (blk *tmtypes.Block,
defer b.mu.Unlock()
blkResult, err := b.conn.websocketRPC.Block(ctx, &height)
if err != nil {
// usually a node does not have all the blocks, in this case we could parse the last block that node has available and start from there.
// "error in json rpc client, with http response metadata: (Status: 200 OK, Protocol HTTP/1.1). RPC error -32603 - Internal error: height 1 is not available, lowest height is 7942001"
// usually a node does not have all the blocks,
// in this case we could parse the last block that node has available and start from there.
// "error in json rpc client, with http response metadata:
// (Status: 200 OK, Protocol HTTP/1.1).
// RPC error -32603 - Internal error: height 1 is not available, lowest height is 7942001"
errString := err.Error()
searchStrInErr := fmt.Sprintf("Internal error: height %d is not available, lowest height is ", height)
idx := strings.Index(errString, searchStrInErr)
Expand Down Expand Up @@ -194,3 +199,12 @@ func (b *chainRPC) CheckTx(ctx context.Context, tx tmtypes.Tx) (err error) {

return nil
}
// create light provider
func (b *chainRPC) LightProvider() (provtypes.Provider) {

lightprovider, err := prov.New(b.ChainID(), b.conn.AddrRPC)
if err != nil {
return nil
}
return lightprovider
}
4 changes: 3 additions & 1 deletion native/blockchian.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

tmtypes "github.com/cometbft/cometbft/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
provtypes "github.com/cometbft/cometbft/light/provider"
)

// Blockchain is the expected blockchain interface the indexer needs to store data in the database.
Expand All @@ -17,4 +18,5 @@ type Blockchain interface {
SubscribeNewBlock(ctx context.Context) (<-chan *tmtypes.Block, error)
Block(ctx context.Context, height int64) (blk *tmtypes.Block, minimumBlkHeight int, err error)
CheckTx(ctx context.Context, tx tmtypes.Tx) (err error)
}
LightProvider() (provtypes.Provider)
}
8 changes: 7 additions & 1 deletion native/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package native
import (
"context"
"encoding/hex"

tmtypes "github.com/cometbft/cometbft/types"
)

Expand All @@ -17,6 +17,12 @@ func (i *Indexer) HandleNewBlock(ctx context.Context, blk *tmtypes.Block) error

// HandleBlock handles the receive of an block from the chain.
func (i *Indexer) HandleBlock(ctx context.Context, blk *tmtypes.Block) error {
// light block
lb, err := i.b.LightProvider().LightBlock(ctx, blk.Header.Height)
if err != nil {
return err
}
i.logger.Info().Int64("light block", lb.SignedHeader.Header.Height).Msg("Light Block ")
for _, tx := range blk.Data.Txs {
if err := i.HandleTx(ctx, int(blk.Header.Height), int(blk.Time.Unix()), tx); err != nil {
i.logger.Err(err).Int64("height", blk.Height).Msg("error handling block")
Expand Down
3 changes: 2 additions & 1 deletion native/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (i *Indexer) IndexOldBlocks(ctx context.Context) {
}

if blk == nil && minimumNodeBlkHeight != 0 {
i.logger.Info().Int("blockHeight", blockHeight).Int("minimumNodeBlkHeight", minimumNodeBlkHeight).Msg("initial block height not available on node")
i.logger.Info().Int("blockHeight", blockHeight).Int("minimumNodeBlkHeight", minimumNodeBlkHeight).Msg(
"initial block height not available on node")
// in this case we should continue to index from the given height.
i.lowestBlock = minimumNodeBlkHeight
i.IndexOldBlocks(ctx)
Expand Down

0 comments on commit 58e94ce

Please sign in to comment.