2015-02-25 12:34:12 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-02-25 12:34:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2015-03-19 23:14:08 +00: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 12:34:12 +00:00
|
|
|
|
|
|
|
func NewHashPool() *TestHashPool {
|
|
|
|
return &TestHashPool{intToHash: make(intToHash), hashToInt: make(hashToInt)}
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
type intToHash map[int]common.Hash
|
2015-02-25 12:34:12 +00:00
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
type hashToInt map[common.Hash]int
|
2015-02-25 12:34:12 +00:00
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
func newHash(i int) common.Hash {
|
|
|
|
return common.BytesToHash(crypto.Sha3([]byte(string(i))))
|
2015-02-25 12:34:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
func (self *TestHashPool) IndexesToHashes(indexes []int) (hashes []common.Hash) {
|
2015-02-25 12:34:12 +00: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-19 22:46:54 +00:00
|
|
|
self.hashToInt[hash] = i
|
2015-02-25 12:34:12 +00:00
|
|
|
}
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:46:54 +00:00
|
|
|
func (self *TestHashPool) HashesToIndexes(hashes []common.Hash) (indexes []int) {
|
2015-02-25 12:34:12 +00:00
|
|
|
self.lock.Lock()
|
|
|
|
defer self.lock.Unlock()
|
|
|
|
for _, hash := range hashes {
|
2015-03-19 22:46:54 +00:00
|
|
|
i, found := self.hashToInt[hash]
|
2015-02-25 12:34:12 +00:00
|
|
|
if !found {
|
|
|
|
i = -1
|
|
|
|
}
|
|
|
|
indexes = append(indexes, i)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|