Skip to content

Commit

Permalink
Merge branch 'master' into sahith/update-auth-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Jul 29, 2020
2 parents 423cbcb + 2da954f commit d9b15df
Show file tree
Hide file tree
Showing 44 changed files with 16,529 additions and 2,042 deletions.
27 changes: 8 additions & 19 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)
Expand Down Expand Up @@ -313,25 +314,13 @@ func getSignBytes(
signMode signing.SignMode, signerData authsigning.SignerData,
txBuilder client.TxBuilder, pubKey crypto.PubKey, txConfig client.TxConfig,
) ([]byte, error) {

sigData := signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
}
sig := signing.SignatureV2{
PubKey: pubKey,
Data: &sigData,
}

// For SIGN_MODE_DIRECT, calling SetSignatures calls SetSignerInfos on
// TxBuilder under the hood, and SignerInfos is needed to generated the
// sign bytes. This is the reason for setting SetSignatures here, with a
// nil signature.
//
// Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it
// also doesn't affect its generated sign bytes, so for code's simplicity
// sake, we put it here.
if err := txBuilder.SetSignatures(sig); err != nil {
// Set signer info, we only support single signature in this function.
err := txBuilder.SetSignerInfo(pubKey, &txtypes.ModeInfo{
Sum: &txtypes.ModeInfo_Single_{
Single: &txtypes.ModeInfo_Single{Mode: signMode},
},
})
if err != nil {
return nil, err
}

Expand Down
4 changes: 4 additions & 0 deletions client/tx_generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package client

import (
"github.com/tendermint/tendermint/crypto"

sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)
Expand Down Expand Up @@ -37,6 +40,7 @@ type (
GetTx() signing.SigFeeMemoTx

SetMsgs(msgs ...sdk.Msg) error
SetSignerInfo(pubKey crypto.PubKey, modeInfo *txtypes.ModeInfo) error
SetSignatures(signatures ...signingtypes.SignatureV2) error
SetMemo(memo string)
SetFeeAmount(amount sdk.Coins)
Expand Down
115 changes: 115 additions & 0 deletions codec/unknownproto/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package unknownproto_test

import (
"sync"
"testing"

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec/unknownproto"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)

var n1BBlob []byte

func init() {
n1B := &testdata.Nested1B{
Id: 1,
Age: 99,
Nested: &testdata.Nested2B{
Id: 2,
Route: "Wintery route",
Fee: 99,
Nested: &testdata.Nested3B{
Id: 3,
Name: "3A this one that one there those oens",
Age: 4588,
B4: []*testdata.Nested4B{
{
Id: 4,
Age: 88,
Name: "Nested4B",
},
},
},
},
}

var err error
n1BBlob, err = proto.Marshal(n1B)
if err != nil {
panic(err)
}
}

func BenchmarkRejectUnknownFields_serial(b *testing.B) {
benchmarkRejectUnknownFields(b, false)
}
func BenchmarkRejectUnknownFields_parallel(b *testing.B) {
benchmarkRejectUnknownFields(b, true)
}

func benchmarkRejectUnknownFields(b *testing.B, parallel bool) {
b.ReportAllocs()

if !parallel {
ckr := new(unknownproto.Checker)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n1A := new(testdata.Nested1A)
if err := ckr.RejectUnknownFields(n1BBlob, n1A); err == nil {
b.Fatal("expected an error")
}
b.SetBytes(int64(len(n1BBlob)))
}
} else {
var mu sync.Mutex
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
ckr := new(unknownproto.Checker)
for pb.Next() {
// To simulate the conditions of multiple transactions being processed in parallel.
n1A := new(testdata.Nested1A)
if err := ckr.RejectUnknownFields(n1BBlob, n1A); err == nil {
b.Fatal("expected an error")
}
mu.Lock()
b.SetBytes(int64(len(n1BBlob)))
mu.Unlock()
}
})
}
}

func BenchmarkProtoUnmarshal_serial(b *testing.B) {
benchmarkProtoUnmarshal(b, false)
}
func BenchmarkProtoUnmarshal_parallel(b *testing.B) {
benchmarkProtoUnmarshal(b, true)
}
func benchmarkProtoUnmarshal(b *testing.B, parallel bool) {
b.ReportAllocs()

if !parallel {
for i := 0; i < b.N; i++ {
n1A := new(testdata.Nested1A)
if err := proto.Unmarshal(n1BBlob, n1A); err == nil {
b.Fatal("expected an error")
}
b.SetBytes(int64(len(n1BBlob)))
}
} else {
var mu sync.Mutex
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
n1A := new(testdata.Nested1A)
if err := proto.Unmarshal(n1BBlob, n1A); err == nil {
b.Fatal("expected an error")
}
mu.Lock()
b.SetBytes(int64(len(n1BBlob)))
mu.Unlock()
}
})
}
}
28 changes: 28 additions & 0 deletions codec/unknownproto/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
unknownproto implements functionality to "type check" protobuf serialized byte sequences
against an expected proto.Message to report:
a) Unknown fields in the stream -- this is indicative of mismatched services, perhaps a malicious actor
b) Mismatched wire types for a field -- this is indicative of mismatched services
Its API signature is similar to proto.Unmarshal([]byte, proto.Message) as
ckr := new(unknownproto.Checker)
if err := ckr.RejectUnknownFields(protoBlob, protoMessage); err != nil {
// Handle the error.
}
and ideally should be added before invoking proto.Unmarshal, if you'd like to enforce the features mentioned above.
By default, for security we report every single field that's unknown, whether a non-critical field or not. To customize
this behavior, please create a Checker and set the AllowUnknownNonCriticals to true, for example:
ckr := &unknownproto.Checker{
AllowUnknownNonCriticals: true,
}
if err := ckr.RejectUnknownFields(protoBlob, protoMessage); err != nil {
// Handle the error.
}
*/
package unknownproto
32 changes: 32 additions & 0 deletions codec/unknownproto/unit_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package unknownproto

import (
"fmt"
"testing"

"google.golang.org/protobuf/encoding/protowire"
)

func TestWireTypeToString(t *testing.T) {
tests := []struct {
typ protowire.Type
want string
}{
{typ: 0, want: "varint"},
{typ: 1, want: "fixed64"},
{typ: 2, want: "bytes"},
{typ: 3, want: "start_group"},
{typ: 4, want: "end_group"},
{typ: 5, want: "fixed32"},
{typ: 95, want: "unknown type: 95"},
}

for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("wireType=%d", tt.typ), func(t *testing.T) {
if g, w := wireTypeToString(tt.typ), tt.want; g != w {
t.Fatalf("Mismatch:\nGot: %q\nWant: %q\n", g, w)
}
})
}
}
Loading

0 comments on commit d9b15df

Please sign in to comment.