2020-04-26 07:15:59 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-06-05 16:12:10 +00:00
|
|
|
"errors"
|
2020-04-26 07:15:59 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-04-26 07:15:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxValidatorsVoted limits the number of validators that one can vote for.
|
|
|
|
const MaxValidatorsVoted = 1024
|
|
|
|
|
|
|
|
// ValidatorsCount represents votes with particular number of consensus nodes
|
|
|
|
// for this number to be changeable by the voting system.
|
|
|
|
type ValidatorsCount [MaxValidatorsVoted]big.Int
|
|
|
|
|
|
|
|
// ValidatorsCountFromBytes converts serialized ValidatorsCount to structure.
|
|
|
|
func ValidatorsCountFromBytes(b []byte) (*ValidatorsCount, error) {
|
|
|
|
vc := new(ValidatorsCount)
|
|
|
|
if len(b) == 0 {
|
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
vc.DecodeBinary(r)
|
|
|
|
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns serialized ValidatorsCount.
|
|
|
|
func (vc *ValidatorsCount) Bytes() []byte {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
vc.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
|
|
|
}
|
|
|
|
return w.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (vc *ValidatorsCount) EncodeBinary(w *io.BinWriter) {
|
2020-06-05 16:12:10 +00:00
|
|
|
w.WriteVarUint(uint64(MaxValidatorsVoted))
|
2020-04-26 07:15:59 +00:00
|
|
|
for i := range vc {
|
2020-06-04 18:11:27 +00:00
|
|
|
w.WriteVarBytes(bigint.ToBytes(&vc[i]))
|
2020-04-26 07:15:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (vc *ValidatorsCount) DecodeBinary(r *io.BinReader) {
|
2020-06-05 16:12:10 +00:00
|
|
|
count := r.ReadVarUint()
|
|
|
|
if count < 0 || count > MaxValidatorsVoted {
|
|
|
|
r.Err = errors.New("invalid validators count")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < int(count); i++ {
|
2020-04-26 07:15:59 +00:00
|
|
|
buf := r.ReadVarBytes()
|
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-04 18:11:27 +00:00
|
|
|
vc[i] = *bigint.FromBytes(buf)
|
2020-04-26 07:15:59 +00:00
|
|
|
}
|
|
|
|
}
|