2018-08-10 14:32:49 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2020-12-04 11:59:10 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-08-10 14:32:49 +00:00
|
|
|
)
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// MerkleBlock represents a merkle block packet payload.
|
2018-08-10 14:32:49 +00:00
|
|
|
type MerkleBlock struct {
|
2021-03-01 13:44:47 +00:00
|
|
|
*block.Header
|
2018-08-10 14:32:49 +00:00
|
|
|
TxCount int
|
|
|
|
Hashes []util.Uint256
|
|
|
|
Flags []byte
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the Serializable interface.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (m *MerkleBlock) DecodeBinary(br *io.BinReader) {
|
2021-03-25 18:46:52 +00:00
|
|
|
m.Header = &block.Header{}
|
2021-03-01 13:44:47 +00:00
|
|
|
m.Header.DecodeBinary(br)
|
2018-08-10 14:32:49 +00:00
|
|
|
|
2020-10-02 14:25:55 +00:00
|
|
|
txCount := int(br.ReadVarUint())
|
2021-03-01 13:49:24 +00:00
|
|
|
if txCount > block.MaxTransactionsPerBlock {
|
2020-10-02 14:25:55 +00:00
|
|
|
br.Err = block.ErrMaxContentsPerBlock
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.TxCount = txCount
|
|
|
|
br.ReadArray(&m.Hashes, m.TxCount)
|
2020-12-04 11:59:10 +00:00
|
|
|
if txCount != len(m.Hashes) {
|
|
|
|
br.Err = errors.New("invalid tx count")
|
|
|
|
}
|
2020-10-02 16:30:29 +00:00
|
|
|
m.Flags = br.ReadVarBytes((txCount + 7) / 8)
|
2018-08-10 14:32:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (m *MerkleBlock) EncodeBinary(bw *io.BinWriter) {
|
2021-03-01 13:44:47 +00:00
|
|
|
m.Header.EncodeBinary(bw)
|
2019-09-16 16:49:48 +00:00
|
|
|
|
|
|
|
bw.WriteVarUint(uint64(m.TxCount))
|
2019-11-14 08:07:23 +00:00
|
|
|
bw.WriteArray(m.Hashes)
|
2019-11-22 10:34:06 +00:00
|
|
|
bw.WriteVarBytes(m.Flags)
|
2018-08-10 14:32:49 +00:00
|
|
|
}
|