Skip to content

Commit

Permalink
[action] add action deserializer (#3190)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Mar 15, 2022
1 parent dbac57f commit 50a49a5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
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) {
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

0 comments on commit 50a49a5

Please sign in to comment.