Skip to content

Commit

Permalink
dao: simplify NewSimple()
Browse files Browse the repository at this point in the history
We no longer need P2PSigExtension flag here, conflicts attribute is a part
of the normal protocol.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Sep 4, 2023
1 parent f3c1283 commit fff7e91
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/compiler/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/blockchain_neotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
9 changes: 4 additions & 5 deletions pkg/core/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions pkg/core/dao/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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{}
Expand All @@ -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{}
Expand All @@ -63,15 +63,15 @@ 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)
require.Nil(t, block)
}

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{
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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{
Expand All @@ -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{})
Expand All @@ -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}
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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))
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/interop/crypto/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions pkg/core/native/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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}))
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/statesync/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fff7e91

Please sign in to comment.