Skip to content

Commit

Permalink
Unit test for ProposeRedemption function
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Jul 4, 2023
1 parent 3e08446 commit 3c3d046
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 2 deletions.
69 changes: 67 additions & 2 deletions pkg/maintainer/wallet/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type LocalChain struct {
pastRedemptionRequestedEvents map[[32]byte][]*tbtc.RedemptionRequestedEvent
averageBlockTime time.Duration
pendingRedemptionRequests map[[32]byte]*tbtc.RedemptionRequest
redemptionProposals []*tbtc.RedemptionProposal
redemptionProposalValidations map[[32]byte]bool
}

func NewLocalChain() *LocalChain {
Expand All @@ -65,6 +67,7 @@ func NewLocalChain() *LocalChain {
walletLocks: make(map[[20]byte]*walletLock),
pastRedemptionRequestedEvents: make(map[[32]byte][]*tbtc.RedemptionRequestedEvent),
pendingRedemptionRequests: make(map[[32]byte]*tbtc.RedemptionRequest),
redemptionProposalValidations: make(map[[32]byte]bool),
}
}

Expand All @@ -75,6 +78,13 @@ func (lc *LocalChain) DepositSweepProposals() []*tbtc.DepositSweepProposal {
return lc.depositSweepProposals
}

func (lc *LocalChain) RedemptionProposals() []*tbtc.RedemptionProposal {
lc.mutex.Lock()
defer lc.mutex.Unlock()

return lc.redemptionProposals
}

func (lc *LocalChain) PastDepositRevealedEvents(
filter *tbtc.DepositRevealedEventFilter,
) ([]*tbtc.DepositRevealedEvent, error) {
Expand Down Expand Up @@ -555,13 +565,68 @@ func (lc *LocalChain) SubmitDepositSweepProposalWithReimbursement(
func (lc *LocalChain) SubmitRedemptionProposalWithReimbursement(
proposal *tbtc.RedemptionProposal,
) error {
panic("unsupported")
lc.mutex.Lock()
defer lc.mutex.Unlock()

lc.redemptionProposals = append(lc.redemptionProposals, proposal)

return nil
}

func (lc *LocalChain) ValidateRedemptionProposal(
proposal *tbtc.RedemptionProposal,
) error {
panic("unsupported")
lc.mutex.Lock()
defer lc.mutex.Unlock()

key, err := buildRedemptionProposalValidationKey(proposal)
if err != nil {
return err
}

result, ok := lc.redemptionProposalValidations[key]
if !ok {
return fmt.Errorf("validation result unknown")
}

if !result {
return fmt.Errorf("validation failed")
}

return nil
}

func (lc *LocalChain) SetRedemptionProposalValidationResult(
proposal *tbtc.RedemptionProposal,
result bool,
) error {
lc.mutex.Lock()
defer lc.mutex.Unlock()

key, err := buildRedemptionProposalValidationKey(proposal)
if err != nil {
return err
}

lc.redemptionProposalValidations[key] = result

return nil
}

func buildRedemptionProposalValidationKey(
proposal *tbtc.RedemptionProposal,
) ([32]byte, error) {
var buffer bytes.Buffer

buffer.Write(proposal.WalletPublicKeyHash[:])

for _, script := range proposal.RedeemersOutputScripts {
buffer.Write(script)
}

buffer.Write(proposal.RedemptionTxFee.Bytes())

return sha256.Sum256(buffer.Bytes()), nil
}

func (lc *LocalChain) GetRedemptionMaxSize() (uint16, error) {
Expand Down
86 changes: 86 additions & 0 deletions pkg/maintainer/wallet/redemptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
walletmtr "github.com/keep-network/keep-core/pkg/maintainer/wallet"
"github.com/keep-network/keep-core/pkg/maintainer/wallet/internal/test"
"github.com/keep-network/keep-core/pkg/tbtc"
"math/big"
"testing"
)

Expand Down Expand Up @@ -137,3 +138,88 @@ func TestFindPendingRedemptions(t *testing.T) {
})
}
}

func TestProposeRedemption(t *testing.T) {
fromHex := func(hexString string) []byte {
bytes, err := hex.DecodeString(hexString)
if err != nil {
t.Fatal(err)
}
return bytes
}

var walletPublicKeyHash [20]byte
copy(walletPublicKeyHash[:], fromHex(""))

redeemersOutputScripts := []bitcoin.Script{
fromHex("00140000000000000000000000000000000000000001"),
fromHex("00140000000000000000000000000000000000000002"),
}

var tests = map[string]struct {
fee int64
expectedProposal *tbtc.RedemptionProposal
}{
"fee provided": {
fee: 10000,
expectedProposal: &tbtc.RedemptionProposal{
WalletPublicKeyHash: walletPublicKeyHash,
RedeemersOutputScripts: redeemersOutputScripts,
RedemptionTxFee: big.NewInt(10000),
},
},
"fee estimated": {
fee: 0, // trigger fee estimation
expectedProposal: &tbtc.RedemptionProposal{
WalletPublicKeyHash: walletPublicKeyHash,
RedeemersOutputScripts: redeemersOutputScripts,
RedemptionTxFee: big.NewInt(4300),
},
},
}

for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
tbtcChain := walletmtr.NewLocalChain()
btcChain := walletmtr.NewLocalBitcoinChain()

btcChain.SetEstimateSatPerVByteFee(1, 25)

for _, script := range redeemersOutputScripts {
tbtcChain.SetPendingRedemptionRequest(
walletPublicKeyHash,
&tbtc.RedemptionRequest{
RedeemerOutputScript: script,
},
)
}

err := tbtcChain.SetRedemptionProposalValidationResult(
test.expectedProposal,
true,
)
if err != nil {
t.Fatal(err)
}

err = walletmtr.ProposeRedemption(
tbtcChain,
btcChain,
walletPublicKeyHash,
test.fee,
redeemersOutputScripts,
false,
)
if err != nil {
t.Fatal(err)
}

if diff := deep.Equal(
tbtcChain.RedemptionProposals(),
[]*tbtc.RedemptionProposal{test.expectedProposal},
); diff != nil {
t.Errorf("invalid deposits: %v", diff)
}
})
}
}

0 comments on commit 3c3d046

Please sign in to comment.