From b1034d8ed66f7b7269405e971a45ba2d3266e461 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 11 Aug 2020 20:42:02 +0300 Subject: [PATCH] state: drop Neo 2 Account and everything related It's substituted with NEP5Balances now. --- pkg/core/blockchain.go | 9 --- pkg/core/blockchainer/blockchainer.go | 1 - pkg/core/dao/cacheddao.go | 35 +-------- pkg/core/dao/cacheddao_test.go | 37 --------- pkg/core/dao/dao.go | 43 ---------- pkg/core/dao/dao_test.go | 19 ----- pkg/core/interop_neo_test.go | 14 ---- pkg/core/state/account.go | 108 -------------------------- pkg/core/state/account_test.go | 51 ------------ pkg/network/helper_test.go | 3 - 10 files changed, 1 insertion(+), 319 deletions(-) delete mode 100644 pkg/core/state/account.go delete mode 100644 pkg/core/state/account_test.go diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index ca2f676a0..842ce3fc3 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1034,15 +1034,6 @@ func (bc *Blockchain) GetContractScriptHash(id int32) (util.Uint160, error) { return bc.dao.GetContractScriptHash(id) } -// GetAccountState returns the account state from its script hash. -func (bc *Blockchain) GetAccountState(scriptHash util.Uint160) *state.Account { - as, err := bc.dao.GetAccountState(scriptHash) - if as == nil && err != storage.ErrKeyNotFound { - bc.log.Warn("failed to get account state", zap.Error(err)) - } - return as -} - // GetConfig returns the config stored in the blockchain. func (bc *Blockchain) GetConfig() config.ProtocolConfiguration { return bc.config diff --git a/pkg/core/blockchainer/blockchainer.go b/pkg/core/blockchainer/blockchainer.go index 6776d4756..34f4f74c0 100644 --- a/pkg/core/blockchainer/blockchainer.go +++ b/pkg/core/blockchainer/blockchainer.go @@ -37,7 +37,6 @@ type Blockchainer interface { CurrentBlockHash() util.Uint256 HasBlock(util.Uint256) bool HasTransaction(util.Uint256) bool - GetAccountState(util.Uint160) *state.Account GetAppExecResult(util.Uint256) (*state.AppExecResult, error) GetNextBlockValidators() ([]*keys.PublicKey, error) GetNEP5Balances(util.Uint160) *state.NEP5Balances diff --git a/pkg/core/dao/cacheddao.go b/pkg/core/dao/cacheddao.go index 123f8eda8..8c8bf1aa6 100644 --- a/pkg/core/dao/cacheddao.go +++ b/pkg/core/dao/cacheddao.go @@ -13,7 +13,6 @@ import ( // objects in the storeBlock(). type Cached struct { DAO - accounts map[util.Uint160]*state.Account contracts map[util.Uint160]*state.Contract balances map[util.Uint160]*state.NEP5Balances transfers map[util.Uint160]map[uint32]*state.NEP5TransferLog @@ -23,34 +22,10 @@ type Cached struct { // NewCached returns new Cached wrapping around given backing store. func NewCached(d DAO) *Cached { - accs := make(map[util.Uint160]*state.Account) ctrs := make(map[util.Uint160]*state.Contract) balances := make(map[util.Uint160]*state.NEP5Balances) transfers := make(map[util.Uint160]map[uint32]*state.NEP5TransferLog) - return &Cached{d.GetWrapped(), accs, ctrs, balances, transfers, false} -} - -// GetAccountStateOrNew retrieves Account from cache or underlying store -// or creates a new one if it doesn't exist. -func (cd *Cached) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) { - if cd.accounts[hash] != nil { - return cd.accounts[hash], nil - } - return cd.DAO.GetAccountStateOrNew(hash) -} - -// GetAccountState retrieves Account from cache or underlying store. -func (cd *Cached) GetAccountState(hash util.Uint160) (*state.Account, error) { - if cd.accounts[hash] != nil { - return cd.accounts[hash], nil - } - return cd.DAO.GetAccountState(hash) -} - -// PutAccountState saves given Account in the cache. -func (cd *Cached) PutAccountState(as *state.Account) error { - cd.accounts[as.ScriptHash] = as - return nil + return &Cached{d.GetWrapped(), ctrs, balances, transfers, false} } // GetContractState returns contract state from cache or underlying store. @@ -149,13 +124,6 @@ func (cd *Cached) Persist() (int, error) { } buf := io.NewBufBinWriter() - for sc := range cd.accounts { - err := cd.DAO.putAccountState(cd.accounts[sc], buf) - if err != nil { - return 0, err - } - buf.Reset() - } for acc, bs := range cd.balances { err := cd.DAO.putNEP5Balances(acc, bs, buf) if err != nil { @@ -177,7 +145,6 @@ func (cd *Cached) Persist() (int, error) { // GetWrapped implements DAO interface. func (cd *Cached) GetWrapped() DAO { return &Cached{cd.DAO.GetWrapped(), - cd.accounts, cd.contracts, cd.balances, cd.transfers, diff --git a/pkg/core/dao/cacheddao_test.go b/pkg/core/dao/cacheddao_test.go index b983da363..7170a46ed 100644 --- a/pkg/core/dao/cacheddao_test.go +++ b/pkg/core/dao/cacheddao_test.go @@ -13,43 +13,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestCachedDaoAccounts(t *testing.T) { - store := storage.NewMemoryStore() - // Persistent DAO to check for backing storage. - pdao := NewSimple(store, netmode.UnitTestNet) - // Cached DAO. - cdao := NewCached(pdao) - - hash := random.Uint160() - _, err := cdao.GetAccountState(hash) - require.NotNil(t, err) - - acc, err := cdao.GetAccountStateOrNew(hash) - require.Nil(t, err) - _, err = pdao.GetAccountState(hash) - require.NotNil(t, err) - - acc.Version = 42 - require.NoError(t, cdao.PutAccountState(acc)) - _, err = pdao.GetAccountState(hash) - require.NotNil(t, err) - - acc2, err := cdao.GetAccountState(hash) - require.Nil(t, err) - require.Equal(t, acc, acc2) - - acc2, err = cdao.GetAccountStateOrNew(hash) - require.Nil(t, err) - require.Equal(t, acc, acc2) - - _, err = cdao.Persist() - require.Nil(t, err) - - acct, err := pdao.GetAccountState(hash) - require.Nil(t, err) - require.Equal(t, acc, acct) -} - func TestCachedDaoContracts(t *testing.T) { store := storage.NewMemoryStore() pdao := NewSimple(store, netmode.UnitTestNet) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index 161e63ed4..efcc9f0cc 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -21,8 +21,6 @@ type DAO interface { AppendNEP5Transfer(acc util.Uint160, index uint32, tr *state.NEP5Transfer) (bool, error) DeleteContractState(hash util.Uint160) error DeleteStorageItem(id int32, key []byte) error - GetAccountState(hash util.Uint160) (*state.Account, error) - GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) GetAndDecode(entity io.Serializable, key []byte) error GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) GetBatch() *storage.MemBatch @@ -46,7 +44,6 @@ type DAO interface { GetWrapped() DAO HasTransaction(hash util.Uint256) bool Persist() (int, error) - PutAccountState(as *state.Account) error PutAppExecResult(aer *state.AppExecResult) error PutContractState(cs *state.Contract) error PutCurrentHeader(hashAndIndex []byte) error @@ -57,7 +54,6 @@ type DAO interface { StoreAsBlock(block *block.Block) error StoreAsCurrentBlock(block *block.Block) error StoreAsTransaction(tx *transaction.Transaction, index uint32) error - putAccountState(as *state.Account, buf *io.BufBinWriter) error putNEP5Balances(acc util.Uint160, bs *state.NEP5Balances, buf *io.BufBinWriter) error } @@ -112,45 +108,6 @@ func (dao *Simple) putWithBuffer(entity io.Serializable, key []byte, buf *io.Buf return dao.Store.Put(key, buf.Bytes()) } -// -- start accounts. - -// GetAccountStateOrNew retrieves Account from temporary or persistent Store -// or creates a new one if it doesn't exist and persists it. -func (dao *Simple) GetAccountStateOrNew(hash util.Uint160) (*state.Account, error) { - account, err := dao.GetAccountState(hash) - if err != nil { - if err != storage.ErrKeyNotFound { - return nil, err - } - account = state.NewAccount(hash) - } - return account, nil -} - -// GetAccountState returns Account from the given Store if it's -// present there. Returns nil otherwise. -func (dao *Simple) GetAccountState(hash util.Uint160) (*state.Account, error) { - account := &state.Account{} - key := storage.AppendPrefix(storage.STAccount, hash.BytesBE()) - err := dao.GetAndDecode(account, key) - if err != nil { - return nil, err - } - return account, err -} - -// PutAccountState saves given Account in given store. -func (dao *Simple) PutAccountState(as *state.Account) error { - return dao.putAccountState(as, io.NewBufBinWriter()) -} - -func (dao *Simple) putAccountState(as *state.Account, buf *io.BufBinWriter) error { - key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE()) - return dao.putWithBuffer(as, key, buf) -} - -// -- end accounts. - // -- start contracts. // GetContractState returns contract state as recorded in the given diff --git a/pkg/core/dao/dao_test.go b/pkg/core/dao/dao_test.go index a9760b3dc..64a85accd 100644 --- a/pkg/core/dao/dao_test.go +++ b/pkg/core/dao/dao_test.go @@ -41,25 +41,6 @@ func (t *TestSerializable) DecodeBinary(reader *io.BinReader) { t.field = reader.ReadString() } -func TestGetAccountStateOrNew_New(t *testing.T) { - dao := NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet) - hash := random.Uint160() - createdAccount, err := dao.GetAccountStateOrNew(hash) - require.NoError(t, err) - require.NotNil(t, createdAccount) -} - -func TestPutAndGetAccountStateOrNew(t *testing.T) { - dao := NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet) - hash := random.Uint160() - accountState := &state.Account{ScriptHash: hash} - err := dao.PutAccountState(accountState) - require.NoError(t, err) - gotAccount, err := dao.GetAccountStateOrNew(hash) - require.NoError(t, err) - require.Equal(t, accountState.ScriptHash, gotAccount.ScriptHash) -} - func TestPutAndGetContractState(t *testing.T) { dao := NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet) contractState := &state.Contract{Script: []byte{}} diff --git a/pkg/core/interop_neo_test.go b/pkg/core/interop_neo_test.go index 17c6e7635..582101c9c 100644 --- a/pkg/core/interop_neo_test.go +++ b/pkg/core/interop_neo_test.go @@ -29,8 +29,6 @@ import ( /* Missing tests: * TestTxGetWitnesses - * TestBcGetAccount - * TestAccountGetBalance * TestAccountIsStandard * TestCreateContractStateFromVM * TestContractCreate @@ -301,18 +299,6 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.C return v, contractState, context, chain } -func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interop.Context, *Blockchain) { - rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302" - hash, err := util.Uint160DecodeStringBE(rawHash) - accountState := state.NewAccount(hash) - - require.NoError(t, err) - chain := newTestChain(t) - context := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil) - v := context.SpawnVM() - return v, accountState, context, chain -} - func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) { script := []byte{byte(opcode.PUSH1), byte(opcode.RET)} tx := transaction.New(netmode.UnitTestNet, script, 0) diff --git a/pkg/core/state/account.go b/pkg/core/state/account.go deleted file mode 100644 index a0b82abd0..000000000 --- a/pkg/core/state/account.go +++ /dev/null @@ -1,108 +0,0 @@ -package state - -import ( - "github.com/nspcc-dev/neo-go/pkg/io" - "github.com/nspcc-dev/neo-go/pkg/util" -) - -// UnspentBalance contains input/output transactons that sum up into the -// account balance for the given asset. -type UnspentBalance struct { - Tx util.Uint256 `json:"txid"` - Index uint16 `json:"n"` - Value util.Fixed8 `json:"value"` -} - -// UnspentBalances is a slice of UnspentBalance (mostly needed to sort them). -type UnspentBalances []UnspentBalance - -// Account represents the state of a NEO account. -type Account struct { - Version uint8 - ScriptHash util.Uint160 - IsFrozen bool - Balances map[util.Uint256][]UnspentBalance -} - -// NewAccount returns a new Account object. -func NewAccount(scriptHash util.Uint160) *Account { - return &Account{ - Version: 0, - ScriptHash: scriptHash, - IsFrozen: false, - Balances: make(map[util.Uint256][]UnspentBalance), - } -} - -// DecodeBinary decodes Account from the given BinReader. -func (s *Account) DecodeBinary(br *io.BinReader) { - s.Version = uint8(br.ReadB()) - s.ScriptHash.DecodeBinary(br) - s.IsFrozen = br.ReadBool() - - s.Balances = make(map[util.Uint256][]UnspentBalance) - lenBalances := br.ReadVarUint() - for i := 0; i < int(lenBalances); i++ { - key := util.Uint256{} - br.ReadBytes(key[:]) - len := int(br.ReadVarUint()) - ubs := make([]UnspentBalance, len) - for j := 0; j < len; j++ { - ubs[j].DecodeBinary(br) - } - s.Balances[key] = ubs - } -} - -// EncodeBinary encodes Account to the given BinWriter. -func (s *Account) EncodeBinary(bw *io.BinWriter) { - bw.WriteB(byte(s.Version)) - s.ScriptHash.EncodeBinary(bw) - bw.WriteBool(s.IsFrozen) - - bw.WriteVarUint(uint64(len(s.Balances))) - for k, v := range s.Balances { - bw.WriteBytes(k[:]) - bw.WriteVarUint(uint64(len(v))) - for i := range v { - v[i].EncodeBinary(bw) - } - } -} - -// DecodeBinary implements io.Serializable interface. -func (u *UnspentBalance) DecodeBinary(r *io.BinReader) { - u.Tx.DecodeBinary(r) - u.Index = r.ReadU16LE() - u.Value.DecodeBinary(r) -} - -// EncodeBinary implements io.Serializable interface. -func (u *UnspentBalance) EncodeBinary(w *io.BinWriter) { - u.Tx.EncodeBinary(w) - w.WriteU16LE(u.Index) - u.Value.EncodeBinary(w) -} - -// GetBalanceValues sums all unspent outputs and returns a map of asset IDs to -// overall balances. -func (s *Account) GetBalanceValues() map[util.Uint256]util.Fixed8 { - res := make(map[util.Uint256]util.Fixed8) - for k, v := range s.Balances { - balance := util.Fixed8(0) - for _, b := range v { - balance += b.Value - } - res[k] = balance - } - return res -} - -// Len returns the length of UnspentBalances (used to sort things). -func (us UnspentBalances) Len() int { return len(us) } - -// Less compares two elements of UnspentBalances (used to sort things). -func (us UnspentBalances) Less(i, j int) bool { return us[i].Value < us[j].Value } - -// Swap swaps two elements of UnspentBalances (used to sort things). -func (us UnspentBalances) Swap(i, j int) { us[i], us[j] = us[j], us[i] } diff --git a/pkg/core/state/account_test.go b/pkg/core/state/account_test.go deleted file mode 100644 index 32a7a6754..000000000 --- a/pkg/core/state/account_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package state - -import ( - "testing" - - "github.com/nspcc-dev/neo-go/pkg/internal/random" - "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" - "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/stretchr/testify/assert" -) - -func TestDecodeEncodeAccountState(t *testing.T) { - var ( - n = 10 - balances = make(map[util.Uint256][]UnspentBalance) - ) - for i := 0; i < n; i++ { - asset := random.Uint256() - for j := 0; j < i+1; j++ { - balances[asset] = append(balances[asset], UnspentBalance{ - Tx: random.Uint256(), - Index: uint16(random.Int(0, 65535)), - Value: util.Fixed8(int64(random.Int(1, 10000))), - }) - } - } - - a := &Account{ - Version: 0, - ScriptHash: random.Uint160(), - IsFrozen: true, - Balances: balances, - } - - testserdes.EncodeDecodeBinary(t, a, new(Account)) -} - -func TestAccountStateBalanceValues(t *testing.T) { - asset1 := random.Uint256() - asset2 := random.Uint256() - as := Account{Balances: make(map[util.Uint256][]UnspentBalance)} - ref := 0 - for i := 0; i < 10; i++ { - ref += i - as.Balances[asset1] = append(as.Balances[asset1], UnspentBalance{Value: util.Fixed8(i)}) - as.Balances[asset2] = append(as.Balances[asset2], UnspentBalance{Value: util.Fixed8(i * 10)}) - } - bVals := as.GetBalanceValues() - assert.Equal(t, util.Fixed8(ref), bVals[asset1]) - assert.Equal(t, util.Fixed8(ref*10), bVals[asset2]) -} diff --git a/pkg/network/helper_test.go b/pkg/network/helper_test.go index fb279e81e..e7cc7a5f0 100644 --- a/pkg/network/helper_test.go +++ b/pkg/network/helper_test.go @@ -88,9 +88,6 @@ func (chain testChain) GetHeader(hash util.Uint256) (*block.Header, error) { panic("TODO") } -func (chain testChain) GetAccountState(util.Uint160) *state.Account { - panic("TODO") -} func (chain testChain) GetNextBlockValidators() ([]*keys.PublicKey, error) { panic("TODO") }