-
Notifications
You must be signed in to change notification settings - Fork 644
Add gas constraints to L2 Pricing storage #3861
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2025, Offchain Labs, Inc. | ||
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md | ||
|
||
package storage | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
) | ||
|
||
const storageVectorNumItensOffset uint64 = 0 | ||
gligneul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// SubStorageVector is a storage space that contains a vector of sub-storages. | ||
// It keeps track of the number of sub-storages and It is possible to push and pop them. | ||
type SubStorageVector struct { | ||
storage *Storage | ||
length StorageBackedUint64 | ||
} | ||
|
||
// OpenSubStorageVector creates a SubStorageVector in given the root storage. | ||
func OpenSubStorageVector(sto *Storage) *SubStorageVector { | ||
return &SubStorageVector{ | ||
sto.WithoutCache(), | ||
sto.OpenStorageBackedUint64(storageVectorNumItensOffset), | ||
} | ||
} | ||
|
||
// Length returns the number of sub-storages. | ||
func (v *SubStorageVector) Length() (uint64, error) { | ||
length, err := v.length.Get() | ||
if err != nil { | ||
return 0, err | ||
} | ||
return length, err | ||
} | ||
|
||
// Push adds a new sub-storage at the end of the vector and return it. | ||
func (v *SubStorageVector) Push() (*Storage, error) { | ||
length, err := v.length.Get() | ||
if err != nil { | ||
return nil, err | ||
} | ||
id := binary.BigEndian.AppendUint64(nil, length) | ||
subStorage := v.storage.OpenSubStorage(id) | ||
if err := v.length.Set(length + 1); err != nil { | ||
eljobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, err | ||
} | ||
return subStorage, nil | ||
} | ||
|
||
// Pop removes the last sub-storage from the end of the vector and return it. | ||
func (v *SubStorageVector) Pop() (*Storage, error) { | ||
length, err := v.length.Get() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if length == 0 { | ||
return nil, errors.New("sub-storage vector: can't pop empty") | ||
} | ||
id := binary.BigEndian.AppendUint64(nil, length-1) | ||
subStorage := v.storage.OpenSubStorage(id) | ||
if err := v.length.Set(length - 1); err != nil { | ||
return nil, err | ||
} | ||
return subStorage, nil | ||
} | ||
|
||
// At returns the substorage at the given index. | ||
// NOTE: This function does not verify out-of-bounds. | ||
func (v *SubStorageVector) At(i uint64) *Storage { | ||
id := binary.BigEndian.AppendUint64(nil, i) | ||
subStorage := v.storage.OpenSubStorage(id) | ||
return subStorage | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2021-2022, Offchain Labs, Inc. | ||
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md | ||
|
||
package arbos | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/offchainlabs/nitro/arbos/arbosState" | ||
"github.com/offchainlabs/nitro/arbos/storage" | ||
"github.com/offchainlabs/nitro/arbos/util" | ||
) | ||
|
||
// This file contains tests for the storage submodule, but it needs to be in the top-level module to | ||
// avoid cross dependencies. | ||
|
||
func TestSubStorageVector(t *testing.T) { | ||
state, statedb := arbosState.NewArbosMemoryBackedArbOSState() | ||
sto := state.BackingStorage().OpenCachedSubStorage([]byte{}) | ||
vec := storage.OpenSubStorageVector(sto) | ||
|
||
stateBefore := statedb.IntermediateRoot(false) | ||
|
||
getLength := func() uint64 { | ||
length, err := vec.Length() | ||
Require(t, err) | ||
return length | ||
} | ||
|
||
// Check it starts empty | ||
if got, want := getLength(), uint64(0); got != want { | ||
t.Fatalf("wrong vector length: got %v, want %v", got, want) | ||
} | ||
|
||
// Adds n elements | ||
const n = uint64(100) | ||
for i := range n { | ||
subStorage, err := vec.Push() | ||
Require(t, err) | ||
err = subStorage.SetByUint64(0, util.UintToHash(i)) | ||
Require(t, err) | ||
} | ||
|
||
// Check the length with n elements | ||
if got, want := getLength(), uint64(100); got != want { | ||
t.Fatalf("wrong vector length: got %v, want %v", got, want) | ||
} | ||
|
||
// Check each element | ||
for i := range n { | ||
subStorage := vec.At(i) | ||
got, err := subStorage.GetByUint64(0) | ||
Require(t, err) | ||
want := util.UintToHash(i) | ||
if got != want { | ||
t.Errorf("wrong sub-storage: got %v, want %v", got, want) | ||
} | ||
} | ||
|
||
// Pop each element and clear storage | ||
for i := range n { | ||
subStorage, err := vec.Pop() | ||
Require(t, err) | ||
got, err := subStorage.GetByUint64(0) | ||
Require(t, err) | ||
want := util.UintToHash(n - i - 1) | ||
if got != want { | ||
t.Errorf("wrong sub-storage: got %v, want %v", got, want) | ||
} | ||
err = subStorage.ClearByUint64(0) | ||
Require(t, err) | ||
} | ||
|
||
// Check it ends empty | ||
if got, want := getLength(), uint64(0); got != want { | ||
t.Fatalf("wrong vector length: got %v, want %v", got, want) | ||
} | ||
if stateBefore != statedb.IntermediateRoot(false) { | ||
Fail(t, "state is not clear") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.