From 8cccd2cdbfd47b35868eacf75dda640860757cc2 Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Thu, 20 Jun 2024 17:05:59 +0100 Subject: [PATCH] Fix tipset stringer max print limit and refine ec chain stringer Fix bug where tipset stringer would panic if its length is less than 16 bytes. Refine `ECChain` stringer to skip building the string value if max length is reached, instead of fully build then truncate. --- gpbft/chain.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/gpbft/chain.go b/gpbft/chain.go index 3c713c30..4d1a20c7 100644 --- a/gpbft/chain.go +++ b/gpbft/chain.go @@ -112,7 +112,7 @@ func (ts *TipSet) String() string { } encTs := base32.StdEncoding.EncodeToString(ts.Key) - return fmt.Sprintf("%s@%d", encTs[:max(16, len(encTs))], ts.Epoch) + return fmt.Sprintf("%s@%d", encTs[:min(16, len(encTs))], ts.Epoch) } // A chain of tipsets comprising a base (the last finalised tipset from which the chain extends). @@ -300,6 +300,9 @@ func (c ECChain) Key() ChainKey { } func (c ECChain) String() string { + if len(c) == 0 { + return "δΈ„" + } var b strings.Builder b.WriteString("[") for i := range c { @@ -307,11 +310,11 @@ func (c ECChain) String() string { if i < len(c)-1 { b.WriteString(", ") } + if b.Len() > 77 { + b.WriteString("...") + break + } } b.WriteString("]") - str := b.String() - if len(str) > 77 { - str = str[:77] + "..." - } - return str + return b.String() }