Jürgen Eckel e99b614e4a
added connection loss handler, extended logging, mqtt msg updates (#400)
* added a bit of logging
* added update messages to mqtt (every second with a timestamp)
* added isConnectionOpen to the interface and analysis this during execution
* added connection loss handler and status check
* put some global objects into an objects to avoid inter service testing issues
* TestingLocker Mutex to RWMutex (increase performance)
* termintationMutex became a sync.RWMutex
* added logging to the mqtt mock client
* made monitor Mutex a RWMutex
* added Mutex protection to the numberOfElements varialbe
* added another Waiting block to the machine attestation methods (CI tests)
* had to adjust the test cases to the impact of that change.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2024-05-16 17:06:19 +02:00

236 lines
7.8 KiB
Go

package machine
import (
"bytes"
"github.com/planetmint/planetmint-go/lib"
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
"github.com/planetmint/planetmint-go/testutil/network"
"github.com/planetmint/planetmint-go/testutil/sample"
machinecli "github.com/planetmint/planetmint-go/x/machine/client/cli"
machinetypes "github.com/planetmint/planetmint-go/x/machine/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
txcli "github.com/cosmos/cosmos-sdk/x/auth/tx"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/feegrant"
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/planetmint/planetmint-go/testutil/moduleobject"
)
// E2ETestSuite struct definition of machine suite
type E2ETestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
feeDenom string
}
// NewE2ETestSuite returns configured machine E2ETestSuite
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
return &E2ETestSuite{cfg: cfg}
}
// SetupSuite initializes machine E2ETestSuite
func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e machine test suite")
s.feeDenom = sample.FeeDenom
s.network = network.Load(s.T(), s.cfg)
// create machine account for attestation
account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic)
s.Require().NoError(err)
err = e2etestutil.FundAccount(s.network, account, s.feeDenom)
s.Require().NoError(err)
}
// TearDownSuite clean up after testing
func (s *E2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e machine test suite")
}
// TestAttestMachine attests machine and query attested machine from chain
func (s *E2ETestSuite) TestAttestMachine() {
val := s.network.Validators[0]
// register Ta
prvKey, pubKey := sample.KeyPair()
ta := moduleobject.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
out, err := e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg1)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err := clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgRegisterTrustAnchor")
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
// Check preAttestationBalance in order to verify that it doesn't change after machine attestation
preAttestationBalanceOutput, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
addr.String(),
})
s.Require().NoError(err)
preAttestationBalance, ok := preAttestationBalanceOutput.(*bytes.Buffer)
if !ok {
err = lib.ErrTypeAssertionFailed
s.Require().NoError(err)
}
assert.Contains(s.T(), preAttestationBalance.String(), "10000")
machine := moduleobject.Machine(sample.Name, pubKey, prvKey, addr.String())
msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, err = e2etestutil.BuildSignBroadcastTx(s.T(), addr, msg2)
s.Require().NoError(err)
// give machine attestation some time to issue the liquid asset
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgAttestMachine")
args := []string{
pubKey,
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)
s.Require().NoError(err)
txResponse, err := lib.GetTxResponseFromOut(out)
s.Require().NoError(err)
txResp, err := txcli.QueryTx(val.ClientCtx, txResponse.TxHash)
s.Require().NoError(err)
assert.Contains(s.T(), txResp.TxHash, txResponse.TxHash)
s.Require().NoError(err)
// Check postAttestationBalance it should be the preAttestationBalance + th 8800 tokens being donated to the machine (no fees are taken)
postAttestationBalanceOutput, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{
addr.String(),
})
s.Require().NoError(err)
postAttestationBalance, ok := postAttestationBalanceOutput.(*bytes.Buffer)
if !ok {
err = lib.ErrTypeAssertionFailed
s.Require().NoError(err)
}
assert.Contains(s.T(), postAttestationBalance.String(), "18800")
}
func (s *E2ETestSuite) TestInvalidAttestMachine() {
val := s.network.Validators[0]
// already used in previous test case
prvKey, pubKey := sample.KeyPair()
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
machine := moduleobject.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
msg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err := lib.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().Equal(int(txResponse.Code), int(4))
unregisteredPubKey, unregisteredPrivKey := sample.KeyPair(2)
machine = moduleobject.Machine(sample.Name, unregisteredPubKey, unregisteredPrivKey, addr.String())
s.Require().NoError(err)
msg = machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ = lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err = lib.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().Equal(int(txResponse.Code), int(3))
}
func (s *E2ETestSuite) TestMachineAllowanceAttestation() {
// create address for machine
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring
account, _, err := kb.NewMnemonic("AllowanceMachine", keyring.English, sample.DefaultDerivationPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
addr, err := account.GetAddress()
s.Require().NoError(err)
// register TA
prvKey, pubKey := sample.KeyPair(3)
ta := moduleobject.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg1)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// create allowance for machine
allowedMsgs := []string{"/planetmintgo.machine.MsgAttestMachine"}
limit := sdk.NewCoins(sdk.NewInt64Coin(s.feeDenom, 2))
basic := feegrant.BasicAllowance{
SpendLimit: limit,
}
var grant feegrant.FeeAllowanceI
grant = &basic
grant, err = feegrant.NewAllowedMsgAllowance(grant, allowedMsgs)
s.Require().NoError(err)
msg2, err := feegrant.NewMsgGrantAllowance(grant, val.Address, addr)
s.Require().NoError(err)
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg2)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// attest machine with fee granter without funding the machine account first
machine := moduleobject.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFeeGranterAddress(val.Address)
libConfig := lib.GetConfig()
libConfig.SetClientCtx(clientCtx)
msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), addr, msg3)
// reset clientCtx to validator ctx
libConfig.SetClientCtx(val.ClientCtx)
s.Require().NoError(err)
// give machine attestation some time to issue the liquid asset
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
s.Require().NoError(s.network.WaitForNextBlock())
args := []string{
pubKey,
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)
s.Require().NoError(err)
}