From fccad11716ed4117a9b9b654d7ac3391e5e707d8 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 23 Jun 2020 21:57:05 +0300 Subject: [PATCH] native: drop accounts with zero balance They make no sense. Fixes preview2 testnet state problem: file BlockStorage_100000/dump-block-70000.json: block 69935: state mismatch for key ffffffff1454a6cb279fbcedc66162ad4ad5d1d910202b92743e000000000000000000000005: Deleted vs Added --- pkg/core/native/native_gas.go | 6 +++++- pkg/core/native/native_neo.go | 6 +++++- pkg/core/native/native_nep5.go | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 000f365d1..08683863a 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -58,7 +58,11 @@ func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.Stor return errors.New("insufficient funds") } acc.Balance.Add(&acc.Balance, amount) - si.Value = acc.Bytes() + if acc.Balance.Sign() != 0 { + si.Value = acc.Bytes() + } else { + si.Value = nil + } return nil } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index fcd256137..55c119b3f 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -180,7 +180,11 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto } } acc.Balance.Add(&acc.Balance, amount) - si.Value = acc.Bytes() + if acc.Balance.Sign() != 0 { + si.Value = acc.Bytes() + } else { + si.Value = nil + } return nil } diff --git a/pkg/core/native/native_nep5.go b/pkg/core/native/native_nep5.go index 597a407e0..a1db0ee7d 100644 --- a/pkg/core/native/native_nep5.go +++ b/pkg/core/native/native_nep5.go @@ -180,7 +180,12 @@ func (c *nep5TokenNative) transfer(ic *interop.Context, from, to util.Uint160, a if err := c.incBalance(ic, from, siFrom, inc); err != nil { return err } - if err := ic.DAO.PutStorageItem(c.ContractID, keyFrom, siFrom); err != nil { + if siFrom.Value == nil { + err = ic.DAO.DeleteStorageItem(c.ContractID, keyFrom) + } else { + err = ic.DAO.PutStorageItem(c.ContractID, keyFrom, siFrom) + } + if err != nil { return err } @@ -193,7 +198,12 @@ func (c *nep5TokenNative) transfer(ic *interop.Context, from, to util.Uint160, a if err := c.incBalance(ic, to, siTo, amount); err != nil { return err } - if err := ic.DAO.PutStorageItem(c.ContractID, keyTo, siTo); err != nil { + if siTo.Value == nil { + err = ic.DAO.DeleteStorageItem(c.ContractID, keyTo) + } else { + err = ic.DAO.PutStorageItem(c.ContractID, keyTo, siTo) + } + if err != nil { return err } }