github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/abci/example/kvstore/helpers.go (about) 1 package kvstore 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/badrootd/nibiru-cometbft/abci/types" 8 cmtrand "github.com/badrootd/nibiru-cometbft/libs/rand" 9 ) 10 11 // RandVal creates one random validator, with a key derived 12 // from the input value 13 func RandVal(i int) types.ValidatorUpdate { 14 pubkey := cmtrand.Bytes(32) 15 power := cmtrand.Uint16() + 1 16 v := types.UpdateValidator(pubkey, int64(power), "") 17 return v 18 } 19 20 // RandVals returns a list of cnt validators for initializing 21 // the application. Note that the keys are deterministically 22 // derived from the index in the array, while the power is 23 // random (Change this if not desired) 24 func RandVals(cnt int) []types.ValidatorUpdate { 25 res := make([]types.ValidatorUpdate, cnt) 26 for i := 0; i < cnt; i++ { 27 res[i] = RandVal(i) 28 } 29 return res 30 } 31 32 // InitKVStore initializes the kvstore app with some data, 33 // which allows tests to pass and is fine as long as you 34 // don't make any tx that modify the validator state 35 func InitKVStore(app *PersistentKVStoreApplication) { 36 app.InitChain(types.RequestInitChain{ 37 Validators: RandVals(1), 38 }) 39 } 40 41 // Create a new transaction 42 func NewTx(key, value string) []byte { 43 return []byte(strings.Join([]string{key, value}, "=")) 44 } 45 46 func NewRandomTx(size int) []byte { 47 if size < 4 { 48 panic("random tx size must be greater than 3") 49 } 50 return NewTx(cmtrand.Str(2), cmtrand.Str(size-3)) 51 } 52 53 func NewRandomTxs(n int) [][]byte { 54 txs := make([][]byte, n) 55 for i := 0; i < n; i++ { 56 txs[i] = NewRandomTx(10) 57 } 58 return txs 59 } 60 61 func NewTxFromID(i int) []byte { 62 return []byte(fmt.Sprintf("%d=%d", i, i)) 63 }