diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 159e7b152..3f1159a77 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -108,7 +108,8 @@ func (g *GAS) Initialize(ic *interop.Context) error { if err := g.nep17TokenNative.Initialize(ic); err != nil { return err } - if g.nep17TokenNative.getTotalSupply(ic.DAO).Sign() != 0 { + _, totalSupply := g.nep17TokenNative.getTotalSupply(ic.DAO) + if totalSupply.Sign() != 0 { return errors.New("already initialized") } h, err := getStandbyValidatorsHash(ic) diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 644188aac..391525f1f 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -189,7 +189,8 @@ func (n *NEO) Initialize(ic *interop.Context) error { return err } - if n.nep17TokenNative.getTotalSupply(ic.DAO).Sign() != 0 { + _, totalSupply := n.nep17TokenNative.getTotalSupply(ic.DAO) + if totalSupply.Sign() != 0 { return errors.New("already initialized") } @@ -977,7 +978,8 @@ func (n *NEO) computeCommitteeMembers(bc blockchainer.Blockchainer, d dao.DAO) ( votersCount := bigint.FromBytes(si) // votersCount / totalSupply must be >= 0.2 votersCount.Mul(votersCount, big.NewInt(effectiveVoterTurnout)) - voterTurnout := votersCount.Div(votersCount, n.getTotalSupply(d)) + _, totalSupply := n.getTotalSupply(d) + voterTurnout := votersCount.Div(votersCount, totalSupply) sbVals := bc.GetStandByCommittee() count := len(sbVals) diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index f08dd0aa1..3b2695fa8 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -95,19 +95,21 @@ func (c *nep17TokenNative) Decimals(_ *interop.Context, _ []stackitem.Item) stac } func (c *nep17TokenNative) TotalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item { - return stackitem.NewBigInteger(c.getTotalSupply(ic.DAO)) + _, supply := c.getTotalSupply(ic.DAO) + return stackitem.NewBigInteger(supply) } -func (c *nep17TokenNative) getTotalSupply(d dao.DAO) *big.Int { +func (c *nep17TokenNative) getTotalSupply(d dao.DAO) (state.StorageItem, *big.Int) { si := d.GetStorageItem(c.ID, totalSupplyKey) if si == nil { - return big.NewInt(0) + si = []byte{} } - return bigint.FromBytes(si) + return si, bigint.FromBytes(si) } -func (c *nep17TokenNative) saveTotalSupply(d dao.DAO, supply *big.Int) error { - return d.PutStorageItem(c.ID, totalSupplyKey, bigint.ToBytes(supply)) +func (c *nep17TokenNative) saveTotalSupply(d dao.DAO, si state.StorageItem, supply *big.Int) error { + si = state.StorageItem(bigint.ToPreallocatedBytes(supply, si)) + return d.PutStorageItem(c.ID, totalSupplyKey, si) } func (c *nep17TokenNative) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item { @@ -283,9 +285,9 @@ func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount panic(err) } - supply := c.getTotalSupply(ic.DAO) + buf, supply := c.getTotalSupply(ic.DAO) supply.Add(supply, amount) - err = c.saveTotalSupply(ic.DAO, supply) + err = c.saveTotalSupply(ic.DAO, buf, supply) if err != nil { panic(err) }