Skip to content

Commit

Permalink
add unit test for IsBlockConfirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Sep 19, 2024
1 parent f7627ea commit 08df27a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion zetaclient/chains/base/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ func (ob *Observer) WithLastBlock(lastBlock uint64) *Observer {
}

// IsBlockConfirmed checks if the given block number is confirmed.
//
// Note: block 100 is confirmed if the last block is 100 and confirmation count is 1.
func (ob *Observer) IsBlockConfirmed(blockNumber uint64) bool {
lastBlock := ob.LastBlock()
confBlock := blockNumber + ob.chainParams.ConfirmationCount
confBlock := blockNumber + ob.chainParams.ConfirmationCount - 1
return lastBlock >= confBlock
}

Expand Down
49 changes: 49 additions & 0 deletions zetaclient/chains/base/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ import (
const (
// defaultAlertLatency is the default alert latency (in seconds) for unit tests
defaultAlertLatency = 60

// defaultConfirmationCount is the default confirmation count for unit tests
defaultConfirmationCount = 2
)

// createObserver creates a new observer for testing
func createObserver(t *testing.T, chain chains.Chain, alertLatency int64) *base.Observer {
// constructor parameters
chainParams := *sample.ChainParams(chain.ChainId)
chainParams.ConfirmationCount = defaultConfirmationCount
zetacoreClient := mocks.NewZetacoreClient(t)
tss := mocks.NewTSSMainnet()

Expand Down Expand Up @@ -267,6 +271,51 @@ func TestObserverGetterAndSetter(t *testing.T) {
})
}

func TestIsBlockConfirmed(t *testing.T) {
tests := []struct {
name string
chain chains.Chain
block uint64
lastBlock uint64
confirmed bool
}{
{
name: "should confirm block 100 when confirmation arrives 2",
chain: chains.BitcoinMainnet,
block: 100,
lastBlock: 101, // got 2 confirmations
confirmed: true,
},
{
name: "should not confirm block 100 when confirmation < 2",
chain: chains.BitcoinMainnet,
block: 100,
lastBlock: 100, // got 1 confirmation, need one more
confirmed: false,
},
{
name: "should confirm block 100 when confirmation arrives 2",
chain: chains.Ethereum,
block: 100,
lastBlock: 99, // last block lagging behind, need to wait
confirmed: false,
},
}

// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// create observer
ob := createObserver(t, tt.chain, defaultAlertLatency)
ob = ob.WithLastBlock(tt.lastBlock)

// check if block is confirmed
confirmed := ob.IsBlockConfirmed(tt.block)
require.Equal(t, tt.confirmed, confirmed)
})
}
}

func TestOutboundID(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string,
return "", err
}

// check confirmation
if !ob.IsBlockConfirmed(uint64(blockVb.Height)) {
return "", fmt.Errorf("block %d is not confirmed yet", blockVb.Height)
}

// calculate depositor fee
depositorFee, err := bitcoin.CalcDepositorFee(ob.btcClient, tx, ob.netParams)
if err != nil {
Expand Down

0 comments on commit 08df27a

Please sign in to comment.