Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
FAB-7983 add and use zap logger
Browse files Browse the repository at this point in the history
 - replace debug print statements in with calls to logger at debug level
 - user facing print statements logged at info or fatal where appropriate

Change-Id: I7e3465068ed14f742ae6b85bac8587ac931b8a5c
Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
  • Loading branch information
MHBauer committed Dec 6, 2018
1 parent 788a84d commit b5144f7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
revision = "6b78f7a22d950103fb2669aabb969f56d42d283b"
name = "github.com/hyperledger/fabric-amcl"

[[constraint]]
name = "go.uber.org/zap"
version = "1.9.0"

#Constraints from fabric-sdk-go
[[override]]
revision = "aafc9e6bc7b7bb53ddaa75a5ef49a17d6e654be5"
Expand Down
34 changes: 17 additions & 17 deletions fabproxy/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
"fmt"
"os"
"strconv"

"go.uber.org/zap"

"github.com/hyperledger/fabric-chaincode-evm/fabproxy"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
Expand All @@ -30,7 +31,12 @@ const usage = `FabProxy uses environment variables to be able to start communica
PORT - Port the FabProxy will be running on. Default is 5000
`

var logger *zap.SugaredLogger

func main() {
rawLogger, _ := zap.NewProduction()
logger := rawLogger.Named("fab3").Sugar()

cfg := grabEnvVar("FABPROXY_CONFIG", true, "Path to the Fabric SDK GO config file")
org := grabEnvVar("FABPROXY_ORG", true, "Org of the user specified")
user := grabEnvVar("FABPROXY_USER", true, "User to be used for proxy")
Expand All @@ -43,52 +49,46 @@ func main() {
var err error
portNumber, err = strconv.Atoi(port)
if err != nil {
fmt.Printf("Failed to convert the environment variable `PORT`, %s, to an int\n", port)
os.Exit(1)
logger.Fatalf("Failed to convert the environment variable `PORT`, %s, to an int\n", port)
}
}

sdk, err := fabsdk.New(config.FromFile(cfg))
if err != nil {
fmt.Printf("Failed to create Fabric SDK Client: %s\n", err.Error())
os.Exit(1)
logger.Fatalf("Failed to create Fabric SDK Client: %s\n", err.Error())
}
defer sdk.Close()

clientChannelContext := sdk.ChannelContext(ch, fabsdk.WithUser(user), fabsdk.WithOrg(org))
client, err := channel.New(clientChannelContext)
if err != nil {
fmt.Printf("Failed to create Fabric SDK Channel Client: %s\n", err.Error())
os.Exit(1)
logger.Fatalf("Failed to create Fabric SDK Channel Client: %s\n", err.Error())
}

ledger, err := ledger.New(clientChannelContext)
if err != nil {
fmt.Printf("Failed to create Fabric SDK Ledger Client: %s\n", err.Error())
os.Exit(1)
logger.Fatalf("Failed to create Fabric SDK Ledger Client: %s\n", err.Error())
}

ethService := fabproxy.NewEthService(client, ledger, ch, ccid)
ethService := fabproxy.NewEthService(client, ledger, ch, ccid, logger)

fmt.Printf("Starting Fab Proxy on port %d\n", portNumber)
logger.Infof("Starting Fab Proxy on port %d\n", portNumber)
proxy := fabproxy.NewFabProxy(ethService)
err = proxy.Start(portNumber)
if err != nil {
fmt.Println(err)
os.Exit(1)
logger.Fatal(err)
}
defer func() {
fmt.Println("Shutting down the Fab Proxy")
logger.Info("Shutting down the Fab Proxy")
proxy.Shutdown()
fmt.Println("Fab Proxy has exited")
logger.Info("Fab Proxy has exited")
}()
}

func grabEnvVar(varName string, required bool, description string) string {
envVar := os.Getenv(varName)
if required && envVar == "" {
fmt.Printf("Fab Proxy requires the environment variable %s to be set\n\n%s\n\n", varName, usage)
os.Exit(1)
logger.Fatalf("Fab Proxy requires the environment variable %s to be set\n\n%s\n\n", varName, usage)
}
return envVar
}
26 changes: 14 additions & 12 deletions fabproxy/ethservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"strings"

"github.com/gogo/protobuf/proto"
"go.uber.org/zap"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
Expand Down Expand Up @@ -64,6 +66,7 @@ type ethService struct {
ledgerClient LedgerClient
channelID string
ccid string
logger *zap.SugaredLogger
}

type EthArgs struct {
Expand Down Expand Up @@ -116,8 +119,8 @@ type Block struct {
Transactions []interface{} `json:"transactions"` // transactions: Array - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
}

func NewEthService(channelClient ChannelClient, ledgerClient LedgerClient, channelID string, ccid string) EthService {
return &ethService{channelClient: channelClient, ledgerClient: ledgerClient, channelID: channelID, ccid: ccid}
func NewEthService(channelClient ChannelClient, ledgerClient LedgerClient, channelID string, ccid string, logger *zap.SugaredLogger) EthService {
return &ethService{channelClient: channelClient, ledgerClient: ledgerClient, channelID: channelID, ccid: ccid, logger: logger.Named("ethservice")}
}

func (s *ethService) GetCode(r *http.Request, arg *string, reply *string) error {
Expand Down Expand Up @@ -231,7 +234,7 @@ func (s *ethService) Accounts(r *http.Request, arg *string, reply *[]string) err
// EVM-chaincode does not require gas to run transactions. The chaincode will
// give enough gas per transaction.
func (s *ethService) EstimateGas(r *http.Request, _ *EthArgs, reply *string) error {
fmt.Println("EstimateGas called")
s.logger.Debug("EstimateGas called")
*reply = "0x0"
return nil
}
Expand All @@ -241,16 +244,16 @@ func (s *ethService) EstimateGas(r *http.Request, _ *EthArgs, reply *string) err
//
// Always returns zero.
func (s *ethService) GetBalance(r *http.Request, p *[]string, reply *string) error {
fmt.Println("GetBalance called")
s.logger.Debug("GetBalance called")
*reply = "0x0"
return nil
}

// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber
func (s *ethService) GetBlockByNumber(r *http.Request, p *[]interface{}, reply *Block) error {
fmt.Println("Received a request for GetBlockByNumber")
s.logger.Debug("Received a request for GetBlockByNumber")
params := *p
fmt.Println("Params are : ", params)
s.logger.Debug("Params are : ", params)

// handle params
// must have two params
Expand All @@ -261,7 +264,7 @@ func (s *ethService) GetBlockByNumber(r *http.Request, p *[]interface{}, reply *
// first arg is string of block to get
number, ok := params[0].(string)
if !ok {
fmt.Printf("Incorrect argument received: %#v", params[0])
s.logger.Debugf("Incorrect argument received: %#v", params[0])
return fmt.Errorf("Incorrect first parameter sent, must be string")
}

Expand Down Expand Up @@ -312,7 +315,7 @@ func (s *ethService) GetBlockByNumber(r *http.Request, p *[]interface{}, reply *

// returning full transactions is unimplemented,
// so the hash-only case is the only case.
fmt.Println("block has transaction hash:", chdr.TxId)
s.logger.Debug("block has transaction hash:", chdr.TxId)

if fullTransactions {
txn := Transaction{
Expand Down Expand Up @@ -340,7 +343,7 @@ func (s *ethService) GetBlockByNumber(r *http.Request, p *[]interface{}, reply *
ParentHash: "0x" + hex.EncodeToString(blkHeader.GetPreviousHash()),
Transactions: txns,
}
fmt.Println("asked for block", number, "found block", blk)
s.logger.Debug("asked for block", number, "found block", blk)

*reply = blk
return nil
Expand All @@ -362,7 +365,7 @@ func (s *ethService) GetTransactionByHash(r *http.Request, txID *string, reply *
if strippedTxId == "" {
return fmt.Errorf("txID was empty")
}
fmt.Println("GetTransactionByHash", strippedTxId) // logging input to function
s.logger.Debug("GetTransactionByHash", strippedTxId) // logging input to function

txn := Transaction{
Hash: "0x" + strippedTxId,
Expand Down Expand Up @@ -421,7 +424,7 @@ func (s *ethService) parseBlockNum(input string) (uint64, error) {

blockchainInfo, err := s.ledgerClient.QueryInfo()
if err != nil {
fmt.Println(err)
s.logger.Debug(err)
return 0, fmt.Errorf("Failed to query the ledger: %v", err)
}

Expand Down Expand Up @@ -540,7 +543,6 @@ func findTransaction(txID string, blockData [][]byte) (string, *common.Payload,
return "", &common.Payload{}, err
}

fmt.Println("transaction hash:", chdr.TxId)
// early exit to try next transaction
if txID != chdr.TxId {
// transaction does not match, go to next
Expand Down
7 changes: 6 additions & 1 deletion fabproxy/ethservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import (
"strconv"
"strings"

"go.uber.org/zap"

"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-evm/fabproxy"
fabproxy_mocks "github.com/hyperledger/fabric-chaincode-evm/mocks/fabproxy"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand All @@ -34,13 +37,15 @@ var _ = Describe("Ethservice", func() {
mockLedgerClient *fabproxy_mocks.MockLedgerClient
channelID string
)
rawLogger, _ := zap.NewProduction()
logger := rawLogger.Sugar()

BeforeEach(func() {
mockChClient = &fabproxy_mocks.MockChannelClient{}
mockLedgerClient = &fabproxy_mocks.MockLedgerClient{}
channelID = "test-channel"

ethservice = fabproxy.NewEthService(mockChClient, mockLedgerClient, channelID, evmcc)
ethservice = fabproxy.NewEthService(mockChClient, mockLedgerClient, channelID, evmcc, logger)
})

Describe("GetCode", func() {
Expand Down

0 comments on commit b5144f7

Please sign in to comment.