2018-03-09 15:55:25 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A HeaderHashList represents a list of header hashes.
|
2019-10-22 14:56:03 +00:00
|
|
|
// This data structure in not routine safe and should be
|
2018-03-14 09:36:59 +00:00
|
|
|
// used under some kind of protection against race conditions.
|
2018-03-09 15:55:25 +00:00
|
|
|
type HeaderHashList struct {
|
|
|
|
hashes []util.Uint256
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewHeaderHashList returns a new pointer to a HeaderHashList.
|
2018-03-09 15:55:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Len returns the length of the underlying hashes slice.
|
2018-03-09 15:55:25 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// WriteTo writes 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 {
|
2020-08-11 17:30:34 +00:00
|
|
|
bw.WriteArray(l.hashes[start : start+n])
|
2019-08-28 16:27:06 +00:00
|
|
|
return bw.Err
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|