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

feat: reorg handling #44

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
82 changes: 79 additions & 3 deletions cwclient/cwclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,44 @@ func (cwClient *CosmWasmClient) QueryIsEnabled() (bool, error) {
return isEnabled, nil
}

func (cwClient *CosmWasmClient) QueryIsBlockForked(blockHeight uint64) (bool, error) {
queryData, err := createIsForkedBlockQueryData(blockHeight)
if err != nil {
return false, err
}

resp, err := cwClient.querySmartContractState(queryData)
if err != nil {
return false, err
}

var isForked bool
if err := json.Unmarshal(resp.Data, &isForked); err != nil {
return false, err
}

return isForked, nil
}

func (cwClient *CosmWasmClient) QueryForkedBlocksInRange(startHeight, endHeight uint64) ([]blockRange, error) {
bap2pecs marked this conversation as resolved.
Show resolved Hide resolved
queryData, err := createForkedBlocksInRangeQueryData(startHeight, endHeight)
if err != nil {
return nil, err
}

resp, err := cwClient.querySmartContractState(queryData)
if err != nil {
return nil, err
}

var forkedBlocks []blockRange
if err := json.Unmarshal(resp.Data, &forkedBlocks); err != nil {
return nil, err
}

return forkedBlocks, nil
}

//////////////////////////////
// INTERNAL
//////////////////////////////
Expand All @@ -118,16 +156,27 @@ type contractConfigResponse struct {
ActivatedHeight uint64 `json:"activated_height"`
}
type ContractQueryMsgs struct {
Config *contractConfig `json:"config,omitempty"`
BlockVoters *blockVotersQuery `json:"block_voters,omitempty"`
IsEnabled *isEnabledQuery `json:"is_enabled,omitempty"`
Config *contractConfig `json:"config,omitempty"`
BlockVoters *blockVotersQuery `json:"block_voters,omitempty"`
IsBlockForked *isBlockForkedQuery `json:"is_block_forked,omitempty"`
ForkedBlocksInRange *blockRange `json:"forked_blocks_in_range,omitempty"`
IsEnabled *isEnabledQuery `json:"is_enabled,omitempty"`
}

type blockRange struct {
Start uint64 `json:"start"`
End uint64 `json:"end"`
}

type blockVotersQuery struct {
Hash string `json:"hash"`
Height uint64 `json:"height"`
}

type isBlockForkedQuery struct {
Height uint64 `json:"height"`
}

type isEnabledQuery struct{}

type contractConfig struct{}
Expand All @@ -154,6 +203,33 @@ func createIsEnabledQueryData() ([]byte, error) {
return data, nil
}

func createIsForkedBlockQueryData(blockHeight uint64) ([]byte, error) {
queryData := ContractQueryMsgs{
IsBlockForked: &isBlockForkedQuery{
Height: blockHeight,
},
}
data, err := json.Marshal(queryData)
if err != nil {
return nil, err
}
return data, nil
}

func createForkedBlocksInRangeQueryData(startHeight, endHeight uint64) ([]byte, error) {
queryData := ContractQueryMsgs{
ForkedBlocksInRange: &blockRange{
Start: startHeight,
End: endHeight,
},
}
data, err := json.Marshal(queryData)
if err != nil {
return nil, err
}
return data, nil
}

// querySmartContractState queries the smart contract state given the contract address and query data
func (cwClient *CosmWasmClient) querySmartContractState(
queryData []byte,
Expand Down
1 change: 1 addition & 0 deletions finalitygadget/expected_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type IBabylonClient interface {
type ICosmWasmClient interface {
QueryListOfVotedFinalityProviders(queryParams *types.Block) ([]string, error)
QueryConsumerId() (string, error)
QueryIsBlockForked(blockHeight uint64) (bool, error)
QueryIsEnabled() (bool, error)
}

Expand Down
9 changes: 9 additions & 0 deletions finalitygadget/finalitygadget.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func (fg *FinalityGadget) QueryIsBlockBabylonFinalizedFromBabylon(block *types.B
return true, nil
}

// check if block is part of reorg whitelist, if so return true to unblock deriv pipeline
isForked, err := fg.cwClient.QueryIsBlockForked(block.BlockHeight)
if err != nil {
return false, err
}
if isForked {
return true, nil
}

// trim prefix 0x for the L2 block hash
block.BlockHash = strings.TrimPrefix(block.BlockHash, "0x")

Expand Down
30 changes: 28 additions & 2 deletions finalitygadget/finalitygadget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func TestQueryIsBlockBabylonFinalized(t *testing.T) {

mockCwClient := mocks.NewMockICosmWasmClient(ctl)
mockCwClient.EXPECT().QueryIsEnabled().Return(true, nil).Times(1)
mockCwClient.EXPECT().QueryIsBlockForked(tc.block.BlockHeight).Return(false, nil).Times(1)
mockCwClient.EXPECT().QueryConsumerId().Return(consumerChainID, nil).Times(1)
mockBTCClient := mocks.NewMockIBitcoinClient(ctl)
mockBTCClient.EXPECT().
Expand Down Expand Up @@ -268,8 +269,9 @@ func TestQueryBlockRangeBabylonFinalized(t *testing.T) {
defer ctl.Finish()

mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockCwClient := mocks.NewMockICosmWasmClient(ctl)

// Setup mock DB responses
// Setup mock DB and cw contract responses
if len(tc.queryBlocks) > 0 && tc.queryDB {
mockDbHandler.EXPECT().
QueryEarliestFinalizedBlock().
Expand All @@ -282,7 +284,8 @@ func TestQueryBlockRangeBabylonFinalized(t *testing.T) {
}

mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
db: mockDbHandler,
cwClient: mockCwClient,
}

res, err := mockFinalityGadget.QueryBlockRangeBabylonFinalized(tc.queryBlocks)
Expand Down Expand Up @@ -668,6 +671,29 @@ func TestQueryIsBlockFinalizedByHashForNonExistentBlock(t *testing.T) {
require.Equal(t, err, types.ErrBlockNotFound)
}

func TestQueryIsBlockFinalizedFromBabylonForked(t *testing.T) {
ctl := gomock.NewController(t)

// mock CwClient
mockCwClient := mocks.NewMockICosmWasmClient(ctl)
mockCwClient.EXPECT().QueryIsEnabled().Return(true, nil).Times(1)
mockCwClient.EXPECT().QueryIsBlockForked(uint64(1)).Return(true, nil).Times(1)

mockTestFinalityGadget := &FinalityGadget{
cwClient: mockCwClient,
bbnClient: nil,
btcClient: nil,
}

res, err := mockTestFinalityGadget.QueryIsBlockBabylonFinalizedFromBabylon(&types.Block{
BlockHeight: 1,
BlockHash: "0x123",
BlockTimestamp: 1000,
})
require.NoError(t, err)
require.True(t, res)
}

func TestQueryLatestFinalizedBlock(t *testing.T) {
// define blocks
first := &types.Block{
Expand Down
121 changes: 66 additions & 55 deletions proto/finalitygadget.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading