Skip to content

Commit

Permalink
[action] add action deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Mar 14, 2022
1 parent 8209ad3 commit 2d323c4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
19 changes: 19 additions & 0 deletions action/action_deserializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 {
}

func (ad *Deserializer) ActionToSealedEnvelope(pbAct *iotextypes.Action) (SealedEnvelope, error) {
var selp SealedEnvelope
err := selp.LoadProto(pbAct)
return selp, err
}
26 changes: 26 additions & 0 deletions action/action_deserializer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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())
}
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 2d323c4

Please sign in to comment.