diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-12-27 09:17:28 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:55 +0800 |
commit | f79d09a12c8de2e1572292ef6bbd82352526930d (patch) | |
tree | 0ec9ce7fba237187b6d5b88b9401ad36798f7fe1 /dex/cache_test.go | |
parent | 509c6899caad7a66f7e64a1ef9718daa9018f7f1 (diff) | |
download | dexon-f79d09a12c8de2e1572292ef6bbd82352526930d.tar.gz dexon-f79d09a12c8de2e1572292ef6bbd82352526930d.tar.zst dexon-f79d09a12c8de2e1572292ef6bbd82352526930d.zip |
dex: add pull randomness (#105)
* vendor: sync to latest core
* dex: Add PullRandomness
Diffstat (limited to 'dex/cache_test.go')
-rw-r--r-- | dex/cache_test.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/dex/cache_test.go b/dex/cache_test.go index 3b43e77aa..536e015f0 100644 --- a/dex/cache_test.go +++ b/dex/cache_test.go @@ -18,6 +18,7 @@ package dex import ( + "math/rand" "sort" "strings" "testing" @@ -203,3 +204,92 @@ func TestCacheBlock(t *testing.T) { } } } + +func randomBytes() []byte { + bytes := make([]byte, 32) + for i := range bytes { + bytes[i] = byte(rand.Int() % 256) + } + return bytes +} + +func TestCacheRandomness(t *testing.T) { + db, err := coreDb.NewMemBackedDB() + if err != nil { + panic(err) + } + cache := newCache(3, db) + rand1 := &coreTypes.BlockRandomnessResult{ + BlockHash: coreCommon.NewRandomHash(), + Randomness: randomBytes(), + } + rand2 := &coreTypes.BlockRandomnessResult{ + BlockHash: coreCommon.NewRandomHash(), + Randomness: randomBytes(), + } + rand3 := &coreTypes.BlockRandomnessResult{ + BlockHash: coreCommon.NewRandomHash(), + Randomness: randomBytes(), + } + rand4 := &coreTypes.BlockRandomnessResult{ + BlockHash: coreCommon.NewRandomHash(), + Randomness: randomBytes(), + } + cache.addRandomness(rand1) + cache.addRandomness(rand2) + cache.addRandomness(rand3) + + hashes := coreCommon.Hashes{rand1.BlockHash, rand2.BlockHash, rand3.BlockHash, rand4.BlockHash} + hashMap := map[coreCommon.Hash]struct{}{ + rand1.BlockHash: {}, + rand2.BlockHash: {}, + rand3.BlockHash: {}, + } + rands := cache.randomness(hashes) + if len(rands) != 3 { + t.Errorf("fail to get rands: have %d, want 3", len(rands)) + } + for _, rand := range rands { + if _, exist := hashMap[rand.BlockHash]; !exist { + t.Errorf("get wrong rand: have %s, want %v", rand, hashMap) + } + } + + cache.addRandomness(rand4) + + rands = cache.randomness(hashes) + hashMap[rand4.BlockHash] = struct{}{} + if len(rands) != 3 { + t.Errorf("fail to get rands: have %d, want 3", len(rands)) + } + hasNewRandomness := false + for _, rand := range rands { + if _, exist := hashMap[rand.BlockHash]; !exist { + t.Errorf("get wrong rand: have %s, want %v", rand, hashMap) + } + if rand.BlockHash.Equal(rand4.BlockHash) { + hasNewRandomness = true + } + } + if !hasNewRandomness { + t.Errorf("expect rand %s in cache, have %v", rand4, rands) + } + + block := &coreTypes.Block{ + Hash: coreCommon.NewRandomHash(), + Finalization: coreTypes.FinalizationResult{ + Randomness: randomBytes(), + }, + } + if err := db.PutBlock(*block); err != nil { + panic(err) + } + rands = cache.randomness(coreCommon.Hashes{block.Hash}) + if len(rands) != 1 { + t.Errorf("fail to get rands: have %d, want 1", len(rands)) + } else { + if !rands[0].BlockHash.Equal(block.Hash) { + t.Errorf("get wrong rand: have %s, want %s", rands[0], block) + } + } +} |