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

Tiered Storage #1832

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
nits
  • Loading branch information
RodrigoVillar committed Dec 13, 2024
commit b5aeaf14b36c993376b5319db92e74a48b1b9060
2 changes: 0 additions & 2 deletions api/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ func (j *JSONRPCServer) SimulateActions(

currentTime := time.Now().UnixMilli()
for _, action := range actions {
// This code block shouldn't use tstateview but rather, the simulated
// scope itself
actionOutput, err := action.Execute(ctx, j.vm.Rules(currentTime), tsv, currentTime, args.Actor, ids.Empty)

var actionResult SimulateActionResult
Expand Down
3 changes: 1 addition & 2 deletions chain/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
for k := range changedKeys {
if changedKeys[k].HasValue() {
if err := tsv.Insert(ctx, []byte(k), binary.BigEndian.AppendUint64(changedKeys[k].Value(), height)); err != nil {
// TODO: improve error message
return nil, nil, nil, fmt.Errorf("%w: unable to append suffix", err)
return nil, nil, nil, fmt.Errorf("%w: unable to append value suffix", err)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion state/tstate/tstate_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type TStateView struct {
// operations allows for reverting state to a certain point-in-time.
ops []*op

// TODO: add documentation for this field
// scope contains the state keys this view has access too along with the
// storage of the parent view.
scope scope.Scope

// Store which keys are modified and how large their values were.
Expand Down
8 changes: 6 additions & 2 deletions vm/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ func (vm *VM) State() (merkledb.MerkleDB, error) {

func (vm *VM) ImmutableState(ctx context.Context) (state.Immutable, error) {
ts := tstate.New(0)
state, err := vm.State()
st, err := vm.State()
if err != nil {
return nil, err
}
return ts.ExportMerkleDBView(ctx, vm.tracer, state)
view, err := ts.ExportMerkleDBView(ctx, vm.tracer, st)
if err != nil {
return nil, err
}
return state.NewTranslatedImmutable(view), nil
}

func (vm *VM) Mempool() chain.Mempool {
Expand Down
47 changes: 25 additions & 22 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,28 +684,7 @@ func (vm *VM) ReadState(ctx context.Context, keys [][]byte) ([][]byte, []error)
// Atomic read to ensure consistency
values, errs := vm.stateDB.GetValues(ctx, keys)

returnValues := make([][]byte, 0, len(values))
returnErrors := make([]error, 0, len(values))

// TODO: could we make this cleaner?
// Assuming that len(values) == len(errs)
for i, v := range values {
if errs[i] != nil {
returnErrors = append(returnErrors, errs[i])
returnValues = append(returnValues, nil)
continue
}
// Time to unsuffix
if len(v) < consts.Uint64Len {
returnErrors = append(returnErrors, ErrValueTooShortForPrefix)
returnValues = append(returnValues, nil)
continue
}
returnValues = append(returnValues, v[:len(v)-consts.Uint64Len])
returnErrors = append(returnErrors, nil)
}

return returnValues, returnErrors
return vm.removeSuffixes(values, errs)
}

func (vm *VM) SetState(ctx context.Context, state snow.State) error {
Expand Down Expand Up @@ -1233,6 +1212,30 @@ func (vm *VM) restoreAcceptedQueue(ctx context.Context) error {
return nil
}

func (*VM) removeSuffixes(values [][]byte, errors []error) ([][]byte, []error) {
returnValues := make([][]byte, 0, len(values))
returnErrors := make([]error, 0, len(values))

// Invariant: len(values) == len(errors)
for i, v := range values {
if errors[i] != nil {
returnErrors = append(returnErrors, errors[i])
returnValues = append(returnValues, nil)
continue
}
// Time to unsuffix
if len(v) < consts.Uint64Len {
returnErrors = append(returnErrors, ErrValueTooShortForPrefix)
returnValues = append(returnValues, nil)
continue
}
returnValues = append(returnValues, v[:len(v)-consts.Uint64Len])
returnErrors = append(returnErrors, nil)
}

return returnValues, returnErrors
}

Comment on lines +1215 to +1238
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be coupled to the VM? In effect, we are grabbing the state from the VM's merkledb, could we apply the same transformation we normally do to a merkledb view before giving actions read access to it?

This function gives raw access to the statedb, should we leave to the caller (that knows where the information is used) to strip the suffixes / transform the data as needed?

// Fatal logs the provided message and then panics to force an exit.
//
// While we could attempt a graceful shutdown, it is not clear that
Expand Down
Loading