Skip to content

Commit

Permalink
flatten layout
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoVillar committed Oct 1, 2024
1 parent 6685ed5 commit 00dd379
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 135 deletions.
12 changes: 7 additions & 5 deletions chain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ava-labs/hypersdk/internal/window"
"github.com/ava-labs/hypersdk/internal/workers"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/layout"
"github.com/ava-labs/hypersdk/utils"
)

Expand Down Expand Up @@ -422,7 +423,7 @@ func (b *StatefulBlock) innerVerify(ctx context.Context, vctx VerifyContext) err
}

// Fetch parent height key and ensure block height is valid
heightKey := b.vm.StateLayout().HeightKey()
heightKey := HeightKey(layout.HeightPrefix())
parentHeightRaw, err := parentView.GetValue(ctx, heightKey)
if err != nil {
return err
Expand All @@ -439,7 +440,7 @@ func (b *StatefulBlock) innerVerify(ctx context.Context, vctx VerifyContext) err
//
// Parent may not be available (if we preformed state sync), so we
// can't rely on being able to fetch it during verification.
timestampKey := b.vm.StateLayout().TimestampKey()
timestampKey := TimestampKey(layout.TimestampPrefix())
parentTimestampRaw, err := parentView.GetValue(ctx, timestampKey)
if err != nil {
return err
Expand Down Expand Up @@ -482,7 +483,7 @@ func (b *StatefulBlock) innerVerify(ctx context.Context, vctx VerifyContext) err
}

// Compute next unit prices to use
feeKey := b.vm.StateLayout().FeeKey()
feeKey := FeeKey(layout.FeePrefix())
feeRaw, err := parentView.GetValue(ctx, feeKey)
if err != nil {
return err
Expand Down Expand Up @@ -735,7 +736,7 @@ func (b *StatefulBlock) View(ctx context.Context, verify bool) (state.View, erro
if err != nil {
return nil, err
}
acceptedHeightRaw, err := acceptedState.Get(b.vm.StateLayout().HeightKey())
acceptedHeightRaw, err := acceptedState.Get(HeightKey(layout.HeightPrefix()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -857,7 +858,8 @@ func (b *StatefulBlock) IsRepeat(
}

func (b *StatefulBlock) GetVerifyContext(ctx context.Context, blockHeight uint64, parent ids.ID) (VerifyContext, error) {
// If [blockHeight] is 0, we throw an error because there is no pre-genesis verification context.
// If [blockHeight] is 0, we throw an error because there is no pre-genesis
// verification context.
if blockHeight == 0 {
return nil, errors.New("cannot get context of genesis block")
}
Expand Down
7 changes: 4 additions & 3 deletions chain/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ava-labs/hypersdk/internal/fees"
"github.com/ava-labs/hypersdk/keys"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/layout"
"github.com/ava-labs/hypersdk/state/tstate"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func BuildBlock(
}

// Compute next unit prices to use
feeKey := vm.StateLayout().FeeKey()
feeKey := FeeKey(layout.FeePrefix())
feeRaw, err := parentView.GetValue(ctx, feeKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -372,9 +373,9 @@ func BuildBlock(
}

// Update chain metadata
heightKey := b.vm.StateLayout().HeightKey()
heightKey := HeightKey(layout.HeightPrefix())
heightKeyStr := string(heightKey)
timestampKey := b.vm.StateLayout().TimestampKey()
timestampKey := TimestampKey(layout.TimestampPrefix())
timestampKeyStr := string(timestampKey)
feeKeyStr := string(feeKey)

Expand Down
1 change: 0 additions & 1 deletion chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ type VM interface {

State() (merkledb.MerkleDB, error)
BalanceHandler() BalanceHandler
StateLayout() state.Layout

Mempool() Mempool
IsRepeat(context.Context, []*Transaction, set.Bits, bool) set.Bits
Expand Down
3 changes: 2 additions & 1 deletion examples/morpheusvm/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/layout"

smath "github.com/ava-labs/avalanchego/utils/math"
)

type ReadState func(context.Context, [][]byte) ([][]byte, []error)

var (
balancePrefix = consts.LowestAvailablePrefix()
balancePrefix = layout.LowestAvailablePrefix()

VMSpecificPrefixes = []byte{balancePrefix}
)
Expand Down
98 changes: 0 additions & 98 deletions state/layout.go

This file was deleted.

66 changes: 66 additions & 0 deletions state/layout/layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package layout

import (
"errors"
"fmt"

"github.com/ava-labs/avalanchego/utils/set"
)

const (
defaultHeightStatePrefix byte = 0x0
defaultTimestampStatePrefix byte = 0x1
defaultFeeStatePrefix byte = 0x2
lowestAvailablePrefix byte = 0x3
)

var ErrConflictingKey = errors.New("conflicting key")

func IsValidLayout(vmSpecificPrefixes []byte) error {
prefixes := []byte{
defaultHeightStatePrefix,
defaultTimestampStatePrefix,
defaultFeeStatePrefix,
}

prefixes = append(prefixes, vmSpecificPrefixes...)

verifiedPrefixes := set.Set[string]{}

for _, k := range prefixes {
keyString := string(k)

for prefix := range verifiedPrefixes {
if prefix == keyString {
return fmt.Errorf("invalid state key %s: %w", string(k), ErrConflictingKey)
}
}

verifiedPrefixes.Add(keyString)
}

return nil
}

func HeightPrefix() []byte {
return []byte{defaultHeightStatePrefix}
}

func TimestampPrefix() []byte {
return []byte{defaultTimestampStatePrefix}
}

func FeePrefix() []byte {
return []byte{defaultFeeStatePrefix}
}

func ConflictingPrefix(prefix byte) bool {
return defaultHeightStatePrefix == prefix || defaultTimestampStatePrefix == prefix || defaultFeeStatePrefix == prefix
}

func LowestAvailablePrefix() byte {
return lowestAvailablePrefix
}
7 changes: 2 additions & 5 deletions vm/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ava-labs/hypersdk/internal/gossiper"
"github.com/ava-labs/hypersdk/internal/workers"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/layout"
"github.com/ava-labs/hypersdk/state/tstate"

internalfees "github.com/ava-labs/hypersdk/internal/fees"
Expand Down Expand Up @@ -385,10 +386,6 @@ func (vm *VM) BalanceHandler() chain.BalanceHandler {
return vm.balanceHandler
}

func (vm *VM) StateLayout() state.Layout {
return vm.stateLayout
}

func (vm *VM) RecordRootCalculated(t time.Duration) {
vm.metrics.rootCalculated.Observe(float64(t))
}
Expand Down Expand Up @@ -470,7 +467,7 @@ func (vm *VM) RecordClearedMempool() {
}

func (vm *VM) UnitPrices(context.Context) (fees.Dimensions, error) {
v, err := vm.stateDB.Get(chain.FeeKey(vm.StateLayout().FeeKey()))
v, err := vm.stateDB.Get(chain.FeeKey(layout.FeePrefix()))
if err != nil {
return fees.Dimensions{}, err
}
Expand Down
30 changes: 8 additions & 22 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/ava-labs/hypersdk/internal/validators"
"github.com/ava-labs/hypersdk/internal/workers"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/layout"
"github.com/ava-labs/hypersdk/storage"
"github.com/ava-labs/hypersdk/utils"

Expand All @@ -61,12 +62,6 @@ const (
MinAcceptedBlockWindow = 1024
)

const (
defaultHeightStatePrefix byte = 0x0
defaultTimestampStatePrefix byte = 0x1
defaultFeeStatePrefix byte = 0x2
)

type VM struct {
DataDir string
v *version.Semantic
Expand Down Expand Up @@ -95,12 +90,11 @@ type VM struct {
stateDB merkledb.MerkleDB
vmDB database.Database
handlers map[string]http.Handler
balanceHandler chain.BalanceHandler
balanceHandler chain.BalanceHandler
actionRegistry chain.ActionRegistry
authRegistry chain.AuthRegistry
outputRegistry chain.OutputRegistry
authEngine map[uint8]AuthEngine
stateLayout state.Layout

tracer avatrace.Tracer
mempool *mempool.Mempool[*chain.Transaction]
Expand Down Expand Up @@ -172,21 +166,13 @@ func New(
allocatedNamespaces.Add(option.Namespace)
}

stateLayout, err := state.NewLayout(
defaultHeightStatePrefix,
defaultTimestampStatePrefix,
defaultFeeStatePrefix,
vmSpecificPrefixes,
)

if err != nil {
if err := layout.IsValidLayout(vmSpecificPrefixes); err != nil {
return nil, err
}

return &VM{
v: v,
balanceHandler: balanceHandler,
stateLayout: stateLayout,
balanceHandler: balanceHandler,
config: NewConfig(),
actionRegistry: actionRegistry,
authRegistry: authRegistry,
Expand Down Expand Up @@ -400,10 +386,10 @@ func (vm *VM) Initialize(

// Update chain metadata
sps = state.NewSimpleMutable(vm.stateDB)
if err := sps.Insert(ctx, chain.HeightKey(vm.stateLayout.HeightKey()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
if err := sps.Insert(ctx, chain.HeightKey(layout.HeightPrefix()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
return err
}
if err := sps.Insert(ctx, chain.TimestampKey(vm.stateLayout.TimestampKey()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
if err := sps.Insert(ctx, chain.TimestampKey(layout.HeightPrefix()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
return err
}
genesisRules := vm.Rules(0)
Expand All @@ -413,7 +399,7 @@ func (vm *VM) Initialize(
feeManager.SetUnitPrice(i, minUnitPrice[i])
snowCtx.Log.Info("set genesis unit price", zap.Int("dimension", int(i)), zap.Uint64("price", feeManager.UnitPrice(i)))
}
if err := sps.Insert(ctx, chain.FeeKey(vm.stateLayout.FeeKey()), feeManager.Bytes()); err != nil {
if err := sps.Insert(ctx, chain.FeeKey(layout.FeePrefix()), feeManager.Bytes()); err != nil {
return err
}

Expand Down Expand Up @@ -897,7 +883,7 @@ func (vm *VM) Submit(
// This will error if a block does not yet have processed state.
return []error{err}
}
feeRaw, err := view.GetValue(ctx, chain.FeeKey(vm.stateLayout.FeeKey()))
feeRaw, err := view.GetValue(ctx, chain.FeeKey(layout.FeePrefix()))
if err != nil {
return []error{err}
}
Expand Down

0 comments on commit 00dd379

Please sign in to comment.