-
Notifications
You must be signed in to change notification settings - Fork 324
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 address
and hash
field in SealedEnvelope
#3420
Changes from 5 commits
b9bf8a4
4dde2a1
880dd09
86f2613
2ef4ce5
92b27ef
bab5af5
8ac5045
ab0de5d
9e4cec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/iotexproject/go-pkgs/hash" | ||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
|
@@ -34,8 +35,12 @@ func TestActionDeserializer(t *testing.T) { | |
r.Equal(_signByte, se.Signature()) | ||
r.Zero(se.Encoding()) | ||
|
||
// use valid signature and reset se.Hash | ||
se.signature = _validSig | ||
se.hash = hash.ZeroHash256 | ||
se.Hash() | ||
se1, err := (&Deserializer{}).ActionToSealedEnvelope(se.Proto()) | ||
se1.Hash() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
r.NoError(err) | ||
r.Equal(se, se1) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
|
||
"github.com/iotexproject/go-pkgs/crypto" | ||
"github.com/iotexproject/go-pkgs/hash" | ||
"github.com/iotexproject/iotex-address/address" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverted |
||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
|
@@ -22,6 +23,8 @@ type SealedEnvelope struct { | |
evmNetworkID uint32 | ||
srcPubkey crypto.PublicKey | ||
signature []byte | ||
srcAddress address.Address | ||
hash hash.Hash256 | ||
} | ||
|
||
// envelopeHash returns the raw hash of embedded Envelope (this is the hash to be signed) | ||
|
@@ -48,6 +51,17 @@ func (sealed *SealedEnvelope) envelopeHash() (hash.Hash256, error) { | |
// Hash returns the hash value of SealedEnvelope. | ||
// an all-0 return value means the transaction is invalid | ||
func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) { | ||
if sealed.hash == hash.ZeroHash256 { | ||
hashVal, hashErr := sealed.calcHash() | ||
if hashErr == nil { | ||
sealed.hash = hashVal | ||
} | ||
return sealed.hash, hashErr | ||
} | ||
return sealed.hash, nil | ||
} | ||
|
||
func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error) { | ||
switch sealed.encoding { | ||
case iotextypes.Encoding_ETHEREUM_RLP: | ||
act, ok := sealed.Action().(EthCompatibleAction) | ||
|
@@ -69,6 +83,14 @@ func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) { | |
// SrcPubkey returns the source public key | ||
func (sealed *SealedEnvelope) SrcPubkey() crypto.PublicKey { return sealed.srcPubkey } | ||
|
||
// SenderAddress returns address of the source public key | ||
func (sealed *SealedEnvelope) SenderAddress() address.Address { | ||
if sealed.srcAddress == nil { | ||
sealed.srcAddress = sealed.srcPubkey.Address() | ||
} | ||
return sealed.srcAddress | ||
} | ||
|
||
// Signature returns signature bytes | ||
func (sealed *SealedEnvelope) Signature() []byte { | ||
sig := make([]byte, len(sealed.signature)) | ||
|
@@ -141,6 +163,8 @@ func (sealed *SealedEnvelope) LoadProto(pbAct *iotextypes.Action) error { | |
sealed.signature = make([]byte, sigSize) | ||
copy(sealed.signature, pbAct.GetSignature()) | ||
sealed.encoding = encoding | ||
sealed.hash = hash.ZeroHash256 | ||
sealed.srcAddress = nil | ||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -902,7 +902,12 @@ func TestActPool_GetUnconfirmedActs(t *testing.T) { | |
require.Equal([]action.SealedEnvelope(nil), acts) | ||
|
||
acts = ap.GetUnconfirmedActs(_addr1) | ||
require.Equal([]action.SealedEnvelope{tsf1, tsf3, tsf4, tsf5}, acts) | ||
validated := []action.SealedEnvelope{tsf1, tsf3, tsf4, tsf5} | ||
for i := range validated { | ||
_, err = validated[i].Hash() | ||
require.NoError(err) | ||
} | ||
require.Equal(validated, acts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. asserting hash |
||
} | ||
|
||
func TestActPool_GetActionByHash(t *testing.T) { | ||
|
@@ -1089,7 +1094,12 @@ func TestActPool_SpeedUpAction(t *testing.T) { | |
appliedActionList = append(appliedActionList, bestAction) | ||
} | ||
// tsf1 is replaced by tsf3 with higher gas price | ||
require.Equal(appliedActionList, []action.SealedEnvelope{tsf3, tsf2}) | ||
validated := []action.SealedEnvelope{tsf3, tsf2} | ||
for i := range validated { | ||
_, err = validated[i].Hash() | ||
require.NoError(err) | ||
} | ||
require.Equal(appliedActionList, validated) | ||
} | ||
|
||
// Helper function to return the correct pending nonce just in case of empty queue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any validation for the return values?