Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update evm_consumer functions implemented #2

Open
wants to merge 29 commits into
base: base/consumer-chain-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
289bbc4
feat: Split into ClientController and ConsumerController (#318)
lesterli May 11, 2024
d451545
feat: Add EVM config (#319)
lesterli May 14, 2024
9aa678e
fix tests & more split
bap2pecs May 18, 2024
e54e921
QueryFinalityProviderVotingPower pseudo code
bap2pecs May 18, 2024
41cd722
add pseudo code for QueryActivatedHeight
bap2pecs May 19, 2024
caec997
nit
bap2pecs May 19, 2024
c5c3c01
enforce `mock-gen` before testing
bap2pecs May 19, 2024
22873ab
nit
bap2pecs May 19, 2024
e8398f7
QueryBestBlock pseudo code
bap2pecs May 19, 2024
3fa7927
refactor
bap2pecs May 19, 2024
feecc8f
evm_consumer update
XiangEnze May 19, 2024
aa9041a
refactor
bap2pecs May 19, 2024
252a328
split ClientController and ConsumerController (Part 2) (#335)
bap2pecs May 20, 2024
5e6e9ad
Refactor QueryBestBlock() to only return the uint64 block height (#336)
bap2pecs May 20, 2024
79ce5c6
Merge branch 'base/eth-support' into feat/SNA-427-QueryBlock-refactor
bap2pecs May 20, 2024
5119c64
nit
bap2pecs May 20, 2024
79a0e20
for comments
bap2pecs May 20, 2024
77421c3
Merge branch 'feat/SNA-427-QueryBlock-refactor' into feat/QueryActiva…
XiangEnze May 20, 2024
b992c72
update after review
XiangEnze May 20, 2024
257a441
rename var b
XiangEnze May 20, 2024
4b3061e
Printf err fix
XiangEnze May 20, 2024
d86f39c
rename respect of rules
XiangEnze May 20, 2024
c12ab96
feat : evm_consumer optimization
XiangEnze May 22, 2024
43b1734
modify : rename queryLatestFinalizedNumber
XiangEnze May 22, 2024
4329808
modify : duplicate l2Client mistake
XiangEnze May 22, 2024
a589bb3
Merge branch 'base/consumer-chain-support' into feat/QueryActivatedHe…
XiangEnze May 24, 2024
a612773
implement : QueryLatestFinalizedBlock()
XiangEnze May 24, 2024
bb0007e
correction : evm.cfg
XiangEnze May 24, 2024
f3cf85b
correction : makefile merge problerm
XiangEnze May 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ build-docker:
.PHONY: build build-docker

test:
make mock-gen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not exist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted

go test ./...

test-e2e:
Expand All @@ -75,7 +76,7 @@ proto-gen:

mock-gen:
mkdir -p $(MOCKS_DIR)
$(MOCKGEN_CMD) -source=clientcontroller/interface.go -package mocks -destination $(MOCKS_DIR)/babylon.go
$(MOCKGEN_CMD) -source=clientcontroller/interface.go -package mocks -destination $(MOCKS_DIR)/clientcontroller.go

.PHONY: mock-gen

Expand Down
165 changes: 0 additions & 165 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clientcontroller
import (
"context"
"fmt"
"time"

sdkErr "cosmossdk.io/errors"
"github.com/btcsuite/btcd/btcec/v2"
Expand All @@ -16,7 +15,6 @@ import (
btclctypes "github.com/babylonchain/babylon/x/btclightclient/types"
btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
ckpttypes "github.com/babylonchain/babylon/x/checkpointing/types"
finalitytypes "github.com/babylonchain/babylon/x/finality/types"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -152,62 +150,6 @@ func (bc *BabylonController) RegisterFinalityProvider(
return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, registeredEpoch, nil
}

// SubmitFinalitySig submits the finality signature via a MsgAddVote to Babylon
func (bc *BabylonController) SubmitFinalitySig(fpPk *btcec.PublicKey, blockHeight uint64, blockHash []byte, sig *btcec.ModNScalar) (*types.TxResponse, error) {
msg := &finalitytypes.MsgAddFinalitySig{
Signer: bc.mustGetTxSigner(),
FpBtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(fpPk),
BlockHeight: blockHeight,
BlockAppHash: blockHash,
FinalitySig: bbntypes.NewSchnorrEOTSSigFromModNScalar(sig),
}

unrecoverableErrs := []*sdkErr.Error{
finalitytypes.ErrInvalidFinalitySig,
finalitytypes.ErrPubRandNotFound,
btcstakingtypes.ErrFpAlreadySlashed,
}

res, err := bc.reliablySendMsg(msg, emptyErrs, unrecoverableErrs)
if err != nil {
return nil, err
}

return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, nil
}

// SubmitBatchFinalitySigs submits a batch of finality signatures to Babylon
func (bc *BabylonController) SubmitBatchFinalitySigs(fpPk *btcec.PublicKey, blocks []*types.BlockInfo, sigs []*btcec.ModNScalar) (*types.TxResponse, error) {
if len(blocks) != len(sigs) {
return nil, fmt.Errorf("the number of blocks %v should match the number of finality signatures %v", len(blocks), len(sigs))
}

msgs := make([]sdk.Msg, 0, len(blocks))
for i, b := range blocks {
msg := &finalitytypes.MsgAddFinalitySig{
Signer: bc.mustGetTxSigner(),
FpBtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(fpPk),
BlockHeight: b.Height,
BlockAppHash: b.Hash,
FinalitySig: bbntypes.NewSchnorrEOTSSigFromModNScalar(sigs[i]),
}
msgs = append(msgs, msg)
}

unrecoverableErrs := []*sdkErr.Error{
finalitytypes.ErrInvalidFinalitySig,
finalitytypes.ErrPubRandNotFound,
btcstakingtypes.ErrFpAlreadySlashed,
}

res, err := bc.reliablySendMsgs(msgs, emptyErrs, unrecoverableErrs)
if err != nil {
return nil, err
}

return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, nil
}

func (bc *BabylonController) QueryFinalityProviderSlashed(fpPk *btcec.PublicKey) (bool, error) {
fpPubKey := bbntypes.NewBIP340PubKeyFromBTCPK(fpPk)
res, err := bc.bbnClient.QueryClient.FinalityProvider(fpPubKey.MarshalHex())
Expand All @@ -220,19 +162,6 @@ func (bc *BabylonController) QueryFinalityProviderSlashed(fpPk *btcec.PublicKey)
return slashed, nil
}

// QueryFinalityProviderVotingPower queries the voting power of the finality provider at a given height
func (bc *BabylonController) QueryFinalityProviderVotingPower(fpPk *btcec.PublicKey, blockHeight uint64) (uint64, error) {
res, err := bc.bbnClient.QueryClient.FinalityProviderPowerAtHeight(
bbntypes.NewBIP340PubKeyFromBTCPK(fpPk).MarshalHex(),
blockHeight,
)
if err != nil {
return 0, fmt.Errorf("failed to query the finality provider's voting power at height %d: %w", blockHeight, err)
}

return res.VotingPower, nil
}

// QueryFinalityProviderRegisteredEpoch queries the registered epoch of the finality provider
func (bc *BabylonController) QueryFinalityProviderRegisteredEpoch(fpPk *btcec.PublicKey) (uint64, error) {
res, err := bc.bbnClient.QueryClient.FinalityProvider(
Expand All @@ -245,10 +174,6 @@ func (bc *BabylonController) QueryFinalityProviderRegisteredEpoch(fpPk *btcec.Pu
return res.FinalityProvider.RegisteredEpoch, nil
}

func (bc *BabylonController) QueryLatestFinalizedBlocks(count uint64) ([]*types.BlockInfo, error) {
return bc.queryLatestBlocks(nil, count, finalitytypes.QueriedBlockStatus_FINALIZED, true)
}

func (bc *BabylonController) QueryLastFinalizedEpoch() (uint64, error) {
resp, err := bc.bbnClient.LatestEpochFromStatus(ckpttypes.Finalized)
if err != nil {
Expand All @@ -257,96 +182,6 @@ func (bc *BabylonController) QueryLastFinalizedEpoch() (uint64, error) {
return resp.RawCheckpoint.EpochNum, nil
}

func (bc *BabylonController) QueryBlocks(startHeight, endHeight, limit uint64) ([]*types.BlockInfo, error) {
if endHeight < startHeight {
return nil, fmt.Errorf("the startHeight %v should not be higher than the endHeight %v", startHeight, endHeight)
}
count := endHeight - startHeight + 1
if count > limit {
count = limit
}
return bc.queryLatestBlocks(sdk.Uint64ToBigEndian(startHeight), count, finalitytypes.QueriedBlockStatus_ANY, false)
}

func (bc *BabylonController) queryLatestBlocks(startKey []byte, count uint64, status finalitytypes.QueriedBlockStatus, reverse bool) ([]*types.BlockInfo, error) {
var blocks []*types.BlockInfo
pagination := &sdkquery.PageRequest{
Limit: count,
Reverse: reverse,
Key: startKey,
}

res, err := bc.bbnClient.QueryClient.ListBlocks(status, pagination)
if err != nil {
return nil, fmt.Errorf("failed to query finalized blocks: %v", err)
}

for _, b := range res.Blocks {
ib := &types.BlockInfo{
Height: b.Height,
Hash: b.AppHash,
}
blocks = append(blocks, ib)
}

return blocks, nil
}

func getContextWithCancel(timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
return ctx, cancel
}

func (bc *BabylonController) QueryBlock(height uint64) (*types.BlockInfo, error) {
res, err := bc.bbnClient.QueryClient.Block(height)
if err != nil {
return nil, fmt.Errorf("failed to query indexed block at height %v: %w", height, err)
}

return &types.BlockInfo{
Height: height,
Hash: res.Block.AppHash,
Finalized: res.Block.Finalized,
}, nil
}

func (bc *BabylonController) QueryActivatedHeight() (uint64, error) {
res, err := bc.bbnClient.QueryClient.ActivatedHeight()
if err != nil {
return 0, fmt.Errorf("failed to query activated height: %w", err)
}

return res.Height, nil
}

func (bc *BabylonController) QueryBestBlock() (*types.BlockInfo, error) {
blocks, err := bc.queryLatestBlocks(nil, 1, finalitytypes.QueriedBlockStatus_ANY, true)
if err != nil || len(blocks) != 1 {
// try query comet block if the index block query is not available
return bc.queryCometBestBlock()
}

return blocks[0], nil
}

func (bc *BabylonController) queryCometBestBlock() (*types.BlockInfo, error) {
ctx, cancel := getContextWithCancel(bc.cfg.Timeout)
// this will return 20 items at max in the descending order (highest first)
chainInfo, err := bc.bbnClient.RPCClient.BlockchainInfo(ctx, 0, 0)
defer cancel()

if err != nil {
return nil, err
}

// Returning response directly, if header with specified number did not exist
// at request will contain nil header
return &types.BlockInfo{
Height: uint64(chainInfo.BlockMetas[0].Header.Height),
Hash: chainInfo.BlockMetas[0].Header.AppHash,
}, nil
}

func (bc *BabylonController) Close() error {
if !bc.bbnClient.IsRunning() {
return nil
Expand Down
Loading