github.com/turingchain2020/turingchain@v1.1.21/store/store_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package store 6 7 import ( 8 "crypto/rand" 9 "fmt" 10 "os" 11 "testing" 12 13 "github.com/turingchain2020/turingchain/common" 14 "github.com/turingchain2020/turingchain/common/log" 15 "github.com/turingchain2020/turingchain/queue" 16 "github.com/turingchain2020/turingchain/types" 17 "github.com/stretchr/testify/assert" 18 19 _ "github.com/turingchain2020/turingchain/system" 20 ) 21 22 func init() { 23 log.SetLogLevel("error") 24 } 25 26 func initEnv() (queue.Queue, queue.Module) { 27 var q = queue.New("channel") 28 cfg := types.NewTuringchainConfig(types.ReadFile("../cmd/turingchain/turingchain.test.toml")) 29 s := New(cfg) 30 s.SetQueueClient(q.Client()) 31 return q, s 32 } 33 34 func set(client queue.Client, hash, key, value []byte) ([]byte, error) { 35 kv := &types.KeyValue{Key: key, Value: value} 36 set := &types.StoreSet{} 37 set.StateHash = hash 38 set.KV = append(set.KV, kv) 39 setwithsync := &types.StoreSetWithSync{Storeset: set, Sync: true} 40 41 msg := client.NewMessage("store", types.EventStoreSet, setwithsync) 42 client.Send(msg, true) 43 msg, err := client.Wait(msg) 44 if err != nil { 45 return nil, err 46 } 47 return msg.GetData().(*types.ReplyHash).GetHash(), nil 48 } 49 50 func setmem(client queue.Client, hash, key, value []byte) ([]byte, error) { 51 kv := &types.KeyValue{Key: key, Value: value} 52 set := &types.StoreSet{} 53 set.StateHash = hash 54 set.KV = append(set.KV, kv) 55 storeset := &types.StoreSetWithSync{Storeset: set, Sync: true} 56 msg := client.NewMessage("store", types.EventStoreMemSet, storeset) 57 client.Send(msg, true) 58 msg, err := client.Wait(msg) 59 if err != nil { 60 return nil, err 61 } 62 return msg.GetData().(*types.ReplyHash).GetHash(), nil 63 } 64 65 func get(client queue.Client, hash, key []byte) ([]byte, error) { 66 query := &types.StoreGet{StateHash: hash, Keys: [][]byte{key}} 67 msg := client.NewMessage("store", types.EventStoreGet, query) 68 client.Send(msg, true) 69 msg, err := client.Wait(msg) 70 if err != nil { 71 return nil, err 72 } 73 values := msg.GetData().(*types.StoreReplyValue).GetValues() 74 return values[0], nil 75 } 76 77 func commit(client queue.Client, hash []byte) ([]byte, error) { 78 req := &types.ReqHash{Hash: hash} 79 msg := client.NewMessage("store", types.EventStoreCommit, req) 80 client.Send(msg, true) 81 msg, err := client.Wait(msg) 82 if err != nil { 83 return nil, err 84 } 85 hash = msg.GetData().(*types.ReplyHash).GetHash() 86 return hash, nil 87 } 88 89 func rollback(client queue.Client, hash []byte) ([]byte, error) { 90 req := &types.ReqHash{Hash: hash} 91 msg := client.NewMessage("store", types.EventStoreRollback, req) 92 client.Send(msg, true) 93 msg, err := client.Wait(msg) 94 if err != nil { 95 return nil, err 96 } 97 hash = msg.GetData().(*types.ReplyHash).GetHash() 98 return hash, nil 99 } 100 101 func TestGetAndSet(t *testing.T) { 102 q, s := initEnv() 103 client := q.Client() 104 var stateHash [32]byte 105 //先set一个数 106 key := []byte("hello") 107 value := []byte("world") 108 109 hash, err := set(client, stateHash[:], key, value) 110 if err != nil { 111 t.Error(err) 112 return 113 } 114 115 value2, err := get(client, hash, key) 116 if err != nil { 117 t.Error(err) 118 return 119 } 120 if string(value2) != string(value) { 121 t.Errorf("values not match") 122 return 123 } 124 s.Close() 125 } 126 127 func randstr() string { 128 var hash [16]byte 129 _, err := rand.Read(hash[:]) 130 if err != nil { 131 panic(err) 132 } 133 return common.ToHex(hash[:]) 134 } 135 136 func TestGetAndSetCommitAndRollback(t *testing.T) { 137 q, s := initEnv() 138 client := q.Client() 139 var stateHash [32]byte 140 //先set一个数 141 key := []byte("hello" + randstr()) 142 value := []byte("world") 143 144 hash, err := setmem(client, stateHash[:], key, value) 145 if err != nil { 146 t.Error(err) 147 return 148 } 149 150 value2, err := get(client, hash, key) 151 if err != nil { 152 t.Error(err) 153 return 154 } 155 if string(value2) != string(value) { 156 t.Errorf("values not match %s %s %x", string(value2), string(value), hash) 157 return 158 } 159 160 rollback(client, hash) 161 value2, err = get(client, hash, key) 162 if err != nil { 163 t.Error(err) 164 return 165 } 166 if len(value2) != 0 { 167 t.Error(err) 168 return 169 } 170 171 hash, err = setmem(client, stateHash[:], key, value) 172 if err != nil { 173 t.Error(err) 174 return 175 } 176 177 commit(client, hash) 178 179 value2, err = get(client, hash, key) 180 if err != nil { 181 t.Error(err) 182 return 183 } 184 if string(value2) != string(value) { 185 t.Errorf("values not match [%s] [%s] %x", string(value2), string(value), hash) 186 return 187 } 188 189 s.Close() 190 } 191 192 func BenchmarkGetKey(b *testing.B) { 193 q, s := initEnv() 194 client := q.Client() 195 var stateHash [32]byte 196 hash := stateHash[:] 197 var err error 198 for i := 0; i < 1000; i++ { 199 key := []byte(fmt.Sprintf("%020d", i)) 200 value := []byte(fmt.Sprintf("%020d", i)) 201 hash, err = set(client, hash, key, value) 202 if err != nil { 203 b.Error(err) 204 return 205 } 206 } 207 b.ResetTimer() 208 for i := 0; i < b.N; i++ { 209 key := []byte(fmt.Sprintf("%020d", i%1000)) 210 value := fmt.Sprintf("%020d", i%1000) 211 value2, err := get(client, hash, key) 212 if err != nil { 213 b.Error(err) 214 return 215 } 216 if string(value2) != value { 217 b.Error(err) 218 return 219 } 220 } 221 s.Close() 222 } 223 224 func BenchmarkSetKeyOneByOne(b *testing.B) { 225 q, s := initEnv() 226 client := q.Client() 227 var stateHash [32]byte 228 hash := stateHash[:] 229 var err error 230 for i := 0; i < b.N; i++ { 231 key := []byte(fmt.Sprintf("%020d", i)) 232 value := []byte(fmt.Sprintf("%020d", i)) 233 hash, err = set(client, hash, key, value) 234 if err != nil { 235 b.Error(err) 236 return 237 } 238 } 239 s.Close() 240 } 241 242 func BenchmarkSetKey1000(b *testing.B) { 243 q, s := initEnv() 244 client := q.Client() 245 var stateHash [32]byte 246 hash := stateHash[:] 247 set := &types.StoreSet{} 248 249 for i := 0; i < b.N; i++ { 250 key := []byte(fmt.Sprintf("%020d", i)) 251 value := []byte(fmt.Sprintf("%020d", i)) 252 kv := &types.KeyValue{Key: key, Value: value} 253 if i%1000 == 0 { 254 set = &types.StoreSet{} 255 set.StateHash = hash 256 } 257 set.KV = append(set.KV, kv) 258 259 if i > 0 && i%1000 == 0 { 260 setwithsync := &types.StoreSetWithSync{Storeset: set, Sync: true} 261 msg := client.NewMessage("store", types.EventStoreSet, setwithsync) 262 client.Send(msg, true) 263 msg, err := client.Wait(msg) 264 if err != nil { 265 b.Error(err) 266 return 267 } 268 hash = msg.GetData().(*types.ReplyHash).GetHash() 269 } 270 } 271 s.Close() 272 } 273 274 var storecfg1 = &types.Store{Name: "mavl", Driver: "leveldb", DbPath: "/tmp/store_test1", DbCache: 100} 275 276 func TestNewMavl(t *testing.T) { 277 os.RemoveAll(storecfg1.DbPath) 278 cfg := types.NewTuringchainConfig(types.ReadFile("../cmd/turingchain/turingchain.test.toml")) 279 store := New(cfg) 280 assert.NotNil(t, store) 281 }