Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client/v2): factory #20623

Merged
merged 49 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d4d2320
first approach
JulianToledano Jun 3, 2024
2a777df
TxBuilder implemented
JulianToledano Jun 5, 2024
ffe66ac
add: txBuilder tests
JulianToledano Jun 6, 2024
3cbcdcb
txconfig
JulianToledano Jun 7, 2024
2ff6edd
factory works
JulianToledano Jun 11, 2024
5fec5af
update: Decoder interface,
JulianToledano Jun 12, 2024
6eb3c98
update: factory
JulianToledano Jun 17, 2024
deb9fa3
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Jun 17, 2024
f3071a4
add: factory tests
JulianToledano Jun 18, 2024
a62bc64
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Jun 18, 2024
b9bcaa6
del: most sdk dependency
JulianToledano Jun 19, 2024
1270a87
fix: dry-run and sign
JulianToledano Jun 19, 2024
2e4895f
update: simplify accountconfig setter
JulianToledano Jun 19, 2024
51ffeb2
fix: dry-run offline
JulianToledano Jun 20, 2024
189e6b3
add: godoc
JulianToledano Jun 20, 2024
4fadeff
lint
JulianToledano Jun 20, 2024
d49ce6f
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Jun 20, 2024
c2b2539
add: SignaturesMarshal
JulianToledano Jun 21, 2024
1a29aa8
lint
JulianToledano Jun 21, 2024
5854380
remove todo
JulianToledano Jun 21, 2024
4094945
fix: go-mod-tidy-all
JulianToledano Jun 21, 2024
9ee9940
update: coderrabbit suggestions
JulianToledano Jun 28, 2024
cd55292
fix: signingTxData encoding
JulianToledano Jul 5, 2024
3dcc970
add: DecodedTx wrapper
JulianToledano Jul 5, 2024
4b55c93
update: encoder decoder
JulianToledano Jul 8, 2024
3db0bab
lint :)
JulianToledano Jul 8, 2024
87677d7
add: readme with diagrams
JulianToledano Jul 8, 2024
d360670
lint
JulianToledano Jul 8, 2024
5a8aba9
update: buildSignedTx
JulianToledano Jul 17, 2024
1e3e29f
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Aug 15, 2024
7acdd15
update: use timeoutTimestamp,
JulianToledano Aug 20, 2024
a0dc147
update: prepare is now only used in constructor
JulianToledano Aug 20, 2024
78bb01e
update: remove generateOnly from params
JulianToledano Aug 21, 2024
1c76296
update: remove offchain from params
JulianToledano Aug 21, 2024
4c8ee9b
update: remove offline from params
JulianToledano Aug 21, 2024
d849302
del: remove default gas
JulianToledano Aug 21, 2024
7ffed78
lint
JulianToledano Aug 21, 2024
738605d
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Sep 17, 2024
4a51df0
avoid flags2
JulianToledano Sep 19, 2024
9153602
lint-happy
JulianToledano Sep 19, 2024
9547499
tidy
JulianToledano Sep 19, 2024
3b02c56
Merge branch 'main' into julian/v2-tx-builder
JulianToledano Oct 1, 2024
15389f0
del: txBuilder
JulianToledano Oct 1, 2024
e0d3e22
txbuilder removed
JulianToledano Oct 2, 2024
9cf6e40
update: docs
JulianToledano Oct 2, 2024
74bb4b5
lint :)
JulianToledano Oct 2, 2024
5d136a0
feedback
julienrbrt Oct 3, 2024
0f59ca6
Merge branch 'main' into julian/v2-tx-builder
julienrbrt Oct 3, 2024
457ea7b
cl + lint
julienrbrt Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/v2/autocli/keyring/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ type Keyring interface {

// Sign signs the given bytes with the key with the given name.
Sign(name string, msg []byte, signMode signingv1beta1.SignMode) ([]byte, error)

// KeyType returns the type of the key.
KeyType(name string) (uint, error)
}
4 changes: 4 additions & 0 deletions client/v2/autocli/keyring/no_keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (k NoKeyring) GetPubKey(name string) (cryptotypes.PubKey, error) {
func (k NoKeyring) Sign(name string, msg []byte, signMode signingv1beta1.SignMode) ([]byte, error) {
return nil, errNoKeyring
}

func (k NoKeyring) KeyType(name string) (uint, error) {
return 0, errNoKeyring
}
31 changes: 31 additions & 0 deletions client/v2/internal/coins/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package coins

import (
"errors"

base "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/math"
)

var (
_ withAmount = &base.Coin{}
_ withAmount = &base.DecCoin{}
)

type withAmount interface {
GetAmount() string
}

// IsZero check if given coins are zero.
func IsZero[T withAmount](coins []T) (bool, error) {
for _, coin := range coins {
amount, ok := math.NewIntFromString(coin.GetAmount())
if !ok {
return false, errors.New("invalid coin amount")
}
if !amount.IsZero() {
return false, nil
}
}
return true, nil
}
83 changes: 83 additions & 0 deletions client/v2/internal/coins/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package coins

import (
"testing"

"github.com/stretchr/testify/require"

base "cosmossdk.io/api/cosmos/base/v1beta1"
)

func TestCoinIsZero(t *testing.T) {
type testCase[T withAmount] struct {
name string
coins []T
isZero bool
}
tests := []testCase[*base.Coin]{
{
name: "not zero coin",
coins: []*base.Coin{
{
Denom: "stake",
Amount: "100",
},
},
isZero: false,
},
{
name: "zero coin",
coins: []*base.Coin{
{
Denom: "stake",
Amount: "0",
},
},
isZero: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IsZero(tt.coins)
require.NoError(t, err)
require.Equal(t, got, tt.isZero)
})
}
}

func TestDecCoinIsZero(t *testing.T) {
type testCase[T withAmount] struct {
name string
coins []T
isZero bool
}
tests := []testCase[*base.DecCoin]{
{
name: "not zero coin",
coins: []*base.DecCoin{
{
Denom: "stake",
Amount: "100",
},
},
isZero: false,
},
{
name: "zero coin",
coins: []*base.DecCoin{
{
Denom: "stake",
Amount: "0",
},
},
isZero: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IsZero(tt.coins)
require.NoError(t, err)
require.Equal(t, got, tt.isZero)
})
}
}
22 changes: 22 additions & 0 deletions client/v2/internal/grpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package grpc

import (
"context"
"fmt"

gogogrpc "github.com/cosmos/gogoproto/grpc"
"google.golang.org/grpc"
)

var _ gogogrpc.ClientConn = ClientConn{}

type ClientConn struct{}

func (c ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...grpc.CallOption) error {
//TODO implement me
panic("implement me")
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
}

func (c ClientConn) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("streaming rpc not supported")
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
}
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion client/v2/offchain/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Sign(ctx client.Context, rawBytes []byte, fromName, indent, encoding, outpu

// sign signs a digest with provided key and SignMode.
func sign(ctx client.Context, fromName, digest string) (*apitx.Tx, error) {
keybase, err := keyring.NewAutoCLIKeyring(ctx.Keyring)
keybase, err := keyring.NewAutoCLIKeyring(ctx.Keyring, ctx.AddressCodec)
if err != nil {
return nil, err
}
Expand Down
110 changes: 110 additions & 0 deletions client/v2/tx/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package tx

import (
"context"
"fmt"
"strconv"

gogogrpc "github.com/cosmos/gogoproto/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"cosmossdk.io/core/address"
authtypes "cosmossdk.io/x/auth/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

// TODO: move to internal

var _ AccountRetriever = accountRetriever{}

// Account defines a read-only version of the auth module's AccountI.
type Account interface {
GetAddress() sdk.AccAddress
GetPubKey() cryptotypes.PubKey // can return nil.
GetAccountNumber() uint64
GetSequence() uint64
}

// AccountRetriever defines the interfaces required by transactions to
// ensure an account exists and to be able to query for account fields necessary
// for signing.
type AccountRetriever interface {
GetAccount(context.Context, sdk.AccAddress) (Account, error)
GetAccountWithHeight(context.Context, sdk.AccAddress) (Account, int64, error)
EnsureExists(context.Context, sdk.AccAddress) error
GetAccountNumberSequence(context.Context, sdk.AccAddress) (accNum, accSeq uint64, err error)
}

type accountRetriever struct {
ac address.Codec
conn gogogrpc.ClientConn
registry codectypes.InterfaceRegistry
}

func newAccountRetriever(ac address.Codec, conn gogogrpc.ClientConn, registry codectypes.InterfaceRegistry) *accountRetriever {
return &accountRetriever{
ac: ac,
conn: conn,
registry: registry,
}
}

func (a accountRetriever) GetAccount(ctx context.Context, addr sdk.AccAddress) (Account, error) {
acc, _, err := a.GetAccountWithHeight(ctx, addr)
return acc, err
}

func (a accountRetriever) GetAccountWithHeight(ctx context.Context, addr sdk.AccAddress) (Account, int64, error) {
var header metadata.MD

qc := authtypes.NewQueryClient(a.conn)

res, err := qc.Account(ctx, &authtypes.QueryAccountRequest{Address: addr.String()}, grpc.Header(&header))
if err != nil {
return nil, 0, err
}

blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
if l := len(blockHeight); l != 1 {
return nil, 0, fmt.Errorf("unexpected '%s' header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, l, 1)
}

nBlockHeight, err := strconv.Atoi(blockHeight[0])
if err != nil {
return nil, 0, fmt.Errorf("failed to parse block height: %w", err)
}

var acc Account
if err := a.registry.UnpackAny(res.Account, &acc); err != nil {
return nil, 0, err
}

return acc, int64(nBlockHeight), nil

}

func (a accountRetriever) EnsureExists(ctx context.Context, addr sdk.AccAddress) error {
if _, err := a.GetAccount(ctx, addr); err != nil {
return err
}
return nil
}

func (a accountRetriever) GetAccountNumberSequence(ctx context.Context, addr sdk.AccAddress) (accNum, accSeq uint64, err error) {
acc, err := a.GetAccount(ctx, addr)
if err != nil {
if status.Code(err) == codes.NotFound {
return 0, 0, nil
}
return 0, 0, err
}

return acc.GetAccountNumber(), acc.GetSequence(), nil
}
Loading
Loading