Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] add action deserializer #3190

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions action/action_deserializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package action

import "github.com/iotexproject/iotex-proto/golang/iotextypes"

// Deserializer de-serializes an action
type Deserializer struct {
}

// ActionToSealedEnvelope converts protobuf to SealedEnvelope
func (ad *Deserializer) ActionToSealedEnvelope(pbAct *iotextypes.Action) (SealedEnvelope, error) {
Copy link
Member Author

@dustinxie dustinxie Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to block deserializer, the purpose of this PR is to contain the pending changes
PR 3188 will change in here

var selp SealedEnvelope
err := selp.LoadProto(pbAct)
return selp, err
}
31 changes: 31 additions & 0 deletions action/action_deserializer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package action

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/require"
)

func TestActionDeserializer(t *testing.T) {
r := require.New(t)
se, err := createSealedEnvelope()
r.NoError(err)
rHash, err := se.Hash()
r.NoError(err)
r.Equal("322884fb04663019be6fb461d9453827487eafdd57b4de3bd89a7d77c9bf8395", hex.EncodeToString(rHash[:]))
r.Equal(publicKey, se.SrcPubkey().HexString())
r.Equal(signByte, se.Signature())
r.Zero(se.Encoding())

se.signature = validSig
se1, err := (&Deserializer{}).ActionToSealedEnvelope(se.Proto())
r.NoError(err)
r.Equal(se, se1)
}
5 changes: 5 additions & 0 deletions action/sealedenvelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestSealedEnvelope_Basic(t *testing.T) {
req.Equal(signByte, se.Signature())
req.Zero(se.Encoding())

var se1 SealedEnvelope
se.signature = validSig
req.NoError(se1.LoadProto(se.Proto()))
req.Equal(se, se1)
}

func TestSealedEnvelope_InvalidType(t *testing.T) {
Expand Down Expand Up @@ -168,6 +172,7 @@ func createSealedEnvelope() (SealedEnvelope, error) {
Build()

cPubKey, err := crypto.HexStringToPublicKey(publicKey)
tsf.srcPubkey = cPubKey
se := SealedEnvelope{}
se.Envelope = evlp
se.srcPubkey = cPubKey
Expand Down
4 changes: 2 additions & 2 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ func (core *coreService) ServerMeta() (packageVersion string, packageCommitID st
// SendAction is the API to send an action to blockchain.
func (core *coreService) SendAction(ctx context.Context, in *iotextypes.Action) (string, error) {
log.Logger("api").Debug("receive send action request")
var selp action.SealedEnvelope
if err := selp.LoadProto(in); err != nil {
selp, err := (&action.Deserializer{}).ActionToSealedEnvelope(in)
if err != nil {
return "", status.Error(codes.InvalidArgument, err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,12 @@ func (cs *ChainService) ReportFullness(_ context.Context, _ iotexrpc.MessageType

// HandleAction handles incoming action request.
func (cs *ChainService) HandleAction(ctx context.Context, actPb *iotextypes.Action) error {
var act action.SealedEnvelope
if err := act.LoadProto(actPb); err != nil {
act, err := (&action.Deserializer{}).ActionToSealedEnvelope(actPb)
if err != nil {
return err
}
ctx = protocol.WithRegistry(ctx, cs.registry)
err := cs.actpool.Add(ctx, act)
err = cs.actpool.Add(ctx, act)
if err != nil {
log.L().Debug(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions gasstation/gasstattion.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (gs *GasStation) SuggestGasPrice() (uint64, error) {

// EstimateGasForAction estimate gas for action
func (gs *GasStation) EstimateGasForAction(actPb *iotextypes.Action) (uint64, error) {
var selp action.SealedEnvelope
if err := selp.LoadProto(actPb); err != nil {
selp, err := (&action.Deserializer{}).ActionToSealedEnvelope(actPb)
if err != nil {
return 0, err
}
// Special handling for executions
Expand Down