2016-12-13 23:59:48 +01:00
|
|
|
package shachain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2017-02-23 04:13:56 +01:00
|
|
|
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2016-12-13 23:59:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// getBit return bit on index at position.
|
|
|
|
func getBit(index index, position uint8) uint8 {
|
|
|
|
return uint8((uint64(index) >> position) & 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPrefix(index index, position uint8) uint64 {
|
|
|
|
// + -------------------------- +
|
|
|
|
// | № | value | mask | return |
|
|
|
|
// + -- + ----- + ---- + ------ +
|
|
|
|
// | 63 | 1 | 0 | 0 |
|
|
|
|
// | 62 | 0 | 0 | 0 |
|
|
|
|
// | 61 | 1 | 0 | 0 |
|
|
|
|
// ....
|
|
|
|
// | 4 | 1 | 0 | 0 |
|
|
|
|
// | 3 | 1 | 0 | 0 |
|
|
|
|
// | 2 | 1 | 1 | 1 | <--- position
|
|
|
|
// | 1 | 0 | 1 | 0 |
|
|
|
|
// | 0 | 1 | 1 | 1 |
|
|
|
|
// + -- + ----- + ---- + ------ +
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
var zero uint64
|
2016-12-13 23:59:48 +01:00
|
|
|
mask := (zero - 1) - uint64((1<<position)-1)
|
|
|
|
return (uint64(index) & mask)
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:03:27 +02:00
|
|
|
// countTrailingZeros counts number of trailing zero bits, this function is
|
2016-12-13 23:59:48 +01:00
|
|
|
// used to determine the number of element bucket.
|
|
|
|
func countTrailingZeros(index index) uint8 {
|
2017-02-23 20:56:47 +01:00
|
|
|
var zeros uint8
|
2016-12-13 23:59:48 +01:00
|
|
|
for ; zeros < maxHeight; zeros++ {
|
|
|
|
|
|
|
|
if getBit(index, zeros) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zeros
|
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:11 +01:00
|
|
|
// hashFromString takes a hex-encoded string as input and creates an instance of
|
2017-02-23 04:13:56 +01:00
|
|
|
// chainhash.Hash. The chainhash.NewHashFromStr function not suitable because
|
|
|
|
// it reverse the given hash.
|
2016-12-13 23:59:48 +01:00
|
|
|
func hashFromString(s string) (*chainhash.Hash, error) {
|
2017-02-23 04:13:56 +01:00
|
|
|
// Return an error if hash string is too long.
|
2016-12-13 23:59:48 +01:00
|
|
|
if len(s) > chainhash.MaxHashStringSize {
|
|
|
|
return nil, chainhash.ErrHashStrSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hex decoder expects the hash to be a multiple of two.
|
|
|
|
if len(s)%2 != 0 {
|
|
|
|
s = "0" + s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert string hash to bytes.
|
|
|
|
buf, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := chainhash.NewHash(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash, nil
|
|
|
|
}
|