From aefb26255a2a724e10d68a453b9849c94409574b Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 16 Feb 2022 18:04:47 +0300 Subject: [PATCH] dao: drop DAO interface It's a remnant from the days when we had Simple and Cached DAO implementations, now it makes zero sense. --- pkg/compiler/interop_test.go | 2 +- pkg/core/blockchain.go | 23 ++++++++-------- pkg/core/dao/dao.go | 44 +------------------------------ pkg/core/interop/context.go | 8 +++--- pkg/core/interop/contract/call.go | 2 +- pkg/core/native/designate.go | 6 ++--- pkg/core/native/ledger.go | 2 +- pkg/core/native/management.go | 18 ++++++------- pkg/core/native/native_gas.go | 2 +- pkg/core/native/native_neo.go | 34 ++++++++++++------------ pkg/core/native/native_nep17.go | 6 ++--- pkg/core/native/notary.go | 14 +++++----- pkg/core/native/oracle.go | 20 +++++++------- pkg/core/native/policy.go | 12 ++++----- pkg/core/native/util.go | 8 +++--- 15 files changed, 79 insertions(+), 122 deletions(-) diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index 9ef16964b..b01cdd373 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -178,7 +178,7 @@ func TestAppCall(t *testing.T) { require.NoError(t, err) ih := hash.Hash160(inner.Script) - var contractGetter = func(_ dao.DAO, h util.Uint160) (*state.Contract, error) { + var contractGetter = func(_ *dao.Simple, h util.Uint160) (*state.Contract, error) { if h.Equals(ih) { return &state.Contract{ ContractBase: state.ContractBase{ diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 034dff3c4..e396cb530 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -519,7 +519,7 @@ func (bc *Blockchain) jumpToStateInternal(p uint32, stage stateJumpStage) error fallthrough case newStorageItemsAdded: - cache := bc.dao.GetWrapped().(*dao.Simple) + cache := bc.dao.GetWrapped() prefix := statesync.TemporaryPrefix(bc.dao.Version.StoragePrefix) bc.dao.Store.Seek(storage.SeekRange{Prefix: []byte{byte(prefix)}}, func(k, _ []byte) bool { // #1468, but don't need to copy here, because it is done by Store. @@ -903,7 +903,7 @@ func (bc *Blockchain) AddHeaders(headers ...*block.Header) error { func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error { var ( start = time.Now() - batch = bc.dao.GetWrapped().(*dao.Simple) + batch = bc.dao.GetWrapped() err error ) @@ -1146,9 +1146,8 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error appExecResults = append(appExecResults, aer) aerchan <- aer close(aerchan) - d := cache.(*dao.Simple) - b := d.GetMPTBatch() - mpt, sr, err := bc.stateRoot.AddMPTBatch(block.Index, b, d.Store) + b := cache.GetMPTBatch() + mpt, sr, err := bc.stateRoot.AddMPTBatch(block.Index, b, cache.Store) if err != nil { // Release goroutines, don't care about errors, we already have one. <-aerdone @@ -1172,7 +1171,7 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error } if bc.config.SaveStorageBatch { - bc.lastBatch = d.GetBatch() + bc.lastBatch = cache.GetBatch() } // Every persist cycle we also compact our in-memory MPT. It's flushed // already in AddMPTBatch, so collapsing it is safe. @@ -1272,7 +1271,7 @@ func (bc *Blockchain) IsExtensibleAllowed(u util.Uint160) bool { return n < len(us) } -func (bc *Blockchain) runPersist(script []byte, block *block.Block, cache dao.DAO, trig trigger.Type) (*state.AppExecResult, error) { +func (bc *Blockchain) runPersist(script []byte, block *block.Block, cache *dao.Simple, trig trigger.Type) (*state.AppExecResult, error) { systemInterop := bc.newInteropContext(trig, cache, block, nil) v := systemInterop.SpawnVM() v.LoadScriptWithFlags(script, callflag.All) @@ -1294,7 +1293,7 @@ func (bc *Blockchain) runPersist(script []byte, block *block.Block, cache dao.DA }, nil } -func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d dao.DAO, +func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d *dao.Simple, transCache map[util.Uint160]transferData, b *block.Block, h util.Uint256) { if note.Name != "Transfer" { return @@ -1337,7 +1336,7 @@ func parseUint160(itm stackitem.Item) (util.Uint160, error) { return util.Uint160DecodeBytesBE(bytes) } -func (bc *Blockchain) processTokenTransfer(cache dao.DAO, transCache map[util.Uint160]transferData, +func (bc *Blockchain) processTokenTransfer(cache *dao.Simple, transCache map[util.Uint160]transferData, h util.Uint256, b *block.Block, sc util.Uint160, from util.Uint160, to util.Uint160, amount *big.Int, tokenID []byte) { var id int32 @@ -1391,7 +1390,7 @@ func (bc *Blockchain) processTokenTransfer(cache dao.DAO, transCache map[util.Ui } } -func appendTokenTransfer(cache dao.DAO, transCache map[util.Uint160]transferData, addr util.Uint160, transfer io.Serializable, +func appendTokenTransfer(cache *dao.Simple, transCache map[util.Uint160]transferData, addr util.Uint160, transfer io.Serializable, token int32, bIndex uint32, bTimestamp uint64, isNEP11 bool) error { transferData, ok := transCache[addr] if !ok { @@ -2154,7 +2153,7 @@ func (bc *Blockchain) GetEnrollments() ([]state.Validator, error) { // GetTestVM returns an interop context with VM set up for a test run. func (bc *Blockchain) GetTestVM(t trigger.Type, tx *transaction.Transaction, b *block.Block) *interop.Context { - d := bc.dao.GetWrapped().(*dao.Simple) + d := bc.dao.GetWrapped() systemInterop := bc.newInteropContext(t, d, b, tx) vm := systemInterop.SpawnVM() vm.SetPriceGetter(systemInterop.GetPrice) @@ -2324,7 +2323,7 @@ func hashAndIndexToBytes(h util.Uint256, index uint32) []byte { return buf.Bytes() } -func (bc *Blockchain) newInteropContext(trigger trigger.Type, d dao.DAO, block *block.Block, tx *transaction.Transaction) *interop.Context { +func (bc *Blockchain) newInteropContext(trigger trigger.Type, d *dao.Simple, block *block.Block, tx *transaction.Transaction) *interop.Context { ic := interop.NewContext(trigger, bc, d, bc.contracts.Management.GetContract, bc.contracts.Contracts, block, tx, bc.log) ic.Functions = systemInterops switch { diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index b3ff1c8b9..6318de884 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -27,48 +27,6 @@ var ( ErrHasConflicts = errors.New("transaction has conflicts") ) -// DAO is a data access object. -type DAO interface { - DeleteBlock(h util.Uint256, buf *io.BufBinWriter) error - DeleteContractID(id int32) - DeleteStorageItem(id int32, key []byte) - GetAndDecode(entity io.Serializable, key []byte) error - GetAppExecResults(hash util.Uint256, trig trigger.Type) ([]state.AppExecResult, error) - GetBatch() *storage.MemBatch - GetBlock(hash util.Uint256) (*block.Block, error) - GetContractScriptHash(id int32) (util.Uint160, error) - GetCurrentBlockHeight() (uint32, error) - GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error) - GetHeaderHashes() ([]util.Uint256, error) - GetTokenTransferInfo(acc util.Uint160) (*state.TokenTransferInfo, error) - GetTokenTransferLog(acc util.Uint160, start uint64, index uint32, isNEP11 bool) (*state.TokenTransferLog, error) - GetStateSyncPoint() (uint32, error) - GetStateSyncCurrentBlockHeight() (uint32, error) - GetStorageItem(id int32, key []byte) state.StorageItem - GetStorageItems(id int32) ([]state.StorageItemWithKey, error) - GetStorageItemsWithPrefix(id int32, prefix []byte) ([]state.StorageItemWithKey, error) - GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error) - GetVersion() (Version, error) - GetWrapped() DAO - HasTransaction(hash util.Uint256) error - Persist() (int, error) - PersistSync() (int, error) - PutContractID(id int32, hash util.Uint160) - PutCurrentHeader(hashAndIndex []byte) - PutTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo) error - PutTokenTransferLog(acc util.Uint160, start uint64, index uint32, isNEP11 bool, lg *state.TokenTransferLog) - PutStateSyncPoint(p uint32) - PutStateSyncCurrentBlockHeight(h uint32) - PutStorageItem(id int32, key []byte, si state.StorageItem) - PutVersion(v Version) - Seek(id int32, rng storage.SeekRange, f func(k, v []byte) bool) - SeekAsync(ctx context.Context, id int32, rng storage.SeekRange) chan storage.KeyValue - StoreAsBlock(block *block.Block, aer1 *state.AppExecResult, aer2 *state.AppExecResult, buf *io.BufBinWriter) error - StoreAsCurrentBlock(block *block.Block, buf *io.BufBinWriter) - StoreAsTransaction(tx *transaction.Transaction, index uint32, aer *state.AppExecResult, buf *io.BufBinWriter) error - putTokenTransferInfo(acc util.Uint160, bs *state.TokenTransferInfo, buf *io.BufBinWriter) error -} - // Simple is memCached wrapper around DB, simple DAO implementation. type Simple struct { Version Version @@ -95,7 +53,7 @@ func (dao *Simple) GetBatch() *storage.MemBatch { // GetWrapped returns new DAO instance with another layer of wrapped // MemCachedStore around the current DAO Store. -func (dao *Simple) GetWrapped() DAO { +func (dao *Simple) GetWrapped() *Simple { d := NewSimple(dao.Store, dao.Version.StateRootInHeader, dao.Version.P2PSigExtensions) d.Version = dao.Version return d diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index 26a0e723c..c18d31fbc 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -54,21 +54,21 @@ type Context struct { Block *block.Block NonceData [16]byte Tx *transaction.Transaction - DAO dao.DAO + DAO *dao.Simple Notifications []state.NotificationEvent Log *zap.Logger VM *vm.VM Functions []Function Invocations map[util.Uint160]int cancelFuncs []context.CancelFunc - getContract func(dao.DAO, util.Uint160) (*state.Contract, error) + getContract func(*dao.Simple, util.Uint160) (*state.Contract, error) baseExecFee int64 signers []transaction.Signer } // NewContext returns new interop context. -func NewContext(trigger trigger.Type, bc Ledger, d dao.DAO, - getContract func(dao.DAO, util.Uint160) (*state.Contract, error), natives []Contract, +func NewContext(trigger trigger.Type, bc Ledger, d *dao.Simple, + getContract func(*dao.Simple, util.Uint160) (*state.Contract, error), natives []Contract, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *Context { baseExecFee := int64(DefaultBaseExecFee) dao := d.GetWrapped() diff --git a/pkg/core/interop/contract/call.go b/pkg/core/interop/contract/call.go index 7daf13073..5c3b6eb0a 100644 --- a/pkg/core/interop/contract/call.go +++ b/pkg/core/interop/contract/call.go @@ -18,7 +18,7 @@ import ( ) type policyChecker interface { - IsBlockedInternal(dao.DAO, util.Uint160) bool + IsBlockedInternal(*dao.Simple, util.Uint160) bool } // LoadToken calls method specified by token id. diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index 08ed87013..2db1fe99b 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -180,7 +180,7 @@ func (s *Designate) hashFromNodes(r noderoles.Role, nodes keys.PublicKeys) util. return hash.Hash160(script) } -func (s *Designate) updateCachedRoleData(v *atomic.Value, d dao.DAO, r noderoles.Role) error { +func (s *Designate) updateCachedRoleData(v *atomic.Value, d *dao.Simple, r noderoles.Role) error { nodeKeys, height, err := s.GetDesignatedByRole(d, r, math.MaxUint32) if err != nil { return err @@ -226,7 +226,7 @@ func (s *Designate) getCachedRoleData(r noderoles.Role) *roleData { } // GetLastDesignatedHash returns last designated hash of a given role. -func (s *Designate) GetLastDesignatedHash(d dao.DAO, r noderoles.Role) (util.Uint160, error) { +func (s *Designate) GetLastDesignatedHash(d *dao.Simple, r noderoles.Role) (util.Uint160, error) { if !s.isValidRole(r) { return util.Uint160{}, ErrInvalidRole } @@ -244,7 +244,7 @@ func (s *Designate) GetLastDesignatedHash(d dao.DAO, r noderoles.Role) (util.Uin } // GetDesignatedByRole returns nodes for role r. -func (s *Designate) GetDesignatedByRole(d dao.DAO, r noderoles.Role, index uint32) (keys.PublicKeys, uint32, error) { +func (s *Designate) GetDesignatedByRole(d *dao.Simple, r noderoles.Role, index uint32) (keys.PublicKeys, uint32, error) { if !s.isValidRole(r) { return nil, 0, ErrInvalidRole } diff --git a/pkg/core/native/ledger.go b/pkg/core/native/ledger.go index 496ccc640..a5d56133b 100644 --- a/pkg/core/native/ledger.go +++ b/pkg/core/native/ledger.go @@ -179,7 +179,7 @@ func getBlockHashFromItem(bc interop.Ledger, item stackitem.Item) util.Uint256 { // getTransactionAndHeight returns transaction and its height if it's present // on the chain. It panics if anything goes wrong. -func getTransactionAndHeight(d dao.DAO, item stackitem.Item) (*transaction.Transaction, uint32, error) { +func getTransactionAndHeight(d *dao.Simple, item stackitem.Item) (*transaction.Transaction, uint32, error) { hashbytes, err := item.TryBytes() if err != nil { panic(err) diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index 936ffbbf7..6e0d3c836 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -145,7 +145,7 @@ func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) sta } // GetContract returns contract with given hash from given DAO. -func (m *Management) GetContract(d dao.DAO, hash util.Uint160) (*state.Contract, error) { +func (m *Management) GetContract(d *dao.Simple, hash util.Uint160) (*state.Contract, error) { m.mtx.RLock() cs, ok := m.contracts[hash] m.mtx.RUnlock() @@ -157,7 +157,7 @@ func (m *Management) GetContract(d dao.DAO, hash util.Uint160) (*state.Contract, return m.getContractFromDAO(d, hash) } -func (m *Management) getContractFromDAO(d dao.DAO, hash util.Uint160) (*state.Contract, error) { +func (m *Management) getContractFromDAO(d *dao.Simple, hash util.Uint160) (*state.Contract, error) { contract := new(state.Contract) key := MakeContractKey(hash) err := getConvertibleFromDAO(m.ID, d, key, contract) @@ -269,7 +269,7 @@ func (m *Management) markUpdated(h util.Uint160) { // Deploy creates contract's hash/ID and saves new contract into the given DAO. // It doesn't run _deploy method and doesn't emit notification. -func (m *Management) Deploy(d dao.DAO, sender util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) { +func (m *Management) Deploy(d *dao.Simple, sender util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) { h := state.CreateContractHash(sender, neff.Checksum, manif.Name) key := MakeContractKey(h) si := d.GetStorageItem(m.ID, key) @@ -329,7 +329,7 @@ func (m *Management) updateWithData(ic *interop.Context, args []stackitem.Item) // Update updates contract's script and/or manifest in the given DAO. // It doesn't run _deploy method and doesn't emit notification. -func (m *Management) Update(d dao.DAO, hash util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) { +func (m *Management) Update(d *dao.Simple, hash util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) { var contract state.Contract oldcontract, err := m.GetContract(d, hash) @@ -380,7 +380,7 @@ func (m *Management) destroy(ic *interop.Context, sis []stackitem.Item) stackite } // Destroy drops given contract from DAO along with its storage. It doesn't emit notification. -func (m *Management) Destroy(d dao.DAO, hash util.Uint160) error { +func (m *Management) Destroy(d *dao.Simple, hash util.Uint160) error { contract, err := m.GetContract(d, hash) if err != nil { return err @@ -404,7 +404,7 @@ func (m *Management) getMinimumDeploymentFee(ic *interop.Context, args []stackit } // GetMinimumDeploymentFee returns the minimum required fee for contract deploy. -func (m *Management) GetMinimumDeploymentFee(dao dao.DAO) int64 { +func (m *Management) GetMinimumDeploymentFee(dao *dao.Simple) int64 { return getIntWithKey(m.ID, dao, keyMinimumDeploymentFee) } @@ -486,7 +486,7 @@ func (m *Management) OnPersist(ic *interop.Context) error { // InitializeCache initializes contract cache with the proper values from storage. // Cache initialisation should be done apart from Initialize because Initialize is // called only when deploying native contracts. -func (m *Management) InitializeCache(d dao.DAO) error { +func (m *Management) InitializeCache(d *dao.Simple) error { m.mtx.Lock() defer m.mtx.Unlock() @@ -558,7 +558,7 @@ func (m *Management) Initialize(ic *interop.Context) error { } // PutContractState saves given contract state into given DAO. -func (m *Management) PutContractState(d dao.DAO, cs *state.Contract) error { +func (m *Management) PutContractState(d *dao.Simple, cs *state.Contract) error { key := MakeContractKey(cs.Hash) if err := putConvertibleToDAO(m.ID, d, key, cs); err != nil { return err @@ -571,7 +571,7 @@ func (m *Management) PutContractState(d dao.DAO, cs *state.Contract) error { return nil } -func (m *Management) getNextContractID(d dao.DAO) (int32, error) { +func (m *Management) getNextContractID(d *dao.Simple) (int32, error) { si := d.GetStorageItem(m.ID, keyNextAvailableID) if si == nil { return 0, errors.New("nextAvailableID is not initialized") diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 5be67bb66..0e51f985f 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -131,7 +131,7 @@ func (g *GAS) PostPersist(ic *interop.Context) error { } // BalanceOf returns native GAS token balance for the acc. -func (g *GAS) BalanceOf(d dao.DAO, acc util.Uint160) *big.Int { +func (g *GAS) BalanceOf(d *dao.Simple, acc util.Uint160) *big.Int { return g.balanceOfInternal(d, acc) } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index f30ddc993..cadd03570 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -241,7 +241,7 @@ func (n *NEO) Initialize(ic *interop.Context) error { // InitializeCache initializes all NEO cache with the proper values from storage. // Cache initialisation should be done apart from Initialize because Initialize is // called only when deploying native contracts. -func (n *NEO) InitializeCache(bc interop.Ledger, d dao.DAO) error { +func (n *NEO) InitializeCache(bc interop.Ledger, d *dao.Simple) error { err := n.initConfigCache(bc) if err != nil { return nil @@ -391,7 +391,7 @@ func (n *NEO) PostPersist(ic *interop.Context) error { return nil } -func (n *NEO) getGASPerVote(d dao.DAO, key []byte, indexes []uint32) []big.Int { +func (n *NEO) getGASPerVote(d *dao.Simple, key []byte, indexes []uint32) []big.Int { sort.Slice(indexes, func(i, j int) bool { return indexes[i] < indexes[j] }) @@ -495,7 +495,7 @@ func (n *NEO) getGASPerBlock(ic *interop.Context, _ []stackitem.Item) stackitem. return stackitem.NewBigInteger(gas) } -func (n *NEO) getSortedGASRecordFromDAO(d dao.DAO) (gasRecord, error) { +func (n *NEO) getSortedGASRecordFromDAO(d *dao.Simple) (gasRecord, error) { grArr, err := d.GetStorageItemsWithPrefix(n.ID, []byte{prefixGASPerBlock}) if err != nil { return gasRecord{}, fmt.Errorf("failed to get gas records from storage: %w", err) @@ -514,7 +514,7 @@ func (n *NEO) getSortedGASRecordFromDAO(d dao.DAO) (gasRecord, error) { } // GetGASPerBlock returns gas generated for block with provided index. -func (n *NEO) GetGASPerBlock(d dao.DAO, index uint32) *big.Int { +func (n *NEO) GetGASPerBlock(d *dao.Simple, index uint32) *big.Int { var ( gr gasRecord err error @@ -575,7 +575,7 @@ func (n *NEO) getRegisterPrice(ic *interop.Context, _ []stackitem.Item) stackite return stackitem.NewBigInteger(big.NewInt(n.getRegisterPriceInternal(ic.DAO))) } -func (n *NEO) getRegisterPriceInternal(d dao.DAO) int64 { +func (n *NEO) getRegisterPriceInternal(d *dao.Simple) int64 { if !n.registerPriceChanged.Load().(bool) { return n.registerPrice.Load().(int64) } @@ -596,7 +596,7 @@ func (n *NEO) setRegisterPrice(ic *interop.Context, args []stackitem.Item) stack return stackitem.Null{} } -func (n *NEO) dropCandidateIfZero(d dao.DAO, pub *keys.PublicKey, c *candidate) (bool, error) { +func (n *NEO) dropCandidateIfZero(d *dao.Simple, pub *keys.PublicKey, c *candidate) (bool, error) { if c.Registered || c.Votes.Sign() != 0 { return false, nil } @@ -630,7 +630,7 @@ func makeVoterKey(pub []byte, prealloc ...[]byte) []byte { // CalculateBonus calculates amount of gas generated for holding value NEO from start to end block // and having voted for active committee member. -func (n *NEO) CalculateBonus(d dao.DAO, acc util.Uint160, end uint32) (*big.Int, error) { +func (n *NEO) CalculateBonus(d *dao.Simple, acc util.Uint160, end uint32) (*big.Int, error) { key := makeAccountKey(acc) si := d.GetStorageItem(n.ID, key) if si == nil { @@ -643,7 +643,7 @@ func (n *NEO) CalculateBonus(d dao.DAO, acc util.Uint160, end uint32) (*big.Int, return n.calculateBonus(d, st.VoteTo, &st.Balance, st.BalanceHeight, end) } -func (n *NEO) calculateBonus(d dao.DAO, vote *keys.PublicKey, value *big.Int, start, end uint32) (*big.Int, error) { +func (n *NEO) calculateBonus(d *dao.Simple, vote *keys.PublicKey, value *big.Int, start, end uint32) (*big.Int, error) { r, err := n.CalculateNEOHolderReward(d, value, start, end) if err != nil || vote == nil { return r, err @@ -659,7 +659,7 @@ func (n *NEO) calculateBonus(d dao.DAO, vote *keys.PublicKey, value *big.Int, st } // CalculateNEOHolderReward return GAS reward for holding `value` of NEO from start to end block. -func (n *NEO) CalculateNEOHolderReward(d dao.DAO, value *big.Int, start, end uint32) (*big.Int, error) { +func (n *NEO) CalculateNEOHolderReward(d *dao.Simple, value *big.Int, start, end uint32) (*big.Int, error) { if value.Sign() == 0 || start >= end { return big.NewInt(0), nil } else if value.Sign() < 0 { @@ -824,7 +824,7 @@ func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.Public // ModifyAccountVotes modifies votes of the specified account by value (can be negative). // typ specifies if this modify is occurring during transfer or vote (with old or new validator). -func (n *NEO) ModifyAccountVotes(acc *state.NEOBalance, d dao.DAO, value *big.Int, isNewVote bool) error { +func (n *NEO) ModifyAccountVotes(acc *state.NEOBalance, d *dao.Simple, value *big.Int, isNewVote bool) error { n.votesChanged.Store(true) if acc.VoteTo != nil { key := makeValidatorKey(acc.VoteTo) @@ -846,7 +846,7 @@ func (n *NEO) ModifyAccountVotes(acc *state.NEOBalance, d dao.DAO, value *big.In return nil } -func (n *NEO) getCandidates(d dao.DAO, sortByKey bool) ([]keyWithVotes, error) { +func (n *NEO) getCandidates(d *dao.Simple, sortByKey bool) ([]keyWithVotes, error) { siArr, err := d.GetStorageItemsWithPrefix(n.ID, []byte{prefixCandidate}) if err != nil { return nil, err @@ -886,7 +886,7 @@ func (n *NEO) getCandidates(d dao.DAO, sortByKey bool) ([]keyWithVotes, error) { // GetCandidates returns current registered validators list with keys // and votes. -func (n *NEO) GetCandidates(d dao.DAO) ([]state.Validator, error) { +func (n *NEO) GetCandidates(d *dao.Simple) ([]state.Validator, error) { kvs, err := n.getCandidates(d, true) if err != nil { return nil, err @@ -932,7 +932,7 @@ func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stacki } // ComputeNextBlockValidators returns an actual list of current validators. -func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, error) { +func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d *dao.Simple) (keys.PublicKeys, error) { numOfCNs := n.cfg.GetNumOfCNs(bc.BlockHeight() + 1) if vals := n.validators.Load().(keys.PublicKeys); vals != nil && numOfCNs == len(vals) { return vals.Copy(), nil @@ -953,7 +953,7 @@ func (n *NEO) getCommittee(ic *interop.Context, _ []stackitem.Item) stackitem.It return pubsToArray(pubs) } -func (n *NEO) modifyVoterTurnout(d dao.DAO, amount *big.Int) error { +func (n *NEO) modifyVoterTurnout(d *dao.Simple, amount *big.Int) error { key := []byte{prefixVotersCount} si := d.GetStorageItem(n.ID, key) if si == nil { @@ -991,7 +991,7 @@ func toKeysWithVotes(pubs keys.PublicKeys) keysWithVotes { } // computeCommitteeMembers returns public keys of nodes in committee. -func (n *NEO) computeCommitteeMembers(bc interop.Ledger, d dao.DAO) (keys.PublicKeys, keysWithVotes, error) { +func (n *NEO) computeCommitteeMembers(bc interop.Ledger, d *dao.Simple) (keys.PublicKeys, keysWithVotes, error) { key := []byte{prefixVotersCount} si := d.GetStorageItem(n.ID, key) if si == nil { @@ -1047,7 +1047,7 @@ func (n *NEO) GetNextBlockValidatorsInternal() keys.PublicKeys { } // BalanceOf returns native NEO token balance for the acc. -func (n *NEO) BalanceOf(d dao.DAO, acc util.Uint160) (*big.Int, uint32) { +func (n *NEO) BalanceOf(d *dao.Simple, acc util.Uint160) (*big.Int, uint32) { key := makeAccountKey(acc) si := d.GetStorageItem(n.ID, key) if si == nil { @@ -1081,7 +1081,7 @@ func toPublicKey(s stackitem.Item) *keys.PublicKey { } // putGASRecord is a helper which creates key and puts GASPerBlock value into the storage. -func (n *NEO) putGASRecord(dao dao.DAO, index uint32, value *big.Int) { +func (n *NEO) putGASRecord(dao *dao.Simple, index uint32, value *big.Int) { key := make([]byte, 5) key[0] = prefixGASPerBlock binary.BigEndian.PutUint32(key[1:], index) diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 8ed1bc570..e03069521 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -99,7 +99,7 @@ func (c *nep17TokenNative) TotalSupply(ic *interop.Context, _ []stackitem.Item) return stackitem.NewBigInteger(supply) } -func (c *nep17TokenNative) getTotalSupply(d dao.DAO) (state.StorageItem, *big.Int) { +func (c *nep17TokenNative) getTotalSupply(d *dao.Simple) (state.StorageItem, *big.Int) { si := d.GetStorageItem(c.ID, totalSupplyKey) if si == nil { si = []byte{} @@ -107,7 +107,7 @@ func (c *nep17TokenNative) getTotalSupply(d dao.DAO) (state.StorageItem, *big.In return si, bigint.FromBytes(si) } -func (c *nep17TokenNative) saveTotalSupply(d dao.DAO, si state.StorageItem, supply *big.Int) { +func (c *nep17TokenNative) saveTotalSupply(d *dao.Simple, si state.StorageItem, supply *big.Int) { si = state.StorageItem(bigint.ToPreallocatedBytes(supply, si)) d.PutStorageItem(c.ID, totalSupplyKey, si) } @@ -237,7 +237,7 @@ func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) return stackitem.NewBigInteger(c.balanceOfInternal(ic.DAO, h)) } -func (c *nep17TokenNative) balanceOfInternal(d dao.DAO, h util.Uint160) *big.Int { +func (c *nep17TokenNative) balanceOfInternal(d *dao.Simple, h util.Uint160) *big.Int { key := makeAccountKey(h) si := d.GetStorageItem(c.ID, key) if si == nil { diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index 9b86f6c97..c13bdebb0 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -293,7 +293,7 @@ func (n *Notary) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem } // BalanceOf is an internal representation of `balanceOf` Notary method. -func (n *Notary) BalanceOf(dao dao.DAO, acc util.Uint160) *big.Int { +func (n *Notary) BalanceOf(dao *dao.Simple, acc util.Uint160) *big.Int { deposit := n.GetDepositFor(dao, acc) if deposit == nil { return big.NewInt(0) @@ -308,7 +308,7 @@ func (n *Notary) expirationOf(ic *interop.Context, args []stackitem.Item) stacki } // ExpirationOf is an internal representation of `expirationOf` Notary method. -func (n *Notary) ExpirationOf(dao dao.DAO, acc util.Uint160) uint32 { +func (n *Notary) ExpirationOf(dao *dao.Simple, acc util.Uint160) uint32 { deposit := n.GetDepositFor(dao, acc) if deposit == nil { return 0 @@ -360,7 +360,7 @@ func (n *Notary) verify(ic *interop.Context, args []stackitem.Item) stackitem.It } // GetNotaryNodes returns public keys of notary nodes. -func (n *Notary) GetNotaryNodes(d dao.DAO) (keys.PublicKeys, error) { +func (n *Notary) GetNotaryNodes(d *dao.Simple) (keys.PublicKeys, error) { nodes, _, err := n.Desig.GetDesignatedByRole(d, noderoles.P2PNotary, math.MaxUint32) return nodes, err } @@ -371,7 +371,7 @@ func (n *Notary) getMaxNotValidBeforeDelta(ic *interop.Context, _ []stackitem.It } // GetMaxNotValidBeforeDelta is an internal representation of Notary getMaxNotValidBeforeDelta method. -func (n *Notary) GetMaxNotValidBeforeDelta(dao dao.DAO) uint32 { +func (n *Notary) GetMaxNotValidBeforeDelta(dao *dao.Simple) uint32 { n.lock.RLock() defer n.lock.RUnlock() if n.isValid { @@ -400,7 +400,7 @@ func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem // GetDepositFor returns state.Deposit for the account specified. It returns nil in case if // deposit is not found in storage and panics in case of any other error. -func (n *Notary) GetDepositFor(dao dao.DAO, acc util.Uint160) *state.Deposit { +func (n *Notary) GetDepositFor(dao *dao.Simple, acc util.Uint160) *state.Deposit { key := append([]byte{prefixDeposit}, acc.BytesBE()...) deposit := new(state.Deposit) err := getConvertibleFromDAO(n.ID, dao, key, deposit) @@ -414,13 +414,13 @@ func (n *Notary) GetDepositFor(dao dao.DAO, acc util.Uint160) *state.Deposit { } // putDepositFor puts deposit on the balance of the specified account in the storage. -func (n *Notary) putDepositFor(dao dao.DAO, deposit *state.Deposit, acc util.Uint160) error { +func (n *Notary) putDepositFor(dao *dao.Simple, deposit *state.Deposit, acc util.Uint160) error { key := append([]byte{prefixDeposit}, acc.BytesBE()...) return putConvertibleToDAO(n.ID, dao, key, deposit) } // removeDepositFor removes deposit from the storage. -func (n *Notary) removeDepositFor(dao dao.DAO, acc util.Uint160) { +func (n *Notary) removeDepositFor(dao *dao.Simple, acc util.Uint160) { key := append([]byte{prefixDeposit}, acc.BytesBE()...) dao.DeleteStorageItem(n.ID, key) } diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index ad09b1d13..44eee0418 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -387,7 +387,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string } // PutRequestInternal puts oracle request with the specified id to d. -func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d dao.DAO) error { +func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d *dao.Simple) error { reqKey := makeRequestKey(id) if err := putConvertibleToDAO(o.ID, d, reqKey, req); err != nil { return err @@ -408,25 +408,25 @@ func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d dao.D } // GetScriptHash returns script hash or oracle nodes. -func (o *Oracle) GetScriptHash(d dao.DAO) (util.Uint160, error) { +func (o *Oracle) GetScriptHash(d *dao.Simple) (util.Uint160, error) { return o.Desig.GetLastDesignatedHash(d, noderoles.Oracle) } // GetOracleNodes returns public keys of oracle nodes. -func (o *Oracle) GetOracleNodes(d dao.DAO) (keys.PublicKeys, error) { +func (o *Oracle) GetOracleNodes(d *dao.Simple) (keys.PublicKeys, error) { nodes, _, err := o.Desig.GetDesignatedByRole(d, noderoles.Oracle, math.MaxUint32) return nodes, err } // GetRequestInternal returns request by ID and key under which it is stored. -func (o *Oracle) GetRequestInternal(d dao.DAO, id uint64) (*state.OracleRequest, error) { +func (o *Oracle) GetRequestInternal(d *dao.Simple, id uint64) (*state.OracleRequest, error) { key := makeRequestKey(id) req := new(state.OracleRequest) return req, o.getConvertibleFromDAO(d, key, req) } // GetIDListInternal returns request by ID and key under which it is stored. -func (o *Oracle) GetIDListInternal(d dao.DAO, url string) (*IDList, error) { +func (o *Oracle) GetIDListInternal(d *dao.Simple, url string) (*IDList, error) { key := makeIDListKey(url) idList := new(IDList) return idList, o.getConvertibleFromDAO(d, key, idList) @@ -440,7 +440,7 @@ func (o *Oracle) getPrice(ic *interop.Context, _ []stackitem.Item) stackitem.Ite return stackitem.NewBigInteger(big.NewInt(o.getPriceInternal(ic.DAO))) } -func (o *Oracle) getPriceInternal(d dao.DAO) int64 { +func (o *Oracle) getPriceInternal(d *dao.Simple) int64 { if !o.requestPriceChanged.Load().(bool) { return o.requestPrice.Load().(int64) } @@ -460,7 +460,7 @@ func (o *Oracle) setPrice(ic *interop.Context, args []stackitem.Item) stackitem. return stackitem.Null{} } -func (o *Oracle) getOriginalTxID(d dao.DAO, tx *transaction.Transaction) util.Uint256 { +func (o *Oracle) getOriginalTxID(d *dao.Simple, tx *transaction.Transaction) util.Uint256 { for i := range tx.Attributes { if tx.Attributes[i].Type == transaction.OracleResponseT { id := tx.Attributes[i].Value.(*transaction.OracleResponse).ID @@ -472,7 +472,7 @@ func (o *Oracle) getOriginalTxID(d dao.DAO, tx *transaction.Transaction) util.Ui } // getRequests returns all requests which have not been finished yet. -func (o *Oracle) getRequests(d dao.DAO) (map[uint64]*state.OracleRequest, error) { +func (o *Oracle) getRequests(d *dao.Simple) (map[uint64]*state.OracleRequest, error) { arr, err := d.GetStorageItemsWithPrefix(o.ID, prefixRequest) if err != nil { return nil, err @@ -504,12 +504,12 @@ func makeIDListKey(url string) []byte { return append(prefixIDList, hash.Hash160([]byte(url)).BytesBE()...) } -func (o *Oracle) getConvertibleFromDAO(d dao.DAO, key []byte, item stackitem.Convertible) error { +func (o *Oracle) getConvertibleFromDAO(d *dao.Simple, key []byte, item stackitem.Convertible) error { return getConvertibleFromDAO(o.ID, d, key, item) } // updateCache updates cached Oracle values if they've been changed. -func (o *Oracle) updateCache(d dao.DAO) error { +func (o *Oracle) updateCache(d *dao.Simple) error { orc, _ := o.Module.Load().(services.Oracle) if orc == nil { return nil diff --git a/pkg/core/native/policy.go b/pkg/core/native/policy.go index 0bd818698..eac58e5c9 100644 --- a/pkg/core/native/policy.go +++ b/pkg/core/native/policy.go @@ -182,7 +182,7 @@ func (p *Policy) getFeePerByte(ic *interop.Context, _ []stackitem.Item) stackite } // GetFeePerByteInternal returns required transaction's fee per byte. -func (p *Policy) GetFeePerByteInternal(dao dao.DAO) int64 { +func (p *Policy) GetFeePerByteInternal(dao *dao.Simple) int64 { p.lock.RLock() defer p.lock.RUnlock() if p.isValid { @@ -192,7 +192,7 @@ func (p *Policy) GetFeePerByteInternal(dao dao.DAO) int64 { } // GetMaxVerificationGas returns maximum gas allowed to be burned during verificaion. -func (p *Policy) GetMaxVerificationGas(_ dao.DAO) int64 { +func (p *Policy) GetMaxVerificationGas(_ *dao.Simple) int64 { if p.isValid { return p.maxVerificationGas } @@ -204,7 +204,7 @@ func (p *Policy) getExecFeeFactor(ic *interop.Context, _ []stackitem.Item) stack } // GetExecFeeFactorInternal returns current execution fee factor. -func (p *Policy) GetExecFeeFactorInternal(d dao.DAO) int64 { +func (p *Policy) GetExecFeeFactorInternal(d *dao.Simple) int64 { p.lock.RLock() defer p.lock.RUnlock() if p.isValid { @@ -235,7 +235,7 @@ func (p *Policy) isBlocked(ic *interop.Context, args []stackitem.Item) stackitem } // IsBlockedInternal checks whether provided account is blocked. -func (p *Policy) IsBlockedInternal(dao dao.DAO, hash util.Uint160) bool { +func (p *Policy) IsBlockedInternal(dao *dao.Simple, hash util.Uint160) bool { p.lock.RLock() defer p.lock.RUnlock() if p.isValid { @@ -257,7 +257,7 @@ func (p *Policy) getStoragePrice(ic *interop.Context, _ []stackitem.Item) stacki } // GetStoragePriceInternal returns current execution fee factor. -func (p *Policy) GetStoragePriceInternal(d dao.DAO) int64 { +func (p *Policy) GetStoragePriceInternal(d *dao.Simple) int64 { p.lock.RLock() defer p.lock.RUnlock() if p.isValid { @@ -341,7 +341,7 @@ func (p *Policy) unblockAccount(ic *interop.Context, args []stackitem.Item) stac // CheckPolicy checks whether transaction conforms to current policy restrictions // like not being signed by blocked account or not exceeding block-level system // fee limit. -func (p *Policy) CheckPolicy(d dao.DAO, tx *transaction.Transaction) error { +func (p *Policy) CheckPolicy(d *dao.Simple, tx *transaction.Transaction) error { for _, signer := range tx.Signers { if p.IsBlockedInternal(d, signer.Account) { return fmt.Errorf("account %s is blocked", signer.Account.StringLE()) diff --git a/pkg/core/native/util.go b/pkg/core/native/util.go index a150d1e3e..b75b40963 100644 --- a/pkg/core/native/util.go +++ b/pkg/core/native/util.go @@ -15,7 +15,7 @@ import ( var intOne = big.NewInt(1) var intTwo = big.NewInt(2) -func getConvertibleFromDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convertible) error { +func getConvertibleFromDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error { si := d.GetStorageItem(id, key) if si == nil { return storage.ErrKeyNotFound @@ -23,7 +23,7 @@ func getConvertibleFromDAO(id int32, d dao.DAO, key []byte, conv stackitem.Conve return stackitem.DeserializeConvertible(si, conv) } -func putConvertibleToDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convertible) error { +func putConvertibleToDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error { data, err := stackitem.SerializeConvertible(conv) if err != nil { return err @@ -32,11 +32,11 @@ func putConvertibleToDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convert return nil } -func setIntWithKey(id int32, dao dao.DAO, key []byte, value int64) { +func setIntWithKey(id int32, dao *dao.Simple, key []byte, value int64) { dao.PutStorageItem(id, key, bigint.ToBytes(big.NewInt(value))) } -func getIntWithKey(id int32, dao dao.DAO, key []byte) int64 { +func getIntWithKey(id int32, dao *dao.Simple, key []byte) int64 { si := dao.GetStorageItem(id, key) if si == nil { panic(fmt.Errorf("item with id = %d and key = %s is not initialized", id, hex.EncodeToString(key)))