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

refactor: switch zetaclient to confirmation params; unify inbound observation as block range based #3496

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Refactor

* [3381](https://github.com/zeta-chain/node/pull/3381) - split Bitcoin observer and signer into small files and organize outbound logic into reusable/testable functions; renaming, type unification, etc.
* [3496](https://github.com/zeta-chain/node/pull/3496) - zetaclient uses `ConfirmationParams` in stead of old `ConfirmationCount`; use block ranged based observation for btc and evm chain.

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetatool/inbound/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func btcInboundBallotIdentifier(
inboundHash,
inboundChain.ChainId,
zetaChainID,
chainParams.ConfirmationCount,
chainParams.InboundConfirmationSafe(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetatool/inbound/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func evmInboundBallotIdentifier(ctx context.Context,

// Signer is unused
c := evmclient.New(evmClient, ethtypes.NewLondonSigner(tx.ChainId()))
confirmed, err := c.IsTxConfirmed(ctx, inboundHash, chainParams.ConfirmationCount)
confirmed, err := c.IsTxConfirmed(ctx, inboundHash, chainParams.InboundConfirmationSafe())
if err != nil {
return "", fmt.Errorf("unable to confirm tx: %w", err)
}
Expand Down
28 changes: 28 additions & 0 deletions x/observer/types/chain_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ func ValidateChainParams(params *ChainParams) error {
return nil
}

// InboundConfirmationSafe returns the safe number of confirmation for inbound observation.
func (cp ChainParams) InboundConfirmationSafe() uint64 {
return cp.ConfirmationParams.SafeInboundCount
}

// InboundConfirmationFast returns the fast number of confirmation for inbound observation.
// It falls back to safe confirmation count if fast mode is disabled.
func (cp ChainParams) InboundConfirmationFast() uint64 {
if cp.ConfirmationParams.FastInboundCount > 0 {
return cp.ConfirmationParams.FastInboundCount
}
return cp.ConfirmationParams.SafeInboundCount
}

// OutboundConfirmationSafe returns the safe number of confirmation for outbound observation.
func (cp ChainParams) OutboundConfirmationSafe() uint64 {
return cp.ConfirmationParams.SafeOutboundCount
}

// OutboundConfirmationFast returns the fast number of confirmation for outbound observation.
// It falls back to safe confirmation count if fast mode is disabled.
func (cp ChainParams) OutboundConfirmationFast() uint64 {
if cp.ConfirmationParams.FastOutboundCount > 0 {
return cp.ConfirmationParams.FastOutboundCount
}
return cp.ConfirmationParams.SafeOutboundCount
}

func validChainContractAddress(address string) bool {
if !strings.HasPrefix(address, "0x") {
return false
Expand Down
53 changes: 53 additions & 0 deletions x/observer/types/chain_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/suite"
. "gopkg.in/check.v1"

"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/observer/types"
)

Expand Down Expand Up @@ -171,6 +172,58 @@ func (s *UpdateChainParamsSuite) TestCoreContractAddresses() {
require.NotNil(s.T(), err)
}

func Test_InboundConfirmationSafe(t *testing.T) {
cp := sample.ChainParams(1)

// set and check safe inbound count
cp.ConfirmationParams.SafeInboundCount = 10
require.Equal(t, uint64(10), cp.InboundConfirmationSafe())
}

func Test_OutboundConfirmationSafe(t *testing.T) {
cp := sample.ChainParams(1)

// set and check safe outbound count
cp.ConfirmationParams.SafeOutboundCount = 10
require.Equal(t, uint64(10), cp.OutboundConfirmationSafe())
}

func Test_InboundConfirmationFast(t *testing.T) {
t.Run("should return fast inbound confirmation count if enabled", func(t *testing.T) {
cp := sample.ChainParams(1)
cp.ConfirmationParams.SafeInboundCount = 2
cp.ConfirmationParams.FastInboundCount = 1
confirmation := cp.InboundConfirmationFast()
require.Equal(t, uint64(1), confirmation)
})

t.Run("should fallback to safe inbound confirmation count if fast confirmation is disabled", func(t *testing.T) {
cp := sample.ChainParams(1)
cp.ConfirmationParams.SafeInboundCount = 2
cp.ConfirmationParams.FastInboundCount = 0
confirmation := cp.InboundConfirmationFast()
require.Equal(t, uint64(2), confirmation)
})
}

func Test_OutboundConfirmationFast(t *testing.T) {
t.Run("should return fast outbound confirmation count if enabled", func(t *testing.T) {
cp := sample.ChainParams(1)
cp.ConfirmationParams.SafeOutboundCount = 2
cp.ConfirmationParams.FastOutboundCount = 1
confirmation := cp.OutboundConfirmationFast()
require.Equal(t, uint64(1), confirmation)
})

t.Run("should fallback to safe outbound confirmation count if fast confirmation is disabled", func(t *testing.T) {
cp := sample.ChainParams(1)
cp.ConfirmationParams.SafeOutboundCount = 2
cp.ConfirmationParams.FastOutboundCount = 0
confirmation := cp.OutboundConfirmationFast()
require.Equal(t, uint64(2), confirmation)
})
}

func (s *UpdateChainParamsSuite) Validate(params *types.ChainParams) {
copy := copyParams(params)
copy.ConfirmationCount = 0
Expand Down
85 changes: 85 additions & 0 deletions zetaclient/chains/base/confirmation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package base

// CalcUnscannedBlockRangeInboundSafe calculates the unscanned block range using inbound safe confirmation count.
// It returns a range of blocks [from, end (exclusive)) that need to be scanned.
func (ob *Observer) CalcUnscannedBlockRangeInboundSafe(blockLimit uint64) (from uint64, end uint64) {
lastBlock := ob.LastBlock()
lastScanned := ob.LastBlockScanned()
confirmation := ob.ChainParams().InboundConfirmationSafe()

return calcUnscannedBlockRange(lastBlock, lastScanned, confirmation, blockLimit)
}

// CalcUnscannedBlockRangeInboundFast calculates the unscanned block range using inbound fast confirmation count.
// It returns a range of blocks [from, end (exclusive)) that need to be scanned.
func (ob *Observer) CalcUnscannedBlockRangeInboundFast(blockLimit uint64) (from uint64, end uint64) {
lastBlock := ob.LastBlock()
lastScanned := ob.LastBlockScanned()
confirmation := ob.ChainParams().InboundConfirmationFast()

return calcUnscannedBlockRange(lastBlock, lastScanned, confirmation, blockLimit)
}

// IsBlockConfirmedForInboundSafe checks if the block number is confirmed using inbound safe confirmation count.
func (ob *Observer) IsBlockConfirmedForInboundSafe(blockNumber uint64) bool {
lastBlock := ob.LastBlock()
confirmation := ob.ChainParams().InboundConfirmationSafe()
return isBlockConfirmed(blockNumber, confirmation, lastBlock)
}

// IsBlockConfirmedForInboundFast checks if the block number is confirmed using inbound fast confirmation count.
// It falls back to safe confirmation count if fast confirmation is disabled.
func (ob *Observer) IsBlockConfirmedForInboundFast(blockNumber uint64) bool {
lastBlock := ob.LastBlock()
confirmation := ob.ChainParams().InboundConfirmationFast()
return isBlockConfirmed(blockNumber, confirmation, lastBlock)
}

// IsBlockConfirmedForOutboundSafe checks if the block number is confirmed using outbound safe confirmation count.
func (ob *Observer) IsBlockConfirmedForOutboundSafe(blockNumber uint64) bool {
lastBlock := ob.LastBlock()
confirmation := ob.ChainParams().OutboundConfirmationSafe()
return isBlockConfirmed(blockNumber, confirmation, lastBlock)
}

// IsBlockConfirmedForOutboundFast checks if the block number is confirmed using outbound fast confirmation count.
// It falls back to safe confirmation count if fast confirmation is disabled.
func (ob *Observer) IsBlockConfirmedForOutboundFast(blockNumber uint64) bool {
lastBlock := ob.LastBlock()
confirmation := ob.ChainParams().OutboundConfirmationFast()
return isBlockConfirmed(blockNumber, confirmation, lastBlock)
}

// calcUnscannedBlockRange calculates the unscanned block range [from, end (exclusive)) within given block limit.
//
// example 1: given lastBlock = 99, lastScanned = 90, confirmation = 10, then no unscanned block
// example 2: given lastBlock = 100, lastScanned = 90, confirmation = 10, then 1 unscanned block (block 91)
func calcUnscannedBlockRange(lastBlock, lastScanned, confirmation, blockLimit uint64) (from uint64, end uint64) {
// got unscanned blocks or not?
if lastBlock < lastScanned+confirmation {
return 0, 0
}

// calculate the highest confirmed block
// example: given lastBlock = 101, confirmation = 10, then the highest confirmed block is 92
highestConfirmed := lastBlock - confirmation + 1

// calculate a range of unscanned blocks within block limit
from = lastScanned + 1
end = from + blockLimit

// 'end' is exclusive, so ensure it is not greater than (highestConfirmed+1)
if end > highestConfirmed+1 {
end = highestConfirmed + 1
}

return from, end
}

// isBlockConfirmed checks if the block number is confirmed.
//
// Note: block 100 is confirmed if the last block is 100 and confirmation count is 1.
func isBlockConfirmed(blockNumber uint64, confirmation uint64, lastBlock uint64) bool {
confHeight := blockNumber + confirmation - 1
return lastBlock >= confHeight
}
Loading