Skip to content

Commit

Permalink
Merge branch 'task/config-get' of github.com:pocockn/iotex-core into …
Browse files Browse the repository at this point in the history
…task/config-get

* 'task/config-get' of github.com:pocockn/iotex-core:
  modify test
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  • Loading branch information
pocockn committed Jul 19, 2022
2 parents d678d3d + ad7588e commit bff8358
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 70 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
70 changes: 54 additions & 16 deletions ioctl/cmd/contract/contractshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func isExist(path string) bool {
}

func rename(oldPath string, newPath string, c chan bool) {
if isExist(_givenPath + "/" + oldPath) {
if isExist(oldPath) {
if err := os.Rename(oldPath, newPath); err != nil {
log.Println("Rename file failed: ", err)
}
Expand All @@ -127,7 +127,7 @@ func rename(oldPath string, newPath string, c chan bool) {
}

func share(args []string) error {
_givenPath = args[0]
_givenPath = filepath.Clean(args[0])
if len(_givenPath) == 0 {
return output.NewError(output.ReadFileError, "failed to get directory", nil)
}
Expand All @@ -154,7 +154,7 @@ func share(args []string) error {
return nil
})

log.Println("Listening on 127.0.0.1:65520, Please open your IDE ( " + _iotexIDE + " ) to connect to local files")
log.Printf("Listening on 127.0.0.1:65520, Please open your IDE ( %s ) to connect to local files", _iotexIDE)

http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
conn, err := _upgrade.Upgrade(writer, request, nil)
Expand Down Expand Up @@ -186,7 +186,7 @@ func share(args []string) error {
case "list":
payload := make(map[string]bool)
for _, ele := range _fileList {
payload[ele] = isReadOnly(_givenPath + "/" + ele)
payload[ele] = isReadOnly(filepath.Join(_givenPath, ele))
}
response.Payload = payload
if err := conn.WriteJSON(&response); err != nil {
Expand All @@ -197,57 +197,95 @@ func share(args []string) error {

t := request.Payload
getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{})
getPayloadPath := getPayload["path"].(string)
upload, err := os.ReadFile(filepath.Clean(_givenPath + "/" + getPayloadPath))
getPayloadPath, err := cleanPath(getPayload["path"].(string))
if err != nil {
log.Println("clean file path failed: ", err)
break
}
getPayloadPath = filepath.Join(_givenPath, getPayloadPath)
upload, err := os.ReadFile(getPayloadPath)
if err != nil {
log.Println("read file failed: ", err)
break
}
payload["content"] = string(upload)
payload["readonly"] = isReadOnly(_givenPath + "/" + getPayloadPath)
payload["readonly"] = isReadOnly(getPayloadPath)
response.Payload = payload
if err := conn.WriteJSON(&response); err != nil {
log.Println("send get response: ", err)
break
}
log.Println("share: " + _givenPath + "/" + getPayloadPath)
log.Printf("share: %s\n", easpcapeString(getPayloadPath))

case "rename":
c := make(chan bool)
t := request.Payload
renamePayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{})
oldPath := renamePayload["oldPath"].(string)
newPath := renamePayload["newPath"].(string)
go rename(oldPath, newPath, c)
oldRenamePath, err := cleanPath(renamePayload["oldPath"].(string))
if err != nil {
log.Println("clean file path failed: ", err)
break
}
newRenamePath, err := cleanPath(renamePayload["newPath"].(string))
if err != nil {
log.Println("clean file path failed: ", err)
break
}
oldRenamePath = filepath.Join(_givenPath, oldRenamePath)
newRenamePath = filepath.Join(_givenPath, newRenamePath)
go rename(oldRenamePath, newRenamePath, c)
response.Payload = <-c
if err := conn.WriteJSON(&response); err != nil {
log.Println("send get response: ", err)
break
}
log.Println("rename: " + _givenPath + "/" + oldPath + " to " + _givenPath + "/" + newPath)
log.Printf("rename: %s to %s\n", easpcapeString(oldRenamePath), easpcapeString(newRenamePath))

case "set":
t := request.Payload
setPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{})
setPath := setPayload["path"].(string)
content := setPayload["content"].(string)
err := os.WriteFile(_givenPath+"/"+setPath, []byte(content), 0777)
setPath, err := cleanPath(setPayload["path"].(string))
if err != nil {
log.Println("clean file path failed: ", err)
break
}
setPath = filepath.Join(_givenPath, setPath)
if err := os.MkdirAll(filepath.Dir(setPath), 0755); err != nil {
log.Println("mkdir failed: ", err)
break
}
if err := os.WriteFile(setPath, []byte(content), 0644); err != nil {
log.Println("set file failed: ", err)
break
}
if err := conn.WriteJSON(&response); err != nil {
log.Println("send set response: ", err)
break
}
log.Println("set: " + _givenPath + "/" + setPath)
log.Printf("set: %s\n", easpcapeString(setPath))

default:
log.Println("Don't support this IDE yet. Can not handle websocket method: " + request.Key)
log.Printf("Don't support this IDE yet. Can not handle websocket method: %s\n", easpcapeString(request.Key))

}
}
})
log.Fatal(http.ListenAndServe(*_addr, nil))

return nil
}

func easpcapeString(str string) string {
escaped := strings.Replace(str, "\n", "", -1)
return strings.Replace(escaped, "\r", "", -1)
}

func cleanPath(path string) (string, error) {
path = filepath.Clean(filepath.Join("/", path))
real, err := filepath.Rel("/", path)
if err != nil {
return "", err
}
return real, nil
}
Loading

0 comments on commit bff8358

Please sign in to comment.