Skip to content

Commit

Permalink
make protojson
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <fredprtnsk@gmail.com>
  • Loading branch information
pfi79 committed Jun 11, 2024
1 parent f9214ce commit 1c558f7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions cc/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cc

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"golang.org/x/crypto/sha3"
"google.golang.org/protobuf/encoding/protojson"
)

// SetAccountInfo sets account info (KYC hash, grayList and blacklist attributes) for address.
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *ACL) GetAccountInfo(stub shim.ChaincodeStubInterface, args []string) pe
if err != nil {
return shim.Error(err.Error())
}
accInfoSerialized, err := json.Marshal(accInfo)
accInfoSerialized, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(accInfo)
if err != nil {
return shim.Error(err.Error())
}
Expand All @@ -113,9 +113,12 @@ func getAccountInfo(stub shim.ChaincodeStubInterface, address string) (*pb.Accou
}

var info pb.AccountInfo
if err = proto.Unmarshal(infoData, &info); err != nil {
return nil, err
if err = protojson.Unmarshal(infoData, &info); err != nil {
if err = proto.Unmarshal(infoData, &info); err != nil {
return nil, err
}
}

return &info, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func SetConfig(stub shim.ChaincodeStubInterface) error {

// This field should be filled automatically to compare while other chaincode invokes ACL
cfg.CcName = ccName
cfgBytes, err = protojson.Marshal(cfg)
cfgBytes, err = protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(cfg)
if err != nil {
return fmt.Errorf("marshalling config: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func StubCreate(t *testing.T) *shimtest.MockStub {
// StubCreateAndInit creates mock stub and initializes it with TestIniArgs
func StubCreateAndInit(t *testing.T) *shimtest.MockStub {
stub := StubCreate(t)
cfgBytes, err := protojson.Marshal(TestInitConfig)
cfgBytes, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(TestInitConfig)
require.NoError(t, err)
var args [][]byte
args = append(args, cfgBytes)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cmn/chaincode_with_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func DeployACLWithError(network *nwo.Network, components *nwo.Components,
testDir string, aclCfg *aclpb.ACLConfig, errorMsg string) {
By("Deploying chaincode acl")
cfgBytesACL, err := protojson.Marshal(aclCfg)
cfgBytesACL, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(aclCfg)
Expect(err).NotTo(HaveOccurred())
ctorACL := cmn.CtorFromSlice([]string{string(cfgBytesACL)})
DeployChaincodeFoundationWithError(network, cmn.ChannelAcl, components,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/access_matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestAclCalledFromChaincode(t *testing.T) {
},
}

cfgBytes, _ := protojson.Marshal(cfg)
cfgBytes, _ := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(cfg)

init := ledgerMock.NewCC("fiat", common.NewFiatToken(token.BaseToken{}), string(cfgBytes))
require.Empty(t, init)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/get_account_info_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package unit

import (
"encoding/json"
"testing"

"github.com/anoideaopen/acl/cc/errs"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/hyperledger/fabric-chaincode-go/shimtest" //nolint:staticcheck
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
)

type seriesGetAccountInfo struct {
Expand Down Expand Up @@ -90,7 +90,7 @@ func validationResultGetAccountInfo(t *testing.T, resp peer.Response, ser *serie
}

addrFromLedger := &pb.AccountInfo{}
require.NoError(t, json.Unmarshal(resp.Payload, addrFromLedger))
require.NoError(t, protojson.Unmarshal(resp.Payload, addrFromLedger))
require.Equal(t, false, addrFromLedger.BlackListed)
require.Equal(t, false, addrFromLedger.GrayListed)
require.Equal(t, kycHash, addrFromLedger.KycHash)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/set_account_info_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package unit

import (
"encoding/json"
"fmt"
"strconv"
"testing"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/hyperledger/fabric-chaincode-go/shimtest" //nolint:staticcheck
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
)

type seriesSetAccountInfo struct {
Expand Down Expand Up @@ -185,7 +185,7 @@ func validationResultSetAccountInfo(t *testing.T, stub *shimtest.MockStub, resp
require.NoError(t, err)

addrFromLedger := &pb.AccountInfo{}
require.NoError(t, json.Unmarshal(check.Payload, addrFromLedger))
require.NoError(t, protojson.Unmarshal(check.Payload, addrFromLedger))
require.Equal(t, "kycHash2", addrFromLedger.KycHash)
require.Equal(t, isGrayListedBool, addrFromLedger.GrayListed)
require.Equal(t, isBlackListedBool, addrFromLedger.BlackListed)
Expand Down

0 comments on commit 1c558f7

Please sign in to comment.