state: implement SignedDataSource on ChangeStateRequest message

This commit is contained in:
Leonard Lyubich 2020-05-11 16:31:39 +03:00
parent 5545b25a95
commit ab198b4049
4 changed files with 89 additions and 0 deletions

View file

@ -1,5 +1,9 @@
package state
import (
"io"
)
// SignedData returns payload bytes of the request.
//
// Always returns an empty slice.
@ -34,3 +38,34 @@ func (m DumpRequest) SignedData() ([]byte, error) {
func (m DumpVarsRequest) SignedData() ([]byte, error) {
return make([]byte, 0), nil
}
// SignedData returns payload bytes of the request.
func (m ChangeStateRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize())
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
}
// SignedDataSize returns payload size of the request.
func (m ChangeStateRequest) SignedDataSize() int {
return m.GetState().Size()
}
// ReadSignedData copies payload bytes to passed buffer.
//
// If the Request size is insufficient, io.ErrUnexpectedEOF returns.
func (m ChangeStateRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() {
return 0, io.ErrUnexpectedEOF
}
var off int
off += copy(p[off:], m.GetState().Bytes())
return off, nil
}