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 address and hash field in SealedEnvelope #3420

Merged
merged 10 commits into from
Jun 9, 2022
Merged

Conversation

saitofun
Copy link
Contributor

@saitofun saitofun commented May 30, 2022

Description

- perf: [action] improve SealedEnvelope accessing `hash`, `hashErr` and `srcAddress`
- test: [action] improve TestSealedEnvelope_Basic TestSealedEnvelope_Proto and TestActionDeserializer
- feat: [action] add `ErrInvalidSigLen` and `ErrUnknownEncType` error func for maintain test and error better

Fixes #2989

Type of change

Please delete options that are not relevant.

  • Code refactor or improvement
  • New feature (non-breaking change which adds functionality)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@saitofun saitofun requested a review from a team as a code owner May 30, 2022 04:59
@CLAassistant
Copy link

CLAassistant commented May 30, 2022

CLA assistant check
All committers have signed the CLA.

hv, err := sealed.calcHash()
sealed.hash = &Hash256Result{hv, err}
}
return sealed.hash.Value()
Copy link
Member

@dustinxie dustinxie May 30, 2022

Choose a reason for hiding this comment

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

return sealed.hash.Hash256, sealed.hash.error would suffice

error
}

func (h *Hash256Result) Value() (hash.Hash256, error) {
Copy link
Member

Choose a reason for hiding this comment

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

no need for this func, see comments below

hash *Hash256Result
}

type Hash256Result struct {
Copy link
Member

Choose a reason for hiding this comment

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

hash256Result, this is internal data struct

Copy link
Member

Choose a reason for hiding this comment

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

Remove this struct

@@ -165,6 +165,7 @@ func TestSealedEnvelope_Proto(t *testing.T) {
{1, "d5dc789026c12cc69f1ea7997fbe0aa1bcc02e85176848c7b2ecf4da6b4560d0"},
} {
se.encoding = v.enc
req.NoError(se.LoadProto(se.Proto()))
Copy link
Member

Choose a reason for hiding this comment

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

this should not be needed?
se.Hash() below will trigger the if hash == nil code path and test its correctness

@@ -35,7 +35,7 @@ func TestSealedEnvelope_Basic(t *testing.T) {
{0, "322884fb04663019be6fb461d9453827487eafdd57b4de3bd89a7d77c9bf8395"},
{1, "80af7840d73772d3022d8bdc46278fb755352e5e9d5f2a1f12ee7ec4f1ea98e9"},
} {
se, err := createSealedEnvelope(v.id)
se, err := createSealedEnvelopeWithSignature(v.id, _signByte)
Copy link
Member

Choose a reason for hiding this comment

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

should not need this? see comments below

se.signature = _validSig
if err = se.LoadProto(se.Proto()); err != nil {
return se, err
}
Copy link
Member

Choose a reason for hiding this comment

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

we don't need this? see comments below

return se, err
}

func createSealedEnvelopeWithSignature(chainID uint32, sig []byte) (SealedEnvelope, error) {
Copy link
Member

Choose a reason for hiding this comment

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

we don't need to add this func? if there's a need (for test) about signature, just directly set the signature (as done in tests now)

@@ -69,6 +93,13 @@ func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) {
// SrcPubkey returns the source public key
func (sealed *SealedEnvelope) SrcPubkey() crypto.PublicKey { return sealed.srcPubkey }

func (sealed *SealedEnvelope) Address() address.Address {
Copy link
Member

@dustinxie dustinxie May 30, 2022

Choose a reason for hiding this comment

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

need a comment like // Address() xxx to satisfy go lint
you run make lint local to make sure no lint error

Copy link
Collaborator

Choose a reason for hiding this comment

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

SenderAddress

Copy link
Member

Choose a reason for hiding this comment

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

change to SenderAddress, it's a better/clear name

@@ -69,6 +93,13 @@ func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) {
// SrcPubkey returns the source public key
func (sealed *SealedEnvelope) SrcPubkey() crypto.PublicKey { return sealed.srcPubkey }

func (sealed *SealedEnvelope) Address() address.Address {
Copy link
Collaborator

Choose a reason for hiding this comment

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

SenderAddress

Comment on lines 187 to 189
hashValue, err := sealed.calcHash()
sealed.hash = &Hash256Result{hashValue, err}
sealed.address = srcPub.Address()
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

_ = se.Address()
// after Hash() and Address() was invoked, the two value are equal
NewWithT(t).Expect(validated).To(Equal(se))
})
Copy link
Member

Choose a reason for hiding this comment

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

revert these, we don't want to introduce a new testing style, pls only do minimum necessary changes to make existing test pass

Copy link
Member

@dustinxie dustinxie Jun 3, 2022

Choose a reason for hiding this comment

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

as we discussed, please revert unnecessary changes, only need these 3 lines to make the test pass

_, err = se.Hash()
r.NoError(err)
se.SenderAddress()
// after Hash() and Address() was invoked, the two value are equal

action/const.go Outdated

// vars
var (
ErrAddress = errors.New("invalid address")
ErrAddress = errors.New("invalid srcAddress")
Copy link
Member

Choose a reason for hiding this comment

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

revert, not necessary

action/const.go Outdated
return errors.Errorf("unknown encoding type %d", enc)
}
)

Copy link
Member

Choose a reason for hiding this comment

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

not very necessary, seems an overkill

Copy link
Member

Choose a reason for hiding this comment

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

as we discussed, we don't really need this

}

// calcHash returns the hash value of SealedEnvelope
func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error) {
Copy link
Member

Choose a reason for hiding this comment

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

keep this, it is clear to have an internal func (which might be used)

return nil
sealed.encoding = pbAct.GetEncoding()
_, err = sealed.Hash()
return err
Copy link
Member

Choose a reason for hiding this comment

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

revert, like we discussed, there's no need to change loadProto at all

tsf2, ok2 := validated.Envelope.Action().(*Transfer)
NewWithT(t).Expect(tsf1).To(Equal(tsf2))
NewWithT(t).Expect(ok1 && ok2).To(BeTrue())
})
Copy link
Member

Choose a reason for hiding this comment

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

no need to change these test files

hv, err := sealed.calcHash()
sealed.hash = &Hash256Result{hv, err}
if sealed.hash != hash.ZeroHash256 || sealed.hashErr != nil {
return sealed.hash, sealed.hashErr
Copy link
Member

@dustinxie dustinxie Jun 2, 2022

Choose a reason for hiding this comment

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

just like the 1st commit

if sealed.hash == hash.ZeroHash256 && sealed.hashErr == nil {
    sealed.hash, sealed.hashErr = sealed.calcHash()
}
return sealed.hash, sealed.hashErr

@saitofun saitofun requested a review from dustinxie June 3, 2022 07:10
}
return rlpSignedHash(tx, sealed.evmNetworkID, sealed.Signature())
sealed.evmNetworkID = config.EVMNetworkID()
Copy link
Member

Choose a reason for hiding this comment

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

why do this? the correct place to set evmNetworkID is in LoadProto()

}
return sealed.hash, sealed.hashErr
Copy link
Member

Choose a reason for hiding this comment

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

as we discussed, please revert unnecessary changes, and make this an internal func calcHash()

se.encoding = v.encoding
se.signature = v.sig
req.Equal(se2.LoadProto(se.Proto()).Error(), v.err.Error())
})
Copy link
Member

Choose a reason for hiding this comment

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

as we discussed, please revert unnecessary changes, do minimum changes to make the test pass

@saitofun saitofun requested a review from dustinxie June 3, 2022 17:17
return sealed.hash, sealed.hashErr
}

func (sealed *SealedEnvelope) calcHash() {
Copy link
Member

Choose a reason for hiding this comment

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

one final comment:

func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error)

then don't need to change this func at all, in the above, just

sealed.hash, sealed.hashErr = calcHash()

@saitofun saitofun requested a review from dustinxie June 3, 2022 18:04
@saitofun saitofun force-pushed the issue2989 branch 2 times, most recently from b10ff27 to c41afb2 Compare June 3, 2022 20:39
@saitofun saitofun force-pushed the issue2989 branch 2 times, most recently from cd34ff4 to c7a4bfe Compare June 3, 2022 20:45
@codecov
Copy link

codecov bot commented Jun 3, 2022

Codecov Report

Merging #3420 (9e4cec1) into master (7d66698) will increase coverage by 0.00%.
The diff coverage is 92.30%.

@@           Coverage Diff           @@
##           master    #3420   +/-   ##
=======================================
  Coverage   75.16%   75.16%           
=======================================
  Files         236      236           
  Lines       22070    22083   +13     
=======================================
+ Hits        16589    16599   +10     
- Misses       4591     4595    +4     
+ Partials      890      889    -1     
Impacted Files Coverage Δ
action/sealedenvelope.go 86.31% <92.30%> (+0.94%) ⬆️
consensus/scheme/rolldpos/rolldposctx.go 73.59% <0.00%> (-0.49%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7d66698...9e4cec1. Read the comment docs.

action/const.go Outdated
@@ -6,7 +6,9 @@

package action

import "github.com/pkg/errors"
import (
Copy link
Member

Choose a reason for hiding this comment

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

revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted

@@ -5,12 +5,13 @@ import (

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
Copy link
Member

Choose a reason for hiding this comment

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

revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted

@@ -22,6 +23,9 @@ type SealedEnvelope struct {
evmNetworkID uint32
srcPubkey crypto.PublicKey
signature []byte
srcAddress address.Address
hash hash.Hash256
hashErr error
Copy link
Member

Choose a reason for hiding this comment

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

remove

@saitofun saitofun requested a review from Liuhaai June 6, 2022 00:39
se.signature = _validSig
se.hash = hash.ZeroHash256
se.Hash()
Copy link
Collaborator

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?

se1, err := (&Deserializer{}).ActionToSealedEnvelope(se.Proto())
se1.Hash()
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

_, err = validated[i].Hash()
require.NoError(err)
}
require.Equal(validated, acts)
Copy link
Member

Choose a reason for hiding this comment

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

asserting hash

@saitofun saitofun merged commit a222431 into master Jun 9, 2022
@saitofun saitofun deleted the issue2989 branch June 9, 2022 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add address and hash field in SealedEnvelope struct
5 participants