2018-03-09 15:55:25 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A HeaderHashList represents a list of header hashes.
|
2018-03-14 09:36:59 +00:00
|
|
|
// This datastructure in not routine safe and should be
|
|
|
|
// used under some kind of protection against race conditions.
|
2018-03-09 15:55:25 +00:00
|
|
|
type HeaderHashList struct {
|
|
|
|
hashes []util.Uint256
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// NewHeaderHashListFromBytes return a new hash list from the given bytes.
|
|
|
|
func NewHeaderHashListFromBytes(b []byte) (*HeaderHashList, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// NewHeaderHashList return a new pointer to a HeaderHashList.
|
|
|
|
func NewHeaderHashList(hashes ...util.Uint256) *HeaderHashList {
|
|
|
|
return &HeaderHashList{
|
|
|
|
hashes: hashes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add appends the given hash to the list of hashes.
|
2018-03-17 11:53:21 +00:00
|
|
|
func (l *HeaderHashList) Add(h ...util.Uint256) {
|
|
|
|
l.hashes = append(l.hashes, h...)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len return the length of the underlying hashes slice.
|
|
|
|
func (l *HeaderHashList) Len() int {
|
|
|
|
return len(l.hashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the hash by the given index.
|
|
|
|
func (l *HeaderHashList) Get(i int) util.Uint256 {
|
2018-03-14 09:36:59 +00:00
|
|
|
if l.Len() <= i {
|
2018-03-09 15:55:25 +00:00
|
|
|
return util.Uint256{}
|
|
|
|
}
|
|
|
|
return l.hashes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last return the last hash in the HeaderHashList.
|
|
|
|
func (l *HeaderHashList) Last() util.Uint256 {
|
|
|
|
return l.hashes[l.Len()-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice return a subslice of the underlying hashes.
|
|
|
|
// Subsliced from start to end.
|
|
|
|
// Example:
|
2018-03-17 11:53:21 +00:00
|
|
|
// headers := headerList.Slice(0, 2000)
|
2018-03-09 15:55:25 +00:00
|
|
|
func (l *HeaderHashList) Slice(start, end int) []util.Uint256 {
|
|
|
|
return l.hashes[start:end]
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// WriteTo will write n underlying hashes to the given BinWriter
|
2018-03-09 15:55:25 +00:00
|
|
|
// starting from start.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (l *HeaderHashList) Write(bw *io.BinWriter, start, n int) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(n))
|
2018-03-09 15:55:25 +00:00
|
|
|
hashes := l.Slice(start, start+n)
|
|
|
|
for _, hash := range hashes {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(hash)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
return bw.Err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|