Skip to content

Commit

Permalink
add redeem coin zero value check and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Jan 11, 2022
1 parent d0dc012 commit d597dc5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 4 additions & 0 deletions server/asset/eth/coiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (eth *Backend) newRedeemCoin(coinID []byte, contractData []byte) (*redeemCo
return nil, err
}

if bc.value != 0 {
return nil, fmt.Errorf("expected tx value of zero for redeem but got: %d", bc.value)
}

redemptions, err := dexeth.ParseRedeemData(bc.txData, ethContractVersion)
if err != nil {
return nil, fmt.Errorf("unable to parse redemption call data: %v", err)
Expand Down
15 changes: 5 additions & 10 deletions server/asset/eth/coiner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,33 @@ func TestNewRedeemCoin(t *testing.T) {
copy(txHash[:], encode.RandomBytes(32))
copy(secret[:], redeemSecretB)
copy(secretHash[:], redeemSecretHashB)
txCoinIDBytes := txHash[:]
contract := dexeth.EncodeContractData(0, secretHash)
const gasPrice = 30
const value = 5e9
const wantGas = 30
tests := []struct {
name string
coinID []byte
contract []byte
tx *types.Transaction
swpErr, txErr error
wantErr bool
}{{
name: "ok redeem",
tx: tTx(gasPrice, 0, contractAddr, redeemCalldata),
coinID: txCoinIDBytes,
contract: dexeth.EncodeContractData(0, secretHash),
contract: contract,
}, {
name: "non zero value with redeem",
tx: tTx(gasPrice, value, contractAddr, redeemCalldata),
coinID: txCoinIDBytes,
contract: redeemSecretHashB,
contract: contract,
wantErr: true,
}, {
name: "unable to decode redeem data, must be redeem for redeem coin type",
tx: tTx(gasPrice, 0, contractAddr, initCalldata),
coinID: txCoinIDBytes,
contract: redeemSecretHashB,
contract: contract,
wantErr: true,
}, {
name: "tx coin id for redeem - contract not in tx",
tx: tTx(gasPrice, value, contractAddr, redeemCalldata),
coinID: txCoinIDBytes,
contract: encode.RandomBytes(32),
wantErr: true,
}}
Expand All @@ -76,7 +71,7 @@ func TestNewRedeemCoin(t *testing.T) {
contractAddr: *contractAddr,
initTxSize: uint32(dexeth.InitGas(1, 0)),
}
rc, err := eth.newRedeemCoin(test.coinID, test.contract)
rc, err := eth.newRedeemCoin(txHash[:], test.contract)
if test.wantErr {
if err == nil {
t.Fatalf("expected error for test %q", test.name)
Expand Down

0 comments on commit d597dc5

Please sign in to comment.