Skip to content

Commit

Permalink
ir: Protect Fixed8Converter with sync.Mutex
Browse files Browse the repository at this point in the history
It will allow real time updating.

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
notimetoname committed Jun 26, 2023
1 parent 33ddd78 commit 5f949c0
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/util/precision/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package precision
import (
"math"
"math/big"
"sync"
)

type (
Expand All @@ -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
}
)
Expand Down Expand Up @@ -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()
}

Expand All @@ -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)))
Expand Down

0 comments on commit 5f949c0

Please sign in to comment.