From feb6ba2ef7deba37ce45075cb1a483cb42791359 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Fri, 15 Jan 2021 11:00:39 +0300 Subject: [PATCH 1/4] io: allow to restrict string size --- pkg/io/binaryReader.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/io/binaryReader.go b/pkg/io/binaryReader.go index b8c935c80..6915f5109 100644 --- a/pkg/io/binaryReader.go +++ b/pkg/io/binaryReader.go @@ -193,7 +193,7 @@ func (r *BinReader) ReadBytes(buf []byte) { } // ReadString calls ReadVarBytes and casts the results as a string. -func (r *BinReader) ReadString() string { - b := r.ReadVarBytes() +func (r *BinReader) ReadString(maxSize ...int) string { + b := r.ReadVarBytes(maxSize...) return string(b) } From 62a11807a88165a49486431e1d66124cfed10f44 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 22 Jan 2021 18:48:24 +0300 Subject: [PATCH 2/4] fixedn: always correctly unmarshal Fixed8 values Quoted or not, they should be unmarshalled without going through float64. --- pkg/util/fixed8.go | 37 ++++++++++++++++--------------------- pkg/util/fixed8_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/pkg/util/fixed8.go b/pkg/util/fixed8.go index c11badae7..676286370 100644 --- a/pkg/util/fixed8.go +++ b/pkg/util/fixed8.go @@ -1,7 +1,6 @@ package util import ( - "encoding/json" "errors" "math" "strconv" @@ -106,34 +105,30 @@ func FixedNFromString(s string, precision int) (int64, error) { // UnmarshalJSON implements the json unmarshaller interface. func (f *Fixed8) UnmarshalJSON(data []byte) error { - return f.unmarshalHelper(func(v interface{}) error { - return json.Unmarshal(data, v) - }) + if len(data) > 2 { + if data[0] == '"' && data[len(data)-1] == '"' { + data = data[1 : len(data)-1] + } + } + return f.setFromString(string(data)) } // UnmarshalYAML implements the yaml unmarshaler interface. func (f *Fixed8) UnmarshalYAML(unmarshal func(interface{}) error) error { - return f.unmarshalHelper(unmarshal) -} - -// unmarshalHelper is an underlying unmarshaller func for JSON and YAML. -func (f *Fixed8) unmarshalHelper(unmarshal func(interface{}) error) error { var s string - if err := unmarshal(&s); err == nil { - p, err := Fixed8FromString(s) - if err != nil { - return err - } - *f = p - return nil - } - - var fl float64 - if err := unmarshal(&fl); err != nil { + err := unmarshal(&s) + if err != nil { return err } + return f.setFromString(s) +} - *f = Fixed8(decimals * fl) +func (f *Fixed8) setFromString(s string) error { + p, err := Fixed8FromString(s) + if err != nil { + return err + } + *f = p return nil } diff --git a/pkg/util/fixed8_test.go b/pkg/util/fixed8_test.go index cd097c9b5..3f6f12151 100644 --- a/pkg/util/fixed8_test.go +++ b/pkg/util/fixed8_test.go @@ -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) { u, err := Fixed8FromString("123.4") assert.NoError(t, err) From d1e02c393d733d001388f478c4ab27218d61c912 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 4 Feb 2021 18:48:09 +0300 Subject: [PATCH 3/4] consensus: only use previous proposal if it has something in it It might just be uninitialized it doesn't really make sense using zero-length previous proposal anyway. --- pkg/consensus/consensus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index cca8d060e..ad1aa8e3d 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -553,7 +553,7 @@ func (s *service) getVerifiedTx() []block.Transaction { 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)) for i := range s.lastProposal { if tx, fee, ok := pool.TryGetValue(s.lastProposal[i]); ok { From 5ff57e890b545c50cffb037aa39b65edb79beb3f Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 4 Feb 2021 18:54:01 +0300 Subject: [PATCH 4/4] consensus: flush previous proposal on new block Reusing proposals from previous blocks doesn't make sense. And reduce some code duplication along the way. --- pkg/consensus/consensus.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index ad1aa8e3d..1343cf2c6 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -229,6 +229,7 @@ func (s *service) eventLoop() { s.log.Debug("new block in the chain", zap.Uint32("dbft index", s.dbft.BlockIndex), zap.Uint32("chain index", s.Chain.BlockHeight())) + s.lastProposal = nil 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.lastProposal = nil } func (s *service) getBlockWitness(_ *coreb.Block) *transaction.Witness {