Skip to content

Commit

Permalink
Merge branch 'master' into track
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Jul 19, 2022
2 parents 809dafd + 2ed5b03 commit c571d60
Show file tree
Hide file tree
Showing 22 changed files with 788 additions and 84 deletions.
38 changes: 0 additions & 38 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
Expand All @@ -35,7 +34,6 @@ import (
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -72,28 +70,8 @@ var (
Time: 60 * time.Second, // Ping the client if it is idle for 60 seconds to ensure the connection is still active
Timeout: 10 * time.Second, // Wait 10 seconds for the ping ack before assuming the connection is dead
}

_apiCallSourceWithChainIDMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_apicallsource_chainid_metrics",
Help: "API call Source ChainID Statistics",
},
[]string{"chain_id"},
)
_apiCallSourceWithOutChainIDMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_apicallsource_nochainid_metrics",
Help: "API call Source Without ChainID Statistics",
},
[]string{"client_ip", "sender"},
)
)

func init() {
prometheus.MustRegister(_apiCallSourceWithChainIDMtc)
prometheus.MustRegister(_apiCallSourceWithOutChainIDMtc)
}

// RecoveryInterceptor handles panic to a custom error
func RecoveryInterceptor() grpc_recovery.Option {
return grpc_recovery.WithRecoveryHandler(func(p interface{}) (err error) {
Expand Down Expand Up @@ -342,22 +320,6 @@ func (svr *gRPCHandler) SendAction(ctx context.Context, in *iotexapi.SendActionR
// tags output
span.SetAttributes(attribute.String("actType", fmt.Sprintf("%T", in.GetAction().GetCore())))
defer span.End()
chainID := strconv.FormatUint(uint64(in.GetAction().GetCore().GetChainID()), 10)
if in.GetAction().GetCore().GetChainID() > 0 {
_apiCallSourceWithChainIDMtc.WithLabelValues(chainID).Inc()
} else {
selp, err := (&action.Deserializer{}).SetEvmNetworkID(svr.coreService.EVMNetworkID()).ActionToSealedEnvelope(in.GetAction())
if err != nil {
return nil, err
}
var clientIP string
if p, ok := peer.FromContext(ctx); ok {
clientIP, _, _ = net.SplitHostPort(p.Addr.String())
} else {
clientIP = "unknownIP"
}
_apiCallSourceWithOutChainIDMtc.WithLabelValues(clientIP, selp.SenderAddress().String()).Inc()
}
actHash, err := svr.coreService.SendAction(ctx, in.GetAction())
if err != nil {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion api/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func TestGrpcServer_SendAction(t *testing.T) {
grpcSvr := newGRPCHandler(core)

for _, test := range _sendActionTests {
core.EXPECT().EVMNetworkID().Return(uint32(1))
core.EXPECT().SendAction(context.Background(), test.actionPb).Return(test.actionHash, nil)
request := &iotexapi.SendActionRequest{Action: test.actionPb}
res, err := grpcSvr.SendAction(context.Background(), request)
Expand All @@ -104,7 +103,20 @@ func TestGrpcServer_GetReceiptByAction(t *testing.T) {
}

func TestGrpcServer_GetServerMeta(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
core := mock_apicoreservice.NewMockCoreService(ctrl)
grpcSvr := newGRPCHandler(core)

core.EXPECT().ServerMeta().Return("packageVersion", "packageCommitID", "gitStatus", "goVersion", "buildTime")
res, err := grpcSvr.GetServerMeta(context.Background(), &iotexapi.GetServerMetaRequest{})
require.NoError(err)
require.Equal("packageVersion", res.ServerMeta.PackageVersion)
require.Equal("packageCommitID", res.ServerMeta.PackageCommitID)
require.Equal("gitStatus", res.ServerMeta.GitStatus)
require.Equal("goVersion", res.ServerMeta.GoVersion)
require.Equal("buildTime", res.ServerMeta.BuildTime)
}

func TestGrpcServer_ReadContract(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ package chainservice

import (
"context"
"strconv"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-election/committee"
"github.com/iotexproject/iotex-proto/golang/iotexrpc"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand All @@ -36,6 +39,28 @@ import (
"github.com/iotexproject/iotex-core/state/factory"
)

var (
_apiCallWithChainIDMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_apicall_chainid_metrics",
Help: "API call ChainID Statistics",
},
[]string{"chain_id"},
)
_apiCallWithOutChainIDMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_apicall_nochainid_metrics",
Help: "API call Without ChainID Statistics",
},
[]string{"sender", "recipient"},
)
)

func init() {
prometheus.MustRegister(_apiCallWithChainIDMtc)
prometheus.MustRegister(_apiCallWithOutChainIDMtc)
}

// ChainService is a blockchain service with all blockchain components.
type ChainService struct {
lifecycle lifecycle.Lifecycle
Expand Down Expand Up @@ -80,9 +105,34 @@ func (cs *ChainService) HandleAction(ctx context.Context, actPb *iotextypes.Acti
if err != nil {
log.L().Debug(err.Error())
}
chainIDmetrics(act)
return err
}

func chainIDmetrics(act action.SealedEnvelope) {
chainID := strconv.FormatUint(uint64(act.ChainID()), 10)
if act.ChainID() > 0 {
_apiCallWithChainIDMtc.WithLabelValues(chainID).Inc()
} else {
recipient, _ := act.Destination()
//it will be empty for staking action, change string to staking in such case
if recipient == "" {
act, ok := act.Action().(action.EthCompatibleAction)
if ok {
if ethTx, err := act.ToEthTx(); err == nil && ethTx.To() != nil {
if add, err := address.FromHex(ethTx.To().Hex()); err == nil {
recipient = add.String()
}
}
}
if recipient == "" {
recipient = "staking"
}
}
_apiCallWithOutChainIDMtc.WithLabelValues(act.SenderAddress().String(), recipient).Inc()
}
}

// HandleBlock handles incoming block request.
func (cs *ChainService) HandleBlock(ctx context.Context, peer string, pbBlock *iotextypes.Block) error {
blk, err := block.NewDeserializer(cs.chain.EvmNetworkID()).FromBlockProto(pbBlock)
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func NewConsensus(
commitBlockCB := func(blk *block.Block) error {
err := bc.CommitBlock(blk)
if err != nil {
log.Logger("consensus").Info("Failed to commit the block.", zap.Error(err), zap.Uint64("height", blk.Height()))
log.Logger("consensus").Error("Failed to commit the block.", zap.Error(err), zap.Uint64("height", blk.Height()))
}
return err
}
Expand Down
1 change: 1 addition & 0 deletions consensus/scheme/rolldpos/rolldposctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ func (ctx *rollDPoSCtx) Commit(msg interface{}) (bool, error) {
case nil:
break
default:
log.L().Error("error when committing the block", zap.Error(err))
return false, errors.Wrap(err, "error when committing a block")
}
// Broadcast the committed block to the network
Expand Down
3 changes: 2 additions & 1 deletion e2etest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"math/big"
"os"
"path/filepath"

"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
Expand Down Expand Up @@ -194,7 +195,7 @@ func addTestingTsfBlocks(bc blockchain.Blockchain, ap actpool.ActPool) error {
}

func copyDB(srcDB, dstDB string) error {
input, err := os.ReadFile(srcDB)
input, err := os.ReadFile(filepath.Clean(srcDB))
if err != nil {
return errors.Wrap(err, "failed to read source db file")
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -257,7 +258,7 @@ func (c *client) NewKeyStore() *keystore.KeyStore {
}

func (c *client) DecryptPrivateKey(passwordOfKeyStore, keyStorePath string) (*ecdsa.PrivateKey, error) {
keyJSON, err := os.ReadFile(keyStorePath)
keyJSON, err := os.ReadFile(filepath.Clean(keyStorePath))
if err != nil {
return nil, fmt.Errorf("keystore file \"%s\" read error", keyStorePath)
}
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func newAccountByKey(alias string, privateKey string, walletDir string) (string,
}

func newAccountByKeyStore(alias, passwordOfKeyStore, keyStorePath string, walletDir string) (string, error) {
keyJSON, err := os.ReadFile(keyStorePath)
keyJSON, err := os.ReadFile(filepath.Clean(keyStorePath))
if err != nil {
return "", output.NewError(output.ReadFileError,
fmt.Sprintf("keystore file \"%s\" read error", keyStorePath), nil)
Expand Down
12 changes: 7 additions & 5 deletions ioctl/cmd/bc/bcbucketlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,26 @@ func (m *bucketlistMessage) String() string {

// getBucketList get bucket list from chain
func getBucketList(method, addr string, args ...string) (err error) {
offset, limit := uint64(0), uint64(1000)
offset, limit := uint32(0), uint32(1000)
if len(args) > 0 {
offset, err = strconv.ParseUint(args[0], 10, 64)
val, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
return output.NewError(output.ValidationError, "invalid offset", err)
}
offset = uint32(val)
}
if len(args) > 1 {
limit, err = strconv.ParseUint(args[1], 10, 64)
val, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return output.NewError(output.ValidationError, "invalid limit", err)
}
limit = uint32(val)
}
switch method {
case _bucketlistMethodByVoter:
return getBucketListByVoter(addr, uint32(offset), uint32(limit))
return getBucketListByVoter(addr, offset, limit)
case _bucketlistMethodByCandidate:
return getBucketListByCand(addr, uint32(offset), uint32(limit))
return getBucketListByCand(addr, offset, limit)
}
return output.NewError(output.InputError, "unknown <method>", nil)
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/cmd/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"fmt"
"os"
"path/filepath"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/compiler"
Expand Down Expand Up @@ -101,7 +102,7 @@ func checkCompilerVersion(solc *compiler.Solidity) bool {
}

func readAbiFile(abiFile string) (*abi.ABI, error) {
abiBytes, err := os.ReadFile(abiFile)
abiBytes, err := os.ReadFile(filepath.Clean(abiFile))
if err != nil {
return nil, output.NewError(output.ReadFileError, "failed to read abi file", err)
}
Expand Down
Loading

0 comments on commit c571d60

Please sign in to comment.