Skip to content

Commit

Permalink
Avoid instantiating the default marshaller before each use (#493)
Browse files Browse the repository at this point in the history
- Fix a typo in the class name

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
  • Loading branch information
liran-funaro authored Feb 13, 2023
1 parent 926a192 commit 17273f1
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion internal/bcdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ func (d *db) responseHeader() *types.ResponseHeader {
}

func (d *db) signature(response interface{}) ([]byte, error) {
responseBytes, err := marshal.DefaultMarshaler().Marshal(response.(proto.Message))
responseBytes, err := marshal.DefaultMarshaller().Marshal(response.(proto.Message))
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/bcdb/ledger_query_procesor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bcdb

import (
"fmt"

"github.com/hyperledger-labs/orion-server/internal/blockstore"
interrors "github.com/hyperledger-labs/orion-server/internal/errors"
"github.com/hyperledger-labs/orion-server/internal/identity"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (p *ledgerQueryProcessor) getTx(userId string, blockNum uint64, txIndex uin
switch block.Payload.(type) {
case *types.Block_DataTxEnvelopes:
dataTxEnvs := block.GetDataTxEnvelopes().Envelopes
if int(txIndex) >= len(dataTxEnvs) {
if int(txIndex) >= len(dataTxEnvs) {
return nil, &interrors.BadRequestError{ErrMsg: fmt.Sprintf("transaction index out of range: %d", txIndex)}
}
dataTxEnv := dataTxEnvs[txIndex]
Expand Down Expand Up @@ -225,7 +226,7 @@ func (p *ledgerQueryProcessor) hasDataTxAccess(userId string, env *types.DataTxE
continue
}

dataTxBytes, err := marshal.DefaultMarshaler().Marshal(dataTx)
dataTxBytes, err := marshal.DefaultMarshaller().Marshal(dataTx)
if err != nil {
p.logger.Errorf("Error during Marshal Tx: %s, error: %s", dataTx, err)
return false, errors.Wrap(err, "failed to Marshal Tx")
Expand Down
2 changes: 1 addition & 1 deletion internal/bcdb/ledger_query_procesor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func createSampleBlock(aliceSigner crypto.Signer, blockNumber uint64, key []stri
e.Signatures["testUser"] = []byte("in must sign so always correct")
e.Signatures["testUser2"] = []byte("bad sig")
if aliceSigner != nil {
eBytes, _ := marshal.DefaultMarshaler().Marshal(e.Payload)
eBytes, _ := marshal.DefaultMarshaller().Marshal(e.Payload)
sig, _ := aliceSigner.Sign(eBytes)
e.Signatures["alice"] = sig
}
Expand Down
2 changes: 1 addition & 1 deletion internal/httphandler/config_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func TestConfigRequestHandler_SubmitConfig(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
txEnv := tt.txEnvFactory()
txResp := tt.txRespFactory()
txBytes, err := marshal.DefaultMarshaler().Marshal(txEnv)
txBytes, err := marshal.DefaultMarshaller().Marshal(txEnv)
require.NoError(t, err)

txReader := bytes.NewReader(txBytes)
Expand Down
3 changes: 1 addition & 2 deletions internal/httphandler/data_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestDataRequestHandler_DataQuery(t *testing.T) {
Key: "key1",
})


testCases := []struct {
name string
requestFactory func() (*http.Request, error)
Expand Down Expand Up @@ -1337,7 +1336,7 @@ func TestDataRequestHandler_DataTransaction(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
txEnv := tt.txEnvFactory()
txResp := tt.txRespFactory()
txBytes, err := marshal.DefaultMarshaler().Marshal(txEnv)
txBytes, err := marshal.DefaultMarshaller().Marshal(txEnv)
require.NoError(t, err)
require.NotNil(t, txBytes)

Expand Down
2 changes: 1 addition & 1 deletion internal/httphandler/db_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func TestDBRequestHandler_DBTransaction(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
txEnv := tt.txEnvFactory()
txBytes, err := marshal.DefaultMarshaler().Marshal(txEnv)
txBytes, err := marshal.DefaultMarshaller().Marshal(txEnv)
require.NoError(t, err)
require.NotNil(t, txBytes)

Expand Down
2 changes: 1 addition & 1 deletion internal/httphandler/users_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func TestUsersRequestHandler_SubmitUserTx(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
txEnv := tt.txEnvFactory()
txBytes, err := marshal.DefaultMarshaler().Marshal(txEnv)
txBytes, err := marshal.DefaultMarshaller().Marshal(txEnv)
txResp := tt.txRespFactory()
require.NoError(t, err)
require.NotNil(t, txBytes)
Expand Down
2 changes: 1 addition & 1 deletion internal/httphandler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func VerifyRequestSignature(
return &types.HttpResponseErr{ErrMsg: "payload is not a protoreflect message"}, http.StatusInternalServerError
}

requestBytes, err := marshal.DefaultMarshaler().Marshal(requestPayload.(proto.Message))
requestBytes, err := marshal.DefaultMarshaller().Marshal(requestPayload.(proto.Message))
if err != nil {
return &types.HttpResponseErr{ErrMsg: "failure during Marshal: " + err.Error()}, http.StatusInternalServerError
}
Expand Down
2 changes: 1 addition & 1 deletion internal/txvalidation/sig_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *txSigValidator) validate(
signature []byte,
txPayload interface{},
) (*types.ValidationInfo, error) {
requestBytes, err := marshal.DefaultMarshaler().Marshal(txPayload.(proto.Message))
requestBytes, err := marshal.DefaultMarshaller().Marshal(txPayload.(proto.Message))
if err != nil {
s.logger.Errorf("Error during Marshal Tx: %s, error: %s", txPayload, err)
return nil, errors.Wrapf(err, "failed to Marshal Tx: %s", txPayload)
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const MultiPartFormData = "multipart/form-data"
func SendHTTPResponse(w http.ResponseWriter, code int, payload interface{}) {
var response []byte
if p, ok := payload.(proto.Message); ok {
response, _ = marshal.DefaultMarshaler().Marshal(p)
response, _ = marshal.DefaultMarshaller().Marshal(p)
} else {
response, _ = json.Marshal(payload)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cryptoservice/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func SignTx(txSigner crypto.Signer, tx interface{}) ([]byte, error) {
}

func SignPayload(signer crypto.Signer, payload interface{}) ([]byte, error) {
payloadBytes, err := marshal.DefaultMarshaler().Marshal(payload.(proto.Message))
payloadBytes, err := marshal.DefaultMarshaller().Marshal(payload.(proto.Message))
if err != nil {
return nil, err
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/marshal/proto_json_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ type DefaultMarshal struct {
marshalOption *protojson.MarshalOptions
}

func DefaultMarshaler() *DefaultMarshal {
return &DefaultMarshal{
marshalOption: &protojson.MarshalOptions{
Multiline: false,
AllowPartial: false,
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: false,
Resolver: nil,
},
}
var defaultMarshallerInstance = &DefaultMarshal{
marshalOption: &protojson.MarshalOptions{
Multiline: false,
AllowPartial: false,
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: false,
Resolver: nil,
},
}

func DefaultMarshaller() *DefaultMarshal {
return defaultMarshallerInstance
}

func (o DefaultMarshal) Marshal(m proto.Message) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/mock/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (c *Client) SubmitTransaction(urlPath string, tx interface{}, serverTimeout
},
)

txBytes, err := marshal.DefaultMarshaler().Marshal(tx.(proto.Message))
txBytes, err := marshal.DefaultMarshaller().Marshal(tx.(proto.Message))
if err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestServerWithDataRequestAndProvenanceQueries(t *testing.T) {
require.NotNil(t, data)
require.NotNil(t, data.Response)

resp, err := marshal.DefaultMarshaler().Marshal(data.GetResponse())
resp, err := marshal.DefaultMarshaller().Marshal(data.GetResponse())
require.NoError(t, err)
err = verifier.Verify(resp, data.GetSignature())
require.NoError(t, err)
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestServerWithDataRequestAndProvenanceQueries(t *testing.T) {
return false
}

dataB, err := marshal.DefaultMarshaler().Marshal(data.GetResponse())
dataB, err := marshal.DefaultMarshaller().Marshal(data.GetResponse())
require.NoError(t, err)

err = verifier.Verify(dataB, data.GetSignature())
Expand All @@ -486,7 +486,7 @@ func TestServerWithDataRequestAndProvenanceQueries(t *testing.T) {
)
require.NoError(t, err)

valuesB, err := marshal.DefaultMarshaler().Marshal(values.GetResponse())
valuesB, err := marshal.DefaultMarshaller().Marshal(values.GetResponse())
require.NoError(t, err)

err = verifier.Verify(valuesB, values.GetSignature())
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestServerWithDataRequestAndProvenanceOff(t *testing.T) {
require.NotNil(t, data)
require.NotNil(t, data.Response)

resp, err := marshal.DefaultMarshaler().Marshal(data.GetResponse())
resp, err := marshal.DefaultMarshaller().Marshal(data.GetResponse())
require.NoError(t, err)
err = verifier.Verify(resp, data.GetSignature())
require.NoError(t, err)
Expand Down Expand Up @@ -575,7 +575,7 @@ func TestServerWithDataRequestAndProvenanceOff(t *testing.T) {
return false
}

dataB, err := marshal.DefaultMarshaler().Marshal(data.GetResponse())
dataB, err := marshal.DefaultMarshaller().Marshal(data.GetResponse())
require.NoError(t, err)

err = verifier.Verify(dataB, data.GetSignature())
Expand Down Expand Up @@ -674,7 +674,7 @@ func TestServerWithUserAdminRequest(t *testing.T) {
return false
}

userB, err := marshal.DefaultMarshaler().Marshal(user.GetResponse())
userB, err := marshal.DefaultMarshaller().Marshal(user.GetResponse())
require.NoError(t, err)

err = verifier.Verify(userB, user.GetSignature())
Expand Down Expand Up @@ -727,7 +727,7 @@ func TestServerWithDBAdminRequest(t *testing.T) {
return false
}

dbB, err := marshal.DefaultMarshaler().Marshal(db.GetResponse())
dbB, err := marshal.DefaultMarshaller().Marshal(db.GetResponse())
require.NoError(t, err)

err = verifier.Verify(dbB, db.GetSignature())
Expand Down Expand Up @@ -784,7 +784,7 @@ func TestServerWithDBAdminRequest(t *testing.T) {
return false
}

dataB, err := marshal.DefaultMarshaler().Marshal(data.GetResponse())
dataB, err := marshal.DefaultMarshaller().Marshal(data.GetResponse())
require.NoError(t, err)

err = verifier.Verify(dataB, data.GetSignature())
Expand Down Expand Up @@ -943,7 +943,7 @@ func TestSyncTxWithServerTLS(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, user.GetResponse())

userBytes, err := marshal.DefaultMarshaler().Marshal(user.GetResponse())
userBytes, err := marshal.DefaultMarshaller().Marshal(user.GetResponse())
require.NoError(t, err)

err = verifier.Verify(userBytes, user.GetSignature())
Expand Down Expand Up @@ -1107,7 +1107,7 @@ func TestSyncTxWithServerAndClientTLS(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, user.GetResponse())

userBytes, err := marshal.DefaultMarshaler().Marshal(user.GetResponse())
userBytes, err := marshal.DefaultMarshaller().Marshal(user.GetResponse())
require.NoError(t, err)

err = verifier.Verify(userBytes, user.GetSignature())
Expand Down Expand Up @@ -1171,7 +1171,7 @@ func TestServerWithRestart(t *testing.T) {
return false
}

userB, err := marshal.DefaultMarshaler().Marshal(user.GetResponse())
userB, err := marshal.DefaultMarshaller().Marshal(user.GetResponse())
require.NoError(t, err)

err = verifier.Verify(userB, user.GetSignature())
Expand Down Expand Up @@ -1205,7 +1205,7 @@ func TestServerWithRestart(t *testing.T) {
return false
}

userB, err := marshal.DefaultMarshaler().Marshal(user.GetResponse())
userB, err := marshal.DefaultMarshaller().Marshal(user.GetResponse())
require.NoError(t, err)

err = verifier.Verify(userB, user.GetSignature())
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/testutils/crypto_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func SignedDBAdministrationTxEnvelope(t *testing.T, signer crypto.Signer, tx *ty
func VerifyPayloadSignature(t *testing.T, rawCert []byte, payload interface{}, sig []byte) {
ver, err := crypto.NewVerifier(rawCert)
require.NoError(t, err)
payloadBytes, err := marshal.DefaultMarshaler().Marshal(payload.(proto.Message))
payloadBytes, err := marshal.DefaultMarshaller().Marshal(payload.(proto.Message))
require.NoError(t, err)
err = ver.Verify(payloadBytes, sig)
require.NoError(t, err)
Expand Down

0 comments on commit 17273f1

Please sign in to comment.