Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove entry helpers #299

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func BenchmarkBalanceTransfer(b *testing.B) {
fmt.Println(s.Stats())

genesisBalance, genesisExists := s.Query(txtypes.GenesisAccount, hashBuff, hashMatches)
require.True(b, genesisExists)
require.False(b, genesisExists)
require.Equal(b, txtypes.Amount(0), genesisBalance)

for _, addr := range accounts {
Expand Down
62 changes: 19 additions & 43 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,21 @@ func (db *DB) deleteSnapshot(
var snapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
snapshotSpace.Find(&snapshotInfoValue, snapshotID, space.StageData, snapshotHashBuff, snapshotHashMatches)

if exists := snapshotInfoValue.Exists(snapshotHashBuff, snapshotHashMatches); !exists {
if exists := snapshotSpace.KeyExists(&snapshotInfoValue, snapshotHashBuff, snapshotHashMatches); !exists {
return errors.Errorf("snapshot %d to delete does not exist", snapshotID)
}
snapshotInfo := snapshotInfoValue.Value(snapshotHashBuff, snapshotHashMatches)
snapshotInfo := snapshotSpace.ReadKey(&snapshotInfoValue, snapshotHashBuff, snapshotHashMatches)

snapshotInfoValue.Delete(tx, snapshotHashBuff, snapshotHashMatches)
snapshotSpace.DeleteKey(&snapshotInfoValue, tx, snapshotHashBuff, snapshotHashMatches)

var nextSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
snapshotSpace.Find(&nextSnapshotInfoValue, snapshotInfo.NextSnapshotID, space.StageData, snapshotHashBuff,
snapshotHashMatches)

if exists := nextSnapshotInfoValue.Exists(snapshotHashBuff, snapshotHashMatches); !exists {
if exists := snapshotSpace.KeyExists(&nextSnapshotInfoValue, snapshotHashBuff, snapshotHashMatches); !exists {
return errors.Errorf("next snapshot %d does not exist", snapshotID)
}
nextSnapshotInfo := nextSnapshotInfoValue.Value(snapshotHashBuff, snapshotHashMatches)
nextSnapshotInfo := snapshotSpace.ReadKey(&nextSnapshotInfoValue, snapshotHashBuff, snapshotHashMatches)

deallocationListsRoot := types.NodeRoot{
Pointer: &snapshotInfo.DeallocationRoot,
Expand Down Expand Up @@ -273,26 +273,16 @@ func (db *DB) deleteSnapshot(
var deallocationListValue space.Entry[deallocationKey, list.Pointer]
deallocationLists.Find(&deallocationListValue, nextDeallocSnapshot.Key, space.StageData, deallocationHashBuff,
deallocationHashMatches)
if err := deallocationListValue.Set(
tx,
volatileAllocator,
nextDeallocSnapshot.Value,
deallocationHashBuff,
deallocationHashMatches,
); err != nil {
if err := deallocationLists.SetKey(&deallocationListValue, tx, volatileAllocator, nextDeallocSnapshot.Value,
deallocationHashBuff, deallocationHashMatches); err != nil {
return err
}
}

nextSnapshotInfo.DeallocationRoot = snapshotInfo.DeallocationRoot
nextSnapshotInfo.PreviousSnapshotID = snapshotInfo.PreviousSnapshotID
if err := nextSnapshotInfoValue.Set(
tx,
volatileAllocator,
nextSnapshotInfo,
snapshotHashBuff,
snapshotHashMatches,
); err != nil {
if err := snapshotSpace.SetKey(&nextSnapshotInfoValue, tx, volatileAllocator, nextSnapshotInfo, snapshotHashBuff,
snapshotHashMatches); err != nil {
return err
}

Expand All @@ -301,20 +291,16 @@ func (db *DB) deleteSnapshot(
snapshotSpace.Find(&previousSnapshotInfoValue, snapshotInfo.PreviousSnapshotID, space.StageData,
snapshotHashBuff, snapshotHashMatches)

if exists := previousSnapshotInfoValue.Exists(snapshotHashBuff, snapshotHashMatches); !exists {
if exists := snapshotSpace.KeyExists(&previousSnapshotInfoValue, snapshotHashBuff,
snapshotHashMatches); !exists {
return errors.Errorf("previous snapshot %d does not exist", snapshotID)
}

previousSnapshotInfo := previousSnapshotInfoValue.Value(snapshotHashBuff, snapshotHashMatches)
previousSnapshotInfo := snapshotSpace.ReadKey(&previousSnapshotInfoValue, snapshotHashBuff, snapshotHashMatches)
previousSnapshotInfo.NextSnapshotID = snapshotInfo.NextSnapshotID

if err := previousSnapshotInfoValue.Set(
tx,
volatileAllocator,
previousSnapshotInfo,
snapshotHashBuff,
snapshotHashMatches,
); err != nil {
if err := snapshotSpace.SetKey(&previousSnapshotInfoValue, tx, volatileAllocator, previousSnapshotInfo,
snapshotHashBuff, snapshotHashMatches); err != nil {
return err
}
}
Expand Down Expand Up @@ -362,13 +348,8 @@ func (db *DB) commit(
SnapshotID: snapshotID,
}, space.StageData, deallocationHashBuff,
deallocationHashMatches)
if err := deallocationListValue.Set(
tx,
volatileAllocator,
*listRoot,
deallocationHashBuff,
deallocationHashMatches,
); err != nil {
if err := deallocationListSpace.SetKey(&deallocationListValue, tx, volatileAllocator, *listRoot,
deallocationHashBuff, deallocationHashMatches); err != nil {
return err
}

Expand Down Expand Up @@ -409,13 +390,8 @@ func (db *DB) commit(
var nextSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
snapshotSpace.Find(&nextSnapshotInfoValue, db.singularityNode.LastSnapshotID, space.StageData, snapshotHashBuff,
snapshotHashMatches)
if err := nextSnapshotInfoValue.Set(
tx,
volatileAllocator,
db.snapshotInfo,
snapshotHashBuff,
snapshotHashMatches,
); err != nil {
if err := snapshotSpace.SetKey(&nextSnapshotInfoValue, tx, volatileAllocator, db.snapshotInfo, snapshotHashBuff,
snapshotHashMatches); err != nil {
return err
}

Expand Down Expand Up @@ -537,7 +513,7 @@ func (db *DB) executeTransactions(ctx context.Context, pipeReader *pipeline.Read
if req.Transaction != nil {
switch tx := req.Transaction.(type) {
case *transfer.Tx:
if err := tx.Execute(req, volatileAllocator, hashBuff, hashMatches); err != nil {
if err := tx.Execute(s, req, volatileAllocator, hashBuff, hashMatches); err != nil {
return err
}
case *deleteSnapshotTx:
Expand Down
85 changes: 41 additions & 44 deletions space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Space[K, V]) Find(
hashBuff []byte,
hashMatches []uint64,
) {
if v.space == nil {
if v.keyHash == 0 {
keyHash := hashKey(&key, nil, 0)
s.initEntry(v, key, keyHash, stage)
}
Expand All @@ -96,6 +96,46 @@ func (s *Space[K, V]) Query(key K, hashBuff []byte, hashMatches []uint64) (V, bo
return s.query(key, keyHash, hashBuff, hashMatches, hashKey)
}

// KeyExists checks if key exists in the space.
func (s *Space[K, V]) KeyExists(
v *Entry[K, V],
hashBuff []byte,
hashMatches []uint64,
) bool {
return s.keyExists(v, hashBuff, hashMatches, hashKey)
}

// ReadKey retrieves value of a key.
func (s *Space[K, V]) ReadKey(
v *Entry[K, V],
hashBuff []byte,
hashMatches []uint64,
) V {
return s.readKey(v, hashBuff, hashMatches, hashKey)
}

// DeleteKey deletes the key from the space.
func (s *Space[K, V]) DeleteKey(
v *Entry[K, V],
tx *pipeline.TransactionRequest,
hashBuff []byte,
hashMatches []uint64,
) {
s.deleteKey(v, tx, hashBuff, hashMatches, hashKey)
}

// SetKey sets key in the space.
func (s *Space[K, V]) SetKey(
v *Entry[K, V],
tx *pipeline.TransactionRequest,
allocator *alloc.Allocator[types.VolatileAddress],
value V,
hashBuff []byte,
hashMatches []uint64,
) error {
return s.setKey(v, tx, allocator, value, hashBuff, hashMatches, hashKey)
}

// Stats returns space-related statistics.
func (s *Space[K, V]) Stats() (uint64, uint64, uint64, uint64, float64) {
if isFree(s.config.SpaceRoot.Pointer.VolatileAddress) {
Expand Down Expand Up @@ -195,7 +235,6 @@ func (s *Space[K, V]) initEntry(
) {
initBytes := unsafe.Slice((*byte)(unsafe.Pointer(v)), s.initSize)
copy(initBytes, s.defaultInit)
v.space = s
v.keyHash = keyHash
v.item.Key = key
v.stage = stage
Expand Down Expand Up @@ -681,7 +720,6 @@ func (s *Space[K, V]) detectUpdate(v *Entry[K, V]) {
type Entry[K, V comparable] struct {
storeRequest pipeline.StoreRequest

space *Space[K, V]
itemP *DataItem[K, V]
keyHashP *types.KeyHash
keyHash types.KeyHash
Expand All @@ -695,47 +733,6 @@ type Entry[K, V comparable] struct {
level uint8
}

// Value returns the value from entry.
func (v *Entry[K, V]) Value(
hashBuff []byte,
hashMatches []uint64,
) V {
return v.space.readKey(v, hashBuff, hashMatches, hashKey[K])
}

// Key returns the key from entry.
func (v *Entry[K, V]) Key() K {
return v.item.Key
}

// Exists returns true if entry exists in the space.
func (v *Entry[K, V]) Exists(
hashBuff []byte,
hashMatches []uint64,
) bool {
return v.space.keyExists(v, hashBuff, hashMatches, hashKey[K])
}

// Set sts value for entry.
func (v *Entry[K, V]) Set(
tx *pipeline.TransactionRequest,
allocator *alloc.Allocator[types.VolatileAddress],
value V,
hashBuff []byte,
hashMatches []uint64,
) error {
return v.space.setKey(v, tx, allocator, value, hashBuff, hashMatches, hashKey[K])
}

// Delete deletes the entry.
func (v *Entry[K, V]) Delete(
tx *pipeline.TransactionRequest,
hashBuff []byte,
hashMatches []uint64,
) {
v.space.deleteKey(v, tx, hashBuff, hashMatches, hashKey[K])
}

// IteratorAndDeallocator iterates over items and deallocates space.
func IteratorAndDeallocator[K, V comparable](
spaceRoot types.Pointer,
Expand Down
8 changes: 1 addition & 7 deletions tx/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ func (t *Tx) Execute(
var v space.Entry[txtypes.Account, txtypes.Amount]
s.Find(&v, a.Account, space.StageData, hashBuff, hashMatches)

if err := v.Set(
tx,
allocator,
a.Amount,
hashBuff,
hashMatches,
); err != nil {
if err := s.SetKey(&v, tx, allocator, a.Amount, hashBuff, hashMatches); err != nil {
return err
}
}
Expand Down
34 changes: 17 additions & 17 deletions tx/transfer/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,38 @@ func (t *Tx) Prepare(

// Execute executes transaction.
func (t *Tx) Execute(
s *space.Space[txtypes.Account, txtypes.Amount],
tx *pipeline.TransactionRequest,
allocator *alloc.Allocator[types.VolatileAddress],
hashBuff []byte,
hashMatches []uint64,
) error {
fromBalance := t.from.Value(hashBuff, hashMatches)
fromBalance := s.ReadKey(&t.from, hashBuff, hashMatches)
if fromBalance < t.Amount {
return errors.Errorf("sender's balance is too low, balance: %d, amount to send: %d", fromBalance, t.Amount)
}

toBalance := t.to.Value(hashBuff, hashMatches)
toBalance := s.ReadKey(&t.to, hashBuff, hashMatches)
if math.MaxUint64-toBalance < t.Amount {
return errors.Errorf(
"transfer cannot be executed because it would cause an overflow on the recipient's balance, balance: %d, amount to send: %d", //nolint:lll
toBalance, t.Amount)
}

if err := t.from.Set(
tx,
allocator,
fromBalance-t.Amount,
hashBuff,
hashMatches,
); err != nil {
return err
fromBalance -= t.Amount
toBalance += t.Amount

if fromBalance == 0 {
s.DeleteKey(&t.from, tx, hashBuff, hashMatches)
} else {
if err := s.SetKey(&t.from, tx, allocator, fromBalance, hashBuff, hashMatches); err != nil {
return err
}
}

if toBalance == 0 {
s.DeleteKey(&t.to, tx, hashBuff, hashMatches)
}

return t.to.Set(
tx,
allocator,
toBalance+t.Amount,
hashBuff,
hashMatches,
)
return s.SetKey(&t.to, tx, allocator, toBalance, hashBuff, hashMatches)
}
Loading