diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..17bee39 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,67 @@ +name: CI + +on: [push, pull_request] + +concurrency: + group: ${{github.workflow}}-${{github.ref}} + cancel-in-progress: true + +jobs: + lint: + name: Run Tests + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '^1.22.3' + + - name: Cache Go Modules + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + /go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Install Dependencies + run: go clean && go mod download && go mod verify + + - name: Check Formatting + run: | + output=$(gofmt -l .) + if [ -n "$output" ]; then + echo "The following files need formatting:" + echo "$output" + exit 1 + else + echo "All files are properly formatted." + fi + + - name: Run Static Code Analysis + run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest && golangci-lint run + + - name: Run Unit Tests + run: bash coverage.sh + + - name: Upload Test Coverage + uses: actions/upload-artifact@v3 + with: + name: coverage-report + path: coverage.out + + - name: Build + run: go build -o wpokt-oracle . + + - name: Upload Build Artifact + uses: actions/upload-artifact@v3 + with: + name: wpokt-oracle + path: wpokt-oracle diff --git a/cosmos/monitor_test.go b/cosmos/monitor_test.go index fe96fad..ded3d04 100644 --- a/cosmos/monitor_test.go +++ b/cosmos/monitor_test.go @@ -21,6 +21,23 @@ import ( log "github.com/sirupsen/logrus" ) +func TestMonitorHeight(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + currentBlockHeight: 100, + } + + height := monitor.Height() + + assert.Equal(t, uint64(100), height) +} + func TestMonitorUpdateCurrentHeight(t *testing.T) { mockDB := dbMocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) @@ -32,193 +49,1146 @@ func TestMonitorUpdateCurrentHeight(t *testing.T) { logger: logger, } - mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), nil) + mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), nil) + + monitor.UpdateCurrentHeight() + + mockClient.AssertExpectations(t) + assert.Equal(t, uint64(100), monitor.currentBlockHeight) +} + +func TestMonitorUpdateCurrentHeight_Error(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), assert.AnError) + + monitor.UpdateCurrentHeight() + + mockClient.AssertExpectations(t) + assert.Equal(t, uint64(0), monitor.currentBlockHeight) +} + +func TestCreateRefund(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + + txRes := &sdk.TxResponse{} + txDoc := &models.Transaction{} + toAddr := []byte("some-address") + amount := sdk.NewCoin("token", math.NewInt(100)) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + } + + mockDB.EXPECT().NewRefund(txRes, txDoc, toAddr, amount).Return(models.Refund{}, nil) + mockDB.EXPECT().InsertRefund(mock.Anything).Return(primitive.ObjectID{}, nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) + + result := monitor.CreateRefund(txRes, txDoc, toAddr, amount) + + mockDB.AssertExpectations(t) + assert.True(t, result) +} + +func TestCreateRefund_NewError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + + txRes := &sdk.TxResponse{} + txDoc := &models.Transaction{} + toAddr := []byte("some-address") + amount := sdk.NewCoin("token", math.NewInt(100)) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + } + + mockDB.EXPECT().NewRefund(txRes, txDoc, toAddr, amount).Return(models.Refund{}, assert.AnError) + + result := monitor.CreateRefund(txRes, txDoc, toAddr, amount) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateRefund_InsertError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + + txRes := &sdk.TxResponse{} + txDoc := &models.Transaction{} + toAddr := []byte("some-address") + amount := sdk.NewCoin("token", math.NewInt(100)) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + } + + mockDB.EXPECT().NewRefund(txRes, txDoc, toAddr, amount).Return(models.Refund{}, nil) + mockDB.EXPECT().InsertRefund(mock.Anything).Return(primitive.ObjectID{}, assert.AnError) + + result := monitor.CreateRefund(txRes, txDoc, toAddr, amount) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateRefund_UpdateError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + + txRes := &sdk.TxResponse{} + txDoc := &models.Transaction{} + toAddr := []byte("some-address") + amount := sdk.NewCoin("token", math.NewInt(100)) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + } + + mockDB.EXPECT().NewRefund(txRes, txDoc, toAddr, amount).Return(models.Refund{}, nil) + mockDB.EXPECT().InsertRefund(mock.Anything).Return(primitive.ObjectID{}, nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(assert.AnError) + + result := monitor.CreateRefund(txRes, txDoc, toAddr, amount) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) + mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) + mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.True(t, result) +} + +func TestCreateMessage_AddressError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: "0xaddress", ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_NewBodyError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, assert.AnError) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_SignerInfoError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_NewContentError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, assert.AnError) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_MintControllerError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_NewMessageError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) + mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, assert.AnError) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_InsertError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) + mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) + mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, assert.AnError) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestCreateMessage_UpdateTxError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} + + txRes := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + txDoc := &models.Transaction{} + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + logger: logger, + mintControllerMap: mintControllerMap, + } + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) + mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) + mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(assert.AnError) + + result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + + mockDB.AssertExpectations(t) + assert.False(t, result) +} + +func TestSyncNewTxs(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 1, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + txResponses := []*sdk.TxResponse{ + {TxHash: "tx1"}, + {TxHash: "tx2"}, + } + + mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, nil).Once() + mockDB.EXPECT().NewCosmosTransaction(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.Transaction{}, nil).Twice() + mockDB.EXPECT().InsertTransaction(mock.Anything).Return(primitive.ObjectID{}, nil).Twice() + result := &util.ValidateTxResult{ + Confirmations: 0, + TxStatus: models.TransactionStatusPending, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.True(t, success) +} + +func TestSyncNewTxs_NoNewBlocks(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 10, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.True(t, success) +} + +func TestSyncNewTxs_ClientError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 1, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + txResponses := []*sdk.TxResponse{ + {TxHash: "tx1"}, + {TxHash: "tx2"}, + } + + mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, assert.AnError).Once() + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, success) +} + +func TestSyncNewTxs_ValidateError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 1, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + txResponses := []*sdk.TxResponse{ + {TxHash: "tx1"}, + {TxHash: "tx2"}, + } + + mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, nil).Once() + result := &util.ValidateTxResult{ + Confirmations: 0, + TxStatus: models.TransactionStatusPending, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, assert.AnError + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, success) +} + +func TestSyncNewTxs_NewError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 1, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + txResponses := []*sdk.TxResponse{ + {TxHash: "tx1"}, + {TxHash: "tx2"}, + } + + mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, nil).Once() + mockDB.EXPECT().NewCosmosTransaction(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.Transaction{}, assert.AnError).Once() + mockDB.EXPECT().NewCosmosTransaction(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.Transaction{}, nil).Once() + mockDB.EXPECT().InsertTransaction(mock.Anything).Return(primitive.ObjectID{}, nil).Once() + result := &util.ValidateTxResult{ + Confirmations: 0, + TxStatus: models.TransactionStatusPending, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, success) +} + +func TestSyncNewTxs_InsertError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + startBlockHeight: 1, + currentBlockHeight: 10, + config: models.CosmosNetworkConfig{ + MultisigAddress: multisigAddress.Hex(), + }, + multisigAddressBytes: multisigAddress.Bytes(), + } + + txResponses := []*sdk.TxResponse{ + {TxHash: "tx1"}, + {TxHash: "tx2"}, + } + + mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, nil).Once() + mockDB.EXPECT().NewCosmosTransaction(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.Transaction{}, nil).Twice() + mockDB.EXPECT().InsertTransaction(mock.Anything).Return(primitive.ObjectID{}, assert.AnError).Once() + mockDB.EXPECT().InsertTransaction(mock.Anything).Return(primitive.ObjectID{}, nil).Once() + result := &util.ValidateTxResult{ + Confirmations: 0, + TxStatus: models.TransactionStatusPending, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.SyncNewTxs() + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, success) +} + +func TestValidateAndConfirmTx(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txResponse := &sdk.TxResponse{} + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, bson.M{ + "confirmations": uint64(2), + "status": models.TransactionStatusConfirmed, + }).Return(nil) + + valid := monitor.ValidateAndConfirmTx(txDoc) + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.True(t, valid) +} + +func TestValidateAndConfirmTx_ClientError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txResponse := &sdk.TxResponse{} + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, assert.AnError) + + valid := monitor.ValidateAndConfirmTx(txDoc) + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, valid) +} + +func TestValidateAndConfirmTx_ValidateError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txResponse := &sdk.TxResponse{} + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, assert.AnError + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + valid := monitor.ValidateAndConfirmTx(txDoc) + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, valid) +} + +func TestValidateAndConfirmTx_UpdateError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txResponse := &sdk.TxResponse{} + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + } + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, bson.M{ + "confirmations": uint64(2), + "status": models.TransactionStatusConfirmed, + }).Return(assert.AnError) + + valid := monitor.ValidateAndConfirmTx(txDoc) + + mockClient.AssertExpectations(t) + mockDB.AssertExpectations(t) + assert.False(t, valid) +} + +func TestConfirmTxs(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txs := []models.Transaction{ + {ID: &primitive.ObjectID{}, Hash: "hash1"}, + {ID: &primitive.ObjectID{}, Hash: "hash2"}, + } + + mockDB.EXPECT().GetPendingTransactionsTo(mock.Anything, mock.Anything).Return(txs, nil) + mockClient.EXPECT().GetTx("hash1").Return(&sdk.TxResponse{}, nil) + mockClient.EXPECT().GetTx("hash2").Return(&sdk.TxResponse{}, nil) + + mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, mock.Anything).Return(nil) + mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, mock.Anything).Return(nil) + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + }, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.ConfirmTxs() + + mockDB.AssertExpectations(t) + mockClient.AssertExpectations(t) + assert.True(t, success) +} + +func TestConfirmTxs_ClientError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + txs := []models.Transaction{ + {ID: &primitive.ObjectID{}, Hash: "hash1"}, + {ID: &primitive.ObjectID{}, Hash: "hash2"}, + } + + mockDB.EXPECT().GetPendingTransactionsTo(mock.Anything, mock.Anything).Return(txs, assert.AnError) + + success := monitor.ConfirmTxs() + + mockDB.AssertExpectations(t) + mockClient.AssertExpectations(t) + assert.False(t, success) +} + +func TestValidateTxAndCreate(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + amountCoin := sdk.NewCoin("token", math.NewInt(100)) + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, + } + + txResponse := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + NeedsRefund: false, + SenderAddress: senderAddress.Bytes(), + Amount: sdk.NewCoin("token", math.NewInt(100)), + Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, + Tx: tx, + } + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + mockDB.EXPECT().LockWriteTransaction(txDoc).Return("lock-id", nil) + mockDB.EXPECT().Unlock("lock-id").Return(nil) + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, nil + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) + mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) + mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) + mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) - monitor.UpdateCurrentHeight() + success := monitor.ValidateTxAndCreate(txDoc) + mockDB.AssertExpectations(t) mockClient.AssertExpectations(t) - assert.Equal(t, uint64(100), monitor.currentBlockHeight) + assert.True(t, success) } -func TestCreateRefund(t *testing.T) { +func TestValidateTxAndCreate_ClientError(t *testing.T) { mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") - txRes := &sdk.TxResponse{} - txDoc := &models.Transaction{} - toAddr := []byte("some-address") - amount := sdk.NewCoin("token", math.NewInt(100)) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } monitor := &CosmosMessageMonitorRunnable{ - db: mockDB, - logger: logger, + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, } - mockDB.EXPECT().NewRefund(txRes, txDoc, toAddr, amount).Return(models.Refund{}, nil) - mockDB.EXPECT().InsertRefund(mock.Anything).Return(primitive.ObjectID{}, nil) - mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) + txResponse := &sdk.TxResponse{} - result := monitor.CreateRefund(txRes, txDoc, toAddr, amount) + mockClient.EXPECT().GetTx("hash1").Return(txResponse, assert.AnError) + + success := monitor.ValidateTxAndCreate(txDoc) mockDB.AssertExpectations(t) - assert.True(t, result) + mockClient.AssertExpectations(t) + assert.False(t, success) } -func TestCreateMessage(t *testing.T) { +func TestValidateTxAndCreate_ValidateError(t *testing.T) { mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) - amountCoin := sdk.NewCoin("token", math.NewInt(100)) - memo := models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"} - - txRes := &sdk.TxResponse{} - tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} - txDoc := &models.Transaction{} mintControllerMap := make(map[uint32][]byte) mintControllerMap[1] = mintControllerAddress.Bytes() + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", + } + monitor := &CosmosMessageMonitorRunnable{ db: mockDB, + client: mockClient, logger: logger, mintControllerMap: mintControllerMap, } - mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) - mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) - mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) - mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, nil) - mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) + txResponse := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} - result := monitor.CreateMessage(txRes, tx, txDoc, senderAddress[:], amountCoin, memo) + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + NeedsRefund: false, + SenderAddress: senderAddress.Bytes(), + Amount: sdk.NewCoin("token", math.NewInt(100)), + Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, + Tx: tx, + } + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { + return result, assert.AnError + } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() + + success := monitor.ValidateTxAndCreate(txDoc) mockDB.AssertExpectations(t) - assert.True(t, result) + mockClient.AssertExpectations(t) + assert.False(t, success) } -func TestSyncNewTxs(t *testing.T) { +func TestValidateTxAndCreate_TxPending(t *testing.T) { mockDB := dbMocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") - multisigAddress := ethcommon.BytesToAddress([]byte("multisigAddress")) - monitor := &CosmosMessageMonitorRunnable{ - db: mockDB, - client: mockClient, - logger: logger, - startBlockHeight: 1, - currentBlockHeight: 10, - config: models.CosmosNetworkConfig{ - MultisigAddress: multisigAddress.Hex(), - }, - multisigAddressBytes: multisigAddress.Bytes(), + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", } - txResponses := []*sdk.TxResponse{ - {TxHash: "tx1"}, - {TxHash: "tx2"}, + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, } - mockClient.EXPECT().GetTxsSentToAddressAfterHeight(multisigAddress.Hex(), uint64(1)).Return(txResponses, nil).Once() - mockDB.EXPECT().NewCosmosTransaction(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.Transaction{}, nil).Twice() - mockDB.EXPECT().InsertTransaction(mock.Anything).Return(primitive.ObjectID{}, nil).Twice() + txResponse := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} + result := &util.ValidateTxResult{ - Confirmations: 0, + Confirmations: 2, TxStatus: models.TransactionStatusPending, + NeedsRefund: false, + SenderAddress: senderAddress.Bytes(), + Amount: sdk.NewCoin("token", math.NewInt(100)), + Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, + Tx: tx, } + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { return result, nil } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() - success := monitor.SyncNewTxs() + success := monitor.ValidateTxAndCreate(txDoc) - mockClient.AssertExpectations(t) mockDB.AssertExpectations(t) - assert.True(t, success) + mockClient.AssertExpectations(t) + assert.False(t, success) } -func TestValidateAndConfirmTx(t *testing.T) { +func TestValidateTxAndCreate_TxInvalid(t *testing.T) { mockDB := dbMocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + txDoc := &models.Transaction{ ID: &primitive.ObjectID{}, Hash: "hash1", } monitor := &CosmosMessageMonitorRunnable{ - db: mockDB, - client: mockClient, - logger: logger, + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, } txResponse := &sdk.TxResponse{} - - mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} result := &util.ValidateTxResult{ Confirmations: 2, - TxStatus: models.TransactionStatusConfirmed, + TxStatus: models.TransactionStatusInvalid, + NeedsRefund: false, + SenderAddress: senderAddress.Bytes(), + Amount: sdk.NewCoin("token", math.NewInt(100)), + Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, + Tx: tx, } + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { return result, nil } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() - mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, bson.M{ - "confirmations": uint64(2), - "status": models.TransactionStatusConfirmed, - }).Return(nil) + mockDB.EXPECT().UpdateTransaction(txDoc.ID, bson.M{"status": result.TxStatus}).Return(nil) - valid := monitor.ValidateAndConfirmTx(txDoc) + success := monitor.ValidateTxAndCreate(txDoc) - mockClient.AssertExpectations(t) mockDB.AssertExpectations(t) - assert.True(t, valid) + mockClient.AssertExpectations(t) + assert.True(t, success) } -func TestConfirmTxs(t *testing.T) { +func TestValidateTxAndCreate_LockError(t *testing.T) { mockDB := dbMocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") - monitor := &CosmosMessageMonitorRunnable{ - db: mockDB, - client: mockClient, - logger: logger, + senderAddress := ethcommon.BytesToAddress([]byte("sender")) + recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + txDoc := &models.Transaction{ + ID: &primitive.ObjectID{}, + Hash: "hash1", } - txs := []models.Transaction{ - {ID: &primitive.ObjectID{}, Hash: "hash1"}, - {ID: &primitive.ObjectID{}, Hash: "hash2"}, + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, } - mockDB.EXPECT().GetPendingTransactionsTo(mock.Anything, mock.Anything).Return(txs, nil) - mockClient.EXPECT().GetTx("hash1").Return(&sdk.TxResponse{}, nil) - mockClient.EXPECT().GetTx("hash2").Return(&sdk.TxResponse{}, nil) + txResponse := &sdk.TxResponse{} + tx := &tx.Tx{AuthInfo: &tx.AuthInfo{SignerInfos: []*tx.SignerInfo{{Sequence: 1}}}} - mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, mock.Anything).Return(nil) - mockDB.EXPECT().UpdateTransaction(&primitive.ObjectID{}, mock.Anything).Return(nil) + result := &util.ValidateTxResult{ + Confirmations: 2, + TxStatus: models.TransactionStatusConfirmed, + NeedsRefund: false, + SenderAddress: senderAddress.Bytes(), + Amount: sdk.NewCoin("token", math.NewInt(100)), + Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, + Tx: tx, + } + + mockClient.EXPECT().GetTx("hash1").Return(txResponse, nil) + mockDB.EXPECT().LockWriteTransaction(txDoc).Return("lock-id", assert.AnError) utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { - return &util.ValidateTxResult{ - Confirmations: 2, - TxStatus: models.TransactionStatusConfirmed, - }, nil + return result, nil } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() - success := monitor.ConfirmTxs() + success := monitor.ValidateTxAndCreate(txDoc) mockDB.AssertExpectations(t) mockClient.AssertExpectations(t) - assert.True(t, success) + assert.False(t, success) } -func TestValidateTxAndCreate(t *testing.T) { +func TestValidateTxAndCreate_Refund(t *testing.T) { mockDB := dbMocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "monitor") @@ -226,7 +1196,6 @@ func TestValidateTxAndCreate(t *testing.T) { senderAddress := ethcommon.BytesToAddress([]byte("sender")) recipientAddress := ethcommon.BytesToAddress([]byte("recipient")) mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) - amountCoin := sdk.NewCoin("token", math.NewInt(100)) mintControllerMap := make(map[uint32][]byte) mintControllerMap[1] = mintControllerAddress.Bytes() @@ -249,7 +1218,7 @@ func TestValidateTxAndCreate(t *testing.T) { result := &util.ValidateTxResult{ Confirmations: 2, TxStatus: models.TransactionStatusConfirmed, - NeedsRefund: false, + NeedsRefund: true, SenderAddress: senderAddress.Bytes(), Amount: sdk.NewCoin("token", math.NewInt(100)), Memo: models.MintMemo{Address: recipientAddress.Hex(), ChainID: "1"}, @@ -263,11 +1232,12 @@ func TestValidateTxAndCreate(t *testing.T) { utilValidateTxToCosmosMultisig = func(*sdk.TxResponse, models.CosmosNetworkConfig, map[uint32]bool, uint64) (*util.ValidateTxResult, error) { return result, nil } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() - mockDB.EXPECT().NewMessageBody(senderAddress[:], amountCoin.Amount.BigInt(), recipientAddress[:]).Return(models.MessageBody{}, nil) - mockDB.EXPECT().NewMessageContent(uint32(1), uint32(0), senderAddress[:], uint32(1), mintControllerAddress[:], models.MessageBody{}).Return(models.MessageContent{}, nil) - mockDB.EXPECT().NewMessage(txDoc, mock.Anything, models.MessageStatusPending).Return(models.Message{}, nil) - mockDB.EXPECT().InsertMessage(mock.Anything).Return(primitive.ObjectID{}, nil) + amount := sdk.NewCoin("token", math.NewInt(100)) + + mockDB.EXPECT().NewRefund(txResponse, txDoc, senderAddress.Bytes(), amount).Return(models.Refund{}, nil) + mockDB.EXPECT().InsertRefund(mock.Anything).Return(primitive.ObjectID{}, nil) mockDB.EXPECT().UpdateTransaction(txDoc.ID, mock.Anything).Return(nil) success := monitor.ValidateTxAndCreate(txDoc) @@ -318,6 +1288,7 @@ func TestCreateRefundsOrMessagesForConfirmedTxs(t *testing.T) { Tx: tx, }, nil } + defer func() { utilValidateTxToCosmosMultisig = util.ValidateTxToCosmosMultisig }() mockDB.EXPECT().NewMessageBody(mock.Anything, mock.Anything, mock.Anything).Return(models.MessageBody{}, nil) mockDB.EXPECT().NewMessageContent(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(models.MessageContent{}, nil) @@ -332,6 +1303,34 @@ func TestCreateRefundsOrMessagesForConfirmedTxs(t *testing.T) { assert.True(t, success) } +func TestCreateRefundsOrMessagesForConfirmedTxs_ClientError(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "monitor") + mintControllerAddress := ethcommon.BytesToAddress([]byte("mintController")) + mintControllerMap := make(map[uint32][]byte) + mintControllerMap[1] = mintControllerAddress.Bytes() + + monitor := &CosmosMessageMonitorRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + mintControllerMap: mintControllerMap, + } + + txs := []models.Transaction{ + {ID: &primitive.ObjectID{}, Hash: "hash1"}, + {ID: &primitive.ObjectID{}, Hash: "hash2"}, + } + + mockDB.EXPECT().GetConfirmedTransactionsTo(mock.Anything, mock.Anything).Return(txs, assert.AnError) + success := monitor.CreateRefundsOrMessagesForConfirmedTxs() + + mockDB.AssertExpectations(t) + mockClient.AssertExpectations(t) + assert.False(t, success) +} + func TestMonitorInitStartBlockHeight(t *testing.T) { logger := log.New().WithField("test", "monitor") lastHealth := &models.RunnerServiceStatus{BlockHeight: 100} diff --git a/cosmos/relayer_test.go b/cosmos/relayer_test.go index a5214ce..43e41bd 100644 --- a/cosmos/relayer_test.go +++ b/cosmos/relayer_test.go @@ -21,7 +21,22 @@ import ( ethcommon "github.com/ethereum/go-ethereum/common" ) -func TestUpdateCurrentHeight(t *testing.T) { +func TestRelayerHeight(t *testing.T) { + mockDB := mocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + + relayer := &CosmosMessageRelayerRunnable{ + db: mockDB, + client: mockClient, + currentBlockHeight: 100, + } + + height := relayer.Height() + + assert.Equal(t, uint64(100), height) +} + +func TestRelayerUpdateCurrentHeight(t *testing.T) { mockDB := mocks.NewMockDB(t) mockClient := clientMocks.NewMockCosmosClient(t) logger := logrus.New().WithField("test", "relayer") @@ -40,6 +55,25 @@ func TestUpdateCurrentHeight(t *testing.T) { assert.Equal(t, uint64(100), relayer.currentBlockHeight) } +func TestRelayerUpdateCurrentHeight_Error(t *testing.T) { + mockDB := mocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := logrus.New().WithField("test", "relayer") + + relayer := &CosmosMessageRelayerRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), assert.AnError) + + relayer.UpdateCurrentHeight() + + mockClient.AssertExpectations(t) + assert.Equal(t, uint64(0), relayer.currentBlockHeight) +} + func TestUpdateRefund(t *testing.T) { mockDB := mocks.NewMockDB(t) logger := logrus.New().WithField("test", "relayer") diff --git a/cosmos/signer_test.go b/cosmos/signer_test.go index 761aaf0..a53523e 100644 --- a/cosmos/signer_test.go +++ b/cosmos/signer_test.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" - cosmosMocks "github.com/dan13ram/wpokt-oracle/cosmos/client/mocks" + clientMocks "github.com/dan13ram/wpokt-oracle/cosmos/client/mocks" dbMocks "github.com/dan13ram/wpokt-oracle/db/mocks" "github.com/dan13ram/wpokt-oracle/models" @@ -40,12 +40,29 @@ import ( log "github.com/sirupsen/logrus" ) +func TestSignerHeight(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "signer") + + signer := &CosmosMessageSignerRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + currentBlockHeight: 100, + } + + height := signer.Height() + + assert.Equal(t, uint64(100), height) +} + func TestSignerUpdateCurrentHeight(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -53,10 +70,29 @@ func TestSignerUpdateCurrentHeight(t *testing.T) { mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), nil) - monitor.UpdateCurrentHeight() + signer.UpdateCurrentHeight() + + mockClient.AssertExpectations(t) + assert.Equal(t, uint64(100), signer.currentBlockHeight) +} + +func TestSignerUpdateCurrentHeight_Error(t *testing.T) { + mockDB := dbMocks.NewMockDB(t) + mockClient := clientMocks.NewMockCosmosClient(t) + logger := log.New().WithField("test", "signer") + + signer := &CosmosMessageSignerRunnable{ + db: mockDB, + client: mockClient, + logger: logger, + } + + mockClient.EXPECT().GetLatestBlockHeight().Return(int64(100), assert.AnError) + + signer.UpdateCurrentHeight() mockClient.AssertExpectations(t) - assert.Equal(t, uint64(100), monitor.currentBlockHeight) + assert.Equal(t, uint64(0), signer.currentBlockHeight) } func TestSignerUpdateMessage(t *testing.T) { @@ -66,14 +102,14 @@ func TestSignerUpdateMessage(t *testing.T) { message := &models.Message{ID: &primitive.ObjectID{}} update := bson.M{"status": models.MessageStatusSigned} - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, logger: logger, } mockDB.EXPECT().UpdateMessage(message.ID, update).Return(nil) - result := monitor.UpdateMessage(message, update) + result := signer.UpdateMessage(message, update) mockDB.AssertExpectations(t) assert.True(t, result) @@ -81,7 +117,7 @@ func TestSignerUpdateMessage(t *testing.T) { func TestSignMessage(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") signerKey := secp256k1.GenPrivKey() @@ -97,7 +133,7 @@ func TestSignMessage(t *testing.T) { Sequence: new(uint64), } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -117,9 +153,9 @@ func TestSignMessage(t *testing.T) { // Sequence: 1, // } - txBuilder := cosmosMocks.NewMockTxBuilder(t) - txConfig := cosmosMocks.NewMockTxConfig(t) - tx := cosmosMocks.NewMockTx(t) + txBuilder := clientMocks.NewMockTxBuilder(t) + txConfig := clientMocks.NewMockTxConfig(t) + tx := clientMocks.NewMockTx(t) mockDB.EXPECT().LockWriteSequence().Return("lock-id", nil) mockDB.EXPECT().Unlock("lock-id").Return(nil) @@ -164,7 +200,7 @@ func TestSignMessage(t *testing.T) { mockClient.EXPECT().GetAccount("multisigAddress").Return(&authtypes.BaseAccount{AccountNumber: 1, Sequence: 1}, nil) - result := monitor.SignMessage(message) + result := signer.SignMessage(message) mockDB.AssertExpectations(t) txBuilder.AssertExpectations(t) @@ -174,7 +210,7 @@ func TestSignMessage(t *testing.T) { func TestValidateAndFindDispatchIdEvent(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") ethClient := ethMocks.NewMockEthereumClient(t) @@ -197,7 +233,7 @@ func TestValidateAndFindDispatchIdEvent(t *testing.T) { MessageID: messageID, } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -215,7 +251,7 @@ func TestValidateAndFindDispatchIdEvent(t *testing.T) { mailbox.EXPECT().ParseDispatchId(mock.Anything).Return(&autogen.MailboxDispatchId{MessageId: [32]byte{}}, nil) - result, err := monitor.ValidateAndFindDispatchIdEvent(message) + result, err := signer.ValidateAndFindDispatchIdEvent(message) mockDB.AssertExpectations(t) ethClient.AssertExpectations(t) @@ -230,7 +266,7 @@ func TestValidateAndFindDispatchIdEvent(t *testing.T) { /* func TestValidateEthereumTxAndSignMessage(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") ethClient := ethMocks.NewMockEthereumClient(t) @@ -246,7 +282,7 @@ func TestValidateEthereumTxAndSignMessage(t *testing.T) { Signatures: []models.Signature{}, } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -265,7 +301,7 @@ func TestValidateEthereumTxAndSignMessage(t *testing.T) { mockDB.EXPECT().UpdateMessage( message.ID, mock.Anything).Return(nil) - result := monitor.ValidateEthereumTxAndSignMessage(message) + result := signer.ValidateEthereumTxAndSignMessage(message) mockDB.AssertExpectations(t) ethClient.AssertExpectations(t) @@ -275,7 +311,7 @@ func TestValidateEthereumTxAndSignMessage(t *testing.T) { func TestSignMessages(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") message := &models.Message{ @@ -285,7 +321,7 @@ func TestSignMessages(t *testing.T) { Signatures: []models.Signature{}, } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -301,7 +337,7 @@ func TestSignMessages(t *testing.T) { }, nil } - result := monitor.SignMessages() + result := signer.SignMessages() mockDB.AssertExpectations(t) assert.True(t, result) @@ -309,7 +345,7 @@ func TestSignMessages(t *testing.T) { func TestBroadcastMessage(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") message := &models.Message{ @@ -319,14 +355,14 @@ func TestBroadcastMessage(t *testing.T) { Signatures: []models.Signature{}, } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, } - txBuilder := cosmosMocks.NewMockTxBuilder(t) - txConfig := cosmosMocks.NewMockTxConfig(t) + txBuilder := clientMocks.NewMockTxBuilder(t) + txConfig := clientMocks.NewMockTxConfig(t) utilWrapTxBuilder = func(bech32Prefix, txBody string) (client.TxBuilder, client.TxConfig, error) { return txBuilder, txConfig, nil @@ -341,7 +377,7 @@ func TestBroadcastMessage(t *testing.T) { mockClient.EXPECT().BroadcastTx", mock.Anything).Return("txHash( nil) mockDB.EXPECT().UpdateMessage( message.ID, mock.Anything).Return(nil) - result := monitor.BroadcastMessage(message) + result := signer.BroadcastMessage(message) mockClient.AssertExpectations(t) mockDB.AssertExpectations(t) @@ -352,7 +388,7 @@ func TestBroadcastMessage(t *testing.T) { func TestBroadcastMessages(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") message := &models.Message{ @@ -362,7 +398,7 @@ func TestBroadcastMessages(t *testing.T) { Signatures: []models.Signature{}, } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -378,7 +414,7 @@ func TestBroadcastMessages(t *testing.T) { }, nil } - result := monitor.BroadcastMessages() + result := signer.BroadcastMessages() mockDB.AssertExpectations(t) assert.True(t, result) @@ -386,7 +422,7 @@ func TestBroadcastMessages(t *testing.T) { func TestSignRefunds(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") refund := &models.Refund{ @@ -396,7 +432,7 @@ func TestSignRefunds(t *testing.T) { Amount: "100", } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -412,7 +448,7 @@ func TestSignRefunds(t *testing.T) { }, nil } - result := monitor.SignRefunds() + result := signer.SignRefunds() mockDB.AssertExpectations(t) assert.True(t, result) @@ -420,7 +456,7 @@ func TestSignRefunds(t *testing.T) { func TestBroadcastRefund(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") refund := &models.Refund{ @@ -430,14 +466,14 @@ func TestBroadcastRefund(t *testing.T) { Amount: "100", } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, } - txBuilder := cosmosMocks.NewMockTxBuilder(t) - txConfig := cosmosMocks.NewMockTxConfig(t) + txBuilder := clientMocks.NewMockTxBuilder(t) + txConfig := clientMocks.NewMockTxConfig(t) utilWrapTxBuilder = func(string, string) (client.TxBuilder, client.TxConfig, error) { return txBuilder, txConfig, nil @@ -452,7 +488,7 @@ func TestBroadcastRefund(t *testing.T) { mockClient.EXPECT().BroadcastTx", mock.Anything).Return("txHash( nil) mockDB.EXPECT().UpdateRefund( refund.ID, mock.Anything).Return(nil) - result := monitor.BroadcastRefund(nil, refund, []byte("spender"), sdk.Coin{}) + result := signer.BroadcastRefund(nil, refund, []byte("spender"), sdk.Coin{}) mockClient.AssertExpectations(t) mockDB.AssertExpectations(t) @@ -463,7 +499,7 @@ func TestBroadcastRefund(t *testing.T) { func TestBroadcastRefunds(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") refund := &models.Refund{ @@ -473,7 +509,7 @@ func TestBroadcastRefunds(t *testing.T) { Amount: "100", } - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -489,22 +525,22 @@ func TestBroadcastRefunds(t *testing.T) { }, nil } - result := monitor.BroadcastRefunds() + result := signer.BroadcastRefunds() mockDB.AssertExpectations(t) assert.True(t, result) } func TestValidateSignatures(t *testing.T) { - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") - txBuilder := cosmosMocks.NewMockTxBuilder(t) - txConfig := cosmosMocks.NewMockTxConfig(t) + txBuilder := clientMocks.NewMockTxBuilder(t) + txConfig := clientMocks.NewMockTxConfig(t) multisigPk := &multisig.LegacyAminoPubKey{} - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ client: mockClient, logger: logger, multisigPk: multisigPk, @@ -518,7 +554,7 @@ func TestValidateSignatures(t *testing.T) { return []byte("{}"), nil }) - result := monitor.ValidateSignatures("hash1", 1, txConfig, txBuilder) + result := signer.ValidateSignatures("hash1", 1, txConfig, txBuilder) mockClient.AssertExpectations(t) txBuilder.AssertExpectations(t) @@ -528,10 +564,10 @@ func TestValidateSignatures(t *testing.T) { func TestFindMaxSequence(t *testing.T) { mockDB := dbMocks.NewMockDB(t) - mockClient := cosmosMocks.NewMockCosmosClient(t) + mockClient := clientMocks.NewMockCosmosClient(t) logger := log.New().WithField("test", "signer") - monitor := &CosmosMessageSignerRunnable{ + signer := &CosmosMessageSignerRunnable{ db: mockDB, client: mockClient, logger: logger, @@ -542,7 +578,7 @@ func TestFindMaxSequence(t *testing.T) { mockDB.EXPECT().FindMaxSequence( mock.Anything).Return(nil, nil) mockClient.EXPECT().GetAccount( mock.Anything).Return(&authtypes.BaseAccount{AccountNumber: 1, Sequence: 1}, nil) - sequence, err := monitor.FindMaxSequence() + sequence, err := signer.FindMaxSequence() mockDB.AssertExpectations(t) mockClient.AssertExpectations(t) diff --git a/ethereum/autogen/MintController.go b/ethereum/autogen/MintController.go index b0f0661..381544c 100644 --- a/ethereum/autogen/MintController.go +++ b/ethereum/autogen/MintController.go @@ -63,23 +63,23 @@ type MintControllerFilterer struct { // MintControllerSession is an auto generated Go binding around an Ethereum contract, // with pre-set call and transact options. type MintControllerSession struct { - Contract *MintController // Generic contract binding to set the session for - CallOpts bind.CallOpts // Call options to use throughout this session - TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session + Contract *MintController // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session } // MintControllerCallerSession is an auto generated read-only Go binding around an Ethereum contract, // with pre-set call options. type MintControllerCallerSession struct { Contract *MintControllerCaller // Generic contract caller binding to set the session for - CallOpts bind.CallOpts // Call options to use throughout this session + CallOpts bind.CallOpts // Call options to use throughout this session } // MintControllerTransactorSession is an auto generated write-only Go binding around an Ethereum contract, // with pre-set transact options. type MintControllerTransactorSession struct { Contract *MintControllerTransactor // Generic contract transactor binding to set the session for - TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session } // MintControllerRaw is an auto generated low-level Go binding around an Ethereum contract. diff --git a/main.go b/main.go index 4ae214b..3c8e43b 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ import ( func NewMintControllerMap(config models.Config) map[uint32][]byte { mintControllerMap := make(map[uint32][]byte) - for _, ethNetwork := range config.EthereumNetworks { + for _, ethNetwork := range config.EthereumNetworks { mintController, err := common.BytesFromAddressHex(ethNetwork.MintControllerAddress) if err != nil { logger. @@ -31,7 +31,7 @@ func NewMintControllerMap(config models.Config) map[uint32][]byte { Fatalf("Error parsing mint controller address") return nil } - ethChain := ethUtil.ParseChain(ethNetwork) + ethChain := ethUtil.ParseChain( ethNetwork) mintControllerMap[ethChain.ChainDomain] = mintController }