From a5d4c04c4afab4b79e7a6988af29ea65036be35e Mon Sep 17 00:00:00 2001 From: YACOVM Date: Mon, 8 May 2017 11:06:34 +0300 Subject: [PATCH] [FAB-3712] Optimize struct memory alignment https://github.com/opennota/check can be used for checking struct alignment. Our code has some places in which we could use to have better struct alignment. This attends to all reported problems in gossip, and some in core. Change-Id: I7145e8b08a910c5cf20f1ae1b1e4366392e66031 Signed-off-by: Yacov Manevich --- core/chaincode/chaincode_support.go | 4 ++-- core/comm/server.go | 8 +++---- .../txmgmt/rwsetutil/query_results_helper.go | 13 +++++++----- core/scc/sysccapi.go | 8 +++---- gossip/comm/comm_impl.go | 6 +++--- gossip/gossip/msgstore/msgs.go | 21 ++++++++----------- gossip/gossip/pull/pullstore.go | 4 ++-- gossip/state/state.go | 4 ++-- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/core/chaincode/chaincode_support.go b/core/chaincode/chaincode_support.go index 2a10e1248a2..36c70b9f355 100644 --- a/core/chaincode/chaincode_support.go +++ b/core/chaincode/chaincode_support.go @@ -217,10 +217,8 @@ type ChaincodeSupport struct { runningChaincodes *runningChaincodes peerAddress string ccStartupTimeout time.Duration - userRunsCC bool peerNetworkID string peerID string - peerTLS bool peerTLSCertFile string peerTLSKeyFile string peerTLSSvrHostOrd string @@ -229,6 +227,8 @@ type ChaincodeSupport struct { shimLogLevel string logFormat string executetimeout time.Duration + userRunsCC bool + peerTLS bool } // DuplicateChaincodeHandlerError returned if attempt to register same chaincodeID while a stream already exists. diff --git a/core/comm/server.go b/core/comm/server.go index 482e6c486d4..143f329b8e3 100644 --- a/core/comm/server.go +++ b/core/comm/server.go @@ -32,8 +32,6 @@ import ( //A SecureServerConfig structure is used to configure security (e.g. TLS) for a //GRPCServer instance type SecureServerConfig struct { - //Whether or not to use TLS for communication - UseTLS bool //PEM-encoded X509 public key to be used by the server for TLS communication ServerCertificate []byte //PEM-encoded private key to be used by the server for TLS communication @@ -41,11 +39,13 @@ type SecureServerConfig struct { //Set of PEM-encoded X509 certificate authorities to optionally send //as part of the server handshake ServerRootCAs [][]byte - //Whether or not TLS client must present certificates for authentication - RequireClientCert bool //Set of PEM-encoded X509 certificate authorities to use when verifying //client certificates ClientRootCAs [][]byte + //Whether or not to use TLS for communication + UseTLS bool + //Whether or not TLS client must present certificates for authentication + RequireClientCert bool } //GRPCServer defines an interface representing a GRPC-based server diff --git a/core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go b/core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go index 872aea0886d..5adaa66d1ae 100644 --- a/core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go +++ b/core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go @@ -61,14 +61,17 @@ var ( // The `Done` function does the final processing and returns the final output type RangeQueryResultsHelper struct { pendingResults []*kvrwset.KVRead - hashingEnabled bool - maxDegree uint32 mt *merkleTree + maxDegree uint32 + hashingEnabled bool } // NewRangeQueryResultsHelper constructs a RangeQueryResultsHelper func NewRangeQueryResultsHelper(enableHashing bool, maxDegree uint32) (*RangeQueryResultsHelper, error) { - helper := &RangeQueryResultsHelper{nil, enableHashing, maxDegree, nil} + helper := &RangeQueryResultsHelper{pendingResults: nil, + hashingEnabled: enableHashing, + maxDegree: maxDegree, + mt: nil} if enableHashing { var err error if helper.mt, err = newMerkleTree(maxDegree); err != nil { @@ -147,16 +150,16 @@ func serializeKVReads(kvReads []*kvrwset.KVRead) ([]byte, error) { //////////// Merkle tree building code /////// type merkleTree struct { - maxDegree uint32 tree map[MerkleTreeLevel][]Hash maxLevel MerkleTreeLevel + maxDegree uint32 } func newMerkleTree(maxDegree uint32) (*merkleTree, error) { if maxDegree < 2 { return nil, fmt.Errorf("maxDegree [is %d] should not be less than 2 in the merkle tree", maxDegree) } - return &merkleTree{maxDegree, make(map[MerkleTreeLevel][]Hash), 1}, nil + return &merkleTree{make(map[MerkleTreeLevel][]Hash), 1, maxDegree}, nil } // update takes a hash that forms the next leaf level (level-1) node in the merkle tree. diff --git a/core/scc/sysccapi.go b/core/scc/sysccapi.go index bd2fee63942..01cc2bdb8c9 100644 --- a/core/scc/sysccapi.go +++ b/core/scc/sysccapi.go @@ -39,10 +39,6 @@ var sysccLogger = flogging.MustGetLogger("sccapi") // when the fabric comes up. SystemChaincodes are installed by adding an // entry in importsysccs.go type SystemChaincode struct { - // Enabled a convenient switch to enable/disable system chaincode without - // having to remove entry from importsysccs.go - Enabled bool - //Unique name of the system chaincode Name string @@ -65,6 +61,10 @@ type SystemChaincode struct { // by way of a chaincode-to-chaincode // invocation InvokableCC2CC bool + + // Enabled a convenient switch to enable/disable system chaincode without + // having to remove entry from importsysccs.go + Enabled bool } // RegisterSysCC registers the given system chaincode with the peer diff --git a/gossip/comm/comm_impl.go b/gossip/comm/comm_impl.go index b50fdabc9d8..0d6c6c6ee0c 100644 --- a/gossip/comm/comm_impl.go +++ b/gossip/comm/comm_impl.go @@ -145,7 +145,6 @@ func NewCommInstance(s *grpc.Server, cert *tls.Certificate, idStore identity.Map } type commImpl struct { - skipHandshake bool selfCertHash []byte peerIdentity api.PeerIdentityType idMapper identity.Mapper @@ -154,16 +153,17 @@ type commImpl struct { secureDialOpts func() []grpc.DialOption connStore *connectionStore PKIID []byte - port int deadEndpoints chan common.PKIidType msgPublisher *ChannelDeMultiplexer lock *sync.RWMutex lsnr net.Listener gSrv *grpc.Server exitChan chan struct{} - stopping int32 stopWG sync.WaitGroup subscriptions []chan proto.ReceivedMessage + port int + stopping int32 + skipHandshake bool } func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidType) (*connection, error) { diff --git a/gossip/gossip/msgstore/msgs.go b/gossip/gossip/msgstore/msgs.go index 285d7876553..6d31570d311 100644 --- a/gossip/gossip/msgstore/msgs.go +++ b/gossip/gossip/msgstore/msgs.go @@ -105,21 +105,18 @@ type MessageStore interface { } type messageStoreImpl struct { - pol common.MessageReplacingPolicy - lock sync.RWMutex - messages []*msg - invTrigger invalidationTrigger - - expirable bool - msgTTL time.Duration - expiredCount int - + pol common.MessageReplacingPolicy + lock sync.RWMutex + messages []*msg + invTrigger invalidationTrigger + msgTTL time.Duration + expiredCount int externalLock func() externalUnlock func() expireMsgCallback func(msg interface{}) - - doneCh chan struct{} - stopOnce sync.Once + doneCh chan struct{} + stopOnce sync.Once + expirable bool } type msg struct { diff --git a/gossip/gossip/pull/pullstore.go b/gossip/gossip/pull/pullstore.go index 600e0c81d67..ccfab25595a 100644 --- a/gossip/gossip/pull/pullstore.go +++ b/gossip/gossip/pull/pullstore.go @@ -59,9 +59,9 @@ type MembershipService interface { type Config struct { ID string PullInterval time.Duration // Duration between pull invocations - PeerCountToSelect int // Number of peers to initiate pull with - Tag proto.GossipMessage_Tag Channel common.ChainID + PeerCountToSelect int // Number of peers to initiate pull with + Tag proto.GossipMessage_Tag MsgType proto.PullMsgType } diff --git a/gossip/state/state.go b/gossip/state/state.go index 68235db34ba..4aeac0ad882 100644 --- a/gossip/state/state.go +++ b/gossip/state/state.go @@ -106,13 +106,13 @@ type GossipStateProviderImpl struct { stateRequestCh chan proto.ReceivedMessage - stateTransferActive int32 - stopCh chan struct{} done sync.WaitGroup once sync.Once + + stateTransferActive int32 } var logger *logging.Logger // package-level logger