From 5f949c0efe789ab8099e7eb33d20fcee5b9d9243 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 26 Jun 2023 18:48:36 +0300 Subject: [PATCH] ir: Protect Fixed8Converter with `sync.Mutex` It will allow real time updating. Signed-off-by: Pavel Karpy --- pkg/util/precision/converter.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/util/precision/converter.go b/pkg/util/precision/converter.go index bd2948f0a42..d904c797424 100644 --- a/pkg/util/precision/converter.go +++ b/pkg/util/precision/converter.go @@ -3,6 +3,7 @@ package precision import ( "math" "math/big" + "sync" ) type ( @@ -21,6 +22,7 @@ type ( // This is a JSON bound that uses neo node. Neo-go has int64 limit for // `smartcontract.Parameter` of integer type. Fixed8Converter struct { + m *sync.RWMutex converter } ) @@ -56,11 +58,17 @@ func (c converter) toBase(n *big.Int) *big.Int { // ToFixed8 converts n of balance contract precision to Fixed8 precision. func (c Fixed8Converter) ToFixed8(n int64) int64 { + c.m.RLock() + defer c.m.RUnlock() + return c.toBase(new(big.Int).SetInt64(n)).Int64() } // ToBalancePrecision converts n of Fixed8 precision to balance contract precision. func (c Fixed8Converter) ToBalancePrecision(n int64) int64 { + c.m.RLock() + defer c.m.RUnlock() + return c.toTarget(new(big.Int).SetInt64(n)).Int64() } @@ -71,6 +79,9 @@ func (c *Fixed8Converter) SetBalancePrecision(precision uint32) { exp = -exp } + c.m.Lock() + defer c.m.Unlock() + c.base = fixed8Precision c.target = precision c.factor = new(big.Int).SetInt64(int64(math.Pow10(exp)))