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()
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))
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{
config: cfg,
dao: dao.NewSimple(s, cfg.StateRootInHeader, cfg.P2PSigExtensions),
persistent: dao.NewSimple(s, cfg.StateRootInHeader, cfg.P2PSigExtensions),
dao: dao.NewSimple(s, cfg.StateRootInHeader),
persistent: dao.NewSimple(s, cfg.StateRootInHeader),
store: s,
stopCh: 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)
}
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
// Initialize native cache before passing DAO to interop context constructor, because
// 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) {
ps = newPS(t)
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{
Value: "0.0.0",
})

View file

@ -62,17 +62,16 @@ type NativeContractCache interface {
}
// 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)
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{
Version: Version{
StoragePrefix: storage.STStorage,
StateRootInHeader: stateRootInHeader,
P2PSigExtensions: p2pSigExtensions,
},
Store: st,
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
// MemCachedStore around the current DAO Store.
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.nativeCachePS = dao
return d

View file

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

View file

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

View file

@ -18,7 +18,7 @@ import (
func TestDeployGetUpdateDestroyContract(t *testing.T) {
mgmt := newManagement()
mgmt.Policy = newPolicy()
d := dao.NewSimple(storage.NewMemoryStore(), false, false)
d := dao.NewSimple(storage.NewMemoryStore(), false)
ic := &interop.Context{DAO: d}
err := mgmt.Initialize(ic)
require.NoError(t, err)
@ -81,13 +81,13 @@ func TestDeployGetUpdateDestroyContract(t *testing.T) {
func TestManagement_Initialize(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()
require.NoError(t, mgmt.InitializeCache(0, d))
})
/* See #2801
t.Run("invalid contract state", func(t *testing.T) {
d := dao.NewSimple(storage.NewMemoryStore(), false, false)
d := dao.NewSimple(storage.NewMemoryStore(), false)
mgmt := newManagement()
d.PutStorageItem(mgmt.ID, []byte{PrefixContract}, state.StorageItem{0xFF})
require.Error(t, mgmt.InitializeCache(d))
@ -98,7 +98,7 @@ func TestManagement_Initialize(t *testing.T) {
func TestManagement_GetNEP17Contracts(t *testing.T) {
mgmt := newManagement()
mgmt.Policy = newPolicy()
d := dao.NewSimple(storage.NewMemoryStore(), false, false)
d := dao.NewSimple(storage.NewMemoryStore(), false)
err := mgmt.Initialize(&interop.Context{DAO: d})
require.NoError(t, err)
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,
syncStage: headersSynced,
syncInterval: 100500,
dao: dao.NewSimple(actualStorage, true, false),
dao: dao.NewSimple(actualStorage, true),
mptpool: NewPool(),
}
stateSync.billet = mpt.NewBillet(sr, mpt.ModeLatest,