diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 08faf49..8fc5517 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -7,8 +7,8 @@ package attestation import ( "encoding/hex" "errors" - "math" "fmt" + "math" confpkg "mainstay/config" "mainstay/crypto" @@ -43,7 +43,7 @@ const ( ErrorInputMissingForTx = `Missing input for transaction` ErrorInvalidChaincode = `Invalid chaincode provided` ErrorMissingChaincodes = `Missing chaincodes for pubkeys` - ErrorTopUpScriptNumSigs = `Different number of signatures in Init script to top-up script` + ErrorTopUpScriptNumSigs = `Different number of signatures in Init script to top-up script` ) // coin in satoshis diff --git a/attestation/attestclient_test.go b/attestation/attestclient_test.go index 23f6445..05151af 100644 --- a/attestation/attestclient_test.go +++ b/attestation/attestclient_test.go @@ -596,7 +596,7 @@ func TestAttestClient_feeCalculation(t *testing.T) { _, 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, 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)) diff --git a/attestation/attestfees_test.go b/attestation/attestfees_test.go index e672c47..b069fc1 100644 --- a/attestation/attestfees_test.go +++ b/attestation/attestfees_test.go @@ -47,9 +47,9 @@ func TestAttestFees(t *testing.T) { // test setCurrentFee() new_current := 7 - assert.NotEqual(t,new_current, attestFees.currentFee) + assert.NotEqual(t, new_current, attestFees.currentFee) attestFees.setCurrentFee(new_current) - assert.Equal(t,new_current, attestFees.currentFee) + assert.Equal(t, new_current, attestFees.currentFee) } // Attest Fees test with custom feesConfig diff --git a/attestation/attestservice.go b/attestation/attestservice.go index 568f50a..fb5e0bf 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -12,8 +12,8 @@ import ( "time" confpkg "mainstay/config" - "mainstay/models" "mainstay/log" + "mainstay/models" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -196,7 +196,11 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { s.signer.SendConfirmedHash((&lastCommitmentHash).CloneBytes()) // update clients s.state = AStateAwaitConfirmation // update attestation state - walletTx, _ := s.config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) + walletTx, getTxError := s.config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) + if s.setFailure(getTxError) { + log.Info("********** failed to find unconfirmed transaction in mempool, re-initialising attestation") + return // will rebound to init + } confirmTime = time.Unix(walletTx.Time, 0) //set fee to unconfirmed tx's fee @@ -530,7 +534,7 @@ func (s *AttestService) doStateAwaitConfirmation() { confirmedHash := s.attestation.CommitmentHash() s.signer.SendConfirmedHash((&confirmedHash).CloneBytes()) // update clients - s.state = AStateNextCommitment // update attestation state + s.state = AStateNextCommitment // update attestation state // add new attestation waiting time with confimation time and signature // waiting time subtracted so that attestations are ~1 hour apart attestDelay = atimeNewAttestation - time.Since(confirmTime) - ATimeSigs diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index 6701b59..7f63c8f 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -124,7 +124,7 @@ func verifyStateAwaitConfirmationToNextCommitment(t *testing.T, attestService *A assert.Equal(t, true, attestService.attestation.Confirmed) assert.Equal(t, txid, attestService.attestation.Txid) assert.Equal(t, true, attestDelay < timeNew) - assert.Equal(t, true, attestDelay + ATimeSigs > (timeNew-time.Since(confirmTime))) + assert.Equal(t, true, attestDelay+ATimeSigs > (timeNew-time.Since(confirmTime))) assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), @@ -132,7 +132,7 @@ func verifyStateAwaitConfirmationToNextCommitment(t *testing.T, attestService *A Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) } // verify AStateAwaitConfirmation to AStateHandleUnconfirmed @@ -797,9 +797,14 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { server := NewAttestServer(dbFake) prevAttestation := models.NewAttestationDefault() - for i := range []int{1,2,3} { + for i := range []int{1, 2, 3} { attestService := NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) + // manually set fee to test pick up of unconfirmedtx fee after restart + if i == 0 { + attestService.attester.Fees.setCurrentFee(23) + } + // Test initial state of attest service verifyStateInit(t, attestService) // Test AStateInit -> AStateNextCommitment @@ -836,6 +841,16 @@ func TestAttestService_FailureSendAttestation(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 + if i == 0 { + assert.GreaterOrEqual(t, attestService.attester.Fees.GetFee(), 22) // In AttestFees + assert.LessOrEqual(t, attestService.attester.Fees.GetFee(), 24) // In AttestFees + _, unconfirmedTxid, _ := attestService.attester.getUnconfirmedTx() + tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) + assert.GreaterOrEqual(t, int(tx.Fee*Coin)/attestService.attestation.Tx.SerializeSize(), 22) // In attestation tx + assert.LessOrEqual(t, int(tx.Fee*Coin)/attestService.attestation.Tx.SerializeSize(), 24) + } + // generate new block to confirm attestation config.MainClient().Generate(1) rawTx, _ := config.MainClient().GetRawTransaction(&txid) @@ -852,7 +867,7 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) // failure - re init attestation service from inner state failure attestService.state = AStateInit @@ -870,7 +885,7 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) prevAttestation = attestService.attestation } @@ -925,7 +940,7 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { 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) @@ -942,7 +957,7 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) // failure - re init attestation service from inner state attestService.state = AStateInit @@ -959,7 +974,7 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) } // Test Attest Service states @@ -1102,57 +1117,8 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { Amount: rawTx.MsgTx().TxOut[0].Value, Time: walletTx.Time}, attestService.attestation.Info, - ) + ) prevAttestation = attestService.attestation } } - -// Test Attest Service failre -// Test unconfirmed tx fee picked up after failure -func TestAttestService_FailurePickUpUnconfirmedTxFee(t *testing.T) { - // Test INIT - test := test.NewTest(false, false) - config := test.Config - - dbFake := db.NewDbFake() - server := NewAttestServer(dbFake) - - attestService := NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) - - // Manually set fee - attestService.attester.Fees.setCurrentFee(23) - - // Test initial state of attest service - verifyStateInit(t, attestService) - // Test AStateInit -> AStateNextCommitment - attestService.doAttestation() - - // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creating new attestation - hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b3") - latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) - - // Test AStateNewAttestation -> AStateSignAttestation - verifyStateNewAttestationToSignAttestation(t, attestService) - // Test AStateSignAttestation -> AStatePreSendStore - verifyStateSignAttestationToPreSendStore(t, attestService) - // Test AStatePreSendStore -> AStateSendAttestation - verifyStatePreSendStoreToSendAttestation(t, attestService) - // Test AStateSendAttestation -> AStateAwaitConfirmation - txid := verifyStateSendAttestationToAwaitConfirmation(t, attestService) - - // failure - re init attestation service - attestService = NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) - - // Test AStateInit -> AStateAwaitConfirmation - verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) - - // 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.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 -} diff --git a/attestation/attestsigner_zmq.go b/attestation/attestsigner_zmq.go index 9b725a5..e8ea3df 100644 --- a/attestation/attestsigner_zmq.go +++ b/attestation/attestsigner_zmq.go @@ -9,8 +9,8 @@ import ( confpkg "mainstay/config" "mainstay/crypto" - "mainstay/messengers" "mainstay/log" + "mainstay/messengers" zmq "github.com/pebbe/zmq4" ) diff --git a/cmd/clientsignuptool/clientsignuptool.go b/cmd/clientsignuptool/clientsignuptool.go index c9f502d..47c76dd 100644 --- a/cmd/clientsignuptool/clientsignuptool.go +++ b/cmd/clientsignuptool/clientsignuptool.go @@ -10,13 +10,13 @@ import ( "bufio" "context" "encoding/hex" - "os" "fmt" + "os" "mainstay/config" "mainstay/db" - "mainstay/models" "mainstay/log" + "mainstay/models" "github.com/btcsuite/btcd/btcec" "github.com/satori/go.uuid" diff --git a/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go b/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go index 1870f21..db833bb 100644 --- a/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go +++ b/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go @@ -7,8 +7,8 @@ package main // Token generator import ( - "mainstay/log" "github.com/satori/go.uuid" + "mainstay/log" ) // main diff --git a/cmd/confirmationtool/confirmationtool.go b/cmd/confirmationtool/confirmationtool.go index 3e388d9..6b16579 100644 --- a/cmd/confirmationtool/confirmationtool.go +++ b/cmd/confirmationtool/confirmationtool.go @@ -13,8 +13,8 @@ import ( "mainstay/clients" "mainstay/config" - "mainstay/staychain" "mainstay/log" + "mainstay/staychain" "github.com/btcsuite/btcd/chaincfg/chainhash" ) diff --git a/cmd/txsigningtool/txsigningtool.go b/cmd/txsigningtool/txsigningtool.go index 9f3c648..ad70e0b 100644 --- a/cmd/txsigningtool/txsigningtool.go +++ b/cmd/txsigningtool/txsigningtool.go @@ -16,9 +16,9 @@ import ( "mainstay/attestation" confpkg "mainstay/config" _ "mainstay/crypto" + "mainstay/log" "mainstay/messengers" "mainstay/test" - "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg/chainhash" diff --git a/db/db_mongo.go b/db/db_mongo.go index dc6f1f0..1a8d02c 100644 --- a/db/db_mongo.go +++ b/db/db_mongo.go @@ -10,8 +10,8 @@ import ( "fmt" "mainstay/config" - "mainstay/models" "mainstay/log" + "mainstay/models" "github.com/btcsuite/btcd/chaincfg/chainhash" "go.mongodb.org/mongo-driver/mongo" diff --git a/log/log.go b/log/log.go index 81d3684..bc799bc 100644 --- a/log/log.go +++ b/log/log.go @@ -5,9 +5,9 @@ package log import ( - "log" - "os" - "fmt" + "fmt" + "log" + "os" ) /* @@ -26,30 +26,34 @@ var errorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfi // Info log entries print a message only, use for standard message output // standard INFO print func Info(v ...interface{}) { - infoLogger.Output(2, fmt.Sprint(v...)) + infoLogger.Output(2, fmt.Sprint(v...)) } + // INFO print with formatting func Infof(format string, v ...interface{}) { - infoLogger.Output(2, fmt.Sprintf(format, v...)) + infoLogger.Output(2, fmt.Sprintf(format, v...)) } + // standard INFO print with new line func Infoln(v ...interface{}) { - infoLogger.Output(2, fmt.Sprintln(v...)) + infoLogger.Output(2, fmt.Sprintln(v...)) } // Warn log entries print a message only but wiht WARN marker, use for non-fatal // errors // standard WARN print func Warn(v ...interface{}) { - warnLogger.Output(2, fmt.Sprint(v...)) + warnLogger.Output(2, fmt.Sprint(v...)) } + // WARN print with formatting func Warnf(format string, v ...interface{}) { - warnLogger.Output(2, fmt.Sprintf(format, v...)) + warnLogger.Output(2, fmt.Sprintf(format, v...)) } + // standard WARN print with new line func Warnln(v ...interface{}) { - warnLogger.Output(2, fmt.Sprintln(v...)) + warnLogger.Output(2, fmt.Sprintln(v...)) } // Error log entries print a message then halt execution @@ -58,8 +62,9 @@ func Error(v ...interface{}) { errorLogger.Output(2, fmt.Sprint(v...)) os.Exit(1) } + // ERROR print with formatting func Errorf(format string, v ...interface{}) { - errorLogger.Output(2, fmt.Sprintf(format, v...)) - os.Exit(1) + errorLogger.Output(2, fmt.Sprintf(format, v...)) + os.Exit(1) } diff --git a/main.go b/main.go index 622276f..ecfc292 100644 --- a/main.go +++ b/main.go @@ -16,8 +16,8 @@ import ( "mainstay/attestation" "mainstay/config" "mainstay/db" - "mainstay/test" "mainstay/log" + "mainstay/test" ) var ( diff --git a/models/commitmentmerkletree.go b/models/commitmentmerkletree.go index 062e456..af903eb 100644 --- a/models/commitmentmerkletree.go +++ b/models/commitmentmerkletree.go @@ -6,8 +6,8 @@ package models import ( _ "errors" - "math" "fmt" + "math" "mainstay/log"