github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/pvtdatastorage/kv_encoding_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pvtdatastorage 8 9 import ( 10 "bytes" 11 math "math" 12 "testing" 13 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestDataKeyEncoding(t *testing.T) { 18 dataKey1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns1", coll: "coll1", blkNum: 2}, txNum: 5} 19 datakey2, err := decodeDatakey(encodeDataKey(dataKey1)) 20 require.NoError(t, err) 21 require.Equal(t, dataKey1, datakey2) 22 } 23 24 func TestDataKeyRange(t *testing.T) { 25 blockNum := uint64(20) 26 startKey, endKey := datakeyRange(blockNum) 27 var txNum uint64 28 for txNum = 0; txNum < 100; txNum++ { 29 keyOfBlock := encodeDataKey( 30 &dataKey{ 31 nsCollBlk: nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum}, 32 txNum: txNum, 33 }, 34 ) 35 keyOfPreviousBlock := encodeDataKey( 36 &dataKey{ 37 nsCollBlk: nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum - 1}, 38 txNum: txNum, 39 }, 40 ) 41 keyOfNextBlock := encodeDataKey( 42 &dataKey{ 43 nsCollBlk: nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum + 1}, 44 txNum: txNum, 45 }, 46 ) 47 require.Equal(t, bytes.Compare(keyOfPreviousBlock, startKey), -1) 48 require.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) 49 require.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) 50 require.Equal(t, bytes.Compare(keyOfNextBlock, endKey), 1) 51 } 52 } 53 54 func TestEligibleMissingDataRange(t *testing.T) { 55 blockNum := uint64(20) 56 startKey, endKey := eligibleMissingdatakeyRange(blockNum) 57 var txNum uint64 58 for txNum = 0; txNum < 100; txNum++ { 59 keyOfBlock := encodeElgPrioMissingDataKey( 60 &missingDataKey{ 61 nsCollBlk: nsCollBlk{ 62 ns: "ns", 63 coll: "coll", 64 blkNum: blockNum, 65 }, 66 }, 67 ) 68 keyOfPreviousBlock := encodeElgPrioMissingDataKey( 69 &missingDataKey{ 70 nsCollBlk: nsCollBlk{ 71 ns: "ns", 72 coll: "coll", 73 blkNum: blockNum - 1, 74 }, 75 }, 76 ) 77 keyOfNextBlock := encodeElgPrioMissingDataKey( 78 &missingDataKey{ 79 nsCollBlk: nsCollBlk{ 80 ns: "ns", 81 coll: "coll", 82 blkNum: blockNum + 1, 83 }, 84 }, 85 ) 86 require.Equal(t, bytes.Compare(keyOfNextBlock, startKey), -1) 87 require.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) 88 require.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) 89 require.Equal(t, bytes.Compare(keyOfPreviousBlock, endKey), 1) 90 } 91 } 92 93 func TestEncodeDecodeMissingdataKey(t *testing.T) { 94 for i := 0; i < 1000; i++ { 95 testEncodeDecodeMissingdataKey(t, uint64(i)) 96 } 97 testEncodeDecodeMissingdataKey(t, math.MaxUint64) // corner case 98 } 99 100 func testEncodeDecodeMissingdataKey(t *testing.T, blkNum uint64) { 101 key := &missingDataKey{ 102 nsCollBlk: nsCollBlk{ 103 ns: "ns", 104 coll: "coll", 105 blkNum: blkNum, 106 }, 107 } 108 109 t.Run("ineligibileKey", 110 func(t *testing.T) { 111 decodedKey := decodeInelgMissingDataKey( 112 encodeInelgMissingDataKey(key), 113 ) 114 require.Equal(t, key, decodedKey) 115 }, 116 ) 117 118 t.Run("eligiblePrioritizedKey", 119 func(t *testing.T) { 120 decodedKey := decodeElgMissingDataKey( 121 encodeElgPrioMissingDataKey(key), 122 ) 123 require.Equal(t, key, decodedKey) 124 }, 125 ) 126 127 t.Run("eligibleDeprioritizedKey", 128 func(t *testing.T) { 129 decodedKey := decodeElgMissingDataKey( 130 encodeElgDeprioMissingDataKey(key), 131 ) 132 require.Equal(t, key, decodedKey) 133 }, 134 ) 135 } 136 137 func TestBasicEncodingDecoding(t *testing.T) { 138 for i := 0; i < 10000; i++ { 139 value := encodeReverseOrderVarUint64(uint64(i)) 140 nextValue := encodeReverseOrderVarUint64(uint64(i + 1)) 141 if !(bytes.Compare(value, nextValue) > 0) { 142 t.Fatalf("A smaller integer should result into greater bytes. Encoded bytes for [%d] is [%x] and for [%d] is [%x]", 143 i, i+1, value, nextValue) 144 } 145 decodedValue, _ := decodeReverseOrderVarUint64(value) 146 if decodedValue != uint64(i) { 147 t.Fatalf("Value not same after decoding. Original value = [%d], decode value = [%d]", i, decodedValue) 148 } 149 } 150 } 151 152 func TestDecodingAppendedValues(t *testing.T) { 153 appendedValues := []byte{} 154 for i := 0; i < 1000; i++ { 155 appendedValues = append(appendedValues, encodeReverseOrderVarUint64(uint64(i))...) 156 } 157 158 len := 0 159 value := uint64(0) 160 for i := 0; i < 1000; i++ { 161 appendedValues = appendedValues[len:] 162 value, len = decodeReverseOrderVarUint64(appendedValues) 163 if value != uint64(i) { 164 t.Fatalf("expected value = [%d], decode value = [%d]", i, value) 165 } 166 } 167 } 168 169 func TestEncodingDecodingLastBlockInSnapshotVal(t *testing.T) { 170 t.Run("basic-coding-encoding", func(t *testing.T) { 171 for i := uint64(0); i < 100; i++ { 172 encoded := encodeLastBlockInBootSnapshotVal(i) 173 decoded, err := decodeLastBlockInBootSnapshotVal(encoded) 174 require.NoError(t, err) 175 require.Equal(t, i, decoded) 176 } 177 }) 178 179 t.Run("error-case", func(t *testing.T) { 180 _, err := decodeLastBlockInBootSnapshotVal([]byte{0xff}) 181 require.EqualError(t, err, "unexpected bytes for interpreting as varint") 182 }) 183 }