Merge pull request #1761 from nspcc-dev/backport-from-3.0

[2.x] Backport from 3.0
This commit is contained in:
Roman Khimov 2021-02-19 09:11:27 +03:00 committed by GitHub
commit 1179fdb44d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 24 deletions

View file

@ -229,6 +229,7 @@ func (s *service) eventLoop() {
s.log.Debug("new block in the chain", s.log.Debug("new block in the chain",
zap.Uint32("dbft index", s.dbft.BlockIndex), zap.Uint32("dbft index", s.dbft.BlockIndex),
zap.Uint32("chain index", s.Chain.BlockHeight())) zap.Uint32("chain index", s.Chain.BlockHeight()))
s.lastProposal = nil
s.dbft.InitializeConsensus(0) s.dbft.InitializeConsensus(0)
} }
} }
@ -492,6 +493,7 @@ func (s *service) processBlock(b block.Block) {
s.log.Warn("error on add block", zap.Error(err)) s.log.Warn("error on add block", zap.Error(err))
} }
} }
s.lastProposal = nil
} }
func (s *service) getBlockWitness(_ *coreb.Block) *transaction.Witness { func (s *service) getBlockWitness(_ *coreb.Block) *transaction.Witness {
@ -553,7 +555,7 @@ func (s *service) getVerifiedTx() []block.Transaction {
var txx []mempool.TxWithFee var txx []mempool.TxWithFee
if s.dbft.ViewNumber > 0 { if s.dbft.ViewNumber > 0 && len(s.lastProposal) > 0 {
txx = make([]mempool.TxWithFee, 0, len(s.lastProposal)) txx = make([]mempool.TxWithFee, 0, len(s.lastProposal))
for i := range s.lastProposal { for i := range s.lastProposal {
if tx, fee, ok := pool.TryGetValue(s.lastProposal[i]); ok { if tx, fee, ok := pool.TryGetValue(s.lastProposal[i]); ok {

View file

@ -193,7 +193,7 @@ func (r *BinReader) ReadBytes(buf []byte) {
} }
// ReadString calls ReadVarBytes and casts the results as a string. // ReadString calls ReadVarBytes and casts the results as a string.
func (r *BinReader) ReadString() string { func (r *BinReader) ReadString(maxSize ...int) string {
b := r.ReadVarBytes() b := r.ReadVarBytes(maxSize...)
return string(b) return string(b)
} }

View file

@ -1,7 +1,6 @@
package util package util
import ( import (
"encoding/json"
"errors" "errors"
"math" "math"
"strconv" "strconv"
@ -106,20 +105,25 @@ func FixedNFromString(s string, precision int) (int64, error) {
// UnmarshalJSON implements the json unmarshaller interface. // UnmarshalJSON implements the json unmarshaller interface.
func (f *Fixed8) UnmarshalJSON(data []byte) error { func (f *Fixed8) UnmarshalJSON(data []byte) error {
return f.unmarshalHelper(func(v interface{}) error { if len(data) > 2 {
return json.Unmarshal(data, v) if data[0] == '"' && data[len(data)-1] == '"' {
}) data = data[1 : len(data)-1]
}
}
return f.setFromString(string(data))
} }
// UnmarshalYAML implements the yaml unmarshaler interface. // UnmarshalYAML implements the yaml unmarshaler interface.
func (f *Fixed8) UnmarshalYAML(unmarshal func(interface{}) error) error { func (f *Fixed8) UnmarshalYAML(unmarshal func(interface{}) error) error {
return f.unmarshalHelper(unmarshal) var s string
err := unmarshal(&s)
if err != nil {
return err
}
return f.setFromString(s)
} }
// unmarshalHelper is an underlying unmarshaller func for JSON and YAML. func (f *Fixed8) setFromString(s string) error {
func (f *Fixed8) unmarshalHelper(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err == nil {
p, err := Fixed8FromString(s) p, err := Fixed8FromString(s)
if err != nil { if err != nil {
return err return err
@ -128,15 +132,6 @@ func (f *Fixed8) unmarshalHelper(unmarshal func(interface{}) error) error {
return nil return nil
} }
var fl float64
if err := unmarshal(&fl); err != nil {
return err
}
*f = Fixed8(decimals * fl)
return nil
}
// MarshalJSON implements the json marshaller interface. // MarshalJSON implements the json marshaller interface.
func (f Fixed8) MarshalJSON() ([]byte, error) { func (f Fixed8) MarshalJSON() ([]byte, error) {
return []byte(`"` + f.String() + `"`), nil return []byte(`"` + f.String() + `"`), nil

View file

@ -138,6 +138,19 @@ func TestFixed8UnmarshalJSON(t *testing.T) {
} }
} }
func TestFixed8_Unmarshal(t *testing.T) {
var expected = Fixed8(223719420)
var cases = []string{"2.2371942", `"2.2371942"`} // this easily gives 223719419 if interpreted as float
for _, c := range cases {
var u1, u2 Fixed8
assert.Nil(t, json.Unmarshal([]byte(c), &u1))
assert.Equal(t, expected, u1)
assert.Nil(t, yaml.Unmarshal([]byte(c), &u2))
assert.Equal(t, expected, u2)
}
}
func TestFixed8_MarshalJSON(t *testing.T) { func TestFixed8_MarshalJSON(t *testing.T) {
u, err := Fixed8FromString("123.4") u, err := Fixed8FromString("123.4")
assert.NoError(t, err) assert.NoError(t, err)