Skip to content

Commit

Permalink
Add mock generation to makefile and cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Jan 5, 2018
1 parent 400dbfe commit 55bcd13
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 105 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ mock: ##@other Regenerate mocks
mockgen -source=geth/mailservice/mailservice.go -destination=geth/mailservice/mailservice_mock.go -package=mailservice
mockgen -source=geth/common/notification.go -destination=geth/common/notification_mock.go -package=common -imports fcm=github.com/NaySoftware/go-fcm
mockgen -source=geth/notification/fcm/client.go -destination=geth/notification/fcm/client_mock.go -package=fcm -imports fcm=github.com/NaySoftware/go-fcm
mockgen -source=geth/txqueue/fake/txservice.go -destination=geth/txqueue/fake/mock.go -package=fake

test: test-unit-coverage ##@tests Run basic, short tests during development

Expand Down
4 changes: 2 additions & 2 deletions geth/txqueue/ethtxclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/status-im/status-go/geth/rpc"
)

// EthereumTransactor provides methods to create transactions for ethereum network.
type EthereumTransactor interface {
// EthTransactor provides methods to create transactions for ethereum network.
type EthTransactor interface {
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
ethereum.GasEstimator
ethereum.GasPricer
Expand Down
73 changes: 36 additions & 37 deletions geth/txqueue/fake/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions geth/txqueue/fake/txservice.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package fake

import (
context "context"
big "math/big"
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
gomock "github.com/golang/mock/gomock"
"github.com/golang/mock/gomock"
)

func NewTestServer(ctrl *gomock.Controller) (*rpc.Server, *MockFakePublicTxApi) {
func NewTestServer(ctrl *gomock.Controller) (*rpc.Server, *MockFakePublicTransactionPoolAPI) {
srv := rpc.NewServer()
svc := NewMockFakePublicTxApi(ctrl)
svc := NewMockFakePublicTransactionPoolAPI(ctrl)
if err := srv.RegisterName("eth", svc); err != nil {
panic(err)
}
Expand All @@ -29,10 +29,10 @@ type CallArgs struct {
Data hexutil.Bytes `json:"data"`
}

// FakePublicTxApi used to generate mock by mockgen util.
// FakePublicTransactionPoolAPI used to generate mock by mockgen util.
// This was done because PublicTransactionPoolAPI is located in internal/ethapi module
// and there is no easy way to generate mocks from internal modules.
type FakePublicTxApi interface {
type FakePublicTransactionPoolAPI interface {
GasPrice(ctx context.Context) (*big.Int, error)
EstimateGas(ctx context.Context, args CallArgs) (*hexutil.Big, error)
GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*hexutil.Uint64, error)
Expand Down
22 changes: 9 additions & 13 deletions geth/txqueue/txqueue_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

defaultGas = 90000

cancelTimeout = time.Minute
defaultTimeout = time.Minute
)

// Send transaction response codes
Expand All @@ -51,7 +51,7 @@ type Manager struct {
nodeManager common.NodeManager
accountManager common.AccountManager
txQueue *TxQueue
ethTxClient EthereumTransactor
ethTxClient EthTransactor
addrLock *AddrLocker
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func (m *Manager) CompleteTransaction(id common.QueuedTxID, password string) (ge
return gethcommon.Hash{}, ErrInvalidCompleteTxSender
}
// Send the transaction finally.
hash, err := m.completeTransaction(selectedAccount, queuedTx, password)
hash, err := m.completeTransaction(queuedTx, selectedAccount, password)

// when incorrect sender tries to complete the account,
// notify and keep tx in queue (so that correct sender can complete)
Expand All @@ -183,10 +183,9 @@ func (m *Manager) CompleteTransaction(id common.QueuedTxID, password string) (ge
return hash, err
}

func (m *Manager) completeTransaction(selectedAccount *common.SelectedExtKey, queuedTx *common.QueuedTx, password string) (gethcommon.Hash, error) {
func (m *Manager) completeTransaction(queuedTx *common.QueuedTx, selectedAccount *common.SelectedExtKey, password string) (gethcommon.Hash, error) {
log.Info("complete transaction", "id", queuedTx.ID)
var emptyHash gethcommon.Hash
log.Info("verifying account password for transaction", "id", queuedTx.ID)
config, err := m.nodeManager.NodeConfig()
if err != nil {
return emptyHash, err
Expand All @@ -198,7 +197,7 @@ func (m *Manager) completeTransaction(selectedAccount *common.SelectedExtKey, qu
}

// update transaction with nonce, gas price and gas estimates
ctx, cancel := context.WithTimeout(context.Background(), cancelTimeout)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
m.addrLock.LockAddr(queuedTx.Args.From)
defer m.addrLock.UnlockAddr(queuedTx.Args.From)
Expand All @@ -207,17 +206,14 @@ func (m *Manager) completeTransaction(selectedAccount *common.SelectedExtKey, qu
return emptyHash, err
}
args := queuedTx.Args
var gasPrice *big.Int
gasPrice := (*big.Int)(args.GasPrice)
if args.GasPrice == nil {
ctx, cancel = context.WithTimeout(context.Background(), cancelTimeout)
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
gasPrice, err = m.ethTxClient.SuggestGasPrice(ctx)
if err != nil {
return emptyHash, err
}

} else {
gasPrice = (*big.Int)(args.GasPrice)
}

chainID := big.NewInt(int64(config.NetworkID))
Expand All @@ -227,7 +223,7 @@ func (m *Manager) completeTransaction(selectedAccount *common.SelectedExtKey, qu
if args.To != nil {
toAddr = *args.To
}
ctx, cancel = context.WithTimeout(context.Background(), cancelTimeout)
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
gas, err := m.ethTxClient.EstimateGas(ctx, ethereum.CallMsg{
From: args.From,
Expand Down Expand Up @@ -257,7 +253,7 @@ func (m *Manager) completeTransaction(selectedAccount *common.SelectedExtKey, qu
if err != nil {
return emptyHash, err
}
ctx, cancel = context.WithTimeout(context.Background(), cancelTimeout)
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if err := m.ethTxClient.SendTransaction(ctx, signedTx); err != nil {
return emptyHash, err
Expand Down
Loading

0 comments on commit 55bcd13

Please sign in to comment.