MCPcopy Create free account
hub / github.com/ethstorage/es-node / TestEncoding

Function TestEncoding

ethstorage/downloader/blob_cache_test.go:90–147  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

88}
89
90func TestEncoding(t *testing.T) {
91 setup(t)
92 t.Cleanup(func() {
93 teardown(t)
94 })
95
96 blockBlobsParams := []struct {
97 blockNum uint64
98 blobLen uint64
99 }{
100 {0, 1},
101 {1000, 4},
102 {1, 5},
103 {222, 6},
104 {12345, 2},
105 {2000000, 3},
106 }
107
108 df, err := ethstorage.Create(fileName, shardID, kvEntries, 0, kvSize, ethstorage.ENCODE_BLOB_POSEIDON, minerAddr, kvSize)
109 if err != nil {
110 t.Fatalf("Create failed %v", err)
111 }
112 shardMgr := ethstorage.NewShardManager(common.Address{}, kvSize, kvEntries, kvSize)
113 shardMgr.AddDataShard(shardID)
114 shardMgr.AddDataFile(df)
115 sm := ethstorage.NewStorageManager(shardMgr, nil, lg)
116 defer func() {
117 sm.Close()
118 os.Remove(fileName)
119 }()
120
121 // download and save to cache
122 for _, tt := range blockBlobsParams {
123 bb, err := newBlockBlobs(tt.blockNum, tt.blobLen)
124 if err != nil {
125 t.Fatalf("failed to create block blobs: %v", err)
126 }
127 for i, b := range bb.blobs {
128 bb.blobs[i].data = sm.EncodeBlob(b.data, b.hash, b.kvIndex.Uint64(), kvSize)
129 }
130 if err := cache.SetBlockBlobs(bb); err != nil {
131 t.Fatalf("failed to set block blobs: %v", err)
132 }
133 }
134
135 // load from cache and verify
136 for i, kvHash := range kvHashes {
137 kvIndex := uint64(i)
138 t.Run(fmt.Sprintf("test kv: %d", i), func(t *testing.T) {
139 blobEncoded := cache.GetKeyValueByIndex(kvIndex, kvHash)
140 blobDecoded := sm.DecodeBlob(blobEncoded, kvHash, kvIndex, kvSize)
141 bytesWant := fmt.Appendf(nil, blobData, kvIndex)
142 if !bytes.Equal(blobDecoded[:len(bytesWant)], bytesWant) {
143 t.Errorf("GetKeyValueByIndex and decoded = %s, want %s", blobDecoded[:len(bytesWant)], bytesWant)
144 }
145 })
146 }
147}

Callers

nothing calls this directly

Calls 14

AddDataShardMethod · 0.95
AddDataFileMethod · 0.95
CloseMethod · 0.95
EncodeBlobMethod · 0.95
DecodeBlobMethod · 0.95
CreateFunction · 0.92
NewShardManagerFunction · 0.92
NewStorageManagerFunction · 0.92
newBlockBlobsFunction · 0.85
setupFunction · 0.70
teardownFunction · 0.70
CleanupMethod · 0.65

Tested by

no test coverage detected