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