github.com/Night-mk/quorum@v21.1.0+incompatible/core/state/account_extra_data_test.go (about) 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 17 package state 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/private/engine" 25 "github.com/ethereum/go-ethereum/rlp" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 type privacyMetadataOld struct { 30 CreationTxHash common.EncryptedPayloadHash 31 PrivacyFlag engine.PrivacyFlagType 32 } 33 34 // privacyMetadataToBytes is the utility function under test from previous implementation 35 func privacyMetadataToBytes(pm *privacyMetadataOld) ([]byte, error) { 36 return rlp.EncodeToBytes(pm) 37 } 38 39 // bytesToPrivacyMetadata is the utility function under test from previous implementation 40 func bytesToPrivacyMetadata(b []byte) (*privacyMetadataOld, error) { 41 var data *privacyMetadataOld 42 if err := rlp.DecodeBytes(b, &data); err != nil { 43 return nil, fmt.Errorf("unable to decode privacy metadata. Cause: %v", err) 44 } 45 return data, nil 46 } 47 48 func TestRLP_PrivacyMetadata_DecodeBackwardCompatibility(t *testing.T) { 49 existingPM := &privacyMetadataOld{ 50 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-hash")), 51 PrivacyFlag: engine.PrivacyFlagStateValidation, 52 } 53 existing, err := privacyMetadataToBytes(existingPM) 54 assert.NoError(t, err) 55 56 var actual PrivacyMetadata 57 err = rlp.DecodeBytes(existing, &actual) 58 59 assert.NoError(t, err, "Must decode PrivacyMetadata successfully") 60 assert.Equal(t, existingPM.CreationTxHash, actual.CreationTxHash) 61 assert.Equal(t, existingPM.PrivacyFlag, actual.PrivacyFlag) 62 } 63 64 func TestRLP_PrivacyMetadata_DecodeForwardCompatibility(t *testing.T) { 65 pm := &PrivacyMetadata{ 66 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-hash")), 67 PrivacyFlag: engine.PrivacyFlagStateValidation, 68 } 69 existing, err := rlp.EncodeToBytes(pm) 70 assert.NoError(t, err) 71 72 var actual *privacyMetadataOld 73 actual, err = bytesToPrivacyMetadata(existing) 74 75 assert.NoError(t, err, "Must encode PrivacyMetadata successfully") 76 assert.Equal(t, pm.CreationTxHash, actual.CreationTxHash) 77 assert.Equal(t, pm.PrivacyFlag, actual.PrivacyFlag) 78 } 79 80 // From initial privacy enhancements, the privacy metadata is RLP encoded 81 // we now wrap PrivacyMetadata in a more generic struct. This test is to make sure 82 // we support backward compatibility. 83 func TestRLP_AccountExtraData_BackwardCompatibility(t *testing.T) { 84 // prepare existing RLP bytes 85 arbitraryExistingMetadata := &PrivacyMetadata{ 86 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-existing-privacy-metadata-creation-hash")), 87 PrivacyFlag: engine.PrivacyFlagPartyProtection, 88 } 89 existing, err := rlp.EncodeToBytes(arbitraryExistingMetadata) 90 assert.NoError(t, err) 91 92 // now try to decode with the new struct 93 var actual AccountExtraData 94 err = rlp.DecodeBytes(existing, &actual) 95 96 assert.NoError(t, err, "Must decode successfully") 97 assert.Equal(t, arbitraryExistingMetadata.CreationTxHash, actual.PrivacyMetadata.CreationTxHash) 98 assert.Equal(t, arbitraryExistingMetadata.PrivacyFlag, actual.PrivacyMetadata.PrivacyFlag) 99 } 100 101 func TestRLP_AccountExtraData_withField_ManagedParties(t *testing.T) { 102 // prepare existing RLP bytes 103 arbitraryExtraData := &AccountExtraData{ 104 PrivacyMetadata: &PrivacyMetadata{ 105 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-existing-privacy-metadata-creation-hash")), 106 PrivacyFlag: engine.PrivacyFlagPartyProtection, 107 }, 108 ManagedParties: []string{"Arbitrary PK1", "Arbitrary PK2"}, 109 } 110 existing, err := rlp.EncodeToBytes(arbitraryExtraData) 111 assert.NoError(t, err) 112 113 // now try to decode with the new struct 114 var actual AccountExtraData 115 err = rlp.DecodeBytes(existing, &actual) 116 117 assert.NoError(t, err, "Must decode successfully") 118 assert.Equal(t, arbitraryExtraData.PrivacyMetadata.CreationTxHash, actual.PrivacyMetadata.CreationTxHash) 119 assert.Equal(t, arbitraryExtraData.PrivacyMetadata.PrivacyFlag, actual.PrivacyMetadata.PrivacyFlag) 120 assert.Equal(t, arbitraryExtraData.ManagedParties, actual.ManagedParties) 121 } 122 123 func TestRLP_AccountExtraData_whenTypical(t *testing.T) { 124 expected := AccountExtraData{ 125 PrivacyMetadata: &PrivacyMetadata{ 126 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-payload-hash")), 127 PrivacyFlag: engine.PrivacyFlagPartyProtection, 128 }, 129 ManagedParties: []string{"XYZ"}, 130 } 131 132 data, err := rlp.EncodeToBytes(&expected) 133 assert.NoError(t, err) 134 135 var actual AccountExtraData 136 assert.NoError(t, rlp.DecodeBytes(data, &actual)) 137 assert.Equal(t, expected.PrivacyMetadata.CreationTxHash, actual.PrivacyMetadata.CreationTxHash) 138 assert.Equal(t, expected.PrivacyMetadata.PrivacyFlag, actual.PrivacyMetadata.PrivacyFlag) 139 assert.Equal(t, expected.ManagedParties, actual.ManagedParties) 140 } 141 142 func TestRLP_AccountExtraData_whenHavingPrivacyMetadataOnly(t *testing.T) { 143 expected := AccountExtraData{ 144 PrivacyMetadata: &PrivacyMetadata{ 145 CreationTxHash: common.BytesToEncryptedPayloadHash([]byte("arbitrary-payload-hash")), 146 PrivacyFlag: engine.PrivacyFlagPartyProtection, 147 }, 148 } 149 150 data, err := rlp.EncodeToBytes(&expected) 151 assert.NoError(t, err) 152 153 var actual AccountExtraData 154 assert.NoError(t, rlp.DecodeBytes(data, &actual)) 155 assert.Equal(t, expected.PrivacyMetadata.CreationTxHash, actual.PrivacyMetadata.CreationTxHash) 156 assert.Equal(t, expected.PrivacyMetadata.PrivacyFlag, actual.PrivacyMetadata.PrivacyFlag) 157 } 158 159 func TestRLP_AccountExtraData_whenHavingNilManagedParties(t *testing.T) { 160 expected := AccountExtraData{ 161 PrivacyMetadata: nil, 162 ManagedParties: nil, 163 } 164 165 data, err := rlp.EncodeToBytes(&expected) 166 assert.NoError(t, err) 167 168 var actual AccountExtraData 169 assert.NoError(t, rlp.DecodeBytes(data, &actual)) 170 assert.Nil(t, actual.ManagedParties) 171 assert.Nil(t, actual.PrivacyMetadata) 172 } 173 174 func TestRLP_AccountExtraData_whenHavingEmptyManagedParties(t *testing.T) { 175 expected := AccountExtraData{ 176 PrivacyMetadata: nil, 177 ManagedParties: []string{}, 178 } 179 180 data, err := rlp.EncodeToBytes(&expected) 181 assert.NoError(t, err) 182 183 var actual AccountExtraData 184 assert.NoError(t, rlp.DecodeBytes(data, &actual)) 185 assert.Nil(t, actual.ManagedParties) 186 assert.Nil(t, actual.PrivacyMetadata) 187 } 188 189 func TestCopy_whenNil(t *testing.T) { 190 var testObj *AccountExtraData = nil 191 192 assert.Nil(t, testObj.copy()) 193 }