mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
refactor(lib): wip
Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
733fd5dbd4
commit
77cd83655d
@ -8,25 +8,22 @@ import (
|
||||
"github.com/planetmint/planetmint-go/lib/params"
|
||||
)
|
||||
|
||||
// makeEncodingConfig creates an EncodingConfig for an amino based test configuration.
|
||||
func makeEncodingConfig() params.EncodingConfig {
|
||||
// MakeEncodingConfig creates a fully configured EncodingConfig for testing
|
||||
func MakeEncodingConfig() params.EncodingConfig {
|
||||
amino := codec.NewLegacyAmino()
|
||||
interfaceRegistry := types.NewInterfaceRegistry()
|
||||
marshaler := codec.NewProtoCodec(interfaceRegistry)
|
||||
txCfg := tx.NewTxConfig(marshaler, tx.DefaultSignModes)
|
||||
txConfig := tx.NewTxConfig(marshaler, tx.DefaultSignModes)
|
||||
|
||||
return params.EncodingConfig{
|
||||
encodingConfig := params.EncodingConfig{
|
||||
InterfaceRegistry: interfaceRegistry,
|
||||
Marshaler: marshaler,
|
||||
TxConfig: txCfg,
|
||||
TxConfig: txConfig,
|
||||
Amino: amino,
|
||||
}
|
||||
}
|
||||
|
||||
// MakeEncodingConfig creates an EncodingConfig for testing
|
||||
func MakeEncodingConfig() params.EncodingConfig {
|
||||
encodingConfig := makeEncodingConfig()
|
||||
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
|
||||
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
||||
|
||||
return encodingConfig
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func (s *E2ETestSuite) TestBankSendBroadcastTxWithFileLock() {
|
||||
out, err := lib.BroadcastTxWithFileLock(val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "received wrong fee denom; got: plmnt required: stake: invalid coins", txResponse.RawLog)
|
||||
|
||||
@ -72,7 +72,7 @@ func (s *E2ETestSuite) TestBankSendBroadcastTxWithFileLock() {
|
||||
out, err = lib.BroadcastTxWithFileLock(val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err = lib.GetTxResponseFromOut(out)
|
||||
txResponse, err = lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
|
||||
@ -87,7 +87,7 @@ func (s *E2ETestSuite) TestBankSendBroadcastTxWithFileLock() {
|
||||
out, err = lib.BroadcastTxWithFileLock(val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err = lib.GetTxResponseFromOut(out)
|
||||
txResponse, err = lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
assert.Equal(s.T(), "[]", txResponse.RawLog)
|
||||
}
|
||||
@ -120,7 +120,7 @@ func (s *E2ETestSuite) TestOccSigning() {
|
||||
out, err := lib.BroadcastTxWithFileLock(addr, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal("[]", txResponse.RawLog)
|
||||
s.Require().Equal(uint32(0), txResponse.Code)
|
||||
|
@ -207,7 +207,7 @@ func BroadcastTxWithFileLock(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (out *
|
||||
return
|
||||
}
|
||||
|
||||
sequenceFromFile, errFile := getSequenceFromFile(file)
|
||||
sequenceFromFile, errFile := readSequenceFromFile(file)
|
||||
sequenceFromChain, errChain := getSequenceFromChain(clientCtx)
|
||||
|
||||
var sequence uint64
|
||||
@ -231,7 +231,7 @@ func BroadcastTxWithFileLock(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (out *
|
||||
return
|
||||
}
|
||||
|
||||
txResponse, err := GetTxResponseFromOut(out)
|
||||
txResponse, err := ParseTxResponse(out)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
82
lib/utils.go
82
lib/utils.go
@ -5,7 +5,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
@ -18,64 +18,63 @@ import (
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func getSequenceFromFile(seqFile *os.File) (sequence uint64, err error) {
|
||||
var sequenceString string
|
||||
lineCount := int64(0)
|
||||
func readSequenceFromFile(seqFile *os.File) (sequence uint64, err error) {
|
||||
scanner := bufio.NewScanner(seqFile)
|
||||
for scanner.Scan() {
|
||||
sequenceString = scanner.Text()
|
||||
lineCount++
|
||||
|
||||
// read the first line
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return 0, fmt.Errorf("reading sequence file: %w", err)
|
||||
}
|
||||
err = scanner.Err()
|
||||
if err != nil {
|
||||
return
|
||||
return 0, fmt.Errorf("sequence file empty: %s", seqFile.Name())
|
||||
}
|
||||
if lineCount == 0 {
|
||||
err = errors.New("Sequence file empty " + seqFile.Name() + ": no lines")
|
||||
return
|
||||
} else if lineCount != 1 {
|
||||
err = errors.New("Malformed " + seqFile.Name() + ": wrong number of lines")
|
||||
return
|
||||
|
||||
sequenceString := scanner.Text()
|
||||
|
||||
// check for additional lines
|
||||
if scanner.Scan() {
|
||||
return 0, fmt.Errorf("malformed sequence file %s: contains multiple lines", seqFile.Name())
|
||||
}
|
||||
|
||||
sequence, err = strconv.ParseUint(sequenceString, 10, 64)
|
||||
if err != nil {
|
||||
return
|
||||
return 0, fmt.Errorf("parsing sequence number: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getSequenceFromChain(clientCtx client.Context) (sequence uint64, err error) {
|
||||
// get sequence number from chain
|
||||
account, err := clientCtx.AccountRetriever.GetAccount(clientCtx, clientCtx.FromAddress)
|
||||
if err != nil {
|
||||
return
|
||||
return 0, fmt.Errorf("retrieving account: %w", err)
|
||||
}
|
||||
sequence = account.GetSequence()
|
||||
return
|
||||
return account.GetSequence(), nil
|
||||
}
|
||||
|
||||
func createSequenceDirectory() (path string, err error) {
|
||||
func getOrCreateSequenceDirectory() (path string, err error) {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
return
|
||||
return "", fmt.Errorf("getting current user: %w", err)
|
||||
}
|
||||
homeDir := usr.HomeDir
|
||||
|
||||
path = filepath.Join(GetConfig().RootDir, "sequence")
|
||||
|
||||
// expand tilde to user's home directory
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
path = filepath.Join(homeDir, path[2:])
|
||||
path = filepath.Join(usr.HomeDir, path[2:])
|
||||
}
|
||||
_, err = os.Stat(path)
|
||||
// directory already exists
|
||||
if !os.IsNotExist(err) {
|
||||
return
|
||||
|
||||
// create directory if it doesn't exist
|
||||
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
||||
return "", fmt.Errorf("creating sequence directory: %w", err)
|
||||
}
|
||||
err = os.Mkdir(path, os.ModePerm)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func openSequenceFile(fromAddress sdk.AccAddress) (file *os.File, err error) {
|
||||
path, err := createSequenceDirectory()
|
||||
path, err := getOrCreateSequenceDirectory()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -85,23 +84,28 @@ func openSequenceFile(fromAddress sdk.AccAddress) (file *os.File, err error) {
|
||||
|
||||
file, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, fmt.Errorf("opening sequence file: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetTxResponseFromOut converts strings to numbers and unmarshalles out into TxResponse struct
|
||||
func GetTxResponseFromOut(out *bytes.Buffer) (txResponse sdk.TxResponse, err error) {
|
||||
m := regexp.MustCompile(`"([0-9]+?)"`)
|
||||
str := m.ReplaceAllString(out.String(), "${1}")
|
||||
// ParseTxResponse converts output buffer to TxResponse
|
||||
func ParseTxResponse(out *bytes.Buffer) (txResponse sdk.TxResponse, err error) {
|
||||
// Convert string numbers to actual numbers
|
||||
numericRegex := regexp.MustCompile(`"([0-9]+?)"`)
|
||||
jsonStr := numericRegex.ReplaceAllString(out.String(), "${1}")
|
||||
|
||||
// We might have YAML here, so we need to convert to JSON first, because TxResponse struct lacks `yaml:"height,omitempty"`, etc.
|
||||
// Since JSON is a subset of YAML, passing JSON through YAMLToJSON is a no-op and the result is the byte array of the JSON again.
|
||||
j, err := yaml.YAMLToJSON([]byte(str))
|
||||
jsonBytes, err := yaml.YAMLToJSON([]byte(jsonStr))
|
||||
if err != nil {
|
||||
return
|
||||
return sdk.TxResponse{}, fmt.Errorf("converting YAML to JSON: %w", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(jsonBytes, &txResponse); err != nil {
|
||||
return sdk.TxResponse{}, fmt.Errorf("unmarshaling transaction response: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(j, &txResponse)
|
||||
return
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ func (s *E2ETestSuite) TestNotarizeAsset() {
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
@ -149,7 +149,7 @@ func (s *E2ETestSuite) TestMintToken() {
|
||||
out, err = lib.BroadcastTxWithFileLock(addr, msg1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(2), int(txResponse.Code))
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ func (s *RestrictedMsgsE2ESuite) TestRestrictedMsgsValidator() {
|
||||
out, err := lib.BroadcastTxWithFileLock(val.Address, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(0), int(txResponse.Code))
|
||||
}
|
||||
@ -71,7 +71,7 @@ func (s *RestrictedMsgsE2ESuite) TestRestrictedMsgsNonValidator() {
|
||||
out, err := lib.BroadcastTxWithFileLock(addr, msg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(18), int(txResponse.Code))
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
@ -303,7 +303,7 @@ func (s *SelectionE2ETestSuite) TestTokenRedeemClaim() {
|
||||
out, err := lib.BroadcastTxWithFileLock(addr, createClaimMsg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(0), int(txResponse.Code))
|
||||
|
||||
@ -334,7 +334,7 @@ func (s *SelectionE2ETestSuite) TestTokenRedeemClaim() {
|
||||
out, err = lib.BroadcastTxWithFileLock(val.Address, valConfirmMsg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResponse, err = lib.GetTxResponseFromOut(out)
|
||||
txResponse, err = lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(0), int(txResponse.Code))
|
||||
|
||||
|
@ -113,7 +113,7 @@ func (s *E2ETestSuite) TestAttestMachine() {
|
||||
|
||||
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.GetCmdMachineByPublicKey(), args)
|
||||
s.Require().NoError(err)
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
|
||||
txResp, err := txcli.QueryTx(val.ClientCtx, txResponse.TxHash)
|
||||
@ -150,7 +150,7 @@ func (s *E2ETestSuite) TestInvalidAttestMachine() {
|
||||
|
||||
msg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(txResponse.Code), int(4))
|
||||
|
||||
@ -160,7 +160,7 @@ func (s *E2ETestSuite) TestInvalidAttestMachine() {
|
||||
|
||||
msg = machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||
out, _ = lib.BroadcastTxWithFileLock(addr, msg)
|
||||
txResponse, err = lib.GetTxResponseFromOut(out)
|
||||
txResponse, err = lib.ParseTxResponse(out)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(int(txResponse.Code), int(3))
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []st
|
||||
return
|
||||
}
|
||||
|
||||
txResponse, err := lib.GetTxResponseFromOut(output)
|
||||
txResponse, err := lib.ParseTxResponse(output)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -49,7 +49,7 @@ func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []st
|
||||
|
||||
// GetRawLogFromTxOut queries the TxHash of out from the chain and returns the RawLog from the answer.
|
||||
func GetRawLogFromTxOut(val *network.Validator, out *bytes.Buffer) (rawLog string, err error) {
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -72,7 +72,7 @@ func GetRawLogFromTxOut(val *network.Validator, out *bytes.Buffer) (rawLog strin
|
||||
return
|
||||
}
|
||||
|
||||
txRes, err := lib.GetTxResponseFromOut(out)
|
||||
txRes, err := lib.ParseTxResponse(out)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ func BuildSignBroadcastTx(t *testing.T, addr sdk.AccAddress, msgs ...sdk.Msg) (o
|
||||
t.Log("broadcast tx failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
if err != nil {
|
||||
t.Log("getting tx response from out failed: " + err.Error())
|
||||
return
|
||||
|
@ -31,7 +31,7 @@ func buildSignBroadcastTx(goCtx context.Context, loggingContext string, sendingV
|
||||
GetAppLogger().Error(ctx, err, loggingContext+" broadcast tx failed: %v, %v", addr, msg)
|
||||
return
|
||||
}
|
||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||
txResponse, err := lib.ParseTxResponse(out)
|
||||
if err != nil {
|
||||
GetAppLogger().Error(ctx, err, loggingContext+" getting tx response from out failed: %v", out)
|
||||
return
|
||||
|
Loading…
x
Reference in New Issue
Block a user