Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
86667 committed Nov 21, 2019
1 parent d8e7fb1 commit b676059
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 86 deletions.
4 changes: 2 additions & 2 deletions attestation/attestclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package attestation
import (
"encoding/hex"
"errors"
"math"
"fmt"
"math"

confpkg "mainstay/config"
"mainstay/crypto"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion attestation/attestclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions attestation/attestfees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions attestation/attestservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
82 changes: 24 additions & 58 deletions attestation/attestservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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(),
Blockhash: walletTx.BlockHash,
Amount: rawTx.MsgTx().TxOut[0].Value,
Time: walletTx.Time},
attestService.attestation.Info,
)
)
}

// verify AStateAwaitConfirmation to AStateHandleUnconfirmed
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion attestation/attestsigner_zmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

confpkg "mainstay/config"
"mainstay/crypto"
"mainstay/messengers"
"mainstay/log"
"mainstay/messengers"

zmq "github.com/pebbe/zmq4"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/clientsignuptool/clientsignuptool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cmd/clientsignuptool/tokengenerator/tokengeneratortool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package main
// Token generator

import (
"mainstay/log"
"github.com/satori/go.uuid"
"mainstay/log"
)

// main
Expand Down
2 changes: 1 addition & 1 deletion cmd/confirmationtool/confirmationtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"mainstay/clients"
"mainstay/config"
"mainstay/staychain"
"mainstay/log"
"mainstay/staychain"

"github.com/btcsuite/btcd/chaincfg/chainhash"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/txsigningtool/txsigningtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion db/db_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 16 additions & 11 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package log

import (
"log"
"os"
"fmt"
"fmt"
"log"
"os"
)

/*
Expand All @@ -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
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"mainstay/attestation"
"mainstay/config"
"mainstay/db"
"mainstay/test"
"mainstay/log"
"mainstay/test"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion models/commitmentmerkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package models

import (
_ "errors"
"math"
"fmt"
"math"

"mainstay/log"

Expand Down

0 comments on commit b676059

Please sign in to comment.