From bbbcb5628c66c185861987a8efb6acc1bffba945 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 5 Nov 2019 13:03:40 +0000 Subject: [PATCH 01/14] update doc and build.sh --- README.md | 3 +-- scripts/build.sh | 12 +++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6c4f1ca..aa98f14 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Mainstay -The mainstay repository is an application that implements the [Mainstay protocol](https://www.commerceblock.com/wp-content/uploads/2018/03/commerceblock-mainstay-whitepaper.pdf) designed by [CommerceBlock](https://www.commerceblock.com). It consists of a Go daemon that performs attestations of the [Ocean](https://github.com/commerceblock/ocean) network along with client commitments to Bitcoin in the form of a commitment merkle tree. +The mainstay repository is an application that implements the [Mainstay protocol](https://cloudflare-ipfs.com/ipns/ipfs.commerceblock.com/commerceblock-whitepaper-mainstay.pdf) designed by [CommerceBlock](https://www.commerceblock.com). It consists of a Go daemon that performs attestations of the [Ocean](https://github.com/commerceblock/ocean) network along with client commitments to Bitcoin in the form of a commitment merkle tree. Mainstay is accompanied by a Confirmation tool that can be run in parallel with the Bitcoin network to confirm attestations and prove the commitment inclusion in Mainstay attestations. @@ -75,4 +75,3 @@ The multisig tool `cmd/multisigtool` can be used to generate multisig scripts an For more information go to [tool guidelines](/cmd/README.md). For example use cases go to [docs](/doc/). - diff --git a/scripts/build.sh b/scripts/build.sh index 19c54ca..d1cd2c2 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -3,13 +3,23 @@ brew install go go env GOROOT GOPATH +# Add GOPATH to env variables +export GOPATH=`go env GOPATH` + mkdir $GOPATH/src mkdir $GOPATH/bin +# Add GOBIN to env variables export GOBIN=$GOPATH/bin git clone https://github.com/commerceblock/mainstay $GOPATH/src/mainstay cd $GOPATH/src/mainstay +# Add bin to $PATH +PATH="$GOPATH/bin:$PATH" + +# Download and install dependencies +go get + +# Compile packages and dependencies go build -go install From 4f6967e03bb6dd5151230d8f9551cd3819521136 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Thu, 7 Nov 2019 17:02:41 +0000 Subject: [PATCH 02/14] issue 111 --- attestation/attestclient.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 3425953..0e9071b 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -43,6 +43,7 @@ const ( ErrorInputMissingForTx = `Missing input for transaction` ErrorInvalidChaincode = `Invalid chaincode provided` ErrorMissingChaincodes = `Missing chaincodes for pubkeys` + ErrorTopUpScriptNumSigs = `Top up script has invalid number of signatures` ) // coin in satoshis @@ -155,6 +156,11 @@ func newMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcutil multisig := config.InitScript() pubkeys, numOfSigs := crypto.ParseRedeemScript(multisig) + // Verify same amount of sigs in topupscript and init script + if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); topUpnumOfSigs == topUpnumOfSigs { + log.Fatal(ErrorTopUpScriptNumSigs) + } + // get chaincodes of pubkeys from config chaincodesStr := config.InitChaincodes() if len(chaincodesStr) != len(pubkeys) { From 362f31cb0a98f430da0076bf5d6054d1525adc14 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Fri, 8 Nov 2019 16:35:38 +0000 Subject: [PATCH 03/14] log INFO, WARN, ERROR functionality --- attestation/attestclient.go | 34 +++--- attestation/attestfees.go | 32 +++--- attestation/attestservice.go | 70 ++++++------ attestation/attestsigner_fake.go | 7 +- attestation/attestsigner_zmq.go | 4 +- cmd/clientsignuptool/clientsignuptool.go | 90 +++++++-------- .../tokengenerator/tokengeneratortool.go | 14 +-- cmd/commitmenttool/commitmenttool.go | 108 ++++++++++-------- cmd/confirmationtool/confirmationtool.go | 38 +++--- .../keyextraction/keyextractiontool.go | 8 +- cmd/multisigtool/multisigtool.go | 42 +++---- cmd/txsigningtool/txsigningtool.go | 26 ++--- config/conf.json | 52 +++++---- config/config.go | 6 +- crypto/scripts.go | 11 +- db/db_mongo.go | 8 +- log/doc.go | 5 + log/log.go | 56 +++++++++ main.go | 10 +- models/commitmentmerkleproof.go | 16 +-- models/commitmentmerkletree.go | 9 +- test/test.go | 22 ++-- 22 files changed, 372 insertions(+), 296 deletions(-) create mode 100644 log/doc.go create mode 100644 log/log.go diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 0e9071b..1fae4df 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -7,12 +7,12 @@ package attestation import ( "encoding/hex" "errors" - "fmt" - "log" "math" + "fmt" confpkg "mainstay/config" "mainstay/crypto" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcjson" @@ -43,7 +43,7 @@ const ( ErrorInputMissingForTx = `Missing input for transaction` ErrorInvalidChaincode = `Invalid chaincode provided` ErrorMissingChaincodes = `Missing chaincodes for pubkeys` - ErrorTopUpScriptNumSigs = `Top up script has invalid number of signatures` + ErrorTopUpScriptNumSigs = `Different number of signatures in Init script to top-up script` ) // coin in satoshis @@ -104,11 +104,11 @@ func parseTopupKeys(config *confpkg.Config, isSigner bool) *btcutil.WIF { if pkTopup != "" { topupWif, errPkWifTopup := crypto.GetWalletPrivKey(pkTopup) if errPkWifTopup != nil { - log.Fatalf("%s %s\n%v\n", ErrorInvalidPk, pkTopup, errPkWifTopup) + log.Errorf("%s %s\n%v\n", ErrorInvalidPk, pkTopup, errPkWifTopup) } return topupWif } else { - log.Println(WarningTopupPkMissing) + log.Infoln(WarningTopupPkMissing) } } return nil @@ -121,11 +121,11 @@ func parseMainKeys(config *confpkg.Config, isSigner bool) *btcutil.WIF { pk := config.InitPK() pkWif, errPkWif := crypto.GetWalletPrivKey(pk) if errPkWif != nil { - log.Fatalf("%s %s\n%v\n", ErrorInvalidPk, pk, errPkWif) + log.Errorf("%s %s\n%v\n", ErrorInvalidPk, pk, errPkWif) } return pkWif } else if config.InitScript() == "" { - log.Fatal(ErrorMissingMultisig) + log.Error(ErrorMissingMultisig) } return nil } @@ -157,20 +157,20 @@ func newMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcutil pubkeys, numOfSigs := crypto.ParseRedeemScript(multisig) // Verify same amount of sigs in topupscript and init script - if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); topUpnumOfSigs == topUpnumOfSigs { - log.Fatal(ErrorTopUpScriptNumSigs) + if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); topUpnumOfSigs != topUpnumOfSigs { + log.Errorf(fmt.Sprintf("%s. %d != %d", ErrorTopUpScriptNumSigs, numOfSigs, topUpnumOfSigs)) } // get chaincodes of pubkeys from config chaincodesStr := config.InitChaincodes() if len(chaincodesStr) != len(pubkeys) { - log.Fatal(fmt.Sprintf("%s %d != %d", ErrorMissingChaincodes, len(chaincodesStr), len(pubkeys))) + log.Error(fmt.Sprintf("%s %d != %d", ErrorMissingChaincodes, len(chaincodesStr), len(pubkeys))) } chaincodes := make([][]byte, len(pubkeys)) for i_c := range chaincodesStr { ccBytes, ccBytesErr := hex.DecodeString(chaincodesStr[i_c]) if ccBytesErr != nil || len(ccBytes) != 32 { - log.Fatal(fmt.Sprintf("%s %s", ErrorInvalidChaincode, chaincodesStr[i_c])) + log.Error(fmt.Sprintf("%s %s", ErrorInvalidChaincode, chaincodesStr[i_c])) } chaincodes[i_c] = append(chaincodes[i_c], ccBytes...) } @@ -186,7 +186,7 @@ func newMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcutil } } if !myFound { - log.Fatal(ErrorMissingAddress) + log.Error(ErrorMissingAddress) } } @@ -235,14 +235,14 @@ func NewAttestClient(config *confpkg.Config, signerFlag ...bool) *AttestClient { topupScriptStr := config.TopupScript() var pkWifTopup *btcutil.WIF if topupAddrStr != "" && topupScriptStr != "" { - log.Printf("*Client* importing top-up addr: %s ...\n", topupAddrStr) + log.Infof("*Client* importing top-up addr: %s ...\n", topupAddrStr) importErr := config.MainClient().ImportAddressRescan(topupAddrStr, "", false) if importErr != nil { - log.Printf("%s (%s)\n%v\n", WarningFailureImportingTopupAddress, topupAddrStr, importErr) + log.Infof("%s (%s)\n%v\n", WarningFailureImportingTopupAddress, topupAddrStr, importErr) } pkWifTopup = parseTopupKeys(config, isSigner) } else { - log.Println(WarningTopupInfoMissing) + log.Infoln(WarningTopupInfoMissing) } // main config @@ -403,7 +403,7 @@ func (w *AttestClient) createAttestation(paytoaddr btcutil.Address, unspent []bt // print warning if txout value less than 100*maxfee target if msgTx.TxOut[0].Value < 100*maxFee { - log.Println(WarningInsufficientFunds) + log.Infoln(WarningInsufficientFunds) } // add fees using best fee-per-byte estimate @@ -527,7 +527,7 @@ func (w *AttestClient) getTransactionPreImages(hash chainhash.Hash, msgTx *wire. if len(msgTx.TxIn) > 1 { topupScriptSer, topupDecodeErr := hex.DecodeString(w.scriptTopup) if topupDecodeErr != nil { - log.Printf("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) + log.Infof("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) return preImageTxs, nil } for i := 1; i < len(msgTx.TxIn); i++ { diff --git a/attestation/attestfees.go b/attestation/attestfees.go index 2fe681b..0e8b1ab 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -6,10 +6,10 @@ package attestation import ( "encoding/json" - "log" "net/http" "mainstay/config" + "mainstay/log" ) // Utility functions to get best bitcoin fees from a remote API @@ -69,27 +69,27 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { if feesConfig.MinFee > 0 && feesConfig.MinFee < DefaultMaxFee { minFee = feesConfig.MinFee } else { - log.Printf("%s (%d)\n", WarningInvalidMinFeeArg, feesConfig.MinFee) + log.Infof("%s (%d)\n", WarningInvalidMinFeeArg, feesConfig.MinFee) } - log.Printf("*Fees* Min fee set to: %d\n", minFee) + log.Infof("*Fees* Min fee set to: %d\n", minFee) - // max fee with lower limit min_fee && 0 and max fee default + // Infox fee with lower limit min_fee && 0 and max fee default maxFee := DefaultMaxFee if feesConfig.MaxFee > 0 && feesConfig.MaxFee > minFee && feesConfig.MaxFee < DefaultMaxFee { maxFee = feesConfig.MaxFee } else { - log.Printf("%s (%d)\n", WarningInvalidMaxFeeArg, feesConfig.MaxFee) + log.Infof("%s (%d)\n", WarningInvalidMaxFeeArg, feesConfig.MaxFee) } - log.Printf("*Fees* Max fee set to: %d\n", maxFee) + log.Infof("*Fees* Max fee set to: %d\n", maxFee) // fee increment with lower limit 0 feeIncrement := DefaultFeeIncrement if feesConfig.FeeIncrement > 0 { feeIncrement = feesConfig.FeeIncrement } else { - log.Printf("%s (%d)\n", WarningInvalidFeeIncrementArg, feesConfig.FeeIncrement) + log.Infof("%s (%d)\n", WarningInvalidFeeIncrementArg, feesConfig.FeeIncrement) } - log.Printf("*Fees* Fee increment set to: %d\n", feeIncrement) + log.Infof("*Fees* Fee increment set to: %d\n", feeIncrement) attestFees := AttestFees{ minFee: minFee, @@ -103,13 +103,13 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { // Get current fee func (a AttestFees) GetFee() int { - log.Printf("*Fees* Current fee value: %d\n", a.currentFee) + log.Infof("*Fees* Current fee value: %d\n", a.currentFee) return a.currentFee } // Get previous fee func (a AttestFees) GetPrevFee() int { - log.Printf("*Fees* Previous fee value: %d\n", a.prevFee) + log.Infof("*Fees* Previous fee value: %d\n", a.prevFee) return a.prevFee } @@ -129,16 +129,16 @@ func (a *AttestFees) ResetFee(useMinimum ...bool) { } a.currentFee = fee a.prevFee = 0 - log.Printf("*Fees* Current fee set to value: %d\n", a.currentFee) + log.Infof("*Fees* Current fee set to value: %d\n", a.currentFee) } // Bump fee upon request using increment value and not allowing values higher than max configured fee func (a *AttestFees) BumpFee() { a.prevFee = a.currentFee a.currentFee += a.feeIncrement - log.Printf("*Fees* Bumping fee value to: %d\n", a.currentFee) + log.Infof("*Fees* Bumping fee value to: %d\n", a.currentFee) if a.currentFee > a.maxFee { - log.Printf("*Fees* Max allowed fee value reached: %d\n", a.currentFee) + log.Infof("*Fees* Max allowed fee value reached: %d\n", a.currentFee) a.currentFee = a.maxFee } } @@ -158,7 +158,7 @@ func getBestFee(customFeeType ...string) int { func getFeeFromAPI(feeType string) int { resp, getErr := http.Get(FeeApiUrl) if getErr != nil { - log.Println("*Fees* API request failed") + log.Infoln("*Fees* API request failed") return -1 } @@ -167,13 +167,13 @@ func getFeeFromAPI(feeType string) int { var respJson map[string]float64 decErr := dec.Decode(&respJson) if decErr != nil { - log.Println("*Fees* API response decoding failed") + log.Infoln("*Fees* API response decoding failed") return -1 } fee, ok := respJson[feeType] if !ok { - log.Println("*Fees* API response incorrect format") + log.Infoln("*Fees* API response incorrect format") return -1 } diff --git a/attestation/attestservice.go b/attestation/attestservice.go index a0f922f..625f2d9 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -8,12 +8,12 @@ import ( "bytes" "context" "errors" - "log" "sync" "time" confpkg "mainstay/config" "mainstay/models" + "mainstay/log" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -115,7 +115,7 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer // Check init txid validity _, errInitTx := chainhash.NewHashFromStr(config.InitTx()) if errInitTx != nil { - log.Fatalf("Incorrect initial transaction id %s\n", config.InitTx()) + log.Errorf("Incorrect initial transaction id %s\n", config.InitTx()) } // initiate attestation client @@ -127,16 +127,16 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer if config.TimingConfig().NewAttestationMinutes > 0 { atimeNewAttestation = time.Duration(config.TimingConfig().NewAttestationMinutes) * time.Minute } else { - log.Printf("%s (%v)\n", WarningInvalidATimeNewAttestationArg, config.TimingConfig().NewAttestationMinutes) + log.Infof("%s (%v)\n", WarningInvalidATimeNewAttestationArg, config.TimingConfig().NewAttestationMinutes) } - log.Printf("Time new attestation set to: %v\n", atimeNewAttestation) + log.Infof("Time new attestation set to: %v\n", atimeNewAttestation) atimeHandleUnconfirmed = DefaultATimeHandleUnconfirmed if config.TimingConfig().HandleUnconfirmedMinutes > 0 { atimeHandleUnconfirmed = time.Duration(config.TimingConfig().HandleUnconfirmedMinutes) * time.Minute } else { - log.Printf("%s (%v)\n", WarningInvalidATimeHandleUnconfirmedArg, config.TimingConfig().HandleUnconfirmedMinutes) + log.Infof("%s (%v)\n", WarningInvalidATimeHandleUnconfirmedArg, config.TimingConfig().HandleUnconfirmedMinutes) } - log.Printf("Time handle unconfirmed set to: %v\n", atimeHandleUnconfirmed) + log.Infof("Time handle unconfirmed set to: %v\n", atimeHandleUnconfirmed) return &AttestService{ctx, wg, config, attester, server, signer, AStateInit, models.NewAttestationDefault(), nil, config.Regtest()} } @@ -151,7 +151,7 @@ func (s *AttestService) Run() { timer := time.NewTimer(attestDelay) select { case <-s.ctx.Done(): - log.Println("Shutting down Attestation Service...") + log.Infoln("Shutting down Attestation Service...") return case <-timer.C: // do next attestation state @@ -162,7 +162,7 @@ func (s *AttestService) Run() { attestDelay = 5 * time.Second } - log.Printf("********** sleeping for: %s ...\n", attestDelay.String()) + log.Infof("********** sleeping for: %s ...\n", attestDelay.String()) } } } @@ -170,8 +170,8 @@ func (s *AttestService) Run() { // AStateError // - Print error state and re-initiate attestation func (s *AttestService) doStateError() { - log.Println("*AttestService* ATTESTATION SERVICE FAILURE") - log.Println(s.errorState) + log.Infoln("*AttestService* ATTESTATION SERVICE FAILURE") + log.Infoln(s.errorState) s.state = AStateInit // update attestation state } @@ -183,7 +183,7 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { if s.setFailure(commitmentErr) { return // will rebound to init } - log.Printf("********** found unconfirmed attestation: %s\n", unconfirmedTxid.String()) + log.Infof("********** found unconfirmed attestation: %s\n", unconfirmedTxid.String()) s.attestation = models.NewAttestation(unconfirmedTxid, &commitment) // initialise attestation rawTx, _ := s.config.MainClient().GetRawTransaction(&unconfirmedTxid) s.attestation.Tx = *rawTx.MsgTx() // set msgTx @@ -210,7 +210,7 @@ func (s *AttestService) stateInitUnspent(unspent btcjson.ListUnspentResult) { if s.setFailure(commitmentErr) { return // will rebound to init } else if (commitment.GetCommitmentHash() != chainhash.Hash{}) { - log.Printf("********** found confirmed attestation: %s\n", unspentTxid.String()) + log.Infof("********** found confirmed attestation: %s\n", unspentTxid.String()) s.attestation = models.NewAttestation(*unspentTxid, &commitment) // update server with latest confirmed attestation s.attestation.Confirmed = true @@ -231,7 +231,7 @@ func (s *AttestService) stateInitUnspent(unspent btcjson.ListUnspentResult) { attestDelay = atimeNewAttestation - lastDelay } } else { - log.Println("********** found unspent transaction, initiating staychain") + log.Infoln("********** found unspent transaction, initiating staychain") s.attestation = models.NewAttestationDefault() } confirmedHash := s.attestation.CommitmentHash() @@ -246,7 +246,7 @@ func (s *AttestService) stateInitUnspent(unspent btcjson.ListUnspentResult) { // both latest unconfirmed and confirmed attestation addresses to wallet func (s *AttestService) stateInitWalletFailure() { - log.Println("********** wallet failure") + log.Infoln("********** wallet failure") // get last confirmed commitment from server lastCommitmentHash, latestErr := s.server.GetLatestAttestationCommitmentHash() @@ -259,7 +259,7 @@ func (s *AttestService) stateInitWalletFailure() { if s.setFailure(addrErr) { return // will rebound to init } - log.Printf("********** importing latest confirmed addr: %s ...\n", paytoaddr.String()) + log.Infof("********** importing latest confirmed addr: %s ...\n", paytoaddr.String()) importErr := s.attester.ImportAttestationAddr(paytoaddr) if s.setFailure(importErr) { return // will rebound to init @@ -276,7 +276,7 @@ func (s *AttestService) stateInitWalletFailure() { if s.setFailure(addrErr) { return // will rebound to init } - log.Printf("********** importing latest unconfirmed addr: %s ...\n", paytoaddr.String()) + log.Infof("********** importing latest unconfirmed addr: %s ...\n", paytoaddr.String()) importErr = s.attester.ImportAttestationAddr(paytoaddr) if s.setFailure(importErr) { return // will rebound to init @@ -291,7 +291,7 @@ func (s *AttestService) stateInitWalletFailure() { // - If no transaction found wait, else initiate new attestation // - If no attestation found, check last unconfirmed from db func (s *AttestService) doStateInit() { - log.Println("*AttestService* INITIATING ATTESTATION PROCESS") + log.Infoln("*AttestService* INITIATING ATTESTATION PROCESS") // find the state of the attestation unconfirmed, unconfirmedTxid, unconfirmedErr := s.attester.getUnconfirmedTx() @@ -320,7 +320,7 @@ func (s *AttestService) doStateInit() { // - Send commitment to client signers // - Initialise new attestation func (s *AttestService) doStateNextCommitment() { - log.Println("*AttestService* NEW ATTESTATION COMMITMENT") + log.Infoln("*AttestService* NEW ATTESTATION COMMITMENT") // get latest commitment hash from server latestCommitment, latestErr := s.server.GetClientCommitment() @@ -330,9 +330,9 @@ func (s *AttestService) doStateNextCommitment() { latestCommitmentHash := latestCommitment.GetCommitmentHash() // check if commitment has already been attested - log.Printf("********** received commitment hash: %s\n", latestCommitmentHash.String()) + log.Infof("********** received commitment hash: %s\n", latestCommitmentHash.String()) if latestCommitmentHash == s.attestation.CommitmentHash() { - log.Printf("********** Skipping attestation - Client commitment already attested") + log.Infof("********** Skipping attestation - Client commitment already attested") attestDelay = ATimeSkip // sleep return // will remain at the same state } @@ -351,7 +351,7 @@ func (s *AttestService) doStateNextCommitment() { // - Publish unsigned transaction to signer clients // - add ATimeSigs waiting time func (s *AttestService) doStateNewAttestation() { - log.Println("*AttestService* NEW ATTESTATION") + log.Infoln("*AttestService* NEW ATTESTATION") // Get key and address for next attestation using client commitment key, keyErr := s.attester.GetNextAttestationKey(s.attestation.CommitmentHash()) @@ -362,7 +362,7 @@ func (s *AttestService) doStateNewAttestation() { if s.setFailure(addrErr) { return // will rebound to init } - log.Printf("********** importing pay-to addr: %s ...\n", paytoaddr.String()) + log.Infof("********** importing pay-to addr: %s ...\n", paytoaddr.String()) importErr := s.attester.ImportAttestationAddr(paytoaddr, false) // no rescan needed here if s.setFailure(importErr) { return // will rebound to init @@ -381,7 +381,7 @@ func (s *AttestService) doStateNewAttestation() { if s.setFailure(topupUnspentErr) { return // will rebound to init } else if topupFound { - log.Printf("********** found topup unspent: %s\n", topupUnspent.TxID) + log.Infof("********** found topup unspent: %s\n", topupUnspent.TxID) unspentList = append(unspentList, topupUnspent) } @@ -392,7 +392,7 @@ func (s *AttestService) doStateNewAttestation() { } s.attestation.Tx = *newTx - log.Printf("********** pre-sign txid: %s\n", s.attestation.Tx.TxHash().String()) + log.Infof("********** pre-sign txid: %s\n", s.attestation.Tx.TxHash().String()) // get last confirmed commitment from server lastCommitmentHash, latestErr := s.server.GetLatestAttestationCommitmentHash() @@ -427,12 +427,12 @@ func (s *AttestService) doStateNewAttestation() { // - Collect signatures from client signers // - Combine signatures them and sign the attestation transaction func (s *AttestService) doStateSignAttestation() { - log.Println("*AttestService* SIGN ATTESTATION") + log.Infoln("*AttestService* SIGN ATTESTATION") // Read sigs using subscribers sigs := s.signer.GetSigs() for sigForInput, _ := range sigs { - log.Printf("********** received %d signatures for input %d \n", + log.Infof("********** received %d signatures for input %d \n", len(sigs[sigForInput]), sigForInput) } @@ -445,7 +445,7 @@ func (s *AttestService) doStateSignAttestation() { // sign attestation with combined sigs and last commitment signedTx, signErr := s.attester.signAttestation(&s.attestation.Tx, sigs, lastCommitmentHash) if s.setFailure(signErr) { - log.Printf("********** signer failure. resubscribing to signers...") + log.Infof("********** signer failure. resubscribing to signers...") s.signer.ReSubscribe() return // will rebound to init } @@ -458,7 +458,7 @@ func (s *AttestService) doStateSignAttestation() { // AStatePreSendStore // - Store unconfirmed attestation to server prior to sending func (s *AttestService) doStatePreSendStore() { - log.Println("*AttestService* PRE SEND STORE") + log.Infoln("*AttestService* PRE SEND STORE") // update server with latest unconfirmed attestation, in case the service fails errUpdate := s.server.UpdateLatestAttestation(*s.attestation) @@ -474,7 +474,7 @@ func (s *AttestService) doStatePreSendStore() { // - add ATimeConfirmation waiting time // - start time for confirmation time func (s *AttestService) doStateSendAttestation() { - log.Println("*AttestService* SEND ATTESTATION") + log.Infoln("*AttestService* SEND ATTESTATION") // sign attestation with combined signatures and send through client to network txid, attestationErr := s.attester.sendAttestation(&s.attestation.Tx) @@ -482,7 +482,7 @@ func (s *AttestService) doStateSendAttestation() { return // will rebound to init } s.attestation.Txid = txid - log.Printf("********** attestation transaction committed with txid: (%s)\n", txid) + log.Infof("********** attestation transaction committed with txid: (%s)\n", txid) s.state = AStateAwaitConfirmation // update attestation state attestDelay = ATimeConfirmation // add confirmation waiting time @@ -496,7 +496,7 @@ func (s *AttestService) doStateSendAttestation() { // - Check if ATIME_HANDLE_UNCONFIRMED has elapsed since attestation was sent // - add ATIME_NEW_ATTESTATION if confirmed or ATimeConfirmation if not to waiting time func (s *AttestService) doStateAwaitConfirmation() { - log.Printf("*AttestService* AWAITING CONFIRMATION \ntxid: (%s)\ncommitment: (%s)\n", s.attestation.Txid.String(), s.attestation.CommitmentHash().String()) + log.Infof("*AttestService* AWAITING CONFIRMATION \ntxid: (%s)\ncommitment: (%s)\n", s.attestation.Txid.String(), s.attestation.CommitmentHash().String()) // if attestation has been unconfirmed for too long // set to handle unconfirmed state @@ -511,7 +511,7 @@ func (s *AttestService) doStateAwaitConfirmation() { } if newTx.BlockHash != "" { - log.Printf("********** attestation confirmed with txid: (%s)\n", s.attestation.Txid.String()) + log.Infof("********** attestation confirmed with txid: (%s)\n", s.attestation.Txid.String()) // update server with latest confirmed attestation s.attestation.Confirmed = true @@ -537,9 +537,9 @@ func (s *AttestService) doStateAwaitConfirmation() { // - Handle attestations that have been unconfirmed for too long // - Bump attestation fees and re-initiate sign and send process func (s *AttestService) doStateHandleUnconfirmed() { - log.Println("*AttestService* HANDLE UNCONFIRMED") + log.Infoln("*AttestService* HANDLE UNCONFIRMED") - log.Printf("********** bumping fees for attestation txid: %s\n", s.attestation.Tx.TxHash().String()) + log.Infof("********** bumping fees for attestation txid: %s\n", s.attestation.Tx.TxHash().String()) currentTx := &s.attestation.Tx bumpErr := s.attester.bumpAttestationFees(currentTx, isFeeBumped) if s.setFailure(bumpErr) { @@ -548,7 +548,7 @@ func (s *AttestService) doStateHandleUnconfirmed() { isFeeBumped = true s.attestation.Tx = *currentTx - log.Printf("********** new pre-sign txid: %s\n", s.attestation.Tx.TxHash().String()) + log.Infof("********** new pre-sign txid: %s\n", s.attestation.Tx.TxHash().String()) // get last confirmed commitment from server lastCommitmentHash, latestErr := s.server.GetLatestAttestationCommitmentHash() diff --git a/attestation/attestsigner_fake.go b/attestation/attestsigner_fake.go index 100a3de..fb9cfeb 100644 --- a/attestation/attestsigner_fake.go +++ b/attestation/attestsigner_fake.go @@ -5,10 +5,9 @@ package attestation import ( - "log" - confpkg "mainstay/config" "mainstay/crypto" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -58,7 +57,7 @@ func (f AttestSignerFake) GetSigs() [][]crypto.Sig { // get confirmed hash from received confirmed hash bytes hash, hashErr := chainhash.NewHash(signerConfirmedHashBytes) if hashErr != nil { - log.Printf("%v\n", hashErr) + log.Infof("%v\n", hashErr) return nil } @@ -86,7 +85,7 @@ func (f AttestSignerFake) GetSigs() [][]crypto.Sig { sig, signErr = client.WalletPrivTopup.PrivKey.Sign(txPreImageHash.CloneBytes()) } if signErr != nil { - log.Printf("%v\n", signErr) + log.Infof("%v\n", signErr) return nil } diff --git a/attestation/attestsigner_zmq.go b/attestation/attestsigner_zmq.go index 9ff0747..9b725a5 100644 --- a/attestation/attestsigner_zmq.go +++ b/attestation/attestsigner_zmq.go @@ -6,11 +6,11 @@ package attestation import ( "fmt" - "log" confpkg "mainstay/config" "mainstay/crypto" "mainstay/messengers" + "mainstay/log" zmq "github.com/pebbe/zmq4" ) @@ -195,7 +195,7 @@ func (z AttestSignerZmq) GetSigs() [][]crypto.Sig { for { sockets, pollErr := poller.Poll(-1) if pollErr != nil { - log.Println(pollErr) + log.Infoln(pollErr) } found := false diff --git a/cmd/clientsignuptool/clientsignuptool.go b/cmd/clientsignuptool/clientsignuptool.go index 7525390..73015c9 100644 --- a/cmd/clientsignuptool/clientsignuptool.go +++ b/cmd/clientsignuptool/clientsignuptool.go @@ -10,13 +10,13 @@ import ( "bufio" "context" "encoding/hex" - "fmt" - "log" "os" + "fmt" "mainstay/config" "mainstay/db" "mainstay/models" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/satori/go.uuid" @@ -33,32 +33,32 @@ var ( func init() { confFile, confErr := config.GetConfFile(os.Getenv("GOPATH") + ConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } var mainConfigErr error mainConfig, mainConfigErr = config.NewConfig(confFile) if mainConfigErr != nil { - log.Fatal(mainConfigErr) + log.Error(mainConfigErr) } } // print client details func printClientDetails() { // Read existing clients and get next available client position - fmt.Println("existing clients") + log.Infoln("existing clients") details, errDb := dbMongo.GetClientDetails() if errDb != nil { - log.Fatal(errDb) + log.Error(errDb) } if len(details) == 0 { - fmt.Println("no existing client positions") + log.Infoln("no existing client positions") return } for _, client := range details { - fmt.Printf("client_position: %d pubkey: %s name: %s\n", + log.Infof("client_position: %d pubkey: %s name: %s\n", client.ClientPosition, client.Pubkey, client.ClientName) } - fmt.Println() + log.Infoln() } // read client details and get client position @@ -66,7 +66,7 @@ func clientPosition() int32 { // Read existing clients and get next available client position details, errDb := dbMongo.GetClientDetails() if errDb != nil { - log.Fatal(errDb) + log.Error(errDb) } var maxClientPosition int32 if len(details) == 0 { @@ -87,58 +87,58 @@ func main() { dbMongo = db.NewDbMongo(ctx, mainConfig.DbConfig()) - fmt.Println() - fmt.Println("*********************************************") - fmt.Println("************ Client Signup Tool *************") - fmt.Println("*********************************************") - fmt.Println() + log.Infoln() + log.Infoln("*********************************************") + log.Infoln("************ Client Signup Tool *************") + log.Infoln("*********************************************") + log.Infoln() printClientDetails() nextClientPosition := clientPosition() - fmt.Printf("next available position: %d\n", nextClientPosition) - fmt.Println() + log.Infof("next available position: %d\n", nextClientPosition) + log.Infoln() // Insert client pubkey details and verify - fmt.Println("*********************************************") - fmt.Println("************ Client Pubkey Info *************") - fmt.Println("*********************************************") - fmt.Println() - fmt.Print("Insert pubkey (optional): ") + log.Infoln("*********************************************") + log.Infoln("************ Client Pubkey Info *************") + log.Infoln("*********************************************") + log.Infoln() + log.Info("Insert pubkey (optional): ") var pubKey string fmt.Scanln(&pubKey) if pubKey == "" { - fmt.Println("no pubkey authentication") + log.Infoln("no pubkey authentication") } else { pubKeyBytes, pubKeyBytesErr := hex.DecodeString(pubKey) if pubKeyBytesErr != nil { - log.Fatal(pubKeyBytesErr) + log.Error(pubKeyBytesErr) } _, errPub := btcec.ParsePubKey(pubKeyBytes, btcec.S256()) if errPub != nil { - log.Fatal(errPub) + log.Error(errPub) } - fmt.Println("pubkey verified") - fmt.Println() + log.Infoln("pubkey verified") + log.Infoln() } // New auth token ID for client - fmt.Println("*********************************************") - fmt.Println("***** Client Auth Token identification ******") - fmt.Println("*********************************************") - fmt.Println() + log.Infoln("*********************************************") + log.Infoln("***** Client Auth Token identification ******") + log.Infoln("*********************************************") + log.Infoln() uuid, err := uuid.NewV4() if err != nil { - log.Fatal(err) + log.Error(err) } - fmt.Printf("new-uuid: %s\n", uuid.String()) - fmt.Println() + log.Infof("new-uuid: %s\n", uuid.String()) + log.Infoln() // Create new client details - fmt.Println("*********************************************") - fmt.Println("*********** Inserting New Client ************") - fmt.Println("*********************************************") - fmt.Println() - fmt.Print("Insert client name: ") + log.Info("*********************************************") + log.Info("*********** Inserting New Client ************") + log.Info("*********************************************") + log.Info() + log.Info("Insert client name: ") // scan input client name scanner := bufio.NewScanner(os.Stdin) @@ -152,12 +152,12 @@ func main() { ClientName: clientName} saveErr := dbMongo.SaveClientDetails(newClientDetails) if saveErr != nil { - log.Fatal(saveErr) + log.Error(saveErr) } - fmt.Println("NEW CLIENT DETAILS") - fmt.Printf("client_position: %d\n", newClientDetails.ClientPosition) - fmt.Printf("auth_token: %s\n", newClientDetails.AuthToken) - fmt.Printf("pubkey: %s\n", newClientDetails.Pubkey) - fmt.Println() + log.Infoln("NEW CLIENT DETAILS") + log.Infof("client_position: %d\n", newClientDetails.ClientPosition) + log.Infof("auth_token: %s\n", newClientDetails.AuthToken) + log.Infof("pubkey: %s\n", newClientDetails.Pubkey) + log.Infoln() printClientDetails() } diff --git a/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go b/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go index 1341042..1870f21 100644 --- a/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go +++ b/cmd/clientsignuptool/tokengenerator/tokengeneratortool.go @@ -7,21 +7,19 @@ package main // Token generator import ( - "fmt" - "log" - + "mainstay/log" "github.com/satori/go.uuid" ) // main func main() { // New auth token ID for client - fmt.Println() - fmt.Println("***** Client Auth Token identification ******") + log.Infoln() + log.Infoln("***** Client Auth Token identification ******") uuid, err := uuid.NewV4() if err != nil { - log.Fatal(err) + log.Error(err) } - fmt.Printf("new-uuid: %s\n", uuid.String()) - fmt.Println() + log.Infof("new-uuid: %s\n", uuid.String()) + log.Infoln() } diff --git a/cmd/commitmenttool/commitmenttool.go b/cmd/commitmenttool/commitmenttool.go index 464e647..1d40ea0 100644 --- a/cmd/commitmenttool/commitmenttool.go +++ b/cmd/commitmenttool/commitmenttool.go @@ -14,13 +14,13 @@ import ( "errors" "flag" "fmt" - "log" "net/http" "os" "strings" "time" "mainstay/config" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -69,23 +69,31 @@ func init() { // Generate new ECDSA priv-pub key pair for the client to use // when signing new commitments and sending to Mainstay API func doInitMode() { - fmt.Println("****************************") - fmt.Println("****** Init mode ***********") - fmt.Println("****************************") - - fmt.Printf("Generating new key...\n") + log.Infoln("****************************") + log.Infoln("****************************") + log.Infoln("****** Init mode ***********") + log.Infoln("****** Init mode ***********") + log.Infoln("****************************") + log.Infoln("****************************") + + log.Infof("Generating new key...\n") + log.Infof("Generating new key...\n") newPriv, newPrivErr := btcec.NewPrivateKey(btcec.S256()) if newPrivErr != nil { - log.Fatal(newPrivErr) + log.Error(newPrivErr) } newPrivBytesStr := hex.EncodeToString(newPriv.Serialize()) - fmt.Printf("generated priv: %s\n", newPrivBytesStr) + log.Infof("generated priv: %s\n", newPrivBytesStr) + log.Infof("generated priv: %s\n", newPrivBytesStr) newPubBytesStr := hex.EncodeToString(newPriv.PubKey().SerializeCompressed()) - fmt.Printf("generated pub: %s\n", newPubBytesStr) + log.Infof("generated pub: %s\n", newPubBytesStr) + log.Infof("generated pub: %s\n", newPubBytesStr) - fmt.Printf("The private key should be used for signing future client commitments\n") - fmt.Printf("The public key should be provided when posting these to Mainstay API\n") + log.Infof("The private key should be used for signing future client commitments\n") + log.Infof("The private key should be used for signing future client commitments\n") + log.Infof("The public key should be provided when posting these to Mainstay API\n") + log.Infof("The public key should be provided when posting these to Mainstay API\n") } // Send commitment and signature to Mainstay API @@ -117,7 +125,7 @@ func send(sig []byte, msg string) error { } defer resp.Body.Close() - fmt.Println("response Status:", resp.Status) + log.Infof("Response status: %s", resp.Status) // check status response if resp.StatusCode == 200 { @@ -143,14 +151,14 @@ func sign(msg []byte) []byte { // try key decoding privkeyBytes, decodeErr := hex.DecodeString(privkey) if decodeErr != nil { - log.Fatal(fmt.Sprintf("Key ('%s') decode error: %v\n", privkey, decodeErr)) + log.Error(fmt.Sprintf("Key ('%s') decode error: %v\n", privkey, decodeErr)) } privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privkeyBytes) // sign message sig, signErr := privKey.Sign(msg) if signErr != nil { - log.Fatal(fmt.Sprintf("Signing error: %v\n", signErr)) + log.Error(fmt.Sprintf("Signing error: %v\n", signErr)) } return sig.Serialize() } @@ -159,19 +167,19 @@ func sign(msg []byte) []byte { // Recurrent commitments of Ocean blockhash to Mainstay API // At regular intervals, fetch commitment, sign and send func doOceanMode() { - fmt.Println("****************************") - fmt.Println("****** Ocean mode **********") - fmt.Println("****************************") + log.Infoln("****************************") + log.Infoln("****** Ocean mode **********") + log.Infoln("****************************") // check priv key is set if privkey == "" { - fmt.Println("no private key provided") + log.Infoln("no private key provided") } // get conf file confFile, confErr := config.GetConfFile(os.Getenv("GOPATH") + ConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } // get ocean sidechain client from config @@ -182,14 +190,14 @@ func doOceanMode() { timer := time.NewTimer(sleepTime) select { case <-timer.C: - fmt.Println("Fetching next blockhash commitment...") + log.Infoln("Fetching next blockhash commitment...") // get next blockhash blockhash, blockhashErr := client.GetBestBlockHash() if blockhashErr != nil { - log.Fatal(fmt.Sprintf("Client fetching error: %v\n", blockhashErr)) + log.Error(fmt.Sprintf("Client fetching error: %v\n", blockhashErr)) } - fmt.Println("Commitment: ", blockhash.String()) + log.Infoln("Commitment: ", blockhash.String()) // get reverse blockhash bytes as this is how blockhashes are displayed revBlockHashBytes, _ := hex.DecodeString(blockhash.String()) @@ -203,13 +211,13 @@ func doOceanMode() { // send signed commitment sendErr := send(sigBytes, hex.EncodeToString(revBlockHashBytes)) if sendErr != nil { - log.Fatal(fmt.Sprintf("Commitment send error: %v\n", sendErr)) + log.Error(fmt.Sprintf("Commitment send error: %v\n", sendErr)) } else { - fmt.Println("Success!") + log.Infoln("Success!") } sleepTime = time.Duration(delay) * time.Minute - fmt.Printf("********** sleeping for: %s ...\n", sleepTime.String()) + log.Infof("********** sleeping for: %s ...\n", sleepTime.String()) } } } @@ -218,76 +226,76 @@ func doOceanMode() { // One time commitment to the Mainstay API // Sign the commitment provided and POST to API func doStandardMode() { - fmt.Println("****************************") - fmt.Println("****** Commitment mode *****") - fmt.Println("****************************") + log.Infoln("****************************") + log.Infoln("****** Commitment mode *****") + log.Infoln("****************************") - fmt.Println() - fmt.Print("Insert commitment: ") + log.Infoln() + log.Info("Insert commitment: ") var commitment string fmt.Scanln(&commitment) // try commitment decoding commitmentBytes, decodeErr := hex.DecodeString(commitment) if decodeErr != nil { - log.Fatal(fmt.Sprintf("Commitment ('%s') decode error: %v\n", commitment, decodeErr)) + log.Error(fmt.Sprintf("Commitment ('%s') decode error: %v\n", commitment, decodeErr)) } _, hashErr := chainhash.NewHash(commitmentBytes) if hashErr != nil { - log.Fatal(fmt.Sprintf("Commitment ('%s') to hash error: %v\n", commitment, hashErr)) + log.Error(fmt.Sprintf("Commitment ('%s') to hash error: %v\n", commitment, hashErr)) } - fmt.Println() - fmt.Print("Sign commitment, send commitment or both? ") + log.Infoln() + log.Info("Sign commitment, send commitment or both? ") var whatToDo string fmt.Scanln(&whatToDo) var sigBytes []byte if strings.ToLower(whatToDo) == "send" { - fmt.Println() - fmt.Print("Insert signature: ") + log.Infoln() + log.Info("Insert signature: ") var signature string fmt.Scanln(&signature) if signature == "" { - fmt.Println("no signature provided") + log.Infoln("no signature provided") } else { var sigBytesErr error sigBytes, sigBytesErr = b64.StdEncoding.DecodeString(signature) if sigBytesErr != nil { - log.Fatal(fmt.Sprintf("Signature (%s) decoding error: %v\n", signature, sigBytesErr)) + log.Error(fmt.Sprintf("Signature (%s) decoding error: %v\n", signature, sigBytesErr)) } } } else if strings.ToLower(whatToDo) == "sign" || strings.ToLower(whatToDo) == "both" { - fmt.Println() - fmt.Print("Insert private key: ") + log.Infoln() + log.Info("Insert private key: ") fmt.Scanln(&privkey) if privkey == "" { - fmt.Println("no private key provided") + log.Infoln("no private key provided") } else { sigBytes = sign(commitmentBytes) - fmt.Println() - fmt.Println("Signature: " + b64.StdEncoding.EncodeToString(sigBytes)) + log.Infoln() + log.Infoln("Signature: " + b64.StdEncoding.EncodeToString(sigBytes)) } } else { - log.Fatal("Invalid option") + log.Error("Invalid option") } if strings.ToLower(whatToDo) == "send" || strings.ToLower(whatToDo) == "both" { // ask for position and auth token - fmt.Println() - fmt.Print("Insert position: ") + log.Infoln() + log.Info("Insert position: ") fmt.Scan(&position) - fmt.Println() - fmt.Print("Insert auth token: ") + log.Infoln() + log.Info("Insert auth token: ") fmt.Scan(&authtoken) // send signed commitment sendErr := send(sigBytes, commitment) if sendErr != nil { - log.Fatal(fmt.Sprintf("Commitment send error: %v\n", sendErr)) + log.Error(fmt.Sprintf("Commitment send error: %v\n", sendErr)) } - fmt.Println("Success!") + log.Infoln("Success!") } } diff --git a/cmd/confirmationtool/confirmationtool.go b/cmd/confirmationtool/confirmationtool.go index 9fff6e5..20a6c27 100644 --- a/cmd/confirmationtool/confirmationtool.go +++ b/cmd/confirmationtool/confirmationtool.go @@ -8,13 +8,13 @@ package main import ( "flag" - "log" "os" "strings" "mainstay/clients" "mainstay/config" "mainstay/staychain" + "mainstay/log" "github.com/btcsuite/btcd/chaincfg/chainhash" ) @@ -48,17 +48,17 @@ func init() { if tx == "" || script == "" || position == -1 || chaincodes == "" { flag.PrintDefaults() - log.Fatalf("Need to provide all -tx, -script, -chaincodes and -position argument.") + log.Errorf("Need to provide all -tx, -script, -chaincodes and -position argument.") } confFile, confErr := config.GetConfFile(os.Getenv("GOPATH") + ConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } var mainConfigErr error mainConfig, mainConfigErr = config.NewConfig(confFile) if mainConfigErr != nil { - log.Fatal(mainConfigErr) + log.Error(mainConfigErr) } client = config.NewClientFromConfig(ClientChainName, false, confFile) } @@ -76,11 +76,11 @@ func main() { // await new attestations and verify for transaction := range chain.Updates() { - log.Println("Verifying attestation") - log.Printf("txid: %s\n", transaction.Txid) + log.Infoln("Verifying attestation") + log.Infof("txid: %s\n", transaction.Txid) info, err := verifier.Verify(transaction) if err != nil { - log.Fatal(err) + log.Error(err) } else { printAttestation(transaction, info) } @@ -91,30 +91,30 @@ func main() { func getRawTxFromHash(hashstr string) staychain.Tx { txhash, errHash := chainhash.NewHashFromStr(hashstr) if errHash != nil { - log.Println("Invalid tx id provided") - log.Fatal(errHash) + log.Warnln("Invalid tx id provided") + log.Error(errHash) } txraw, errGet := mainConfig.MainClient().GetRawTransactionVerbose(txhash) if errGet != nil { - log.Println("Inititial transcaction does not exist") - log.Fatal(errGet) + log.Warnln("Inititial transaction does not exist") + log.Error(errGet) } return staychain.Tx(*txraw) } // print attestation information func printAttestation(tx staychain.Tx, info staychain.ChainVerifierInfo) { - log.Println("Attestation Verified") + log.Infoln("Attestation Verified") if showDetails { - log.Printf("%+v\n", tx) + log.Infof("%+v\n", tx) } else { - log.Printf("Bitcoin blockhash: %s\n", tx.BlockHash) + log.Infof("Bitcoin blockhash: %s\n", tx.BlockHash) } if info != (staychain.ChainVerifierInfo{}) { - log.Printf("CLIENT blockhash: %s\n", info.Hash().String()) - log.Printf("CLIENT blockheight: %d\n", info.Height()) + log.Infof("CLIENT blockhash: %s\n", info.Hash().String()) + log.Infof("CLIENT blockheight: %d\n", info.Height()) } - log.Printf("\n") - log.Printf("\n") - log.Printf("\n") + log.Infof("\n") + log.Infof("\n") + log.Infof("\n") } diff --git a/cmd/confirmationtool/keyextraction/keyextractiontool.go b/cmd/confirmationtool/keyextraction/keyextractiontool.go index 0b1f83a..8bf3a21 100644 --- a/cmd/confirmationtool/keyextraction/keyextractiontool.go +++ b/cmd/confirmationtool/keyextraction/keyextractiontool.go @@ -3,9 +3,9 @@ package main import ( "encoding/hex" - "fmt" "mainstay/crypto" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg" @@ -31,7 +31,7 @@ func main() { tweakedExtndKey, _ := crypto.TweakExtendedKey(extndKey, tweakHash.CloneBytes()) tweakedExtndPriv, _ := tweakedExtndKey.ECPrivKey() tweakedWif, _ := btcutil.NewWIF(tweakedExtndPriv, &chaincfg.MainNetParams, wif.CompressPubKey) - fmt.Println(tweakedWif.String()) + log.Infoln(tweakedWif.String()) redeemScript := "xxx-redeemscript-xxx" chaincodes := []string{"ffdf7ece79e83f0f479a37832d770294014edc6884b0c8bfa2e0aaf51fb00229", "ffdf7ece79e83f0f479a37832d770294014edc6884b0c8bfa2e0aaf51fb00229"} @@ -55,6 +55,6 @@ func main() { tweakedPubkeys = append(tweakedPubkeys, tweakedPub) } multisigAddr, redeemScript := crypto.CreateMultisig(tweakedPubkeys, numOfSigs, &chaincfg.MainNetParams) - fmt.Println(multisigAddr) - fmt.Println(redeemScript) + log.Infoln(multisigAddr) + log.Infoln(redeemScript) } diff --git a/cmd/multisigtool/multisigtool.go b/cmd/multisigtool/multisigtool.go index ff10873..4b3f171 100644 --- a/cmd/multisigtool/multisigtool.go +++ b/cmd/multisigtool/multisigtool.go @@ -9,11 +9,11 @@ import ( "encoding/hex" "flag" "fmt" - "log" "math/big" "strings" "mainstay/crypto" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg" @@ -51,15 +51,15 @@ func init() { // main func main() { if chain == "regtest" { - fmt.Println("REGTEST") + log.Infoln("REGTEST") chainCfg = chaincfg.RegressionNetParams doRegtest() } else { if chain == "testnet" { - fmt.Println("TESTNET") + log.Infoln("TESTNET") chainCfg = chaincfg.TestNet3Params } else { - fmt.Println("MAINNET") + log.Infoln("MAINNET") chainCfg = chaincfg.MainNetParams } doMain() @@ -76,38 +76,38 @@ func infoFromPubs(pubs []string, nKeys int, nSigs int) { for _, pub := range pubs { pubBytes, pubBytesErr := hex.DecodeString(pub) if pubBytesErr != nil { - log.Fatal(fmt.Sprintf("failed decoding pub %s %v", pub, pubBytesErr)) + log.Errorf("failed decoding pub %s %v", pub, pubBytesErr) } pubP2pkh, pubP2pkhErr := btcutil.NewAddressPubKeyHash(btcutil.Hash160(pubBytes), &chainCfg) if pubP2pkhErr != nil { - log.Fatal(fmt.Sprintf("failed generating addr from pub %s %v", pub, pubP2pkhErr)) + log.Errorf("failed generating addr from pub %s %v", pub, pubP2pkhErr) } - fmt.Printf("pub P2PKH:\t%s\n", pubP2pkh) + log.Infof("pub P2PKH:\t%s\n", pubP2pkh) pubmultistr += "21" + pub } pubmultistr += fmt.Sprintf("5%d", nKeys) pubmultistr += "ae" - fmt.Printf("%d-of-%d MULTISIG script: %s\n", nSigs, nKeys, pubmultistr) + log.Infof("%d-of-%d MULTISIG script: %s\n", nSigs, nKeys, pubmultistr) // generate P2SH address pubmultibytes, _ := hex.DecodeString(pubmultistr) addr, err := btcutil.NewAddressScriptHash(pubmultibytes, &chainCfg) if err != nil { - fmt.Println(err) + log.Infoln(err) } - fmt.Printf("%d-of-%d P2SH address: %s\n", nSigs, nKeys, addr.String()) + log.Infof("%d-of-%d P2SH address: %s\n", nSigs, nKeys, addr.String()) } // Generate multisig script and p2sh address for mainstay // from a list of pubkeys or pubX/pubY coordinates func doMain() { if nKeys <= 0 || nKeys > 15 || nSigs <= 0 || nSigs > 15 || nSigs > nKeys { - log.Fatal(fmt.Sprintf("invalid nSigs(%d) or nKeys(%d)", nSigs, nKeys)) + log.Errorf("invalid nSigs(%d) or nKeys(%d)", nSigs, nKeys) } if keys == "" && keysX == "" && keysY == "" { - log.Fatal("Keys missing. Either provide -keys or -keysX and -keysY.") + log.Error("Keys missing. Either provide -keys or -keysX and -keysY.") } if keys == "" { @@ -115,8 +115,8 @@ func doMain() { keysYSplit := strings.Split(keysY, ",") if len(keysXSplit) != nKeys && len(keysYSplit) != nKeys { - log.Fatal(fmt.Sprintf("nKeys(%d) but %d keysX and %d keysY provided", - nKeys, len(keysXSplit), len(keysYSplit))) + log.Errorf("nKeys(%d) but %d keysX and %d keysY provided", + nKeys, len(keysXSplit), len(keysYSplit)) } pubs := make([]string, nKeys) @@ -128,7 +128,7 @@ func doMain() { } else { keysSplit := strings.Split(keys, ",") if len(keysSplit) != nKeys { - log.Fatal(fmt.Sprintf("nKeys(%d) but %d keys provided", nKeys, len(keysSplit))) + log.Errorf("nKeys(%d) but %d keys provided", nKeys, len(keysSplit)) } infoFromPubs(keysSplit, nKeys, nSigs) } @@ -140,11 +140,11 @@ func pubFromCoordinates(xStr string, yStr string) *btcec.PublicKey { y := new(big.Int) _, errX := fmt.Sscan(xStr, x) if errX != nil { - fmt.Println("fail-x") + log.Warnln("Get btcec PublicKey fail-x") } _, errY := fmt.Sscan(yStr, y) if errY != nil { - fmt.Println("fail-y") + log.Warnln("Get btcec PublicKey fail-y") } return (*btcec.PublicKey)(&ecdsa.PublicKey{btcec.S256(), x, y}) @@ -162,15 +162,15 @@ func doRegtest() { // hsm pubkey pub := pubFromCoordinates(hsmPubX, hsmPubY) pubEnc := hex.EncodeToString(pub.SerializeCompressed()) - fmt.Printf("pub:\t\t%s\n", pubEnc) + log.Infof("pub:\t\t%s\n", pubEnc) p2pkh, _ := crypto.GetAddressFromPubKey(pub, &chainCfg) - fmt.Printf("pub P2PKH:\t%s\n\n", p2pkh) + log.Infof("pub P2PKH:\t%s\n\n", p2pkh) // main pubkey - fmt.Printf("pubMain:\t%s\n", mainPub) + log.Infof("pubMain:\t%s\n", mainPub) pubmainbytes, _ := hex.DecodeString(mainPub) pubMainp2pkh, _ := btcutil.NewAddressPubKeyHash(btcutil.Hash160(pubmainbytes), &chainCfg) - fmt.Printf("pubMain P2PKH:\t%s\n\n", pubMainp2pkh) + log.Infof("pubMain P2PKH:\t%s\n\n", pubMainp2pkh) infoFromPubs([]string{mainPub, pubEnc}, 2, 1) } diff --git a/cmd/txsigningtool/txsigningtool.go b/cmd/txsigningtool/txsigningtool.go index 4319326..9f3c648 100644 --- a/cmd/txsigningtool/txsigningtool.go +++ b/cmd/txsigningtool/txsigningtool.go @@ -8,7 +8,6 @@ import ( "encoding/hex" "flag" "fmt" - "log" "os" "os/exec" "strings" @@ -19,6 +18,7 @@ import ( _ "mainstay/crypto" "mainstay/messengers" "mainstay/test" + "mainstay/log" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -77,7 +77,7 @@ func parseFlags() { if pk0 == "" && !isRegtest { flag.PrintDefaults() - log.Fatalf("Need to provide -pk argument. To use test configuration set the -regtest flag.") + log.Errorf("Need to provide -pk argument. To use test configuration set the -regtest flag.") } } @@ -92,17 +92,17 @@ func init() { cmd := exec.Command("/bin/sh", os.Getenv("GOPATH")+DemoInitPath) _, err := cmd.Output() if err != nil { - log.Fatal(err) + log.Error(err) } confFile, confErr := confpkg.GetConfFile(os.Getenv("GOPATH") + DemoConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } var configErr error config, configErr = confpkg.NewConfig(confFile) if configErr != nil { - log.Fatal(configErr) + log.Error(configErr) } pk0 = test.PrivMain script0 = test.Script @@ -115,18 +115,18 @@ func init() { // use conf file to setup config confFile, confErr := confpkg.GetConfFile(os.Getenv("GOPATH") + ConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } var configErr error config, configErr = confpkg.NewConfig(confFile) if configErr != nil { - log.Fatal(configErr) + log.Error(configErr) } // if init script is not set throw error if script0 == "" && config.InitScript() == "" { flag.PrintDefaults() - log.Fatalf(`Need to provide -script argument. + log.Error(`Need to provide -script argument. To use test configuration set the -regtest flag.`) } } @@ -168,7 +168,7 @@ func main() { for { select { case <-timer.C: - log.Println("resubscribing to mainstay...") + log.Infoln("resubscribing to mainstay...") // remove socket and close sub.Close(poller) // re-assign subscriber socket @@ -185,7 +185,7 @@ func main() { processTx(msg) case attestation.TopicConfirmedHash: attestedHash = processHash(msg) - log.Printf("attestedhash %s\n", attestedHash.String()) + log.Infof("attestedhash %s\n", attestedHash.String()) } } } @@ -198,7 +198,7 @@ func main() { func processHash(msg []byte) chainhash.Hash { hash, hashErr := chainhash.NewHash(msg) if hashErr != nil { - log.Fatal(hashErr) + log.Error(hashErr) } return *hash } @@ -228,13 +228,13 @@ func processTx(msg []byte) { sig, signErr = client.WalletPrivTopup.PrivKey.Sign(txPreImageHash.CloneBytes()) } if signErr != nil { - log.Fatalf("%v\n", signErr) + log.Error(signErr) } // add hash type to signature as well sigBytes := append(sig.Serialize(), []byte{byte(1)}...) - log.Printf("sending sig(%d) %s\n", txIt, hex.EncodeToString(sigBytes)) + log.Infof("sending sig(%d) %s\n", txIt, hex.EncodeToString(sigBytes)) sigs = append(sigs, sigBytes) } diff --git a/config/conf.json b/config/conf.json index 7704248..11305dd 100644 --- a/config/conf.json +++ b/config/conf.json @@ -1,41 +1,49 @@ { "staychain": { - "initTx": "MAINSTAY_INIT_TX", - "initScript": "MAINSTAY_INIT_SCRIPT", - "initChaincodes": "MAINSTAY_INIT_CHAINCODES", - "topupAddress": "MAINSTAY_TOPUP_ADDRESS", - "topupScript": "MAINSTAY_TOPUP_SCRIPT" + "initTx": "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1", + "initScript": "51210381324c14a482646e9ad7cf82372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae", + "initChaincodes": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa,0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa", + "topupAddress": "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB", + "topupScript": "512103e52cf15e0a5cf6612314f077bb65cf9a6596b76c0fcb34b682f673a8314c7b332102f3a78a7bd6cf01c56312e7e828bef74134dfb109e59afd088526212d96518e7552ae", + "regtest": "1" }, "main": { - "rpcurl": "MAINSTAY_MAIN_URL", - "rpcuser": "MAINSTAY_MAIN_USER", - "rpcpass": "MAINSTAY_MAIN_PASS", - "chain": "MAINSTAY_MAIN_CHAIN" + "rpcurl": "127.0.0.1:5000", + "rpcuser": "user", + "rpcpass": "pass", + "chain": "testnet3" + }, + "clientchain": + { + "rpcurl": "127.0.0.1:19000", + "rpcuser": "USERNAME", + "rpcpass": "PASSWORD", + "chain": "main" }, "signer": { - "publisher": "MAINSTAY_SIGNER_PUBLISHER", - "signers": "MAINSTAY_SIGNER_SIGNERS" + "publisher": "*:5000", + "signers": "node0:1000,node1:1001" }, "db": { - "user": "MAINSTAY_DB_USER", - "password": "MAINSTAY_DB_PASS", - "host": "MAINSTAY_DB_HOST", - "port": "MAINSTAY_DB_PORT", - "name": "MAINSTAY_DB_NAME" + "user": "serviceUser", + "password": "servicePass", + "host": "localhost", + "port": "27017", + "name": "mainstay" }, "fees": { - "minFee": "MAINSTAY_FEES_MIN", - "maxFee": "MAINSTAY_FEES_MAX", - "feeIncrement": "MAINSTAY_FEES_INCREMENT" + "minFee": "5", + "maxFee": "50", + "feeIncrement": "2" }, "timing": { - "newAttestationMinutes": "MAINSTAY_NEW_ATTESTATION_MINUTES", - "handleUnconfirmedMinutes": "MAINSTAY_HANDLE_UNCONFIRMED_MINUTES" + "newAttestationMinutes": "60", + "handleUnconfirmedMinutes": "60" } -} \ No newline at end of file +} diff --git a/config/config.go b/config/config.go index 1aaaa7e..a256aed 100644 --- a/config/config.go +++ b/config/config.go @@ -5,12 +5,12 @@ package config import ( - "log" "os" "strconv" "strings" "mainstay/clients" + "mainstay/log" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/rpcclient" @@ -276,14 +276,14 @@ func NewClientFromConfig(chainName string, isTest bool, customConf ...[]byte) cl var confErr error conf, confErr = GetConfFile(os.Getenv("GOPATH") + ConfPath) if confErr != nil { - log.Fatal(confErr) + log.Error(confErr) } } // get side client rpc sideClient, rpcErr := GetRPC(chainName, conf) if rpcErr != nil { - log.Fatal(rpcErr) + log.Error(rpcErr) } return clients.NewSidechainClientOcean(sideClient) } diff --git a/crypto/scripts.go b/crypto/scripts.go index f540dcd..4e79078 100644 --- a/crypto/scripts.go +++ b/crypto/scripts.go @@ -7,9 +7,10 @@ package crypto import ( "encoding/hex" "fmt" - "log" "strconv" + "mainstay/log" + "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcutil" @@ -27,12 +28,12 @@ func ParseRedeemScript(script string) ([]*btcec.PublicKey, int) { op := script[0] op1 := script[lscript-4] if !(string(op) == string(op1)) && (string(op1) == "5") { - log.Fatal("Incorrect opcode in redeem script") + log.Error("Incorrect opcode in redeem script") } // check multisig if script[lscript-2:] != "ae" { - log.Fatal("Checkmultisig missing from redeem script") + log.Error("Checkmultisig missing from redeem script") } numOfSigs, _ := strconv.Atoi(string(script[1])) @@ -43,13 +44,13 @@ func ParseRedeemScript(script string) ([]*btcec.PublicKey, int) { for i := 0; i < numOfKeys; i++ { keysize, _ := strconv.ParseInt(string(script[startIndex:startIndex+2]), 16, 16) if !(keysize == 65 || keysize == 33) { - log.Fatal("Incorrect pubkey size") + log.Error("Incorrect pubkey size") } keystr := script[startIndex+2 : startIndex+2+2*keysize] keybytes, _ := hex.DecodeString(keystr) pubkey, err := btcec.ParsePubKey(keybytes, btcec.S256()) if err != nil { - log.Fatal(err) + log.Error(err) } startIndex += 2 + 2*keysize keys = append(keys, pubkey) diff --git a/db/db_mongo.go b/db/db_mongo.go index e352278..6144fbd 100644 --- a/db/db_mongo.go +++ b/db/db_mongo.go @@ -8,10 +8,10 @@ import ( "context" "errors" "fmt" - "log" "mainstay/config" "mainstay/models" + "mainstay/log" "github.com/btcsuite/btcd/chaincfg/chainhash" "go.mongodb.org/mongo-driver/mongo" @@ -103,7 +103,7 @@ type DbMongo struct { func NewDbMongo(ctx context.Context, dbConnectivity config.DbConfig) *DbMongo { db, errConnect := dbConnect(ctx, dbConnectivity) if errConnect != nil { - log.Fatal(errConnect) + log.Error(errConnect) } return &DbMongo{ctx, dbConnectivity, db} @@ -422,14 +422,14 @@ func (d *DbMongo) GetAttestationMerkleCommitments(txid chainhash.Hash) ([]models for res.Next(d.ctx) { var commitmentDoc bsonx.Doc if err := res.Decode(&commitmentDoc); err != nil { - fmt.Printf("%s\n", BadDataMerkleCommitmentCol) + log.Infof("%s\n", BadDataMerkleCommitmentCol) return []models.CommitmentMerkleCommitment{}, err } // decode document result to Commitment model and get hash commitmentModel := &models.CommitmentMerkleCommitment{} modelErr := models.GetModelFromDocument(&commitmentDoc, commitmentModel) if modelErr != nil { - fmt.Printf("%s\n", BadDataMerkleCommitmentCol) + log.Infof("%s\n", BadDataMerkleCommitmentCol) return []models.CommitmentMerkleCommitment{}, modelErr } merkleCommitments = append(merkleCommitments, *commitmentModel) diff --git a/log/doc.go b/log/doc.go new file mode 100644 index 0000000..e39bd2c --- /dev/null +++ b/log/doc.go @@ -0,0 +1,5 @@ +/* +Package log extends log with custom INFO, WARN, ERROR log types + +*/ +package log diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..857b391 --- /dev/null +++ b/log/log.go @@ -0,0 +1,56 @@ +// Copyright (c) 2018 CommerceBlock Team +// Use of this source code is governed by an MIT +// license that can be found in the LICENSE file. + +package log + +import ( + "log" + "os" + "fmt" +) + +/* Extended logging functionality providing Info, Warn and Error messages for + log entries. + Functions Infof(), Warnf(), Errorf() handles parameters in the same + way that fmt.Printf handles parameters. + Info(), Warn(), Error() take individual variables as parameters - useful + for printing of simple error message constants +*/ + +var infoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) +var warnLogger = log.New(os.Stdout, "WARN: ", log.Ldate|log.Ltime|log.Lshortfile) +var errorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + + +func Info(v ...interface{}) { + infoLogger.Output(2, fmt.Sprint(v...)) +} +func Infof(format string, v ...interface{}) { + infoLogger.Output(2, fmt.Sprintf(format, v...)) +} +func Infoln(v ...interface{}) { + infoLogger.Output(2, fmt.Sprintln(v...)) +} + +func Warn(v ...interface{}) { + warnLogger.Output(2, fmt.Sprint(v...)) +} +func Warnf(format string, v ...interface{}) { + warnLogger.Output(2, fmt.Sprintf(format, v...)) +} +func Warnln(v ...interface{}) { + warnLogger.Output(2, fmt.Sprintln(v...)) +} + +func Error(v ...interface{}) { + errorLogger.Output(2, fmt.Sprint(v...)) + os.Exit(1) +} +func Errorf(format string, v ...interface{}) { + errorLogger.Output(2, fmt.Sprintf(format, v...)) + os.Exit(1) +} +func Errorln(v ...interface{}) { + errorLogger.Output(2, fmt.Sprintln(v...)) +} diff --git a/main.go b/main.go index 7529517..622276f 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ package main import ( "context" "flag" - "log" "os" "os/signal" "strings" @@ -18,6 +17,7 @@ import ( "mainstay/config" "mainstay/db" "mainstay/test" + "mainstay/log" ) var ( @@ -46,19 +46,19 @@ func init() { if isRegtest { test := test.NewTest(true, true) mainConfig = test.Config - log.Printf("Running regtest mode with -tx=%s\n", mainConfig.InitTx()) + log.Infof("Running regtest mode with -tx=%s\n", mainConfig.InitTx()) } else { var mainConfigErr error mainConfig, mainConfigErr = config.NewConfig() if mainConfigErr != nil { - log.Fatal(mainConfigErr) + log.Error(mainConfigErr) } // if either tx or script not set throw error if tx0 == "" || script0 == "" || chaincodes == "" { if mainConfig.InitTx() == "" || mainConfig.InitScript() == "" || len(mainConfig.InitChaincodes()) == 0 { flag.PrintDefaults() - log.Fatalf(`Need to provide all -tx, -script and -chaincode arguments. + log.Error(`Need to provide all -tx, -script and -chaincode arguments. To use test configuration set the -regtest flag.`) } } else { @@ -99,7 +99,7 @@ func main() { defer wg.Done() select { case sig := <-c: - log.Printf("Got %s signal. Aborting...\n", sig) + log.Warnf("Got %s signal. Aborting...\n", sig) case <-ctx.Done(): signal.Stop(c) } diff --git a/models/commitmentmerkleproof.go b/models/commitmentmerkleproof.go index ba21ac7..c6ac862 100644 --- a/models/commitmentmerkleproof.go +++ b/models/commitmentmerkleproof.go @@ -5,7 +5,7 @@ package models import ( - "log" + "mainstay/log" "github.com/btcsuite/btcd/chaincfg/chainhash" "go.mongodb.org/mongo-driver/bson" @@ -64,20 +64,20 @@ func buildMerkleProof(position int, tree []*chainhash.Hash) CommitmentMerkleProo // Prove a commitment using the merkle proof provided func ProveMerkleProof(proof CommitmentMerkleProof) bool { hash := proof.Commitment - log.Printf("client position: %d\n", proof.ClientPosition) - log.Printf("client commitment: %s\n", hash.String()) + log.Infof("client position: %d\n", proof.ClientPosition) + log.Infof("client commitment: %s\n", hash.String()) for i := range proof.Ops { if proof.Ops[i].Append { - log.Printf("append: %s\n", proof.Ops[i].Commitment.String()) + log.Infof("append: %s\n", proof.Ops[i].Commitment.String()) hash = *hashLeaves(hash, proof.Ops[i].Commitment) - log.Printf("result: %s\n", hash.String()) + log.Infof("result: %s\n", hash.String()) } else { - log.Printf("prepend: %s\n", proof.Ops[i].Commitment.String()) + log.Infof("prepend: %s\n", proof.Ops[i].Commitment.String()) hash = *hashLeaves(proof.Ops[i].Commitment, hash) - log.Printf("result: %s\n", hash.String()) + log.Infof("result: %s\n", hash.String()) } } - log.Printf("merkle root: %s\n", proof.MerkleRoot.String()) + log.Infof("merkle root: %s\n", proof.MerkleRoot.String()) return hash == proof.MerkleRoot } diff --git a/models/commitmentmerkletree.go b/models/commitmentmerkletree.go index d3e5117..e49912e 100644 --- a/models/commitmentmerkletree.go +++ b/models/commitmentmerkletree.go @@ -6,9 +6,10 @@ package models import ( _ "errors" - "fmt" "math" + "mainstay/log" + "github.com/btcsuite/btcd/chaincfg/chainhash" ) @@ -20,12 +21,12 @@ func printMerkleTree(tree []*chainhash.Hash) { if i == num { i = 0 num = num / 2 - fmt.Printf("\n") + log.Infof("\n") } i += 1 - fmt.Printf("%v ", hash) + log.Infof("%v ", hash) } - fmt.Printf("\n") + log.Infof("\n") } // Build merkle tree store from a list of commitments diff --git a/test/test.go b/test/test.go index a927fa5..c6790f8 100644 --- a/test/test.go +++ b/test/test.go @@ -6,7 +6,6 @@ package test import ( "context" - "log" "os" "os/exec" "strings" @@ -17,6 +16,7 @@ import ( confpkg "mainstay/config" "mainstay/db" "mainstay/models" + "mainstay/log" ) // For regtest attestation demonstration @@ -85,23 +85,23 @@ func NewTest(logOutput bool, isRegtest bool) *Test { cmd := exec.Command("/bin/sh", initPath) output, err := cmd.Output() if err != nil { - log.Fatal(err) + log.Error(err) } if logOutput { - log.Println(string(output)) + log.Infoln(string(output)) } // if not a regtest, then unittest config, configErr := confpkg.NewConfig(testConf) if configErr != nil { - log.Fatal(configErr) + log.Error(configErr) } oceanClient := confpkg.NewClientFromConfig("ocean", true, testConf) // Get transaction for Address as initial TX for attestation chain unspent, errUnspent := config.MainClient().ListTransactions("*") if errUnspent != nil { - log.Fatal(errUnspent) + log.Error(errUnspent) } var txid string for _, vout := range unspent { @@ -139,7 +139,7 @@ func DoRegtestWork(dbMongo *db.DbMongo, config *confpkg.Config, wg *sync.WaitGro // generate and get hash hash, genErr := config.MainClient().Generate(1) if genErr != nil { - log.Println(genErr) + log.Infoln(genErr) } // every other block generation commit @@ -152,7 +152,7 @@ func DoRegtestWork(dbMongo *db.DbMongo, config *confpkg.Config, wg *sync.WaitGro saveErr := dbMongo.SaveClientCommitment(newClientCommitment) if saveErr != nil { - log.Println(saveErr) + log.Infoln(saveErr) } doCommit = false } else { @@ -186,19 +186,19 @@ func NewTestMulti() *TestMulti { cmd := exec.Command("/bin/sh", initPath) _, err := cmd.Output() if err != nil { - log.Fatal(err) + log.Error(err) } // get config config, configErr := confpkg.NewConfig(testConf) if configErr != nil { - log.Fatal(configErr) + log.Error(configErr) } // Get transaction for Address as initial TX for attestation chain unspent, errUnspent := config.MainClient().ListTransactions("*") if errUnspent != nil { - log.Fatal(errUnspent) + log.Error(errUnspent) } var txid string for _, vout := range unspent { @@ -216,7 +216,7 @@ func NewTestMulti() *TestMulti { // get config config, configErr := confpkg.NewConfig(testConf) if configErr != nil { - log.Fatal(configErr) + log.Error(configErr) } config.SetInitTx(txid) From 5023bb6ac3dab33cde61cc5eee0a9b17bca4f695 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Fri, 8 Nov 2019 16:55:24 +0000 Subject: [PATCH 04/14] set conf.json to default, corrections --- attestation/attestfees.go | 2 +- cmd/clientsignuptool/clientsignuptool.go | 8 ++-- cmd/commitmenttool/commitmenttool.go | 8 ---- cmd/confirmationtool/confirmationtool.go | 2 +- config/conf.json | 50 ++++++++++-------------- models/commitmentmerkletree.go | 5 ++- 6 files changed, 30 insertions(+), 45 deletions(-) diff --git a/attestation/attestfees.go b/attestation/attestfees.go index 0e8b1ab..a7c1c5c 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -73,7 +73,7 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { } log.Infof("*Fees* Min fee set to: %d\n", minFee) - // Infox fee with lower limit min_fee && 0 and max fee default + // max fee with lower limit min_fee && 0 and max fee default maxFee := DefaultMaxFee if feesConfig.MaxFee > 0 && feesConfig.MaxFee > minFee && feesConfig.MaxFee < DefaultMaxFee { maxFee = feesConfig.MaxFee diff --git a/cmd/clientsignuptool/clientsignuptool.go b/cmd/clientsignuptool/clientsignuptool.go index 73015c9..c9f502d 100644 --- a/cmd/clientsignuptool/clientsignuptool.go +++ b/cmd/clientsignuptool/clientsignuptool.go @@ -134,10 +134,10 @@ func main() { log.Infoln() // Create new client details - log.Info("*********************************************") - log.Info("*********** Inserting New Client ************") - log.Info("*********************************************") - log.Info() + log.Infoln("*********************************************") + log.Infoln("*********** Inserting New Client ************") + log.Infoln("*********************************************") + log.Infoln() log.Info("Insert client name: ") // scan input client name diff --git a/cmd/commitmenttool/commitmenttool.go b/cmd/commitmenttool/commitmenttool.go index 1d40ea0..21904af 100644 --- a/cmd/commitmenttool/commitmenttool.go +++ b/cmd/commitmenttool/commitmenttool.go @@ -70,13 +70,9 @@ func init() { // when signing new commitments and sending to Mainstay API func doInitMode() { log.Infoln("****************************") - log.Infoln("****************************") - log.Infoln("****** Init mode ***********") log.Infoln("****** Init mode ***********") log.Infoln("****************************") - log.Infoln("****************************") - log.Infof("Generating new key...\n") log.Infof("Generating new key...\n") newPriv, newPrivErr := btcec.NewPrivateKey(btcec.S256()) if newPrivErr != nil { @@ -85,14 +81,10 @@ func doInitMode() { newPrivBytesStr := hex.EncodeToString(newPriv.Serialize()) log.Infof("generated priv: %s\n", newPrivBytesStr) - log.Infof("generated priv: %s\n", newPrivBytesStr) newPubBytesStr := hex.EncodeToString(newPriv.PubKey().SerializeCompressed()) log.Infof("generated pub: %s\n", newPubBytesStr) - log.Infof("generated pub: %s\n", newPubBytesStr) log.Infof("The private key should be used for signing future client commitments\n") - log.Infof("The private key should be used for signing future client commitments\n") - log.Infof("The public key should be provided when posting these to Mainstay API\n") log.Infof("The public key should be provided when posting these to Mainstay API\n") } diff --git a/cmd/confirmationtool/confirmationtool.go b/cmd/confirmationtool/confirmationtool.go index 20a6c27..3e388d9 100644 --- a/cmd/confirmationtool/confirmationtool.go +++ b/cmd/confirmationtool/confirmationtool.go @@ -48,7 +48,7 @@ func init() { if tx == "" || script == "" || position == -1 || chaincodes == "" { flag.PrintDefaults() - log.Errorf("Need to provide all -tx, -script, -chaincodes and -position argument.") + log.Error("Need to provide all -tx, -script, -chaincodes and -position argument.") } confFile, confErr := config.GetConfFile(os.Getenv("GOPATH") + ConfPath) diff --git a/config/conf.json b/config/conf.json index 11305dd..60e69a0 100644 --- a/config/conf.json +++ b/config/conf.json @@ -1,49 +1,41 @@ { "staychain": { - "initTx": "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1", - "initScript": "51210381324c14a482646e9ad7cf82372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae", - "initChaincodes": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa,0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa", - "topupAddress": "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB", - "topupScript": "512103e52cf15e0a5cf6612314f077bb65cf9a6596b76c0fcb34b682f673a8314c7b332102f3a78a7bd6cf01c56312e7e828bef74134dfb109e59afd088526212d96518e7552ae", - "regtest": "1" + "initTx": "MAINSTAY_INIT_TX", + "initScript": "MAINSTAY_INIT_SCRIPT", + "initChaincodes": "MAINSTAY_INIT_CHAINCODES", + "topupAddress": "MAINSTAY_TOPUP_ADDRESS", + "topupScript": "MAINSTAY_TOPUP_SCRIPT" }, "main": { - "rpcurl": "127.0.0.1:5000", - "rpcuser": "user", - "rpcpass": "pass", - "chain": "testnet3" - }, - "clientchain": - { - "rpcurl": "127.0.0.1:19000", - "rpcuser": "USERNAME", - "rpcpass": "PASSWORD", - "chain": "main" + "rpcurl": "MAINSTAY_MAIN_URL", + "rpcuser": "MAINSTAY_MAIN_USER", + "rpcpass": "MAINSTAY_MAIN_PASS", + "chain": "MAINSTAY_MAIN_CHAIN" }, "signer": { - "publisher": "*:5000", - "signers": "node0:1000,node1:1001" + "publisher": "MAINSTAY_SIGNER_PUBLISHER", + "signers": "MAINSTAY_SIGNER_SIGNERS" }, "db": { - "user": "serviceUser", - "password": "servicePass", - "host": "localhost", - "port": "27017", - "name": "mainstay" + "user": "MAINSTAY_DB_USER", + "password": "MAINSTAY_DB_PASS", + "host": "MAINSTAY_DB_HOST", + "port": "MAINSTAY_DB_PORT", + "name": "MAINSTAY_DB_NAME" }, "fees": { - "minFee": "5", - "maxFee": "50", - "feeIncrement": "2" + "minFee": "MAINSTAY_FEES_MIN", + "maxFee": "MAINSTAY_FEES_MAX", + "feeIncrement": "MAINSTAY_FEES_INCREMENT" }, "timing": { - "newAttestationMinutes": "60", - "handleUnconfirmedMinutes": "60" + "newAttestationMinutes": "MAINSTAY_NEW_ATTESTATION_MINUTES", + "handleUnconfirmedMinutes": "MAINSTAY_HANDLE_UNCONFIRMED_MINUTES" } } diff --git a/models/commitmentmerkletree.go b/models/commitmentmerkletree.go index e49912e..c7a491f 100644 --- a/models/commitmentmerkletree.go +++ b/models/commitmentmerkletree.go @@ -7,6 +7,7 @@ package models import ( _ "errors" "math" + "fmt" "mainstay/log" @@ -21,12 +22,12 @@ func printMerkleTree(tree []*chainhash.Hash) { if i == num { i = 0 num = num / 2 - log.Infof("\n") + fmt.Println("") } i += 1 log.Infof("%v ", hash) } - log.Infof("\n") + fmt.Println("") } // Build merkle tree store from a list of commitments From 7e8f9683420b255523b8a984c2923107fae224f0 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Mon, 11 Nov 2019 10:43:25 +0000 Subject: [PATCH 05/14] comment log.go --- log/log.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/log/log.go b/log/log.go index 857b391..81d3684 100644 --- a/log/log.go +++ b/log/log.go @@ -10,7 +10,8 @@ import ( "fmt" ) -/* Extended logging functionality providing Info, Warn and Error messages for +/* + Extended logging functionality providing Info, Warn and Error messages for log entries. Functions Infof(), Warnf(), Errorf() handles parameters in the same way that fmt.Printf handles parameters. @@ -22,35 +23,43 @@ var infoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile var warnLogger = log.New(os.Stdout, "WARN: ", log.Ldate|log.Ltime|log.Lshortfile) var errorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - +// 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...)) } +// INFO print with formatting func Infof(format string, v ...interface{}) { infoLogger.Output(2, fmt.Sprintf(format, v...)) } +// standard INFO print with new line func Infoln(v ...interface{}) { 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...)) } +// WARN print with formatting func Warnf(format string, v ...interface{}) { warnLogger.Output(2, fmt.Sprintf(format, v...)) } +// standard WARN print with new line func Warnln(v ...interface{}) { warnLogger.Output(2, fmt.Sprintln(v...)) } +// Error log entries print a message then halt execution +// standard ERROR print. 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) } -func Errorln(v ...interface{}) { - errorLogger.Output(2, fmt.Sprintln(v...)) -} From 6535dc0f1110a0710021262140e8fcc5b06ccdff Mon Sep 17 00:00:00 2001 From: tomossomot Date: Mon, 11 Nov 2019 11:59:49 +0000 Subject: [PATCH 06/14] make use of log.errorf --- attestation/attestclient.go | 6 +++--- cmd/commitmenttool/commitmenttool.go | 16 ++++++++-------- staychain/chainverifier.go | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 1fae4df..7f3b35d 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -158,19 +158,19 @@ func newMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcutil // Verify same amount of sigs in topupscript and init script if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); topUpnumOfSigs != topUpnumOfSigs { - log.Errorf(fmt.Sprintf("%s. %d != %d", ErrorTopUpScriptNumSigs, numOfSigs, topUpnumOfSigs)) + log.Errorf("%s. %d != %d", ErrorTopUpScriptNumSigs, numOfSigs, topUpnumOfSigs) } // get chaincodes of pubkeys from config chaincodesStr := config.InitChaincodes() if len(chaincodesStr) != len(pubkeys) { - log.Error(fmt.Sprintf("%s %d != %d", ErrorMissingChaincodes, len(chaincodesStr), len(pubkeys))) + log.Errorf("%s %d != %d", ErrorMissingChaincodes, len(chaincodesStr), len(pubkeys)) } chaincodes := make([][]byte, len(pubkeys)) for i_c := range chaincodesStr { ccBytes, ccBytesErr := hex.DecodeString(chaincodesStr[i_c]) if ccBytesErr != nil || len(ccBytes) != 32 { - log.Error(fmt.Sprintf("%s %s", ErrorInvalidChaincode, chaincodesStr[i_c])) + log.Errorf("%s %s", ErrorInvalidChaincode, chaincodesStr[i_c]) } chaincodes[i_c] = append(chaincodes[i_c], ccBytes...) } diff --git a/cmd/commitmenttool/commitmenttool.go b/cmd/commitmenttool/commitmenttool.go index 21904af..f458c5e 100644 --- a/cmd/commitmenttool/commitmenttool.go +++ b/cmd/commitmenttool/commitmenttool.go @@ -143,14 +143,14 @@ func sign(msg []byte) []byte { // try key decoding privkeyBytes, decodeErr := hex.DecodeString(privkey) if decodeErr != nil { - log.Error(fmt.Sprintf("Key ('%s') decode error: %v\n", privkey, decodeErr)) + log.Errorf("Key ('%s') decode error: %v\n", privkey, decodeErr) } privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privkeyBytes) // sign message sig, signErr := privKey.Sign(msg) if signErr != nil { - log.Error(fmt.Sprintf("Signing error: %v\n", signErr)) + log.Errorf("Signing error: %v\n", signErr) } return sig.Serialize() } @@ -187,7 +187,7 @@ func doOceanMode() { // get next blockhash blockhash, blockhashErr := client.GetBestBlockHash() if blockhashErr != nil { - log.Error(fmt.Sprintf("Client fetching error: %v\n", blockhashErr)) + log.Errorf("Client fetching error: %v\n", blockhashErr) } log.Infoln("Commitment: ", blockhash.String()) @@ -203,7 +203,7 @@ func doOceanMode() { // send signed commitment sendErr := send(sigBytes, hex.EncodeToString(revBlockHashBytes)) if sendErr != nil { - log.Error(fmt.Sprintf("Commitment send error: %v\n", sendErr)) + log.Errorf("Commitment send error: %v\n", sendErr) } else { log.Infoln("Success!") } @@ -230,11 +230,11 @@ func doStandardMode() { // try commitment decoding commitmentBytes, decodeErr := hex.DecodeString(commitment) if decodeErr != nil { - log.Error(fmt.Sprintf("Commitment ('%s') decode error: %v\n", commitment, decodeErr)) + log.Errorf("Commitment ('%s') decode error: %v\n", commitment, decodeErr) } _, hashErr := chainhash.NewHash(commitmentBytes) if hashErr != nil { - log.Error(fmt.Sprintf("Commitment ('%s') to hash error: %v\n", commitment, hashErr)) + log.Errorf("Commitment ('%s') to hash error: %v\n", commitment, hashErr) } log.Infoln() @@ -254,7 +254,7 @@ func doStandardMode() { var sigBytesErr error sigBytes, sigBytesErr = b64.StdEncoding.DecodeString(signature) if sigBytesErr != nil { - log.Error(fmt.Sprintf("Signature (%s) decoding error: %v\n", signature, sigBytesErr)) + log.Errorf("Signature (%s) decoding error: %v\n", signature, sigBytesErr) } } } else if strings.ToLower(whatToDo) == "sign" || strings.ToLower(whatToDo) == "both" { @@ -285,7 +285,7 @@ func doStandardMode() { // send signed commitment sendErr := send(sigBytes, commitment) if sendErr != nil { - log.Error(fmt.Sprintf("Commitment send error: %v\n", sendErr)) + log.Errorf("Commitment send error: %v\n", sendErr) } log.Infoln("Success!") } diff --git a/staychain/chainverifier.go b/staychain/chainverifier.go index 637747b..17a7877 100644 --- a/staychain/chainverifier.go +++ b/staychain/chainverifier.go @@ -100,7 +100,7 @@ func NewChainVerifier(cfgMain *chaincfg.Params, side clients.SidechainClient, po // get chaincodes of pubkeys from config if len(chaincodesStr) != len(pubkeys) { - log.Fatal(fmt.Sprintf("Missing chaincodes for pubkeys %d != %d", len(chaincodesStr), len(pubkeys))) + log.Fatalf("Missing chaincodes for pubkeys %d != %d", len(chaincodesStr), len(pubkeys)) } // get chaincode bytes @@ -108,7 +108,7 @@ func NewChainVerifier(cfgMain *chaincfg.Params, side clients.SidechainClient, po for i_c := range chaincodesStr { ccBytes, ccBytesErr := hex.DecodeString(chaincodesStr[i_c]) if ccBytesErr != nil || len(ccBytes) != 32 { - log.Fatal(fmt.Sprintf("Invalid chaincode provided %s", chaincodesStr[i_c])) + log.Fatalf("Invalid chaincode provided %s", chaincodesStr[i_c]) } chaincodes[i_c] = append(chaincodes[i_c], ccBytes...) } From f4d5624bd052ac2baea38408405f9a69a63ff937 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Mon, 11 Nov 2019 16:26:41 +0000 Subject: [PATCH 07/14] timing precision - issue 123 --- attestation/attestfees.go | 12 ++++++------ attestation/attestservice.go | 22 ++++++++++++---------- attestation/attestservice_test.go | 2 +- config/conf.json | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/attestation/attestfees.go b/attestation/attestfees.go index a7c1c5c..fd54398 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -25,9 +25,9 @@ const ( // warnings for arguments const ( - WarningInvalidMinFeeArg = "Warning - Invalid min fee config value" - WarningInvalidMaxFeeArg = "Warning - Invalid max fee config value" - WarningInvalidFeeIncrementArg = "Warning - Invalid fee increment config value" + WarningInvalidMinFeeArg = "Invalid min fee config value" + WarningInvalidMaxFeeArg = "Invalid max fee config value" + WarningInvalidFeeIncrementArg = "Invalid fee increment config value" ) // fee api config @@ -69,7 +69,7 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { if feesConfig.MinFee > 0 && feesConfig.MinFee < DefaultMaxFee { minFee = feesConfig.MinFee } else { - log.Infof("%s (%d)\n", WarningInvalidMinFeeArg, feesConfig.MinFee) + log.Warnf("%s (%d)\n", WarningInvalidMinFeeArg, feesConfig.MinFee) } log.Infof("*Fees* Min fee set to: %d\n", minFee) @@ -78,7 +78,7 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { if feesConfig.MaxFee > 0 && feesConfig.MaxFee > minFee && feesConfig.MaxFee < DefaultMaxFee { maxFee = feesConfig.MaxFee } else { - log.Infof("%s (%d)\n", WarningInvalidMaxFeeArg, feesConfig.MaxFee) + log.Warnf("%s (%d)\n", WarningInvalidMaxFeeArg, feesConfig.MaxFee) } log.Infof("*Fees* Max fee set to: %d\n", maxFee) @@ -87,7 +87,7 @@ func NewAttestFees(feesConfig config.FeesConfig) AttestFees { if feesConfig.FeeIncrement > 0 { feeIncrement = feesConfig.FeeIncrement } else { - log.Infof("%s (%d)\n", WarningInvalidFeeIncrementArg, feesConfig.FeeIncrement) + log.Warnf("%s (%d)\n", WarningInvalidFeeIncrementArg, feesConfig.FeeIncrement) } log.Infof("*Fees* Fee increment set to: %d\n", feeIncrement) diff --git a/attestation/attestservice.go b/attestation/attestservice.go index 625f2d9..6a2db36 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -44,8 +44,8 @@ const ( const ( ErroUnspentNotFound = "No valid unspent found" - WarningInvalidATimeNewAttestationArg = "Warning - Invalid new attestation time config value" - WarningInvalidATimeHandleUnconfirmedArg = "Warning - Invalid handle unconfirmed time config value" + WarningInvalidATimeNewAttestationArg = "Invalid new attestation time config value" + WarningInvalidATimeHandleUnconfirmedArg = "Invalid handle unconfirmed time config value" ) // waiting time schedules @@ -127,14 +127,14 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer if config.TimingConfig().NewAttestationMinutes > 0 { atimeNewAttestation = time.Duration(config.TimingConfig().NewAttestationMinutes) * time.Minute } else { - log.Infof("%s (%v)\n", WarningInvalidATimeNewAttestationArg, config.TimingConfig().NewAttestationMinutes) + log.Warnf("%s (%v)\n", WarningInvalidATimeNewAttestationArg, config.TimingConfig().NewAttestationMinutes) } log.Infof("Time new attestation set to: %v\n", atimeNewAttestation) atimeHandleUnconfirmed = DefaultATimeHandleUnconfirmed if config.TimingConfig().HandleUnconfirmedMinutes > 0 { atimeHandleUnconfirmed = time.Duration(config.TimingConfig().HandleUnconfirmedMinutes) * time.Minute } else { - log.Infof("%s (%v)\n", WarningInvalidATimeHandleUnconfirmedArg, config.TimingConfig().HandleUnconfirmedMinutes) + log.Warnf("%s (%v)\n", WarningInvalidATimeHandleUnconfirmedArg, config.TimingConfig().HandleUnconfirmedMinutes) } log.Infof("Time handle unconfirmed set to: %v\n", atimeHandleUnconfirmed) @@ -170,8 +170,8 @@ func (s *AttestService) Run() { // AStateError // - Print error state and re-initiate attestation func (s *AttestService) doStateError() { - log.Infoln("*AttestService* ATTESTATION SERVICE FAILURE") - log.Infoln(s.errorState) + log.Warnln("*AttestService* ATTESTATION SERVICE FAILURE") + log.Warnln(s.errorState) s.state = AStateInit // update attestation state } @@ -183,7 +183,7 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { if s.setFailure(commitmentErr) { return // will rebound to init } - log.Infof("********** found unconfirmed attestation: %s\n", unconfirmedTxid.String()) + log.Warnf("********** found unconfirmed attestation: %s\n", unconfirmedTxid.String()) s.attestation = models.NewAttestation(unconfirmedTxid, &commitment) // initialise attestation rawTx, _ := s.config.MainClient().GetRawTransaction(&unconfirmedTxid) s.attestation.Tx = *rawTx.MsgTx() // set msgTx @@ -246,7 +246,7 @@ func (s *AttestService) stateInitUnspent(unspent btcjson.ListUnspentResult) { // both latest unconfirmed and confirmed attestation addresses to wallet func (s *AttestService) stateInitWalletFailure() { - log.Infoln("********** wallet failure") + log.Warnln("********** wallet failure") // get last confirmed commitment from server lastCommitmentHash, latestErr := s.server.GetLatestAttestationCommitmentHash() @@ -526,8 +526,10 @@ func (s *AttestService) doStateAwaitConfirmation() { confirmedHash := s.attestation.CommitmentHash() s.signer.SendConfirmedHash((&confirmedHash).CloneBytes()) // update clients - s.state = AStateNextCommitment // update attestation state - attestDelay = atimeNewAttestation - time.Since(confirmTime) // add new attestation waiting time - subtract waiting time + 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 } else { attestDelay = ATimeConfirmation // add confirmation waiting time } diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index c66d624..902a98f 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 > (timeNew-time.Since(confirmTime))) + assert.Equal(t, true, attestDelay + ATimeSigs > (timeNew-time.Since(confirmTime))) assert.Equal(t, models.AttestationInfo{ Txid: txid.String(), Blockhash: walletTx.BlockHash, diff --git a/config/conf.json b/config/conf.json index 60e69a0..7704248 100644 --- a/config/conf.json +++ b/config/conf.json @@ -38,4 +38,4 @@ "newAttestationMinutes": "MAINSTAY_NEW_ATTESTATION_MINUTES", "handleUnconfirmedMinutes": "MAINSTAY_HANDLE_UNCONFIRMED_MINUTES" } -} +} \ No newline at end of file From 94a3e1324827f28df0f068e63974fd5dab6c69a7 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Mon, 11 Nov 2019 17:44:14 +0000 Subject: [PATCH 08/14] restart fee from unconfirmed - issue 122 --- attestation/attestfees.go | 5 +++++ attestation/attestservice.go | 6 ++++++ models/commitmentmerkletree.go | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/attestation/attestfees.go b/attestation/attestfees.go index fd54398..0e71ddd 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -143,6 +143,11 @@ func (a *AttestFees) BumpFee() { } } +// Manually set current fee +func (a *AttestFees) setCurrentFee(fee int) { + a.currentFee = fee +} + // getBestFee returns the best fee for the type requested from the API func getBestFee(customFeeType ...string) int { var feeType = DefaultBestFeeType diff --git a/attestation/attestservice.go b/attestation/attestservice.go index 6a2db36..dd156f8 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -138,6 +138,12 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer } log.Infof("Time handle unconfirmed set to: %v\n", atimeHandleUnconfirmed) + // If current unconfirmed tx exists set fee to unconfirmed tx's fee (mainstay restart) + if unconfirmed, unconfirmedTxid, _ := attester.getUnconfirmedTx(); unconfirmed { + fee, _ := config.MainClient().GetTransaction(&unconfirmedTxid) + attester.Fees.setCurrentFee(int(fee.Fee)) + } + return &AttestService{ctx, wg, config, attester, server, signer, AStateInit, models.NewAttestationDefault(), nil, config.Regtest()} } diff --git a/models/commitmentmerkletree.go b/models/commitmentmerkletree.go index c7a491f..062e456 100644 --- a/models/commitmentmerkletree.go +++ b/models/commitmentmerkletree.go @@ -27,7 +27,7 @@ func printMerkleTree(tree []*chainhash.Hash) { i += 1 log.Infof("%v ", hash) } - fmt.Println("") + log.Infoln("") } // Build merkle tree store from a list of commitments From 88624c0acfda5878d1609f893676055fb19816d1 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 12 Nov 2019 13:08:18 +0000 Subject: [PATCH 09/14] issue 122 unit test --- attestation/attestfees.go | 3 ++- attestation/attestservice.go | 4 +-- attestation/attestservice_test.go | 45 +++++++++++++++++++------------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/attestation/attestfees.go b/attestation/attestfees.go index 0e71ddd..b3d6152 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -143,8 +143,9 @@ func (a *AttestFees) BumpFee() { } } -// Manually set current fee +// Manually force set current fee regardless of max, min values func (a *AttestFees) setCurrentFee(fee int) { + log.Infof("*Fees* Current value set to: %d\n", a.currentFee) a.currentFee = fee } diff --git a/attestation/attestservice.go b/attestation/attestservice.go index dd156f8..282be8e 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -140,8 +140,8 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer // If current unconfirmed tx exists set fee to unconfirmed tx's fee (mainstay restart) if unconfirmed, unconfirmedTxid, _ := attester.getUnconfirmedTx(); unconfirmed { - fee, _ := config.MainClient().GetTransaction(&unconfirmedTxid) - attester.Fees.setCurrentFee(int(fee.Fee)) + tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) + attester.Fees.setCurrentFee(int(tx.Fee*100000000)) } return &AttestService{ctx, wg, config, attester, server, signer, AStateInit, models.NewAttestationDefault(), nil, config.Regtest()} diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index 902a98f..60a5131 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -194,7 +194,7 @@ func TestAttestService_Multi(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -301,7 +301,7 @@ func TestAttestService_Regular(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -371,7 +371,7 @@ func TestAttestService_Unconfirmed(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") _ = verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -408,7 +408,7 @@ func TestAttestService_Unconfirmed(t *testing.T) { assert.Equal(t, attestService.attester.Fees.minFee, attestService.attester.Fees.GetFee()) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashY, _ := chainhash.NewHashFromStr("baaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") _ = verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashY) @@ -485,7 +485,7 @@ func TestAttestService_WithTopup(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") _ = verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -536,7 +536,7 @@ func TestAttestService_WithTopup(t *testing.T) { verifyStateAwaitConfirmationToNextCommitment(t, attestService, config, txid, DefaultATimeNewAttestation) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashZ, _ := chainhash.NewHashFromStr("caaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") _ = verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashZ) @@ -607,7 +607,7 @@ func TestAttestService_FailureNextCommitment(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -650,7 +650,7 @@ func TestAttestService_FailureNewAttestation(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -662,7 +662,7 @@ func TestAttestService_FailureNewAttestation(t *testing.T) { // Test AStateInit -> AStateNextCommitment verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation attestService.doAttestation() assert.Equal(t, AStateNewAttestation, attestService.state) assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) @@ -696,7 +696,7 @@ func TestAttestService_FailureSignAttestation(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -711,7 +711,7 @@ func TestAttestService_FailureSignAttestation(t *testing.T) { // Test AStateInit -> AStateNextCommitment verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation attestService.doAttestation() assert.Equal(t, AStateNewAttestation, attestService.state) assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) @@ -747,7 +747,7 @@ func TestAttestService_FailurePreSendStore(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -764,7 +764,7 @@ func TestAttestService_FailurePreSendStore(t *testing.T) { // Test AStateInit -> AStateNextCommitment verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation attestService.doAttestation() assert.Equal(t, AStateNewAttestation, attestService.state) assert.Equal(t, latestCommitment.GetCommitmentHash(), attestService.attestation.CommitmentHash()) @@ -794,9 +794,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 for mainstay using uncomfirmed tx's fee upon restart + if i ==1 { + attestService.attester.Fees.setCurrentFee(23) + } + // Test initial state of attest service verifyStateInit(t, attestService) // Test AStateInit -> AStateNextCommitment @@ -814,7 +819,7 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { } // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr(fmt.Sprintf("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b%d", i)) latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -827,9 +832,15 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { // Test AStateSendAttestation -> AStateAwaitConfirmation txid := verifyStateSendAttestationToAwaitConfirmation(t, attestService) + // failure - re init attestation service attestService = NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) + // Test new fee set to unconfirmed tx's fee after restart + _, unconfirmedTxid, _ := attestService.attester.getUnconfirmedTx() + tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) + assert.Equal(t, attestService.attester.Fees.GetFee(), int(tx.Fee*100000000)) + // Test AStateInit -> AStateAwaitConfirmation verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) @@ -887,7 +898,7 @@ func TestAttestService_FailureAwaitConfirmation(t *testing.T) { verifyStateInitToNextCommitment(t, attestService) // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b7") latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -979,7 +990,7 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { } // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creationg new attestation + // set server commitment before creating new attestation hashX, _ := chainhash.NewHashFromStr(fmt.Sprintf("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b%d", i)) latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) From c4a8b71784371da1962efd380a9f2828640a2ddd Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 12 Nov 2019 17:14:49 +0000 Subject: [PATCH 10/14] SetCurrentFee unit test, corrections --- attestation/attestclient.go | 12 ++++++------ attestation/attestfees.go | 2 +- attestation/attestfees_test.go | 6 ++++++ attestation/attestservice.go | 10 +++------- attestation/attestservice_test.go | 6 +++--- db/db_mongo.go | 4 ++-- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 7f3b35d..26c5159 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -108,7 +108,7 @@ func parseTopupKeys(config *confpkg.Config, isSigner bool) *btcutil.WIF { } return topupWif } else { - log.Infoln(WarningTopupPkMissing) + log.Warnln(WarningTopupPkMissing) } } return nil @@ -157,7 +157,7 @@ func newMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcutil pubkeys, numOfSigs := crypto.ParseRedeemScript(multisig) // Verify same amount of sigs in topupscript and init script - if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); topUpnumOfSigs != topUpnumOfSigs { + if _, topUpnumOfSigs := crypto.ParseRedeemScript(config.TopupScript()); numOfSigs != topUpnumOfSigs { log.Errorf("%s. %d != %d", ErrorTopUpScriptNumSigs, numOfSigs, topUpnumOfSigs) } @@ -238,11 +238,11 @@ func NewAttestClient(config *confpkg.Config, signerFlag ...bool) *AttestClient { log.Infof("*Client* importing top-up addr: %s ...\n", topupAddrStr) importErr := config.MainClient().ImportAddressRescan(topupAddrStr, "", false) if importErr != nil { - log.Infof("%s (%s)\n%v\n", WarningFailureImportingTopupAddress, topupAddrStr, importErr) + log.Warnf("%s (%s)\n%v\n", WarningFailureImportingTopupAddress, topupAddrStr, importErr) } pkWifTopup = parseTopupKeys(config, isSigner) } else { - log.Infoln(WarningTopupInfoMissing) + log.Warnln(WarningTopupInfoMissing) } // main config @@ -403,7 +403,7 @@ func (w *AttestClient) createAttestation(paytoaddr btcutil.Address, unspent []bt // print warning if txout value less than 100*maxfee target if msgTx.TxOut[0].Value < 100*maxFee { - log.Infoln(WarningInsufficientFunds) + log.Warnln(WarningInsufficientFunds) } // add fees using best fee-per-byte estimate @@ -527,7 +527,7 @@ func (w *AttestClient) getTransactionPreImages(hash chainhash.Hash, msgTx *wire. if len(msgTx.TxIn) > 1 { topupScriptSer, topupDecodeErr := hex.DecodeString(w.scriptTopup) if topupDecodeErr != nil { - log.Infof("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) + log.warnf("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) return preImageTxs, nil } for i := 1; i < len(msgTx.TxIn); i++ { diff --git a/attestation/attestfees.go b/attestation/attestfees.go index b3d6152..05cf460 100644 --- a/attestation/attestfees.go +++ b/attestation/attestfees.go @@ -145,8 +145,8 @@ func (a *AttestFees) BumpFee() { // Manually force set current fee regardless of max, min values func (a *AttestFees) setCurrentFee(fee int) { - log.Infof("*Fees* Current value set to: %d\n", a.currentFee) a.currentFee = fee + log.Infof("*Fees* Current value set to: %d\n", a.currentFee) } // getBestFee returns the best fee for the type requested from the API diff --git a/attestation/attestfees_test.go b/attestation/attestfees_test.go index 9401e94..e672c47 100644 --- a/attestation/attestfees_test.go +++ b/attestation/attestfees_test.go @@ -44,6 +44,12 @@ func TestAttestFees(t *testing.T) { attestFees.BumpFee() assert.Equal(t, 90, attestFees.GetPrevFee()) assert.Equal(t, attestFees.maxFee, attestFees.GetFee()) + + // test setCurrentFee() + new_current := 7 + assert.NotEqual(t,new_current, attestFees.currentFee) + attestFees.setCurrentFee(new_current) + assert.Equal(t,new_current, attestFees.currentFee) } // Attest Fees test with custom feesConfig diff --git a/attestation/attestservice.go b/attestation/attestservice.go index 282be8e..bb6bab2 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -138,12 +138,6 @@ func NewAttestService(ctx context.Context, wg *sync.WaitGroup, server *AttestSer } log.Infof("Time handle unconfirmed set to: %v\n", atimeHandleUnconfirmed) - // If current unconfirmed tx exists set fee to unconfirmed tx's fee (mainstay restart) - if unconfirmed, unconfirmedTxid, _ := attester.getUnconfirmedTx(); unconfirmed { - tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) - attester.Fees.setCurrentFee(int(tx.Fee*100000000)) - } - return &AttestService{ctx, wg, config, attester, server, signer, AStateInit, models.NewAttestationDefault(), nil, config.Regtest()} } @@ -202,8 +196,10 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { s.signer.SendConfirmedHash((&lastCommitmentHash).CloneBytes()) // update clients s.state = AStateAwaitConfirmation // update attestation state - walletTx, _ := s.config.MainClient().GetTransaction(&unconfirmedTxid) + walletTx, _ := s.config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) confirmTime = time.Unix(walletTx.Time, 0) + + s.attester.Fees.setCurrentFee(int(walletTx.Fee*100000000)) // Set fee to unconfirmed tx's fee } // part of AStateInit diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index 60a5131..a5006b6 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -836,14 +836,14 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { // 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 fee after restart _, unconfirmedTxid, _ := attestService.attester.getUnconfirmedTx() tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) assert.Equal(t, attestService.attester.Fees.GetFee(), int(tx.Fee*100000000)) - // Test AStateInit -> AStateAwaitConfirmation - verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) - // generate new block to confirm attestation config.MainClient().Generate(1) rawTx, _ := config.MainClient().GetRawTransaction(&txid) diff --git a/db/db_mongo.go b/db/db_mongo.go index 6144fbd..dc6f1f0 100644 --- a/db/db_mongo.go +++ b/db/db_mongo.go @@ -422,14 +422,14 @@ func (d *DbMongo) GetAttestationMerkleCommitments(txid chainhash.Hash) ([]models for res.Next(d.ctx) { var commitmentDoc bsonx.Doc if err := res.Decode(&commitmentDoc); err != nil { - log.Infof("%s\n", BadDataMerkleCommitmentCol) + log.Warnf("%s\n", BadDataMerkleCommitmentCol) return []models.CommitmentMerkleCommitment{}, err } // decode document result to Commitment model and get hash commitmentModel := &models.CommitmentMerkleCommitment{} modelErr := models.GetModelFromDocument(&commitmentDoc, commitmentModel) if modelErr != nil { - log.Infof("%s\n", BadDataMerkleCommitmentCol) + log.Warnf("%s\n", BadDataMerkleCommitmentCol) return []models.CommitmentMerkleCommitment{}, modelErr } merkleCommitments = append(merkleCommitments, *commitmentModel) From 3203533dc41188d583ec51bb7920f4437cd72536 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 12 Nov 2019 17:39:46 +0000 Subject: [PATCH 11/14] removed tests for fee defualting before change. Made Travis happy --- attestation/attestclient.go | 2 +- attestation/attestservice_test.go | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 26c5159..08faf49 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -527,7 +527,7 @@ func (w *AttestClient) getTransactionPreImages(hash chainhash.Hash, msgTx *wire. if len(msgTx.TxIn) > 1 { topupScriptSer, topupDecodeErr := hex.DecodeString(w.scriptTopup) if topupDecodeErr != nil { - log.warnf("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) + log.Warnf("%s %s\n", WarningFailedDecodingTopupMultisig, w.scriptTopup) return preImageTxs, nil } for i := 1; i < len(msgTx.TxIn); i++ { diff --git a/attestation/attestservice_test.go b/attestation/attestservice_test.go index a5006b6..0db907f 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -147,8 +147,6 @@ func verifyStateHandleUnconfirmedToSignAttestation(t *testing.T, attestService * assert.Equal(t, 1, len(attestService.attestation.Tx.TxOut)) assert.Equal(t, 0, len(attestService.attestation.Tx.TxIn[0].SignatureScript)) assert.Equal(t, ATimeSigs, attestDelay) - assert.Equal(t, attestService.attester.Fees.minFee+attestService.attester.Fees.feeIncrement, - attestService.attester.Fees.GetFee()) } // Test Attest Service states @@ -1031,8 +1029,6 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { verifyStateAwaitConfirmationToHandleUnconfirmed(t, attestService) // Test AStateHandleUnconfirmed -> AStateSignAttestation verifyStateHandleUnconfirmedToSignAttestation(t, attestService) - assert.Equal(t, attestService.attester.Fees.minFee+attestService.attester.Fees.feeIncrement, - attestService.attester.Fees.GetFee()) // Test AStateSignAttestation -> AStatePreSendStore verifyStateSignAttestationToPreSendStore(t, attestService) @@ -1059,8 +1055,6 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { verifyStateAwaitConfirmationToHandleUnconfirmed(t, attestService) // Test AStateHandleUnconfirmed -> AStateSignAttestation verifyStateHandleUnconfirmedToSignAttestation(t, attestService) - assert.Equal(t, attestService.attester.Fees.minFee+attestService.attester.Fees.feeIncrement, - attestService.attester.Fees.GetFee()) // Test AStateSignAttestation -> AStatePreSendStore verifyStateSignAttestationToPreSendStore(t, attestService) From a0d0282587e8040a817930f2d9a448b282940002 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Fri, 15 Nov 2019 17:29:38 +0000 Subject: [PATCH 12/14] fix and improved tests --- attestation/attestclient.go | 5 + attestation/attestclient_test.go | 4 + attestation/attestservice.go | 6 +- attestation/attestservice_test.go | 180 +++++++++++++++++++++--------- 4 files changed, 141 insertions(+), 54 deletions(-) diff --git a/attestation/attestclient.go b/attestation/attestclient.go index 08faf49..229f7ae 100644 --- a/attestation/attestclient.go +++ b/attestation/attestclient.go @@ -462,6 +462,11 @@ 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 aca7cda..9d40b71 100644 --- a/attestation/attestclient_test.go +++ b/attestation/attestclient_test.go @@ -585,16 +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, 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))) 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, 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))) } diff --git a/attestation/attestservice.go b/attestation/attestservice.go index bb6bab2..d7157aa 100644 --- a/attestation/attestservice.go +++ b/attestation/attestservice.go @@ -199,7 +199,11 @@ func (s *AttestService) stateInitUnconfirmed(unconfirmedTxid chainhash.Hash) { walletTx, _ := s.config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) confirmTime = time.Unix(walletTx.Time, 0) - s.attester.Fees.setCurrentFee(int(walletTx.Fee*100000000)) // Set fee to unconfirmed tx's fee + //set fee to unconfirmed tx's fee + feePerByte := calcSignedTxFeePerByte(int(walletTx.Fee*float64(Coin)), s.attestation.Tx.SerializeSize()) + 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 0db907f..35e5f16 100644 --- a/attestation/attestservice_test.go +++ b/attestation/attestservice_test.go @@ -9,6 +9,7 @@ import ( "fmt" "testing" "time" + "math" confpkg "mainstay/config" "mainstay/db" @@ -125,11 +126,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + models.AttestationInfo{ + Txid: txid.String(), + Blockhash: walletTx.BlockHash, + Amount: rawTx.MsgTx().TxOut[0].Value, + Time: walletTx.Time}, + attestService.attestation.Info, + )) } // verify AStateAwaitConfirmation to AStateHandleUnconfirmed @@ -147,6 +151,20 @@ func verifyStateHandleUnconfirmedToSignAttestation(t *testing.T, attestService * assert.Equal(t, 1, len(attestService.attestation.Tx.TxOut)) assert.Equal(t, 0, len(attestService.attestation.Tx.TxIn[0].SignatureScript)) assert.Equal(t, ATimeSigs, attestDelay) + assert.Equal(t, attestService.attester.Fees.minFee+attestService.attester.Fees.feeIncrement, + 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 @@ -795,11 +813,6 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { for i := range []int{1,2,3} { attestService := NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) - // Manually set fee to test for mainstay using uncomfirmed tx's fee upon restart - if i ==1 { - attestService.attester.Fees.setCurrentFee(23) - } - // Test initial state of attest service verifyStateInit(t, attestService) // Test AStateInit -> AStateNextCommitment @@ -808,7 +821,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.Equal(t, prevAttestation.Info, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest(prevAttestation.Info, attestService.attestation.Info)) if attestService.attestation.Info.Time == 0 { assert.Equal(t, ATimeFixed, attestDelay) } else { @@ -817,7 +830,7 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { } // Test AStateNextCommitment -> AStateNewAttestation - // set server commitment before creating new attestation + // set server commitment before creationg new attestation hashX, _ := chainhash.NewHashFromStr(fmt.Sprintf("aaaaaaa1111d9a1e6cdc3418b54aa57747106bc75e9e84426661f27f98ada3b%d", i)) latestCommitment := verifyStateNextCommitmentToNewAttestation(t, attestService, dbFake, hashX) @@ -830,18 +843,12 @@ func TestAttestService_FailureSendAttestation(t *testing.T) { // 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 fee after restart - _, unconfirmedTxid, _ := attestService.attester.getUnconfirmedTx() - tx, _ := config.MainClient().GetMempoolEntry(unconfirmedTxid.String()) - assert.Equal(t, attestService.attester.Fees.GetFee(), int(tx.Fee*100000000)) - // generate new block to confirm attestation config.MainClient().Generate(1) rawTx, _ := config.MainClient().GetRawTransaction(&txid) @@ -851,11 +858,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + 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 @@ -866,11 +876,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + models.AttestationInfo{ + Txid: txid.String(), + Blockhash: walletTx.BlockHash, + Amount: rawTx.MsgTx().TxOut[0].Value, + Time: walletTx.Time}, + attestService.attestation.Info, + )) prevAttestation = attestService.attestation } @@ -918,11 +931,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + 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) @@ -932,11 +948,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + 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 @@ -946,11 +965,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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + models.AttestationInfo{ + Txid: txid.String(), + Blockhash: walletTx.BlockHash, + Amount: rawTx.MsgTx().TxOut[0].Value, + Time: walletTx.Time}, + attestService.attestation.Info, + )) } // Test Attest Service states @@ -979,7 +1001,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.Equal(t, prevAttestation.Info, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest(prevAttestation.Info, attestService.attestation.Info)) if attestService.attestation.Info.Time == 0 { assert.Equal(t, ATimeFixed, attestDelay) } else { @@ -1009,8 +1031,6 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { verifyStateAwaitConfirmationToHandleUnconfirmed(t, attestService) // Test AStateHandleUnconfirmed -> AStateSignAttestation verifyStateHandleUnconfirmedToSignAttestation(t, attestService) - assert.Equal(t, attestService.attester.Fees.minFee+attestService.attester.Fees.feeIncrement, - attestService.attester.Fees.GetFee()) // failure - re init attestation service with restart attestService = NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) @@ -1038,16 +1058,20 @@ func TestAttestService_FailureHandleUnconfirmed(t *testing.T) { // failure - re init attestation service with restart attestService = NewAttestService(nil, nil, server, NewAttestSignerFake([]*confpkg.Config{config}), config) attestService.attester.Fees.ResetFee(true) + // Test AStateInit -> AStateAwaitConfirmation verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) // failure - re init attestation service from inner state failure attestService.state = AStateInit + + // Test AStateInit -> AStateAwaitConfirmation + verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) + // second time bump fee manually and set is fee bumped flag attestService.attester.Fees.BumpFee() isFeeBumped = true - // Test AStateInit -> AStateAwaitConfirmation - verifyStateInitToAwaitConfirmation(t, attestService, config, latestCommitment, txid) + // set confirm time back to test what happens in handle unconfirmed case confirmTime = confirmTime.Add(-DefaultATimeHandleUnconfirmed) @@ -1084,12 +1108,62 @@ 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.Equal(t, models.AttestationInfo{ - Txid: txid.String(), - Blockhash: walletTx.BlockHash, - Amount: rawTx.MsgTx().TxOut[0].Value, - Time: walletTx.Time}, attestService.attestation.Info) + assert.True(t, EqualAttestationInfoTest( + models.AttestationInfo{ + Txid: txid.String(), + Blockhash: walletTx.BlockHash, + 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.Equal(t, attestService.attester.Fees.GetFee(), 23) // 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 +} From d8e7fb108c0d1e3a1a89abd852fb3e313dea8d20 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Tue, 19 Nov 2019 17:08:13 +0000 Subject: [PATCH 13/14] 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 } From b676059e698c4149bd921041d3cc78a13193e7e7 Mon Sep 17 00:00:00 2001 From: tomossomot Date: Thu, 21 Nov 2019 12:26:28 +0000 Subject: [PATCH 14/14] corrections --- attestation/attestclient.go | 4 +- attestation/attestclient_test.go | 2 +- attestation/attestfees_test.go | 4 +- attestation/attestservice.go | 10 ++- attestation/attestservice_test.go | 82 ++++++------------- attestation/attestsigner_zmq.go | 2 +- cmd/clientsignuptool/clientsignuptool.go | 4 +- .../tokengenerator/tokengeneratortool.go | 2 +- cmd/confirmationtool/confirmationtool.go | 2 +- cmd/txsigningtool/txsigningtool.go | 2 +- db/db_mongo.go | 2 +- log/log.go | 27 +++--- main.go | 2 +- models/commitmentmerkletree.go | 2 +- 14 files changed, 61 insertions(+), 86 deletions(-) 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"