Skip to content

Commit

Permalink
removed fee subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
86667 committed Nov 19, 2019
1 parent a0d0282 commit d8e7fb1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 44 deletions.
5 changes: 0 additions & 5 deletions attestation/attestclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions attestation/attestclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 1 addition & 3 deletions attestation/attestservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 21 additions & 32 deletions attestation/attestservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"testing"
"time"
"math"

confpkg "mainstay/config"
"mainstay/db"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit d8e7fb1

Please sign in to comment.