From 50a49a5f7580b6ec649d2f0e403a76b93f2f2bdb Mon Sep 17 00:00:00 2001 From: dustinxie Date: Tue, 15 Mar 2022 11:09:29 -0700 Subject: [PATCH] [action] add action deserializer (#3190) --- action/action_deserializer.go | 20 +++++++++++++++++++ action/action_deserializer_test.go | 31 ++++++++++++++++++++++++++++++ action/sealedenvelope_test.go | 5 +++++ api/coreservice.go | 4 ++-- chainservice/chainservice.go | 6 +++--- gasstation/gasstattion.go | 4 ++-- 6 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 action/action_deserializer.go create mode 100644 action/action_deserializer_test.go diff --git a/action/action_deserializer.go b/action/action_deserializer.go new file mode 100644 index 0000000000..6dc1b8ebb8 --- /dev/null +++ b/action/action_deserializer.go @@ -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 +} diff --git a/action/action_deserializer_test.go b/action/action_deserializer_test.go new file mode 100644 index 0000000000..b8059baa49 --- /dev/null +++ b/action/action_deserializer_test.go @@ -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) +} diff --git a/action/sealedenvelope_test.go b/action/sealedenvelope_test.go index b9e9a8c42d..72480c37e7 100644 --- a/action/sealedenvelope_test.go +++ b/action/sealedenvelope_test.go @@ -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) { @@ -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 diff --git a/api/coreservice.go b/api/coreservice.go index e65b5ef4a3..19220a5301 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -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()) } diff --git a/chainservice/chainservice.go b/chainservice/chainservice.go index 428823f63f..9bfc00e12b 100644 --- a/chainservice/chainservice.go +++ b/chainservice/chainservice.go @@ -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()) } diff --git a/gasstation/gasstattion.go b/gasstation/gasstattion.go index ba9f6c6733..c7bea2b7ac 100644 --- a/gasstation/gasstattion.go +++ b/gasstation/gasstattion.go @@ -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