2015-02-25 14:34:12 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-02-25 14:34:12 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2015-03-20 01:14:08 +02:00
|
|
|
// hashPool is a test helper, that allows random hashes to be referred to by integers
|
|
|
|
type TestHashPool struct {
|
|
|
|
intToHash
|
|
|
|
hashToInt
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
2015-02-25 14:34:12 +02:00
|
|
|
|
|
|
|
func NewHashPool() *TestHashPool {
|
|
|
|
return &TestHashPool{intToHash: make(intToHash), hashToInt: make(hashToInt)}
|
|
|
|
}
|
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
type intToHash map[int]common.Hash
|
2015-02-25 14:34:12 +02:00
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
type hashToInt map[common.Hash]int
|
2015-02-25 14:34:12 +02:00
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
func newHash(i int) common.Hash {
|
|
|
|
return common.BytesToHash(crypto.Sha3([]byte(string(i))))
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
func (self *TestHashPool) IndexesToHashes(indexes []int) (hashes []common.Hash) {
|
2015-02-25 14:34:12 +02:00
|
|
|
self.lock.Lock()
|
|
|
|
defer self.lock.Unlock()
|
|
|
|
for _, i := range indexes {
|
|
|
|
hash, found := self.intToHash[i]
|
|
|
|
if !found {
|
|
|
|
hash = newHash(i)
|
|
|
|
self.intToHash[i] = hash
|
2015-03-20 00:46:54 +02:00
|
|
|
self.hashToInt[hash] = i
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-20 00:46:54 +02:00
|
|
|
func (self *TestHashPool) HashesToIndexes(hashes []common.Hash) (indexes []int) {
|
2015-02-25 14:34:12 +02:00
|
|
|
self.lock.Lock()
|
|
|
|
defer self.lock.Unlock()
|
|
|
|
for _, hash := range hashes {
|
2015-03-20 00:46:54 +02:00
|
|
|
i, found := self.hashToInt[hash]
|
2015-02-25 14:34:12 +02:00
|
|
|
if !found {
|
|
|
|
i = -1
|
|
|
|
}
|
|
|
|
indexes = append(indexes, i)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|