account_extra_data.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package state
  17. import (
  18. "fmt"
  19. "io"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/private/engine"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. // Quorum
  25. // AccountExtraData is to contain extra data that supplements existing Account data.
  26. // It is also maintained in a trie to support rollback.
  27. // Note:
  28. // - update copy() method
  29. // - update DecodeRLP and EncodeRLP when adding new field
  30. type AccountExtraData struct {
  31. // for privacy enhancements
  32. PrivacyMetadata *PrivacyMetadata
  33. // list of public keys managed by the corresponding Tessera.
  34. // This is for multitenancy
  35. ManagedParties []string
  36. }
  37. func (qmd *AccountExtraData) DecodeRLP(stream *rlp.Stream) error {
  38. var dataRLP struct {
  39. // from state.PrivacyMetadata, this is required to support
  40. // backward compatibility with RLP-encoded state.PrivacyMetadata.
  41. // Refer to rlp/doc.go for decoding rules.
  42. CreationTxHash *common.EncryptedPayloadHash `rlp:"nil"`
  43. // from state.PrivacyMetadata, this is required to support
  44. // backward compatibility with RLP-encoded state.PrivacyMetadata.
  45. // Refer to rlp/doc.go for decoding rules.
  46. PrivacyFlag *engine.PrivacyFlagType `rlp:"nil"`
  47. Rest []rlp.RawValue `rlp:"tail"` // to maintain forward compatibility
  48. }
  49. if err := stream.Decode(&dataRLP); err != nil {
  50. return err
  51. }
  52. if dataRLP.CreationTxHash != nil && dataRLP.PrivacyFlag != nil {
  53. qmd.PrivacyMetadata = &PrivacyMetadata{
  54. CreationTxHash: *dataRLP.CreationTxHash,
  55. PrivacyFlag: *dataRLP.PrivacyFlag,
  56. }
  57. }
  58. if len(dataRLP.Rest) > 0 {
  59. var managedParties []string
  60. if err := rlp.DecodeBytes(dataRLP.Rest[0], &managedParties); err != nil {
  61. return fmt.Errorf("fail to decode managedParties with error %v", err)
  62. }
  63. // As RLP encodes empty slice or nil slice as an empty string (192)
  64. // we won't be able to determine when decoding. So we use pragmatic approach
  65. // to default to nil value. Downstream usage would deal with it easier.
  66. if len(managedParties) == 0 {
  67. qmd.ManagedParties = nil
  68. } else {
  69. qmd.ManagedParties = managedParties
  70. }
  71. }
  72. return nil
  73. }
  74. func (qmd *AccountExtraData) EncodeRLP(writer io.Writer) error {
  75. var (
  76. hash *common.EncryptedPayloadHash
  77. flag *engine.PrivacyFlagType
  78. )
  79. if qmd.PrivacyMetadata != nil {
  80. hash = &qmd.PrivacyMetadata.CreationTxHash
  81. flag = &qmd.PrivacyMetadata.PrivacyFlag
  82. }
  83. return rlp.Encode(writer, struct {
  84. CreationTxHash *common.EncryptedPayloadHash `rlp:"nil"`
  85. PrivacyFlag *engine.PrivacyFlagType `rlp:"nil"`
  86. ManagedParties []string
  87. }{
  88. CreationTxHash: hash,
  89. PrivacyFlag: flag,
  90. ManagedParties: qmd.ManagedParties,
  91. })
  92. }
  93. func (qmd *AccountExtraData) copy() *AccountExtraData {
  94. if qmd == nil {
  95. return nil
  96. }
  97. var copyPM *PrivacyMetadata
  98. if qmd.PrivacyMetadata != nil {
  99. copyPM = &PrivacyMetadata{
  100. CreationTxHash: qmd.PrivacyMetadata.CreationTxHash,
  101. PrivacyFlag: qmd.PrivacyMetadata.PrivacyFlag,
  102. }
  103. }
  104. copyManagedParties := make([]string, len(qmd.ManagedParties))
  105. copy(copyManagedParties, qmd.ManagedParties)
  106. return &AccountExtraData{
  107. PrivacyMetadata: copyPM,
  108. ManagedParties: copyManagedParties,
  109. }
  110. }
  111. // attached to every private contract account
  112. type PrivacyMetadata struct {
  113. CreationTxHash common.EncryptedPayloadHash `json:"creationTxHash"`
  114. PrivacyFlag engine.PrivacyFlagType `json:"privacyFlag"`
  115. }
  116. // Quorum
  117. // privacyMetadataRLP struct is to make sure
  118. // field order is preserved regardless changes in the PrivacyMetadata and its internal
  119. //
  120. // Edit this struct with care to make sure forward and backward compatibility
  121. type privacyMetadataRLP struct {
  122. CreationTxHash common.EncryptedPayloadHash
  123. PrivacyFlag engine.PrivacyFlagType
  124. Rest []rlp.RawValue `rlp:"tail"` // to maintain forward compatibility
  125. }
  126. func (p *PrivacyMetadata) DecodeRLP(stream *rlp.Stream) error {
  127. var dataRLP privacyMetadataRLP
  128. if err := stream.Decode(&dataRLP); err != nil {
  129. return err
  130. }
  131. p.CreationTxHash = dataRLP.CreationTxHash
  132. p.PrivacyFlag = dataRLP.PrivacyFlag
  133. return nil
  134. }
  135. func (p *PrivacyMetadata) EncodeRLP(writer io.Writer) error {
  136. return rlp.Encode(writer, privacyMetadataRLP{
  137. CreationTxHash: p.CreationTxHash,
  138. PrivacyFlag: p.PrivacyFlag,
  139. })
  140. }
  141. func NewStatePrivacyMetadata(creationTxHash common.EncryptedPayloadHash, privacyFlag engine.PrivacyFlagType) *PrivacyMetadata {
  142. return &PrivacyMetadata{
  143. CreationTxHash: creationTxHash,
  144. PrivacyFlag: privacyFlag,
  145. }
  146. }