github.com/Hnampk/fabric@v2.1.1+incompatible/core/ledger/pvtdatastorage/kv_encoding_test.go (about)

     1  /*
     2  Copyright IBM Corp. 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/assert"
    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  	assert.NoError(t, err)
    21  	assert.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  		assert.Equal(t, bytes.Compare(keyOfPreviousBlock, startKey), -1)
    48  		assert.Equal(t, bytes.Compare(keyOfBlock, startKey), 1)
    49  		assert.Equal(t, bytes.Compare(keyOfBlock, endKey), -1)
    50  		assert.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 := encodeMissingDataKey(
    60  			&missingDataKey{
    61  				nsCollBlk:  nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum},
    62  				isEligible: true,
    63  			},
    64  		)
    65  		keyOfPreviousBlock := encodeMissingDataKey(
    66  			&missingDataKey{
    67  				nsCollBlk:  nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum - 1},
    68  				isEligible: true,
    69  			},
    70  		)
    71  		keyOfNextBlock := encodeMissingDataKey(
    72  			&missingDataKey{
    73  				nsCollBlk:  nsCollBlk{ns: "ns", coll: "coll", blkNum: blockNum + 1},
    74  				isEligible: true,
    75  			},
    76  		)
    77  		assert.Equal(t, bytes.Compare(keyOfNextBlock, startKey), -1)
    78  		assert.Equal(t, bytes.Compare(keyOfBlock, startKey), 1)
    79  		assert.Equal(t, bytes.Compare(keyOfBlock, endKey), -1)
    80  		assert.Equal(t, bytes.Compare(keyOfPreviousBlock, endKey), 1)
    81  	}
    82  }
    83  
    84  func TestEncodeDecodeMissingdataKey(t *testing.T) {
    85  	for i := 0; i < 1000; i++ {
    86  		testEncodeDecodeMissingdataKey(t, uint64(i))
    87  	}
    88  	testEncodeDecodeMissingdataKey(t, math.MaxUint64) // corner case
    89  }
    90  
    91  func testEncodeDecodeMissingdataKey(t *testing.T, blkNum uint64) {
    92  	key := &missingDataKey{
    93  		nsCollBlk: nsCollBlk{
    94  			ns:     "ns",
    95  			coll:   "coll",
    96  			blkNum: blkNum,
    97  		},
    98  	}
    99  
   100  	t.Run("ineligibileKey",
   101  		func(t *testing.T) {
   102  			key.isEligible = false
   103  			decodedKey := decodeMissingDataKey(
   104  				encodeMissingDataKey(key),
   105  			)
   106  			assert.Equal(t, key, decodedKey)
   107  		},
   108  	)
   109  
   110  	t.Run("ineligibileKey",
   111  		func(t *testing.T) {
   112  			key.isEligible = true
   113  			decodedKey := decodeMissingDataKey(
   114  				encodeMissingDataKey(key),
   115  			)
   116  			assert.Equal(t, key, decodedKey)
   117  		},
   118  	)
   119  }
   120  
   121  func TestBasicEncodingDecoding(t *testing.T) {
   122  	for i := 0; i < 10000; i++ {
   123  		value := encodeReverseOrderVarUint64(uint64(i))
   124  		nextValue := encodeReverseOrderVarUint64(uint64(i + 1))
   125  		if !(bytes.Compare(value, nextValue) > 0) {
   126  			t.Fatalf("A smaller integer should result into greater bytes. Encoded bytes for [%d] is [%x] and for [%d] is [%x]",
   127  				i, i+1, value, nextValue)
   128  		}
   129  		decodedValue, _ := decodeReverseOrderVarUint64(value)
   130  		if decodedValue != uint64(i) {
   131  			t.Fatalf("Value not same after decoding. Original value = [%d], decode value = [%d]", i, decodedValue)
   132  		}
   133  	}
   134  }
   135  
   136  func TestDecodingAppendedValues(t *testing.T) {
   137  	appendedValues := []byte{}
   138  	for i := 0; i < 1000; i++ {
   139  		appendedValues = append(appendedValues, encodeReverseOrderVarUint64(uint64(i))...)
   140  	}
   141  
   142  	len := 0
   143  	value := uint64(0)
   144  	for i := 0; i < 1000; i++ {
   145  		appendedValues = appendedValues[len:]
   146  		value, len = decodeReverseOrderVarUint64(appendedValues)
   147  		if value != uint64(i) {
   148  			t.Fatalf("expected value = [%d], decode value = [%d]", i, value)
   149  		}
   150  	}
   151  }