Skip to content

Commit

Permalink
Set wallet data in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jun 11, 2024
1 parent c35ddaf commit 2743e64
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
21 changes: 17 additions & 4 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,6 @@ func buildDepositRequestKey(
return sha256.Sum256(append(fundingTxHash[:], buffer...))
}

func (lc *localChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) {
panic("unsupported")
}

func (lc *localChain) CalculateWalletID(
walletPublicKey *ecdsa.PublicKey,
) ([32]byte, error) {
Expand Down Expand Up @@ -896,6 +892,23 @@ func (lc *localChain) GetWallet(walletPublicKeyHash [20]byte) (
return walletChainData, nil
}

func (lc *localChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) {
lc.walletsMutex.Lock()
defer lc.walletsMutex.Unlock()

for _, walletData := range lc.wallets {
if EcdsaWalletID == walletData.EcdsaWalletID {
if walletData.State == StateClosed ||
walletData.State == StateTerminated {
return false, nil
}
return true, nil
}
}

return false, fmt.Errorf("wallet not found")
}

func (lc *localChain) setWallet(
walletPublicKeyHash [20]byte,
walletChainData *WalletChainData,
Expand Down
10 changes: 7 additions & 3 deletions pkg/tbtc/inactivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ func setupInactivityClaimExecutorScenario(t *testing.T) (
keyStorePersistence := createMockKeyStorePersistence(t, signers...)

walletPublicKeyHash := bitcoin.PublicKeyHash(signers[0].wallet.publicKey)
ecdsaWalletID := [32]byte{1, 2, 3}
walletID, err := localChain.CalculateWalletID(signers[0].wallet.publicKey)
if err != nil {
t.Fatal(err)
}

localChain.setWallet(
walletPublicKeyHash,
&WalletChainData{
EcdsaWalletID: ecdsaWalletID,
EcdsaWalletID: walletID,
State: StateLive,
},
)

Expand Down Expand Up @@ -191,7 +195,7 @@ func setupInactivityClaimExecutorScenario(t *testing.T) (
t.Fatal("node is supposed to control wallet signers")
}

return executor, ecdsaWalletID, localChain
return executor, walletID, localChain
}

func TestSignClaim_SigningSuccessful(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/tbtc/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/keep-network/keep-common/pkg/persistence"
"github.com/keep-network/keep-core/internal/testutils"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/generator"
"github.com/keep-network/keep-core/pkg/internal/tecdsatest"
Expand All @@ -31,6 +32,20 @@ func TestNode_GetSigningExecutor(t *testing.T) {

signer := createMockSigner(t)

walletPublicKeyHash := bitcoin.PublicKeyHash(signer.wallet.publicKey)
walletID, err := localChain.CalculateWalletID(signer.wallet.publicKey)
if err != nil {
t.Fatal(err)
}

localChain.setWallet(
walletPublicKeyHash,
&WalletChainData{
EcdsaWalletID: walletID,
State: StateLive,
},
)

// Populate the mock keystore with the mock signer's data. This is
// required to make the node controlling the signer's wallet.
keyStorePersistence := createMockKeyStorePersistence(t, signer)
Expand Down Expand Up @@ -149,6 +164,20 @@ func TestNode_GetCoordinationExecutor(t *testing.T) {

signer := createMockSigner(t)

walletPublicKeyHash := bitcoin.PublicKeyHash(signer.wallet.publicKey)
walletID, err := localChain.CalculateWalletID(signer.wallet.publicKey)
if err != nil {
t.Fatal(err)
}

localChain.setWallet(
walletPublicKeyHash,
&WalletChainData{
EcdsaWalletID: walletID,
State: StateLive,
},
)

// Populate the mock keystore with the mock signer's data. This is
// required to make the node controlling the signer's wallet.
keyStorePersistence := createMockKeyStorePersistence(t, signer)
Expand Down Expand Up @@ -272,6 +301,20 @@ func TestNode_RunCoordinationLayer(t *testing.T) {

signer := createMockSigner(t)

walletPublicKeyHash := bitcoin.PublicKeyHash(signer.wallet.publicKey)
walletID, err := localChain.CalculateWalletID(signer.wallet.publicKey)
if err != nil {
t.Fatal(err)
}

localChain.setWallet(
walletPublicKeyHash,
&WalletChainData{
EcdsaWalletID: walletID,
State: StateLive,
},
)

// Populate the mock keystore with the mock signer's data. This is
// required to make the node controlling the signer's wallet.
keyStorePersistence := createMockKeyStorePersistence(t, signer)
Expand Down
15 changes: 15 additions & 0 deletions pkg/tbtc/signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/keep-network/keep-core/internal/testutils"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/chain/local_v1"
"github.com/keep-network/keep-core/pkg/generator"
Expand Down Expand Up @@ -159,6 +160,20 @@ func setupSigningExecutor(t *testing.T) *signingExecutor {
}
}

walletPublicKeyHash := bitcoin.PublicKeyHash(signers[0].wallet.publicKey)
walletID, err := localChain.CalculateWalletID(signers[0].wallet.publicKey)
if err != nil {
t.Fatal(err)
}

localChain.setWallet(
walletPublicKeyHash,
&WalletChainData{
EcdsaWalletID: walletID,
State: StateLive,
},
)

keyStorePersistence := createMockKeyStorePersistence(t, signers...)

node, err := newNode(
Expand Down

0 comments on commit 2743e64

Please sign in to comment.