mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-07 14:46:39 +00:00
feat: put sequence files into sub-directory (#308)
- aligns the folder structure in `planetmint-go/` - makes sequence number clean up via systemd possible // see https://github.com/rddl-network/issues/issues/61 * refactor: open sequence file into function * refactor: get sequence number into utils Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
bfbe9584a1
commit
23648927c3
61
lib/tx.go
61
lib/tx.go
@ -1,17 +1,12 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
comethttp "github.com/cometbft/cometbft/rpc/client/http"
|
comethttp "github.com/cometbft/cometbft/rpc/client/http"
|
||||||
@ -175,62 +170,10 @@ func broadcastTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) (out
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSequenceFromFile(seqFile *os.File, filename string) (sequence uint64, err error) {
|
|
||||||
var sequenceString string
|
|
||||||
lineCount := int64(0)
|
|
||||||
scanner := bufio.NewScanner(seqFile)
|
|
||||||
for scanner.Scan() {
|
|
||||||
sequenceString = scanner.Text()
|
|
||||||
lineCount++
|
|
||||||
}
|
|
||||||
err = scanner.Err()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if lineCount == 0 {
|
|
||||||
err = errors.New("Sequence file empty " + filename + ": no lines")
|
|
||||||
return
|
|
||||||
} else if lineCount != 1 {
|
|
||||||
err = errors.New("Malformed " + filename + ": wrong number of lines")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sequence, err = strconv.ParseUint(sequenceString, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
sequence = account.GetSequence()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// BroadcastTxWithFileLock broadcasts a transaction via gRPC and synchronises requests via a file lock.
|
// BroadcastTxWithFileLock broadcasts a transaction via gRPC and synchronises requests via a file lock.
|
||||||
func BroadcastTxWithFileLock(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (out *bytes.Buffer, err error) {
|
func BroadcastTxWithFileLock(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (out *bytes.Buffer, err error) {
|
||||||
// open and lock file, if it exists
|
// open and lock file, if it exists
|
||||||
usr, err := user.Current()
|
file, err := openSequenceFile(fromAddress)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
homeDir := usr.HomeDir
|
|
||||||
|
|
||||||
addrHex := hex.EncodeToString(fromAddress)
|
|
||||||
filename := filepath.Join(GetConfig().RootDir, addrHex+".sequence")
|
|
||||||
|
|
||||||
// Expand tilde to user's home directory.
|
|
||||||
if filename == "~" {
|
|
||||||
filename = homeDir
|
|
||||||
} else if strings.HasPrefix(filename, "~/") {
|
|
||||||
filename = filepath.Join(homeDir, filename[2:])
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -253,7 +196,7 @@ func BroadcastTxWithFileLock(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (out *
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sequenceFromFile, errFile := getSequenceFromFile(file, filename)
|
sequenceFromFile, errFile := getSequenceFromFile(file)
|
||||||
sequenceFromChain, errChain := getSequenceFromChain(clientCtx)
|
sequenceFromChain, errChain := getSequenceFromChain(clientCtx)
|
||||||
|
|
||||||
var sequence uint64
|
var sequence uint64
|
||||||
|
81
lib/utils.go
81
lib/utils.go
@ -1,14 +1,95 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getSequenceFromFile(seqFile *os.File) (sequence uint64, err error) {
|
||||||
|
var sequenceString string
|
||||||
|
lineCount := int64(0)
|
||||||
|
scanner := bufio.NewScanner(seqFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
sequenceString = scanner.Text()
|
||||||
|
lineCount++
|
||||||
|
}
|
||||||
|
err = scanner.Err()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
sequence, err = strconv.ParseUint(sequenceString, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
sequence = account.GetSequence()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createSequenceDirectory() (path string, err error) {
|
||||||
|
usr, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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:])
|
||||||
|
}
|
||||||
|
_, err = os.Stat(path)
|
||||||
|
// directory already exists
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = os.Mkdir(path, os.ModePerm)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func openSequenceFile(fromAddress sdk.AccAddress) (file *os.File, err error) {
|
||||||
|
path, err := createSequenceDirectory()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
addrHex := hex.EncodeToString(fromAddress)
|
||||||
|
filename := filepath.Join(path, addrHex)
|
||||||
|
|
||||||
|
file, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetTxResponseFromOut converts strings to numbers and unmarshalles out into TxResponse struct
|
// GetTxResponseFromOut converts strings to numbers and unmarshalles out into TxResponse struct
|
||||||
func GetTxResponseFromOut(out *bytes.Buffer) (txResponse sdk.TxResponse, err error) {
|
func GetTxResponseFromOut(out *bytes.Buffer) (txResponse sdk.TxResponse, err error) {
|
||||||
m := regexp.MustCompile(`"([0-9]+?)"`)
|
m := regexp.MustCompile(`"([0-9]+?)"`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user