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

chore: update query activated height #42

Merged
Merged
Changes from 1 commit
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
64 changes: 54 additions & 10 deletions clientcontroller/opstackl2/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
sdkErr "cosmossdk.io/errors"
wasmdparams "github.com/CosmWasm/wasmd/app/params"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
bbnapp "github.com/babylonlabs-io/babylon/app"

Check failure on line 13 in clientcontroller/opstackl2/consumer.go

View workflow job for this annotation

GitHub Actions / lint_test / build

github.com/babylonlabs-io/babylon-private@v0.9.0-rc.3.0.20240801001431-74a24c962ce2: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/17da197d642e6e74aad8aaf07b22f5aadb19e3813bbb5ec2fa7136f8a64469b1: exit status 128:

Check failure on line 13 in clientcontroller/opstackl2/consumer.go

View workflow job for this annotation

GitHub Actions / lint_test / unit-tests

github.com/babylonlabs-io/babylon-private@v0.9.0-rc.3.0.20240801001431-74a24c962ce2: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/17da197d642e6e74aad8aaf07b22f5aadb19e3813bbb5ec2fa7136f8a64469b1: exit status 128:
bbntypes "github.com/babylonlabs-io/babylon/types"
fgclient "github.com/babylonlabs-io/finality-gadget/client"
"github.com/babylonlabs-io/finality-provider/clientcontroller/api"
cwclient "github.com/babylonlabs-io/finality-provider/cosmwasmclient/client"
cwconfig "github.com/babylonlabs-io/finality-provider/cosmwasmclient/config"
Expand Down Expand Up @@ -353,25 +354,26 @@
}

// QueryActivatedHeight returns the L2 block number at which the finality gadget is activated.
// It is fetched from the configuration of a CosmWasm contract OP finality gadget.
func (cc *OPStackL2ConsumerController) QueryActivatedHeight() (uint64, error) {
queryMsg := &QueryMsg{Config: &Config{}}
jsonData, err := json.Marshal(queryMsg) // `{"config":{}}`
finalityGadgetClient, err := fgclient.NewFinalityGadgetGrpcClient(cc.Cfg.BabylonFinalityGadgetRpc)
if err != nil {
return 0, fmt.Errorf("failed marshaling to JSON: %w", err)
cc.logger.Error("failed to initialize Babylon Finality Gadget Grpc client", zap.Error(err))
return 0, err
bap2pecs marked this conversation as resolved.
Show resolved Hide resolved
}
stateResp, err := cc.CwClient.QuerySmartContractState(cc.Cfg.OPFinalityGadgetAddress, string(jsonData))

activatedTimestamp, err := finalityGadgetClient.QueryBtcStakingActivatedTimestamp()
if err != nil {
return 0, fmt.Errorf("failed to query smart contract state: %w", err)
cc.logger.Error("failed to query BTC staking activate timestamp", zap.Error(err))
return 0, err
}

var resp ConfigResponse
err = json.Unmarshal(stateResp.Data, &resp)
l2BlockNumber, err := cc.GetBlockNumberByTimestamp(context.Background(), activatedTimestamp)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal response: %w", err)
cc.logger.Error("failed to convert L2 block number from the given BTC staking activation timestamp", zap.Error(err))
return 0, err
}

return resp.ActivatedHeight, nil
return l2BlockNumber, nil
}

// QueryLatestBlockHeight gets the latest L2 block number from a RPC call
Expand Down Expand Up @@ -431,6 +433,48 @@
}
}

// GetBlockNumberByTimestamp returns the L2 block number for the given BTC staking activation timestamp.
// It uses a binary search to find the block number.
func (cc *OPStackL2ConsumerController) GetBlockNumberByTimestamp(ctx context.Context, targetTimestamp uint64) (uint64, error) {
// Check if the target timestamp is after the latest block
latestBlock, err := cc.opl2Client.HeaderByNumber(ctx, nil)
if err != nil {
return 0, err
}
if targetTimestamp > latestBlock.Time {
return 0, nil
bap2pecs marked this conversation as resolved.
Show resolved Hide resolved
}
// Check if the target timestamp is before the first block
firstBlock, err := cc.opl2Client.HeaderByNumber(ctx, big.NewInt(1))
if err != nil {
return 0, err
}
if targetTimestamp < firstBlock.Time {
bap2pecs marked this conversation as resolved.
Show resolved Hide resolved
return 0, nil
}

lowerBound := uint64(1)
bap2pecs marked this conversation as resolved.
Show resolved Hide resolved
upperBound := latestBlock.Number.Uint64()

for lowerBound <= upperBound {
midBlockNumber := (lowerBound + upperBound) / 2
block, err := cc.opl2Client.HeaderByNumber(ctx, big.NewInt(int64(midBlockNumber)))
if err != nil {
return 0, err
}

if block.Time < targetTimestamp {
lowerBound = midBlockNumber + 1
} else if block.Time > targetTimestamp {
upperBound = midBlockNumber - 1
} else {
return midBlockNumber, nil
}
}

return lowerBound, nil
}

func (cc *OPStackL2ConsumerController) Close() error {
cc.opl2Client.Close()
return cc.CwClient.Stop()
Expand Down
Loading