dao: simplify NewSimple()

We no longer need P2PSigExtension flag here, conflicts attribute is a part
of the normal protocol.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2023-09-04 16:48:16 +03:00
parent f3c1283ac6
commit fff7e91709
8 changed files with 35 additions and 36 deletions

View file

@ -320,7 +320,7 @@ func TestAppCall(t *testing.T) {
} }
fc := fakechain.NewFakeChain() fc := fakechain.NewFakeChain()
ic := interop.NewContext(trigger.Application, fc, dao.NewSimple(storage.NewMemoryStore(), false, false), ic := interop.NewContext(trigger.Application, fc, dao.NewSimple(storage.NewMemoryStore(), false),
interop.DefaultBaseExecFee, native.DefaultStoragePrice, contractGetter, nil, nil, nil, nil, zaptest.NewLogger(t)) interop.DefaultBaseExecFee, native.DefaultStoragePrice, contractGetter, nil, nil, nil, nil, zaptest.NewLogger(t))
t.Run("valid script", func(t *testing.T) { t.Run("valid script", func(t *testing.T) {

View file

@ -320,8 +320,8 @@ func NewBlockchain(s storage.Store, cfg config.Blockchain, log *zap.Logger) (*Bl
} }
bc := &Blockchain{ bc := &Blockchain{
config: cfg, config: cfg,
dao: dao.NewSimple(s, cfg.StateRootInHeader, cfg.P2PSigExtensions), dao: dao.NewSimple(s, cfg.StateRootInHeader),
persistent: dao.NewSimple(s, cfg.StateRootInHeader, cfg.P2PSigExtensions), persistent: dao.NewSimple(s, cfg.StateRootInHeader),
store: s, store: s,
stopCh: make(chan struct{}), stopCh: make(chan struct{}),
runToExitCh: make(chan struct{}), runToExitCh: make(chan struct{}),
@ -2752,7 +2752,7 @@ func (bc *Blockchain) GetTestHistoricVM(t trigger.Type, tx *transaction.Transact
return nil, fmt.Errorf("failed to retrieve stateroot for height %d: %w", b.Index, err) return nil, fmt.Errorf("failed to retrieve stateroot for height %d: %w", b.Index, err)
} }
s := mpt.NewTrieStore(sr.Root, mode, storage.NewPrivateMemCachedStore(bc.dao.Store)) s := mpt.NewTrieStore(sr.Root, mode, storage.NewPrivateMemCachedStore(bc.dao.Store))
dTrie := dao.NewSimple(s, bc.config.StateRootInHeader, bc.config.P2PSigExtensions) dTrie := dao.NewSimple(s, bc.config.StateRootInHeader)
dTrie.Version = bc.dao.Version dTrie.Version = bc.dao.Version
// Initialize native cache before passing DAO to interop context constructor, because // Initialize native cache before passing DAO to interop context constructor, because
// the constructor will call BaseExecFee/StoragePrice policy methods on the passed DAO. // the constructor will call BaseExecFee/StoragePrice policy methods on the passed DAO.

View file

@ -94,7 +94,7 @@ func TestBlockchain_StartFromExistingDB(t *testing.T) {
t.Run("mismatch storage version", func(t *testing.T) { t.Run("mismatch storage version", func(t *testing.T) {
ps = newPS(t) ps = newPS(t)
cache := storage.NewMemCachedStore(ps) // Extra wrapper to avoid good DB corruption. cache := storage.NewMemCachedStore(ps) // Extra wrapper to avoid good DB corruption.
d := dao.NewSimple(cache, bc.GetConfig().StateRootInHeader, bc.GetConfig().P2PStateExchangeExtensions) d := dao.NewSimple(cache, bc.GetConfig().StateRootInHeader)
d.PutVersion(dao.Version{ d.PutVersion(dao.Version{
Value: "0.0.0", Value: "0.0.0",
}) })

View file

@ -62,17 +62,16 @@ type NativeContractCache interface {
} }
// NewSimple creates a new simple dao using the provided backend store. // NewSimple creates a new simple dao using the provided backend store.
func NewSimple(backend storage.Store, stateRootInHeader bool, p2pSigExtensions bool) *Simple { func NewSimple(backend storage.Store, stateRootInHeader bool) *Simple {
st := storage.NewMemCachedStore(backend) st := storage.NewMemCachedStore(backend)
return newSimple(st, stateRootInHeader, p2pSigExtensions) return newSimple(st, stateRootInHeader)
} }
func newSimple(st *storage.MemCachedStore, stateRootInHeader bool, p2pSigExtensions bool) *Simple { func newSimple(st *storage.MemCachedStore, stateRootInHeader bool) *Simple {
return &Simple{ return &Simple{
Version: Version{ Version: Version{
StoragePrefix: storage.STStorage, StoragePrefix: storage.STStorage,
StateRootInHeader: stateRootInHeader, StateRootInHeader: stateRootInHeader,
P2PSigExtensions: p2pSigExtensions,
}, },
Store: st, Store: st,
nativeCache: make(map[int32]NativeContractCache), nativeCache: make(map[int32]NativeContractCache),
@ -87,7 +86,7 @@ func (dao *Simple) GetBatch() *storage.MemBatch {
// GetWrapped returns a new DAO instance with another layer of wrapped // GetWrapped returns a new DAO instance with another layer of wrapped
// MemCachedStore around the current DAO Store. // MemCachedStore around the current DAO Store.
func (dao *Simple) GetWrapped() *Simple { func (dao *Simple) GetWrapped() *Simple {
d := NewSimple(dao.Store, dao.Version.StateRootInHeader, dao.Version.P2PSigExtensions) d := NewSimple(dao.Store, dao.Version.StateRootInHeader)
d.Version = dao.Version d.Version = dao.Version
d.nativeCachePS = dao d.nativeCachePS = dao
return d return d

View file

@ -18,7 +18,7 @@ import (
) )
func TestPutGetAndDecode(t *testing.T) { func TestPutGetAndDecode(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
serializable := &TestSerializable{field: random.String(4)} serializable := &TestSerializable{field: random.String(4)}
hash := []byte{1} hash := []byte{1}
require.NoError(t, dao.putWithBuffer(serializable, hash, io.NewBufBinWriter())) require.NoError(t, dao.putWithBuffer(serializable, hash, io.NewBufBinWriter()))
@ -42,7 +42,7 @@ func (t *TestSerializable) DecodeBinary(reader *io.BinReader) {
} }
func TestPutGetStorageItem(t *testing.T) { func TestPutGetStorageItem(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
id := int32(random.Int(0, 1024)) id := int32(random.Int(0, 1024))
key := []byte{0} key := []byte{0}
storageItem := state.StorageItem{} storageItem := state.StorageItem{}
@ -52,7 +52,7 @@ func TestPutGetStorageItem(t *testing.T) {
} }
func TestDeleteStorageItem(t *testing.T) { func TestDeleteStorageItem(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
id := int32(random.Int(0, 1024)) id := int32(random.Int(0, 1024))
key := []byte{0} key := []byte{0}
storageItem := state.StorageItem{} storageItem := state.StorageItem{}
@ -63,7 +63,7 @@ func TestDeleteStorageItem(t *testing.T) {
} }
func TestGetBlock_NotExists(t *testing.T) { func TestGetBlock_NotExists(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
hash := random.Uint256() hash := random.Uint256()
block, err := dao.GetBlock(hash) block, err := dao.GetBlock(hash)
require.Error(t, err) require.Error(t, err)
@ -71,7 +71,7 @@ func TestGetBlock_NotExists(t *testing.T) {
} }
func TestPutGetBlock(t *testing.T) { func TestPutGetBlock(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
b := &block.Block{ b := &block.Block{
Header: block.Header{ Header: block.Header{
Script: transaction.Witness{ Script: transaction.Witness{
@ -110,14 +110,14 @@ func TestPutGetBlock(t *testing.T) {
} }
func TestGetVersion_NoVersion(t *testing.T) { func TestGetVersion_NoVersion(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
version, err := dao.GetVersion() version, err := dao.GetVersion()
require.Error(t, err) require.Error(t, err)
require.Equal(t, "", version.Value) require.Equal(t, "", version.Value)
} }
func TestGetVersion(t *testing.T) { func TestGetVersion(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
expected := Version{ expected := Version{
StoragePrefix: 0x42, StoragePrefix: 0x42,
P2PSigExtensions: true, P2PSigExtensions: true,
@ -130,14 +130,14 @@ func TestGetVersion(t *testing.T) {
require.Equal(t, expected, actual) require.Equal(t, expected, actual)
t.Run("invalid", func(t *testing.T) { t.Run("invalid", func(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
dao.Store.Put([]byte{byte(storage.SYSVersion)}, []byte("0.1.2\x00x")) dao.Store.Put([]byte{byte(storage.SYSVersion)}, []byte("0.1.2\x00x"))
_, err := dao.GetVersion() _, err := dao.GetVersion()
require.Error(t, err) require.Error(t, err)
}) })
t.Run("old format", func(t *testing.T) { t.Run("old format", func(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
dao.Store.Put([]byte{byte(storage.SYSVersion)}, []byte("0.1.2")) dao.Store.Put([]byte{byte(storage.SYSVersion)}, []byte("0.1.2"))
version, err := dao.GetVersion() version, err := dao.GetVersion()
@ -147,14 +147,14 @@ func TestGetVersion(t *testing.T) {
} }
func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) { func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
height, err := dao.GetCurrentBlockHeight() height, err := dao.GetCurrentBlockHeight()
require.Error(t, err) require.Error(t, err)
require.Equal(t, uint32(0), height) require.Equal(t, uint32(0), height)
} }
func TestGetCurrentHeaderHeight_Store(t *testing.T) { func TestGetCurrentHeaderHeight_Store(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
b := &block.Block{ b := &block.Block{
Header: block.Header{ Header: block.Header{
Script: transaction.Witness{ Script: transaction.Witness{
@ -170,8 +170,8 @@ func TestGetCurrentHeaderHeight_Store(t *testing.T) {
} }
func TestStoreAsTransaction(t *testing.T) { func TestStoreAsTransaction(t *testing.T) {
t.Run("P2PSigExtensions off", func(t *testing.T) { t.Run("no conflicts", func(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, false) dao := NewSimple(storage.NewMemoryStore(), false)
tx := transaction.New([]byte{byte(opcode.PUSH1)}, 1) tx := transaction.New([]byte{byte(opcode.PUSH1)}, 1)
tx.Signers = append(tx.Signers, transaction.Signer{}) tx.Signers = append(tx.Signers, transaction.Signer{})
tx.Scripts = append(tx.Scripts, transaction.Witness{}) tx.Scripts = append(tx.Scripts, transaction.Witness{})
@ -194,8 +194,8 @@ func TestStoreAsTransaction(t *testing.T) {
require.Equal(t, *aer, gotAppExecResult[0]) require.Equal(t, *aer, gotAppExecResult[0])
}) })
t.Run("P2PSigExtensions on", func(t *testing.T) { t.Run("with conflicts", func(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), false, true) dao := NewSimple(storage.NewMemoryStore(), false)
conflictsH := util.Uint256{1, 2, 3} conflictsH := util.Uint256{1, 2, 3}
signer1 := util.Uint160{1, 2, 3} signer1 := util.Uint160{1, 2, 3}
signer2 := util.Uint160{4, 5, 6} signer2 := util.Uint160{4, 5, 6}
@ -279,7 +279,7 @@ func TestStoreAsTransaction(t *testing.T) {
} }
func BenchmarkStoreAsTransaction(b *testing.B) { func BenchmarkStoreAsTransaction(b *testing.B) {
dao := NewSimple(storage.NewMemoryStore(), false, true) dao := NewSimple(storage.NewMemoryStore(), false)
tx := transaction.New([]byte{byte(opcode.PUSH1)}, 1) tx := transaction.New([]byte{byte(opcode.PUSH1)}, 1)
tx.Attributes = []transaction.Attribute{ tx.Attributes = []transaction.Attribute{
{ {
@ -324,7 +324,7 @@ func BenchmarkStoreAsTransaction(b *testing.B) {
func TestMakeStorageItemKey(t *testing.T) { func TestMakeStorageItemKey(t *testing.T) {
var id int32 = 5 var id int32 = 5
dao := NewSimple(storage.NewMemoryStore(), true, false) dao := NewSimple(storage.NewMemoryStore(), true)
expected := []byte{byte(storage.STStorage), 0, 0, 0, 0, 1, 2, 3} expected := []byte{byte(storage.STStorage), 0, 0, 0, 0, 1, 2, 3}
binary.LittleEndian.PutUint32(expected[1:5], uint32(id)) binary.LittleEndian.PutUint32(expected[1:5], uint32(id))
@ -343,7 +343,7 @@ func TestMakeStorageItemKey(t *testing.T) {
} }
func TestPutGetStateSyncPoint(t *testing.T) { func TestPutGetStateSyncPoint(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), true, false) dao := NewSimple(storage.NewMemoryStore(), true)
// empty store // empty store
_, err := dao.GetStateSyncPoint() _, err := dao.GetStateSyncPoint()
@ -358,7 +358,7 @@ func TestPutGetStateSyncPoint(t *testing.T) {
} }
func TestPutGetStateSyncCurrentBlockHeight(t *testing.T) { func TestPutGetStateSyncCurrentBlockHeight(t *testing.T) {
dao := NewSimple(storage.NewMemoryStore(), true, false) dao := NewSimple(storage.NewMemoryStore(), true)
// empty store // empty store
_, err := dao.GetStateSyncCurrentBlockHeight() _, err := dao.GetStateSyncCurrentBlockHeight()

View file

@ -72,7 +72,7 @@ func initCheckMultisigVMNoArgs(container *transaction.Transaction) *vm.VM {
ic := interop.NewContext( ic := interop.NewContext(
trigger.Verification, trigger.Verification,
fakechain.NewFakeChain(), fakechain.NewFakeChain(),
dao.NewSimple(storage.NewMemoryStore(), false, false), dao.NewSimple(storage.NewMemoryStore(), false),
interop.DefaultBaseExecFee, native.DefaultStoragePrice, nil, nil, nil, nil, interop.DefaultBaseExecFee, native.DefaultStoragePrice, nil, nil, nil, nil,
container, container,
nil) nil)
@ -178,7 +178,7 @@ func TestCheckSig(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
verifyFunc := ECDSASecp256r1CheckSig verifyFunc := ECDSASecp256r1CheckSig
d := dao.NewSimple(storage.NewMemoryStore(), false, false) d := dao.NewSimple(storage.NewMemoryStore(), false)
ic := &interop.Context{Network: uint32(netmode.UnitTestNet), DAO: d} ic := &interop.Context{Network: uint32(netmode.UnitTestNet), DAO: d}
runCase := func(t *testing.T, isErr bool, result any, args ...any) { runCase := func(t *testing.T, isErr bool, result any, args ...any) {
ic.SpawnVM() ic.SpawnVM()

View file

@ -18,7 +18,7 @@ import (
func TestDeployGetUpdateDestroyContract(t *testing.T) { func TestDeployGetUpdateDestroyContract(t *testing.T) {
mgmt := newManagement() mgmt := newManagement()
mgmt.Policy = newPolicy() mgmt.Policy = newPolicy()
d := dao.NewSimple(storage.NewMemoryStore(), false, false) d := dao.NewSimple(storage.NewMemoryStore(), false)
ic := &interop.Context{DAO: d} ic := &interop.Context{DAO: d}
err := mgmt.Initialize(ic) err := mgmt.Initialize(ic)
require.NoError(t, err) require.NoError(t, err)
@ -81,13 +81,13 @@ func TestDeployGetUpdateDestroyContract(t *testing.T) {
func TestManagement_Initialize(t *testing.T) { func TestManagement_Initialize(t *testing.T) {
t.Run("good", func(t *testing.T) { t.Run("good", func(t *testing.T) {
d := dao.NewSimple(storage.NewMemoryStore(), false, false) d := dao.NewSimple(storage.NewMemoryStore(), false)
mgmt := newManagement() mgmt := newManagement()
require.NoError(t, mgmt.InitializeCache(0, d)) require.NoError(t, mgmt.InitializeCache(0, d))
}) })
/* See #2801 /* See #2801
t.Run("invalid contract state", func(t *testing.T) { t.Run("invalid contract state", func(t *testing.T) {
d := dao.NewSimple(storage.NewMemoryStore(), false, false) d := dao.NewSimple(storage.NewMemoryStore(), false)
mgmt := newManagement() mgmt := newManagement()
d.PutStorageItem(mgmt.ID, []byte{PrefixContract}, state.StorageItem{0xFF}) d.PutStorageItem(mgmt.ID, []byte{PrefixContract}, state.StorageItem{0xFF})
require.Error(t, mgmt.InitializeCache(d)) require.Error(t, mgmt.InitializeCache(d))
@ -98,7 +98,7 @@ func TestManagement_Initialize(t *testing.T) {
func TestManagement_GetNEP17Contracts(t *testing.T) { func TestManagement_GetNEP17Contracts(t *testing.T) {
mgmt := newManagement() mgmt := newManagement()
mgmt.Policy = newPolicy() mgmt.Policy = newPolicy()
d := dao.NewSimple(storage.NewMemoryStore(), false, false) d := dao.NewSimple(storage.NewMemoryStore(), false)
err := mgmt.Initialize(&interop.Context{DAO: d}) err := mgmt.Initialize(&interop.Context{DAO: d})
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, mgmt.Policy.Initialize(&interop.Context{DAO: d})) require.NoError(t, mgmt.Policy.Initialize(&interop.Context{DAO: d}))

View file

@ -54,7 +54,7 @@ func TestModule_PR2019_discussion_r689629704(t *testing.T) {
syncPoint: 1000500, syncPoint: 1000500,
syncStage: headersSynced, syncStage: headersSynced,
syncInterval: 100500, syncInterval: 100500,
dao: dao.NewSimple(actualStorage, true, false), dao: dao.NewSimple(actualStorage, true),
mptpool: NewPool(), mptpool: NewPool(),
} }
stateSync.billet = mpt.NewBillet(sr, mpt.ModeLatest, stateSync.billet = mpt.NewBillet(sr, mpt.ModeLatest,