github.com/koko1123/flow-go-1@v0.29.6/ledger/common/testutils/testutils.go (about)

     1  package testutils
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"math/rand"
     8  
     9  	l "github.com/koko1123/flow-go-1/ledger"
    10  	"github.com/koko1123/flow-go-1/ledger/common/hash"
    11  	"github.com/koko1123/flow-go-1/ledger/common/utils"
    12  )
    13  
    14  // TrieUpdateFixture returns a trie update fixture
    15  func TrieUpdateFixture(n int, minPayloadByteSize int, maxPayloadByteSize int) *l.TrieUpdate {
    16  	return &l.TrieUpdate{
    17  		RootHash: RootHashFixture(),
    18  		Paths:    RandomPaths(n),
    19  		Payloads: RandomPayloads(n, minPayloadByteSize, maxPayloadByteSize),
    20  	}
    21  }
    22  
    23  // QueryFixture returns a query fixture
    24  func QueryFixture() *l.Query {
    25  	scBytes, _ := hex.DecodeString("6a7a565add94fb36069d79e8725c221cd1e5740742501ef014ea6db999fd98ad")
    26  	var sc l.State
    27  	copy(sc[:], scBytes)
    28  	k1p1 := l.NewKeyPart(uint16(1), []byte("1"))
    29  	k1p2 := l.NewKeyPart(uint16(22), []byte("2"))
    30  	k1 := l.NewKey([]l.KeyPart{k1p1, k1p2})
    31  
    32  	k2p1 := l.NewKeyPart(uint16(1), []byte("3"))
    33  	k2p2 := l.NewKeyPart(uint16(22), []byte("4"))
    34  	k2 := l.NewKey([]l.KeyPart{k2p1, k2p2})
    35  
    36  	u, _ := l.NewQuery(sc, []l.Key{k1, k2})
    37  	return u
    38  }
    39  
    40  // LightPayload returns a payload with 2 byte key and 2 byte value
    41  func LightPayload(key uint16, value uint16) *l.Payload {
    42  	k := l.Key{KeyParts: []l.KeyPart{{Type: 0, Value: utils.Uint16ToBinary(key)}}}
    43  	v := l.Value(utils.Uint16ToBinary(value))
    44  	return l.NewPayload(k, v)
    45  }
    46  
    47  // LightPayload8 returns a payload with 1 byte key and 1 byte value
    48  func LightPayload8(key uint8, value uint8) *l.Payload {
    49  	k := l.Key{KeyParts: []l.KeyPart{{Type: 0, Value: []byte{key}}}}
    50  	v := l.Value([]byte{value})
    51  	return l.NewPayload(k, v)
    52  }
    53  
    54  // PathByUint8 returns a path (32 bytes) given a uint8
    55  func PathByUint8(inp uint8) l.Path {
    56  	var b l.Path
    57  	b[0] = inp
    58  	return b
    59  }
    60  
    61  // PathByUint16 returns a path (32 bytes) given a uint16 (big endian)
    62  func PathByUint16(inp uint16) l.Path {
    63  	var b l.Path
    64  	binary.BigEndian.PutUint16(b[:], inp)
    65  	return b
    66  }
    67  
    68  // PathByUint16LeftPadded returns a path (32 bytes) given a uint16 (left padded big endian)
    69  func PathByUint16LeftPadded(inp uint16) l.Path {
    70  	var b l.Path
    71  	binary.BigEndian.PutUint16(b[30:], inp)
    72  	return b
    73  }
    74  
    75  // KeyPartFixture returns a key part fixture
    76  func KeyPartFixture(typ uint16, val string) l.KeyPart {
    77  	kp1t := typ
    78  	kp1v := []byte(val)
    79  	return l.NewKeyPart(kp1t, kp1v)
    80  }
    81  
    82  // UpdateFixture returns an update fixture
    83  func UpdateFixture() *l.Update {
    84  	scBytes, _ := hex.DecodeString("6a7a565add94fb36069d79e8725c221cd1e5740742501ef014ea6db999fd98ad")
    85  	var sc l.State
    86  	copy(sc[:], scBytes)
    87  	k1p1 := l.NewKeyPart(uint16(1), []byte("1"))
    88  	k1p2 := l.NewKeyPart(uint16(22), []byte("2"))
    89  	k1 := l.NewKey([]l.KeyPart{k1p1, k1p2})
    90  	v1 := l.Value([]byte{'A'})
    91  
    92  	k2p1 := l.NewKeyPart(uint16(1), []byte("3"))
    93  	k2p2 := l.NewKeyPart(uint16(22), []byte("4"))
    94  	k2 := l.NewKey([]l.KeyPart{k2p1, k2p2})
    95  	v2 := l.Value([]byte{'B'})
    96  
    97  	u, _ := l.NewUpdate(sc, []l.Key{k1, k2}, []l.Value{v1, v2})
    98  	return u
    99  }
   100  
   101  // RootHashFixture returns a root hash fixture
   102  func RootHashFixture() l.RootHash {
   103  	rootBytes, _ := hex.DecodeString("6a7a565add94fb36069d79e8725c221cd1e5740742501ef014ea6db999fd98ad")
   104  	var root l.RootHash
   105  	copy(root[:], rootBytes)
   106  	return root
   107  }
   108  
   109  // TrieProofFixture returns a trie proof fixture
   110  func TrieProofFixture() (*l.TrieProof, l.State) {
   111  	p := l.NewTrieProof()
   112  	p.Path = PathByUint16(330)
   113  	p.Payload = LightPayload8('A', 'A')
   114  	p.Inclusion = true
   115  	p.Flags = []byte{byte(130), byte(0)}
   116  	p.Interims = make([]hash.Hash, 0)
   117  	interim1Bytes, _ := hex.DecodeString("accb0399dd2b3a7a48618b2376f5e61d822e0c7736b044c364a05c2904a2f315")
   118  	interim2Bytes, _ := hex.DecodeString("f3fba426a2f01c342304e3ca7796c3980c62c625f7fd43105ad5afd92b165542")
   119  	var interim1, interim2 hash.Hash
   120  	copy(interim1[:], interim1Bytes)
   121  	copy(interim2[:], interim2Bytes)
   122  	p.Interims = append(p.Interims, interim1)
   123  	p.Interims = append(p.Interims, interim2)
   124  	p.Steps = uint8(7)
   125  	scBytes, _ := hex.DecodeString("4a9f3a15d7257b624b645955576f62edcceff5e125f49585cdf077d9f37c7ac0")
   126  	var sc l.State
   127  	copy(sc[:], scBytes)
   128  	return p, sc
   129  }
   130  
   131  // TrieBatchProofFixture returns a trie batch proof fixture
   132  func TrieBatchProofFixture() (*l.TrieBatchProof, l.State) {
   133  	p, s := TrieProofFixture()
   134  	bp := l.NewTrieBatchProof()
   135  	bp.Proofs = append(bp.Proofs, p)
   136  	bp.Proofs = append(bp.Proofs, p)
   137  	return bp, s
   138  }
   139  
   140  // RandomPathsRandLen generate m random paths.
   141  // the number of paths, m, is also randomly selected from the range [1, maxN]
   142  func RandomPathsRandLen(maxN int) []l.Path {
   143  	numberOfPaths := rand.Intn(maxN) + 1
   144  	return RandomPaths(numberOfPaths)
   145  }
   146  
   147  // RandomPaths generates n random (no repetition)
   148  func RandomPaths(n int) []l.Path {
   149  	paths := make([]l.Path, 0, n)
   150  	alreadySelectPaths := make(map[l.Path]bool)
   151  	i := 0
   152  	for i < n {
   153  		var path l.Path
   154  		rand.Read(path[:])
   155  		// deduplicate
   156  		if _, found := alreadySelectPaths[path]; !found {
   157  			paths = append(paths, path)
   158  			alreadySelectPaths[path] = true
   159  			i++
   160  		}
   161  	}
   162  	return paths
   163  }
   164  
   165  // RandomPayload returns a random payload
   166  func RandomPayload(minByteSize int, maxByteSize int) *l.Payload {
   167  	keyByteSize := minByteSize + rand.Intn(maxByteSize-minByteSize)
   168  	keydata := make([]byte, keyByteSize)
   169  	rand.Read(keydata)
   170  	key := l.Key{KeyParts: []l.KeyPart{{Type: 0, Value: keydata}}}
   171  	valueByteSize := minByteSize + rand.Intn(maxByteSize-minByteSize)
   172  	valuedata := make([]byte, valueByteSize)
   173  	rand.Read(valuedata)
   174  	value := l.Value(valuedata)
   175  	return l.NewPayload(key, value)
   176  }
   177  
   178  // RandomPayloads returns n random payloads
   179  func RandomPayloads(n int, minByteSize int, maxByteSize int) []*l.Payload {
   180  	res := make([]*l.Payload, 0)
   181  	for i := 0; i < n; i++ {
   182  		res = append(res, RandomPayload(minByteSize, maxByteSize))
   183  	}
   184  	return res
   185  }
   186  
   187  // RandomValues returns n random values with variable sizes (minByteSize <= size < maxByteSize)
   188  func RandomValues(n int, minByteSize, maxByteSize int) []l.Value {
   189  	if minByteSize > maxByteSize {
   190  		panic("minByteSize cannot be smaller then maxByteSize")
   191  	}
   192  	values := make([]l.Value, 0)
   193  	for i := 0; i < n; i++ {
   194  		var byteSize = maxByteSize
   195  		if minByteSize < maxByteSize {
   196  			byteSize = minByteSize + rand.Intn(maxByteSize-minByteSize)
   197  		}
   198  		value := make([]byte, byteSize)
   199  		rand.Read(value)
   200  		values = append(values, value)
   201  	}
   202  	return values
   203  }
   204  
   205  // RandomUniqueKeys generates n random keys (each with m random key parts)
   206  func RandomUniqueKeys(n, m, minByteSize, maxByteSize int) []l.Key {
   207  	if minByteSize > maxByteSize {
   208  		panic("minByteSize cannot be smaller then maxByteSize")
   209  	}
   210  	keys := make([]l.Key, 0)
   211  	alreadySelectKeys := make(map[string]bool)
   212  	i := 0
   213  	for i < n {
   214  		keyParts := make([]l.KeyPart, 0)
   215  		for j := 0; j < m; j++ {
   216  			byteSize := maxByteSize
   217  			if minByteSize < maxByteSize {
   218  				byteSize = minByteSize + rand.Intn(maxByteSize-minByteSize)
   219  			}
   220  			keyPartData := make([]byte, byteSize)
   221  			rand.Read(keyPartData)
   222  			keyParts = append(keyParts, l.NewKeyPart(uint16(j), keyPartData))
   223  		}
   224  		key := l.NewKey(keyParts)
   225  
   226  		// deduplicate
   227  		if _, found := alreadySelectKeys[key.String()]; !found {
   228  			keys = append(keys, key)
   229  			alreadySelectKeys[key.String()] = true
   230  			i++
   231  		} else {
   232  			fmt.Println("Already existing")
   233  		}
   234  	}
   235  	return keys
   236  }