Julian Strobl 036ce7cbc1
Enable sequence file locking in tests (#236)
* [lib] Add `FeeGranter` to tx factory
* [lib] Align return value with test cases
* [test] Return error on tx response
* [test] Switch to sequence file locking via libRPC
* [test] Reintegrate `TestPoPResult`

Closes #234


Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-12-18 15:47:44 +01:00

232 lines
7.2 KiB
Go

package machine
import (
"github.com/planetmint/planetmint-go/config"
"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"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
// E2ETestSuite struct definition of machine suite
type E2ETestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
// NewE2ETestSuite returns configured machine E2ETestSuite
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
return &E2ETestSuite{cfg: cfg}
}
// SetupSuite initializes machine E2ETestSuite
func (s *E2ETestSuite) SetupSuite() {
conf := config.GetConfig()
conf.FeeDenom = "stake"
s.T().Log("setting up e2e test suite")
s.network = network.New(s.T())
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring
account, err := kb.NewAccount(sample.Name, sample.Mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1)
s.Require().NoError(err)
addr, _ := account.GetAddress()
// sending funds to machine to initialize account on chain
coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000))
msg := banktypes.NewMsgSend(val.Address, addr, coin)
out, err := lib.BroadcastTxWithFileLock(val.Address, msg)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err := clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "cosmos.bank.v1beta1.MsgSend")
}
// TearDownSuite clean up after testing
func (s *E2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e 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 := sample.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
out, err := lib.BroadcastTxWithFileLock(val.Address, msg1)
s.Require().NoError(err)
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()
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName(sample.Name)
libConfig := lib.GetConfig()
libConfig.SetClientCtx(clientCtx)
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, err = lib.BroadcastTxWithFileLock(addr, msg2)
s.Require().NoError(err)
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)
}
func (s *E2ETestSuite) TestInvalidAttestMachine() {
val := s.network.Validators[0]
// already used in REST test case
prvKey, pubKey := sample.KeyPair(1)
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName(sample.Name)
libConfig := lib.GetConfig()
libConfig.SetClientCtx(clientCtx)
msg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err := clitestutil.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().Equal(int(txResponse.Code), int(4))
unregisteredPubKey, unregisteredPrivKey := sample.KeyPair(2)
machine = sample.Machine(sample.Name, unregisteredPubKey, unregisteredPrivKey, addr.String())
s.Require().NoError(err)
msg = machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ = lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err = clitestutil.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)
// name and address of private key with which to sign
libConfig := lib.GetConfig()
libConfig.SetClientCtx(val.ClientCtx)
ta := sample.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
out, err := lib.BroadcastTxWithFileLock(val.Address, msg1)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// create allowance for machine
allowedMsgs := []string{"/planetmintgo.machine.MsgAttestMachine"}
limit := sdk.NewCoins(sdk.NewInt64Coin("stake", 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 = lib.BroadcastTxWithFileLock(val.Address, msg2)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// attest machine with fee granter without funding the machine account first
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName("AllowanceMachine").
WithFeeGranterAddress(val.Address)
libConfig.SetClientCtx(clientCtx)
msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
_, err = lib.BroadcastTxWithFileLock(addr, msg3)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
args := []string{
pubKey,
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)
s.Require().NoError(err)
}