2018-02-04 19:54:51 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-08-29 17:11:03 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Headers payload
|
|
|
|
type Headers struct {
|
|
|
|
Hdrs []*core.Header
|
|
|
|
}
|
|
|
|
|
2019-08-29 17:11:03 +00:00
|
|
|
// Users can at most request 2k header
|
|
|
|
const (
|
|
|
|
maxHeadersAllowed = 2000
|
|
|
|
)
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (p *Headers) DecodeBinary(r io.Reader) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
lenHeaders := br.ReadVarUint()
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
|
|
|
}
|
2019-08-29 17:11:03 +00:00
|
|
|
// C# node does it silently
|
|
|
|
if lenHeaders > maxHeadersAllowed {
|
|
|
|
log.Warnf("received %d headers, capping to %d", lenHeaders, maxHeadersAllowed)
|
|
|
|
lenHeaders = maxHeadersAllowed
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
|
|
|
|
p.Hdrs = make([]*core.Header, lenHeaders)
|
|
|
|
|
|
|
|
for i := 0; i < int(lenHeaders); i++ {
|
|
|
|
header := &core.Header{}
|
|
|
|
if err := header.DecodeBinary(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Hdrs[i] = header
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
2018-02-07 14:16:50 +00:00
|
|
|
func (p *Headers) EncodeBinary(w io.Writer) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
bw := util.BinWriter{W: w}
|
|
|
|
bw.WriteVarUint(uint64(len(p.Hdrs)))
|
|
|
|
if bw.Err != nil {
|
|
|
|
return bw.Err
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2019-08-28 12:43:56 +00:00
|
|
|
|
2018-02-07 14:16:50 +00:00
|
|
|
for _, header := range p.Hdrs {
|
|
|
|
if err := header.EncodeBinary(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
return nil
|
|
|
|
}
|