mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
refactor: let lib set client context's address and name (#261)
* fix: make a real copy of bytes.Buffer * refactor: rename `address` for maximum clarity in lib Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
3e23260a9c
commit
ae161f11e5
35
lib/tx.go
35
lib/tx.go
@ -38,16 +38,25 @@ func getAccountNumberAndSequence(clientCtx client.Context) (accountNumber, seque
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientContextAndTxFactory(address sdk.AccAddress) (clientCtx client.Context, txf tx.Factory, err error) {
|
func getClientContextAndTxFactory(fromAddress sdk.AccAddress) (clientCtx client.Context, txf tx.Factory, err error) {
|
||||||
clientCtx = GetConfig().ClientCtx
|
clientCtx = GetConfig().ClientCtx
|
||||||
// at least we need an account retriever
|
// at least we need an account retriever
|
||||||
// it would be better to check for an empty client context, but that does not work at the moment
|
// it would be better to check for an empty client context, but that does not work at the moment
|
||||||
if clientCtx.AccountRetriever == nil {
|
if clientCtx.AccountRetriever == nil {
|
||||||
clientCtx, err = getClientContext(address)
|
clientCtx, err = getClientContext(fromAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
record, err := clientCtx.Keyring.KeyByAddress(fromAddress)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// name and address of private key with which to sign
|
||||||
|
clientCtx = clientCtx.
|
||||||
|
WithFromAddress(fromAddress).
|
||||||
|
WithFromName(record.Name)
|
||||||
|
|
||||||
accountNumber, sequence, err := getAccountNumberAndSequence(clientCtx)
|
accountNumber, sequence, err := getAccountNumberAndSequence(clientCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -69,7 +78,7 @@ func getTxFactoryWithAccountNumberAndSequence(clientCtx client.Context, accountN
|
|||||||
WithTxConfig(clientCtx.TxConfig)
|
WithTxConfig(clientCtx.TxConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientContext(address sdk.AccAddress) (clientCtx client.Context, err error) {
|
func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err error) {
|
||||||
encodingConfig := GetConfig().EncodingConfig
|
encodingConfig := GetConfig().EncodingConfig
|
||||||
|
|
||||||
rootDir := GetConfig().RootDir
|
rootDir := GetConfig().RootDir
|
||||||
@ -82,7 +91,7 @@ func getClientContext(address sdk.AccAddress) (clientCtx client.Context, err err
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
record, err := keyring.KeyByAddress(address)
|
record, err := keyring.KeyByAddress(fromAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -101,8 +110,8 @@ func getClientContext(address sdk.AccAddress) (clientCtx client.Context, err err
|
|||||||
ChainID: GetConfig().ChainID,
|
ChainID: GetConfig().ChainID,
|
||||||
Client: wsClient,
|
Client: wsClient,
|
||||||
Codec: codec,
|
Codec: codec,
|
||||||
From: address.String(),
|
From: fromAddress.String(),
|
||||||
FromAddress: address,
|
FromAddress: fromAddress,
|
||||||
FromName: record.Name,
|
FromName: record.Name,
|
||||||
HomeDir: rootDir,
|
HomeDir: rootDir,
|
||||||
Input: input,
|
Input: input,
|
||||||
@ -122,8 +131,8 @@ func getClientContext(address sdk.AccAddress) (clientCtx client.Context, err err
|
|||||||
|
|
||||||
// BuildUnsignedTx builds a transaction to be signed given a set of messages.
|
// BuildUnsignedTx builds a transaction to be signed given a set of messages.
|
||||||
// Once created, the fee, memo, and messages are set.
|
// Once created, the fee, memo, and messages are set.
|
||||||
func BuildUnsignedTx(address sdk.AccAddress, msgs ...sdk.Msg) (txJSON string, err error) {
|
func BuildUnsignedTx(fromAddress sdk.AccAddress, msgs ...sdk.Msg) (txJSON string, err error) {
|
||||||
clientCtx, txf, err := getClientContextAndTxFactory(address)
|
clientCtx, txf, err := getClientContextAndTxFactory(fromAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -160,7 +169,9 @@ func broadcastTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) (out
|
|||||||
|
|
||||||
// Make a copy because we `defer output.Reset()`
|
// Make a copy because we `defer output.Reset()`
|
||||||
out = &bytes.Buffer{}
|
out = &bytes.Buffer{}
|
||||||
*out = *output
|
// This is still copying references: *out = *output
|
||||||
|
// Make a real copy: https://stackoverflow.com/a/69758157
|
||||||
|
out.Write(output.Bytes())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +212,7 @@ func getSequenceFromChain(clientCtx client.Context) (sequence uint64, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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(address 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()
|
usr, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -209,7 +220,7 @@ func BroadcastTxWithFileLock(address sdk.AccAddress, msgs ...sdk.Msg) (out *byte
|
|||||||
}
|
}
|
||||||
homeDir := usr.HomeDir
|
homeDir := usr.HomeDir
|
||||||
|
|
||||||
addrHex := hex.EncodeToString(address)
|
addrHex := hex.EncodeToString(fromAddress)
|
||||||
filename := filepath.Join(GetConfig().RootDir, addrHex+".sequence")
|
filename := filepath.Join(GetConfig().RootDir, addrHex+".sequence")
|
||||||
|
|
||||||
// Expand tilde to user's home directory.
|
// Expand tilde to user's home directory.
|
||||||
@ -237,7 +248,7 @@ func BroadcastTxWithFileLock(address sdk.AccAddress, msgs ...sdk.Msg) (out *byte
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// get basic chain information
|
// get basic chain information
|
||||||
clientCtx, txf, err := getClientContextAndTxFactory(address)
|
clientCtx, txf, err := getClientContextAndTxFactory(fromAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/planetmint/planetmint-go/config"
|
"github.com/planetmint/planetmint-go/config"
|
||||||
"github.com/planetmint/planetmint-go/lib"
|
|
||||||
"github.com/planetmint/planetmint-go/testutil"
|
"github.com/planetmint/planetmint-go/testutil"
|
||||||
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
|
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
|
||||||
"github.com/planetmint/planetmint-go/testutil/network"
|
"github.com/planetmint/planetmint-go/testutil/network"
|
||||||
@ -37,18 +36,6 @@ func (s *RestE2ETestSuite) SetupSuite() {
|
|||||||
s.network = network.New(s.T())
|
s.network = network.New(s.T())
|
||||||
err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0)
|
err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
val := s.network.Validators[0]
|
|
||||||
k, err := val.ClientCtx.Keyring.Key(sample.Name)
|
|
||||||
s.Require().NoError(err)
|
|
||||||
|
|
||||||
addr, _ := k.GetAddress()
|
|
||||||
|
|
||||||
clientCtx := val.ClientCtx.
|
|
||||||
WithFromAddress(addr).
|
|
||||||
WithFromName(sample.Name)
|
|
||||||
libConfig := lib.GetConfig()
|
|
||||||
libConfig.SetClientCtx(clientCtx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TearDownSuite clean up after testing
|
// TearDownSuite clean up after testing
|
||||||
|
@ -37,18 +37,6 @@ func (s *E2ETestSuite) SetupSuite() {
|
|||||||
s.network = network.New(s.T())
|
s.network = network.New(s.T())
|
||||||
err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0)
|
err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
val := s.network.Validators[0]
|
|
||||||
k, err := val.ClientCtx.Keyring.Key(sample.Name)
|
|
||||||
s.Require().NoError(err)
|
|
||||||
|
|
||||||
addr, _ := k.GetAddress()
|
|
||||||
|
|
||||||
clientCtx := val.ClientCtx.
|
|
||||||
WithFromAddress(addr).
|
|
||||||
WithFromName(sample.Name)
|
|
||||||
libConfig := lib.GetConfig()
|
|
||||||
libConfig.SetClientCtx(clientCtx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TearDownSuite clean up after testing
|
// TearDownSuite clean up after testing
|
||||||
|
@ -73,13 +73,6 @@ func (s *E2ETestSuite) TestAttestMachine() {
|
|||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
addr, _ := k.GetAddress()
|
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())
|
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
|
||||||
msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||||
out, err = e2etestutil.BuildSignBroadcastTx(s.T(), addr, msg2)
|
out, err = e2etestutil.BuildSignBroadcastTx(s.T(), addr, msg2)
|
||||||
@ -112,13 +105,6 @@ func (s *E2ETestSuite) TestInvalidAttestMachine() {
|
|||||||
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
|
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
|
||||||
s.Require().NoError(err)
|
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)
|
msg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||||
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
|
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
|
||||||
txResponse, err := lib.GetTxResponseFromOut(out)
|
txResponse, err := lib.GetTxResponseFromOut(out)
|
||||||
@ -150,10 +136,6 @@ func (s *E2ETestSuite) TestMachineAllowanceAttestation() {
|
|||||||
// register TA
|
// register TA
|
||||||
prvKey, pubKey := sample.KeyPair(3)
|
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)
|
ta := sample.TrustAnchor(pubKey)
|
||||||
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
|
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
|
||||||
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg1)
|
_, err = e2etestutil.BuildSignBroadcastTx(s.T(), val.Address, msg1)
|
||||||
@ -183,9 +165,8 @@ func (s *E2ETestSuite) TestMachineAllowanceAttestation() {
|
|||||||
|
|
||||||
// name and address of private key with which to sign
|
// name and address of private key with which to sign
|
||||||
clientCtx := val.ClientCtx.
|
clientCtx := val.ClientCtx.
|
||||||
WithFromAddress(addr).
|
|
||||||
WithFromName("AllowanceMachine").
|
|
||||||
WithFeeGranterAddress(val.Address)
|
WithFeeGranterAddress(val.Address)
|
||||||
|
libConfig := lib.GetConfig()
|
||||||
libConfig.SetClientCtx(clientCtx)
|
libConfig.SetClientCtx(clientCtx)
|
||||||
|
|
||||||
msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||||
@ -193,6 +174,9 @@ func (s *E2ETestSuite) TestMachineAllowanceAttestation() {
|
|||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
s.Require().NoError(s.network.WaitForNextBlock())
|
s.Require().NoError(s.network.WaitForNextBlock())
|
||||||
|
|
||||||
|
// reset clientCtx to validator ctx
|
||||||
|
libConfig.SetClientCtx(val.ClientCtx)
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
pubKey,
|
pubKey,
|
||||||
}
|
}
|
||||||
|
@ -92,13 +92,6 @@ func AttestMachine(network *network.Network, name string, mnemonic string, num i
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// name and address of private key with which to sign
|
|
||||||
clientCtx := val.ClientCtx.
|
|
||||||
WithFromAddress(addr).
|
|
||||||
WithFromName(name)
|
|
||||||
libConfig := lib.GetConfig()
|
|
||||||
libConfig.SetClientCtx(clientCtx)
|
|
||||||
|
|
||||||
machine := sample.Machine(name, pubKey, prvKey, addr.String())
|
machine := sample.Machine(name, pubKey, prvKey, addr.String())
|
||||||
attestMsg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
attestMsg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
|
||||||
_, err = lib.BroadcastTxWithFileLock(addr, attestMsg)
|
_, err = lib.BroadcastTxWithFileLock(addr, attestMsg)
|
||||||
@ -111,8 +104,5 @@ func AttestMachine(network *network.Network, name string, mnemonic string, num i
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset clientCtx to validator ctx
|
|
||||||
libConfig.SetClientCtx(val.ClientCtx)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user