state_transition_pmh_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package core
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/state"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/private/engine"
  9. testifyassert "github.com/stretchr/testify/assert"
  10. )
  11. type stubPmhStateTransition struct {
  12. snapshot int
  13. }
  14. func (s *stubPmhStateTransition) SetTxPrivacyMetadata(pm *types.PrivacyMetadata) {
  15. }
  16. func (s *stubPmhStateTransition) IsPrivacyEnhancementsEnabled() bool {
  17. return true
  18. }
  19. func (s *stubPmhStateTransition) RevertToSnapshot(val int) {
  20. s.snapshot = val
  21. }
  22. func (s *stubPmhStateTransition) GetStatePrivacyMetadata(addr common.Address) (*state.PrivacyMetadata, error) {
  23. return &state.PrivacyMetadata{PrivacyFlag: engine.PrivacyFlagStateValidation, CreationTxHash: common.EncryptedPayloadHash{1}}, nil
  24. }
  25. func (s *stubPmhStateTransition) CalculateMerkleRoot() (common.Hash, error) {
  26. return common.Hash{}, fmt.Errorf("Unable to calculate MerkleRoot")
  27. }
  28. func (s *stubPmhStateTransition) AffectedContracts() []common.Address {
  29. return make([]common.Address, 0)
  30. }
  31. func TestPrivateMessageContextVerify_WithMerkleRootCreationError(t *testing.T) {
  32. assert := testifyassert.New(t)
  33. stateTransitionAPI := &stubPmhStateTransition{}
  34. pmc := newPMH(stateTransitionAPI)
  35. pmc.receivedPrivacyMetadata = &engine.ExtraMetadata{ACMerkleRoot: common.Hash{1}, PrivacyFlag: engine.PrivacyFlagStateValidation}
  36. pmc.snapshot = 10
  37. exitEarly, err := pmc.verify(nil)
  38. assert.Error(err, "verify must return an error due to the MerkleRoot calculation error")
  39. assert.Equal(pmc.snapshot, stateTransitionAPI.snapshot, "Revert should have been called")
  40. assert.True(exitEarly, "Exit early should be true")
  41. }