From d8e7fb108c0d1e3a1a89abd852fb3e313dea8d20 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 19 Nov 2019 17:08:13 +0000 Subject: [PATCH] removed fee subtraction --- attestation/attestclient.go | 5 --- attestation/attestclient_test.go | 8 ++--- attestation/attestservice.go | 4 +-- attestation/attestservice_test.go | 53 ++++++++++++------------------- 4 files changed, 26 insertions(+), 44 deletions(-) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 229f7ae..08faf49 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -462,11 +462,6 @@ func calcSignedTxFee(feePerByte int, unsignedTxSize int, scriptSize int, numOfSi return int64(feePerByte * calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, numOfInputs)) } -// Calculate feePerByte value for a signed transaction -func calcSignedTxFeePerByte(SignedTxFee int, signedTxSize int) int { - return SignedTxFee / signedTxSize -} - // Given a commitment hash return the corresponding client private key tweaked // This method should only be used in the attestation client signer case // Error handling excluded here as method is only for testing purposes diff --git a/attestation/attestclient_test.go b/attestation/attestclient_test.go index 9d40b71..23f6445 100644 --- a/attestation/attestclient_test.go +++ b/attestation/attestclient_test.go @@ -585,20 +585,20 @@ func TestAttestClient_feeCalculation(t *testing.T) { _, numOfSigs := crypto.ParseRedeemScript(testpkg.Script) assert.Equal(t, 229, calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 1)) assert.Equal(t, int64(2290), calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 1)) - assert.Equal(t, 10, calcSignedTxFeePerByte(int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 1)),calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 1))) + assert.Equal(t, 10, int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 1))/calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 1)) assert.Equal(t, 375, calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 2)) assert.Equal(t, int64(3750), calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 2)) - assert.Equal(t, 10, calcSignedTxFeePerByte(int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 2)),calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 2))) + assert.Equal(t, 10, int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize, numOfSigs, 2))/calcSignedTxSize(unsignedTxSize, scriptSize, numOfSigs, 2)) script2 := "52210325bf82856a8fdcc7a2c08a933343d2c6332c4c252974d6b09b6232ea4080462621028ed149d77203c79d7524048689a80cc98f27e3427f2edaec52eae1f630978e08210254a548b59741ba35bfb085744373a8e10b1cf96e71f53356d7d97f807258d38c53ae" scriptSize2 := len(script2) / 2 _, numOfSigs2 := crypto.ParseRedeemScript(script2) assert.Equal(t, 339, calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 1)) assert.Equal(t, int64(3390), calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 1)) - assert.Equal(t, 10, calcSignedTxFeePerByte(int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 1)), calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 1))) + assert.Equal(t, 10, int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 1))/ calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 1)) assert.Equal(t, 851, calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 3)) assert.Equal(t, int64(8510), calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 3)) - assert.Equal(t, 10, calcSignedTxFeePerByte(int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 3)),calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 3))) + assert.Equal(t, 10, int(calcSignedTxFee(feePerByte, unsignedTxSize, scriptSize2, numOfSigs2, 3))/calcSignedTxSize(unsignedTxSize, scriptSize2, numOfSigs2, 3)) } diff --git a/attestation/attestservice.go b/attestation/attestservice.go index d7157aa..568f50a 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -200,10 +200,8 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { confirmTime = time.Unix(walletTx.Time, 0) //set fee to unconfirmed tx's fee - feePerByte := calcSignedTxFeePerByte(int(walletTx.Fee*float64(Coin)), s.attestation.Tx.SerializeSize()) + feePerByte := int(walletTx.Fee*float64(Coin)) / s.attestation.Tx.SerializeSize() // fee in satoshis / tx size s.attester.Fees.setCurrentFee(feePerByte) - s.attestation.Tx.TxOut[0].Value -= calcSignedTxFee(feePerByte, s.attestation.Tx.SerializeSize(), - len(s.attester.script0)/2, s.attester.numOfSigs, len(s.attestation.Tx.TxIn)) } // part of AStateInit diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index 35e5f16..6701b59 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -9,7 +9,6 @@ import ( "fmt" "testing" "time" - "math" confpkg "mainstay/config" "mainstay/db" @@ -126,14 +125,14 @@ func verifyStateAwaitConfirmationToNextCommitment(t *testing.T, attestService *A assert.Equal(t, txid, attestService.attestation.Txid) assert.Equal(t, true, attestDelay < timeNew) assert.Equal(t, true, attestDelay + ATimeSigs > (timeNew-time.Since(confirmTime))) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) } // verify AStateAwaitConfirmation to AStateHandleUnconfirmed @@ -155,18 +154,6 @@ func verifyStateHandleUnconfirmedToSignAttestation(t *testing.T, attestService * attestService.attester.Fees.GetFee()) } -// Test two AttestationInfo models are equal allowing for Amount precision down -// to 6 decimal places only -func EqualAttestationInfoTest(first models.AttestationInfo,second models.AttestationInfo) bool { - // Truncate Amount to account for fee calulation uncertainty - first.Amount = int64(math.Floor(float64(first.Amount)/math.Pow(10,5))) - second.Amount = int64(math.Floor(float64(second.Amount)/math.Pow(10,5))) - if first == second { - return true - } - return false -} - // Test Attest Service states // Regular test cycle through states // Complete test for multiple signatures @@ -821,7 +808,7 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { assert.Equal(t, prevAttestation.CommitmentHash(), attestService.attestation.CommitmentHash()) assert.Equal(t, prevAttestation.Txid, attestService.attestation.Txid) assert.Equal(t, prevAttestation.Confirmed, attestService.attestation.Confirmed) - assert.True(t, EqualAttestationInfoTest(prevAttestation.Info, attestService.attestation.Info)) + assert.Equal(t, prevAttestation.Info, attestService.attestation.Info) if attestService.attestation.Info.Time == 0 { assert.Equal(t, ATimeFixed, attestDelay) } else { @@ -858,14 +845,14 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { assert.Equal(t, AStateNextCommitment, attestService.state) assert.Equal(t, true, attestService.attestation.Confirmed) assert.Equal(t, txid, attestService.attestation.Txid) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) // failure - re init attestation service from inner state failure attestService.state = AStateInit @@ -876,14 +863,14 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) assert.Equal(t, txid, attestService.attestation.Txid) assert.Equal(t, true, attestService.attestation.Confirmed) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) prevAttestation = attestService.attestation } @@ -931,14 +918,14 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { assert.Equal(t, AStateNextCommitment, attestService.state) assert.Equal(t, true, attestService.attestation.Confirmed) assert.Equal(t, txid, attestService.attestation.Txid) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) // failure - re init attestation service attestService = NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) @@ -948,14 +935,14 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) assert.Equal(t, txid, attestService.attestation.Txid) assert.Equal(t, true, attestService.attestation.Confirmed) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) // failure - re init attestation service from inner state attestService.state = AStateInit @@ -965,14 +952,14 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) assert.Equal(t, txid, attestService.attestation.Txid) assert.Equal(t, true, attestService.attestation.Confirmed) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) } // Test Attest Service states @@ -1001,7 +988,7 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { assert.Equal(t, prevAttestation.CommitmentHash(), attestService.attestation.CommitmentHash()) assert.Equal(t, prevAttestation.Txid, attestService.attestation.Txid) assert.Equal(t, prevAttestation.Confirmed, attestService.attestation.Confirmed) - assert.True(t, EqualAttestationInfoTest(prevAttestation.Info, attestService.attestation.Info)) + assert.Equal(t, prevAttestation.Info, attestService.attestation.Info) if attestService.attestation.Info.Time == 0 { assert.Equal(t, ATimeFixed, attestDelay) } else { @@ -1108,14 +1095,14 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { assert.Equal(t, AStateNextCommitment, attestService.state) assert.Equal(t, true, attestService.attestation.Confirmed) assert.Equal(t, txid, attestService.attestation.Txid) - assert.True(t, EqualAttestationInfoTest( + assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - )) + ) prevAttestation = attestService.attestation } @@ -1161,9 +1148,11 @@ func TestAttestService_FailurePickUpUnconfirmedTxFee(t *testing.T) { // Test AStateInit -> AStateAwaitConfirmation verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) - // Test new fee set to unconfirmed tx's feePerByte value (23) after restart - assert.Equal(t, attestService.attester.Fees.GetFee(), 23) // In AttestFees + // Test new fee set to unconfirmed tx's feePerByte value (~23) after restart + assert.GreaterOrEqual(t, attestService.attester.Fees.GetFee(), 18) // In AttestFees + assert.LessOrEqual(t, attestService.attester.Fees.GetFee(), 28) // In AttestFees _, unconfirmedTxid, _ := attestService.attester.getUnconfirmedTx() tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) - assert.Equal(t, calcSignedTxFeePerByte(int(tx.Fee*Coin), attestService.attestation.Tx.SerializeSize()), 23) // In attestation tx + assert.GreaterOrEqual(t, int(tx.Fee*Coin) / attestService.attestation.Tx.SerializeSize(), 18) // In attestation tx + assert.LessOrEqual(t, int(tx.Fee*Coin) / attestService.attestation.Tx.SerializeSize(), 28) // In attestation tx }