github.com/Unheilbar/quorum@v1.0.0/qlight/test/client_cache_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/core/rawdb" 9 "github.com/ethereum/go-ethereum/private/cache" 10 "github.com/ethereum/go-ethereum/private/engine" 11 "github.com/ethereum/go-ethereum/private/engine/qlightptm" 12 "github.com/ethereum/go-ethereum/qlight" 13 "github.com/golang/mock/gomock" 14 gocache "github.com/patrickmn/go-cache" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestClientCache_AddPrivateBlock(t *testing.T) { 19 assert := assert.New(t) 20 ctrl := gomock.NewController(t) 21 defer ctrl.Finish() 22 23 memDB := rawdb.NewMemoryDatabase() 24 cacheWithEmpty := NewMockCacheWithEmpty(ctrl) 25 gocache := gocache.New(cache.DefaultExpiration, cache.CleanupInterval) 26 27 clientCache, _ := qlight.NewClientCacheWithEmpty(memDB, cacheWithEmpty, gocache) 28 29 txHash1 := common.BytesToEncryptedPayloadHash([]byte("TXHash1")) 30 ptd1 := qlight.PrivateTransactionData{ 31 Hash: &txHash1, 32 Payload: []byte("payload"), 33 Extra: &engine.ExtraMetadata{ 34 ACHashes: nil, 35 ACMerkleRoot: common.Hash{}, 36 PrivacyFlag: 0, 37 ManagedParties: nil, 38 Sender: "", 39 MandatoryRecipients: nil, 40 }, 41 IsSender: false, 42 } 43 blockPrivateData := qlight.BlockPrivateData{ 44 BlockHash: common.StringToHash("BlockHash"), 45 PSI: "", 46 PrivateStateRoot: common.StringToHash("PrivateStateRoot"), 47 PrivateTransactions: []qlight.PrivateTransactionData{ptd1}, 48 } 49 50 var capturedCacheItem *qlightptm.CachablePrivateTransactionData 51 cacheWithEmpty.EXPECT().Cache(gomock.Any()).DoAndReturn(func(privateTxData *qlightptm.CachablePrivateTransactionData) error { 52 capturedCacheItem = privateTxData 53 return nil 54 }) 55 56 clientCache.AddPrivateBlock(blockPrivateData) 57 58 assert.Equal(fmt.Sprintf("0x%x", ptd1.Payload), capturedCacheItem.QuorumPrivateTxData.Payload) 59 assert.Equal(ptd1.Hash, &capturedCacheItem.Hash) 60 61 psr, _ := gocache.Get(blockPrivateData.BlockHash.ToBase64()) 62 assert.Equal(blockPrivateData.PrivateStateRoot.ToBase64(), psr) 63 } 64 65 func TestClientCache_ValidatePrivateStateRootSuccess(t *testing.T) { 66 assert := assert.New(t) 67 ctrl := gomock.NewController(t) 68 defer ctrl.Finish() 69 70 memDB := rawdb.NewMemoryDatabase() 71 cacheWithEmpty := NewMockCacheWithEmpty(ctrl) 72 gocache := gocache.New(cache.DefaultExpiration, cache.CleanupInterval) 73 74 clientCache, _ := qlight.NewClientCacheWithEmpty(memDB, cacheWithEmpty, gocache) 75 76 publicStateRoot := common.StringToHash("PublicStateRoot") 77 blockPrivateData := qlight.BlockPrivateData{ 78 BlockHash: common.StringToHash("BlockHash"), 79 PSI: "", 80 PrivateStateRoot: common.StringToHash("PrivateStateRoot"), 81 PrivateTransactions: []qlight.PrivateTransactionData{}, 82 } 83 84 clientCache.AddPrivateBlock(blockPrivateData) 85 rawdb.WritePrivateStateRoot(memDB, publicStateRoot, blockPrivateData.PrivateStateRoot) 86 87 err := clientCache.ValidatePrivateStateRoot(blockPrivateData.BlockHash, publicStateRoot) 88 89 assert.Nil(err) 90 } 91 92 func TestClientCache_ValidatePrivateStateRootMismatch(t *testing.T) { 93 assert := assert.New(t) 94 ctrl := gomock.NewController(t) 95 defer ctrl.Finish() 96 97 memDB := rawdb.NewMemoryDatabase() 98 cacheWithEmpty := NewMockCacheWithEmpty(ctrl) 99 gocache := gocache.New(cache.DefaultExpiration, cache.CleanupInterval) 100 101 clientCache, _ := qlight.NewClientCacheWithEmpty(memDB, cacheWithEmpty, gocache) 102 103 publicStateRoot := common.StringToHash("PublicStateRoot") 104 blockPrivateData := qlight.BlockPrivateData{ 105 BlockHash: common.StringToHash("BlockHash"), 106 PSI: "", 107 PrivateStateRoot: common.StringToHash("PrivateStateRoot"), 108 PrivateTransactions: []qlight.PrivateTransactionData{}, 109 } 110 111 clientCache.AddPrivateBlock(blockPrivateData) 112 rawdb.WritePrivateStateRoot(memDB, publicStateRoot, common.StringToHash("Mismatch")) 113 114 err := clientCache.ValidatePrivateStateRoot(blockPrivateData.BlockHash, publicStateRoot) 115 116 assert.Error(err) 117 } 118 119 func TestClientCache_ValidatePrivateStateRootNoDataInClientCache(t *testing.T) { 120 assert := assert.New(t) 121 ctrl := gomock.NewController(t) 122 defer ctrl.Finish() 123 124 memDB := rawdb.NewMemoryDatabase() 125 cacheWithEmpty := NewMockCacheWithEmpty(ctrl) 126 gocache := gocache.New(cache.DefaultExpiration, cache.CleanupInterval) 127 128 clientCache, _ := qlight.NewClientCacheWithEmpty(memDB, cacheWithEmpty, gocache) 129 130 publicStateRoot := common.StringToHash("PublicStateRoot") 131 blockPrivateData := qlight.BlockPrivateData{ 132 BlockHash: common.StringToHash("BlockHash"), 133 PSI: "", 134 PrivateStateRoot: common.StringToHash("PrivateStateRoot"), 135 PrivateTransactions: []qlight.PrivateTransactionData{}, 136 } 137 138 rawdb.WritePrivateStateRoot(memDB, publicStateRoot, blockPrivateData.PrivateStateRoot) 139 140 err := clientCache.ValidatePrivateStateRoot(blockPrivateData.BlockHash, publicStateRoot) 141 142 assert.Nil(err) 143 }