Skip to content

Commit

Permalink
remove mmpa
Browse files Browse the repository at this point in the history
  • Loading branch information
amsanghi committed Jan 31, 2024
1 parent 19a3868 commit e759768
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 138 deletions.
2 changes: 1 addition & 1 deletion bold
Submodule bold updated 89 files
+2 −40 api/BUILD.bazel
+0 −42 api/assertions.go
+9 −0 api/backend/BUILD.bazel
+290 −0 api/backend/backend.go
+0 −23 api/data.go
+0 −70 api/data_test.go
+26 −1 api/db/BUILD.bazel
+773 −0 api/db/db.go
+530 −0 api/db/db_test.go
+55 −26 api/db/schema.go
+0 −217 api/edges.go
+0 −38 api/edges_test.go
+0 −7 api/log.go
+0 −35 api/method_assertions.go
+0 −141 api/method_assertions_test.go
+0 −322 api/method_database.go
+0 −156 api/method_edges.go
+0 −138 api/method_edges_test.go
+0 −11 api/method_healthz.go
+0 −28 api/method_healthz_test.go
+0 −8 api/scraper/BUILD.bazel
+0 −3 api/scraper/scraper.go
+0 −144 api/server.go
+7 −1 api/server/BUILD.bazel
+456 −45 api/server/methods.go
+66 −1 api/server/server.go
+0 −91 api/server/types.go
+0 −11 api/server_helper_test.go
+0 −28 api/server_test.go
+81 −0 api/types.go
+3 −0 assertions/BUILD.bazel
+15 −4 assertions/poster.go
+90 −9 assertions/scanner.go
+6 −5 assertions/scanner_test.go
+24 −12 chain-abstraction/interfaces.go
+9 −13 chain-abstraction/sol-implementation/assertion_chain.go
+3 −3 chain-abstraction/sol-implementation/edge_challenge_manager.go
+30 −9 chain-abstraction/sol-implementation/types.go
+3 −2 challenge-manager/BUILD.bazel
+2 −0 challenge-manager/chain-watcher/BUILD.bazel
+126 −15 challenge-manager/chain-watcher/watcher.go
+1 −0 challenge-manager/challenge-tree/BUILD.bazel
+194 −0 challenge-manager/challenge-tree/add_edge.go
+50 −33 challenge-manager/challenge-tree/ancestors_test.go
+43 −12 challenge-manager/challenge-tree/generalized_path_timer.go
+74 −68 challenge-manager/challenge-tree/generalized_path_timer_test.go
+7 −7 challenge-manager/challenge-tree/local_timer.go
+28 −22 challenge-manager/challenge-tree/local_timer_test.go
+1 −1 challenge-manager/challenge-tree/mock/edge.go
+39 −228 challenge-manager/challenge-tree/tree.go
+37 −135 challenge-manager/challenge-tree/tree_test.go
+1 −1 challenge-manager/challenges.go
+12 −10 challenge-manager/edge-tracker/tracker.go
+33 −39 challenge-manager/manager.go
+3 −26 challenge-manager/manager_test.go
+18 −0 containers/in-progress-cache/BUILD.bazel
+91 −0 containers/in-progress-cache/cache.go
+67 −0 containers/in-progress-cache/cache_test.go
+4 −1 containers/threadsafe/BUILD.bazel
+27 −3 containers/threadsafe/map.go
+27 −3 containers/threadsafe/set.go
+3 −1 contracts/src/challengeV2/EdgeChallengeManager.sol
+36 −9 contracts/src/challengeV2/libraries/EdgeChallengeManagerLib.sol
+99 −19 contracts/test/challengeV2/EdgeChallengeManagerLib.t.sol
+5 −0 contracts/test/challengeV2/Utils.sol
+2 −12 deps.bzl
+3 −4 go.mod
+0 −4 go.sum
+1 −1 layer2-state-provider/BUILD.bazel
+52 −30 layer2-state-provider/history_commitment_provider.go
+0 −12 mmap/BUILD.bazel
+0 −35 mmap/mmap.go
+2 −1 nogo.json
+1 −1 runtime/retry.go
+0 −2 state-commitments/history/BUILD.bazel
+10 −12 state-commitments/history/commitments.go
+6 −10 state-commitments/history/commitments_test.go
+0 −2 state-commitments/inclusion-proofs/BUILD.bazel
+24 −35 state-commitments/inclusion-proofs/inclusion_proofs.go
+14 −18 state-commitments/inclusion-proofs/inclusion_proofs_test.go
+0 −2 state-commitments/prefix-proofs/BUILD.bazel
+6 −7 state-commitments/prefix-proofs/merkle_expansions.go
+7 −9 state-commitments/prefix-proofs/prefix_proofs.go
+3 −9 state-commitments/prefix-proofs/prefix_proofs_test.go
+30 −3 testing/endtoend/e2e_test.go
+21 −8 testing/mocks/mocks.go
+0 −1 testing/mocks/state-provider/BUILD.bazel
+6 −12 testing/mocks/state-provider/history_provider.go
+6 −14 testing/mocks/state-provider/layer2_state_provider.go
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ require (
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-sqlite3 v1.14.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.53 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
Expand Down Expand Up @@ -256,7 +257,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/samber/lo v1.36.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
Expand Down Expand Up @@ -315,7 +315,7 @@ require (
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
Expand All @@ -327,7 +327,7 @@ require (
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
384 changes: 380 additions & 4 deletions go.sum

Large diffs are not rendered by default.

38 changes: 16 additions & 22 deletions staker/challenge-cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import (

protocol "github.com/OffchainLabs/bold/chain-abstraction"
l2stateprovider "github.com/OffchainLabs/bold/layer2-state-provider"
"github.com/OffchainLabs/bold/mmap"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)
Expand All @@ -60,8 +58,8 @@ var (

// HistoryCommitmentCacher can retrieve history commitment state roots given lookup keys.
type HistoryCommitmentCacher interface {
Get(lookup *Key, numToRead uint64) (mmap.Mmap, error)
Put(lookup *Key, stateRoots mmap.Mmap) error
Get(lookup *Key, numToRead uint64) ([]common.Hash, error)
Put(lookup *Key, stateRoots []common.Hash) error
}

// Cache for history commitments on disk.
Expand Down Expand Up @@ -127,7 +125,7 @@ type Key struct {
func (c *Cache) Get(
lookup *Key,
numToRead uint64,
) (mmap.Mmap, error) {
) ([]common.Hash, error) {
fName, err := determineFilePath(c.baseDir, lookup)
if err != nil {
return nil, err
Expand All @@ -153,7 +151,7 @@ func (c *Cache) Get(
// State roots are saved as files in a directory hierarchy for the cache.
// This function first creates a temporary file, writes the state roots to it, and then renames the file
// to the final directory to ensure atomic writes.
func (c *Cache) Put(lookup *Key, stateRoots mmap.Mmap) error {
func (c *Cache) Put(lookup *Key, stateRoots []common.Hash) error {
// We should error if trying to put 0 state roots to disk.
if len(stateRoots) == 0 {
return ErrNoStateRoots
Expand Down Expand Up @@ -195,15 +193,11 @@ func (c *Cache) Put(lookup *Key, stateRoots mmap.Mmap) error {
}

// Reads 32 bytes at a time from a reader up to a specified height. If none, then read all.
func readStateRoots(r io.Reader, numToRead uint64) (mmap.Mmap, error) {
func readStateRoots(r io.Reader, numToRead uint64) ([]common.Hash, error) {
br := bufio.NewReader(r)
stateRootsMmap, err := mmap.NewMmap(int(numToRead))
if err != nil {
return nil, err
}
stateRoots := make([]common.Hash, 0)
buf := make([]byte, 0, 32)
var totalRead uint64
for totalRead = uint64(0); totalRead < numToRead; totalRead++ {
for totalRead := uint64(0); totalRead < numToRead; totalRead++ {
n, err := br.Read(buf[:cap(buf)])
if err != nil {
// If we try to read but reach EOF, we break out of the loop.
Expand All @@ -216,30 +210,30 @@ func readStateRoots(r io.Reader, numToRead uint64) (mmap.Mmap, error) {
if n != 32 {
return nil, fmt.Errorf("expected to read 32 bytes, got %d bytes", n)
}
stateRootsMmap.Set(int(totalRead), common.BytesToHash(buf))
stateRoots = append(stateRoots, common.BytesToHash(buf))
}
if protocol.Height(numToRead) > protocol.Height(totalRead) {
if protocol.Height(numToRead) > protocol.Height(len(stateRoots)) {
return nil, fmt.Errorf(
"wanted to read %d roots, but only read %d state roots",
numToRead,
totalRead,
len(stateRoots),
)
}
return stateRootsMmap, nil
return stateRoots, nil
}

func writeStateRoots(w io.Writer, stateRoots mmap.Mmap) error {
for i := 0; i < stateRoots.Length(); i++ {
n, err := w.Write(stateRoots.Get(i).Bytes())
func writeStateRoots(w io.Writer, stateRoots []common.Hash) error {
for i, rt := range stateRoots {
n, err := w.Write(rt[:])
if err != nil {
return err
}
if n != len(stateRoots.Get(i)) {
if n != len(rt) {
return fmt.Errorf(
"for state root %d, wrote %d bytes, expected to write %d bytes",
i,
n,
len(stateRoots.Get(i)),
len(rt),
)
}
}
Expand Down
53 changes: 19 additions & 34 deletions staker/challenge-cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"testing"

l2stateprovider "github.com/OffchainLabs/bold/layer2-state-provider"
"github.com/OffchainLabs/bold/mmap"

"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -45,19 +43,16 @@ func TestCache(t *testing.T) {
}
})
t.Run("Putting empty root fails", func(t *testing.T) {
if err := cache.Put(key, mmap.Mmap{}); !errors.Is(err, ErrNoStateRoots) {
if err := cache.Put(key, []common.Hash{}); !errors.Is(err, ErrNoStateRoots) {
t.Fatalf("Unexpected error: %v", err)
}
})
want, err := mmap.NewMmap(3)
want.Set(0, common.BytesToHash([]byte("foo")))
want.Set(1, common.BytesToHash([]byte("bar")))
want.Set(2, common.BytesToHash([]byte("baz")))
if err != nil {
t.Fatal(err)
want := []common.Hash{
common.BytesToHash([]byte("foo")),
common.BytesToHash([]byte("bar")),
common.BytesToHash([]byte("baz")),
}
defer want.Free()
err = cache.Put(key, want)
err := cache.Put(key, want)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -97,7 +92,7 @@ func TestReadWriteStateRoots(t *testing.T) {
if len(roots) == 0 {
t.Fatal("Got no roots")
}
if roots.Get(0) != want {
if roots[0] != want {
t.Fatalf("Wrong root. Expected %#x, got %#x", want, roots[0])
}
})
Expand All @@ -113,30 +108,24 @@ func TestReadWriteStateRoots(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if roots.Length() != 2 {
if len(roots) != 2 {
t.Fatalf("Expected two roots, got %d", len(roots))
}
if roots.Get(0) != foo {
if roots[0] != foo {
t.Fatalf("Wrong root. Expected %#x, got %#x", foo, roots[0])
}
if roots.Get(1) != bar {
if roots[1] != bar {
t.Fatalf("Wrong root. Expected %#x, got %#x", bar, roots[1])
}
})
t.Run("Fails to write enough data to writer", func(t *testing.T) {
m := &mockWriter{wantErr: true}
stateRoots, err := mmap.NewMmap(1)
if err != nil {
t.Fatal(err)
}
defer stateRoots.Free()
stateRoots.Set(0, common.BytesToHash([]byte("foo")))
err = writeStateRoots(m, stateRoots)
err := writeStateRoots(m, []common.Hash{common.BytesToHash([]byte("foo"))})
if err == nil {
t.Fatal("Wanted error")
}
m = &mockWriter{wantErr: false, numWritten: 16}
err = writeStateRoots(m, stateRoots)
err = writeStateRoots(m, []common.Hash{common.BytesToHash([]byte("foo"))})
if err == nil {
t.Fatal("Wanted error")
}
Expand Down Expand Up @@ -235,11 +224,11 @@ func Test_readStateRoots(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(want) != got.Length() {
if len(want) != len(got) {
t.Fatal("Wrong number of roots")
}
for i := 0; i < got.Length(); i++ {
if got.Get(i) != want[i] {
for i, rt := range got {
if rt != want[i] {
t.Fatal("Wrong root")
}
}
Expand Down Expand Up @@ -314,15 +303,11 @@ func BenchmarkCache_Read_32Mb(b *testing.B) {
StepHeights: []l2stateprovider.Height{l2stateprovider.Height(0)},
}
numRoots := 1 << 20
rootsMmap, err := mmap.NewMmap(numRoots)
if err != nil {
b.Fatal(err)
}
defer rootsMmap.Free()
for i := 0; i < numRoots; i++ {
rootsMmap.Set(i, common.BytesToHash([]byte(fmt.Sprintf("%d", i))))
roots := make([]common.Hash, numRoots)
for i := range roots {
roots[i] = common.BytesToHash([]byte(fmt.Sprintf("%d", i)))
}
if err := cache.Put(key, rootsMmap); err != nil {
if err := cache.Put(key, roots); err != nil {
b.Fatal(err)
}
b.StartTimer()
Expand Down
Loading

0 comments on commit e759768

Please sign in to comment.