Skip to content

Commit

Permalink
Merge branch 'main' into wojciechos/tests-update
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciechos authored Oct 17, 2024
2 parents 8903f09 + f4d9d69 commit e750af4
Show file tree
Hide file tree
Showing 33 changed files with 784 additions and 184 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/find-smallest-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ on:
- starknet/compiler/rust/*
workflow_dispatch:


permissions:
issues: write # Required for sending comments
pull-requests: write # Required for sending comments

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand All @@ -37,6 +42,14 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Minimum supported Rust version is `' + msrv + '`, previous `' + previous_msrv + '`. Please update the README file and this workflow.'
body: 'Minimum supported Rust version is `' + msrv + '`, previous `' + previous_msrv + '`. Please update the README file, the Makefile, and this workflow.'
})
}
- name: "Check README and Makefile"
run: |
set -euxo pipefail
echo "Checking if version is set in README"
cat README.md | grep "\[Rust\]" | grep "${{ steps.rust-version.outputs.highest-msrv }}"
echo "Checking makefile"
cat Makefile | grep "MINIMUM_RUST_VERSION = ${{ steps.rust-version.outputs.highest-msrv }}"
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ endif
ifeq ($(shell uname -s),Darwin)
export CGO_LDFLAGS=-framework Foundation -framework SystemConfiguration

# Set macOS deployment target in order to avoid linker warnings linke
# "ld: warning: object file XX was built for newer macOS version (14.4) than being linked (14.0)"
# Set macOS deployment target in order to avoid linker warnings linke
# "ld: warning: object file XX was built for newer macOS version (14.4) than being linked (14.0)"
export MACOSX_DEPLOYMENT_TARGET=$(shell sw_vers --productVersion)

# for test-race we need to pass -ldflags to fix linker warnings on macOS
Expand All @@ -31,7 +31,7 @@ endif

MAKEFLAGS += -j$(NPROCS)

rustdeps: vm core-rust compiler
rustdeps: check-rust vm core-rust compiler

juno: rustdeps ## compile
@mkdir -p build
Expand All @@ -41,6 +41,13 @@ juno-cached:
@mkdir -p build
@go build $(GO_TAGS) -ldflags="-X main.Version=$(shell git describe --tags)" -o build/juno ./cmd/juno/


MINIMUM_RUST_VERSION = 1.80.1
CURR_RUST_VERSION = $(shell rustc --version | grep -o '[0-9.]\+' | head -n1)
check-rust: ## Ensure rust version is greater than minimum
@echo "Checking if current rust version >= $(MINIMUM_RUST_VERSION)"
@bash -c '[[ $(CURR_RUST_VERSION) < $(MINIMUM_RUST_VERSION) ]] && (echo "Rust version must be >= $(MINIMUM_RUST_VERSION). Found version $(CURR_RUST_VERSION)" && exit 1) || echo "Current rust version is $(CURR_RUST_VERSION)"'

vm:
$(MAKE) -C vm/rust $(VM_TARGET)

Expand Down Expand Up @@ -74,7 +81,7 @@ test-cover: clean-testcache rustdeps ## tests with coverage
go test $(GO_TAGS) -coverpkg=./... -coverprofile=coverage/coverage.out -covermode=atomic ./...
go tool cover -html=coverage/coverage.out -o coverage/coverage.html

install-deps: | install-gofumpt install-mockgen install-golangci-lint## install some project dependencies
install-deps: | install-gofumpt install-mockgen install-golangci-lint check-rust ## install some project dependencies

install-gofumpt:
go install mvdan.cc/gofumpt@latest
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ After following these steps, Juno should be up and running on your machine, util
- Starknet state construction and storage using a path-based Merkle Patricia trie.
- Feeder gateway synchronisation of Blocks, Transactions, Receipts, State Updates and Classes.
- Block and Transaction hash verification.
- Plugins

## 🛣 Roadmap

Expand Down
18 changes: 17 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,23 @@ func (b *Blockchain) RevertHead() error {
return b.database.Update(b.revertHead)
}

func (b *Blockchain) GetReverseStateDiff() (*core.StateDiff, error) {
var reverseStateDiff *core.StateDiff
return reverseStateDiff, b.database.View(func(txn db.Transaction) error {
blockNumber, err := chainHeight(txn)
if err != nil {
return err
}
stateUpdate, err := stateUpdateByNumber(txn, blockNumber)
if err != nil {
return err
}
state := core.NewState(txn)
reverseStateDiff, err = state.GetReverseStateDiff(blockNumber, stateUpdate.StateDiff)
return err
})
}

func (b *Blockchain) revertHead(txn db.Transaction) error {
blockNumber, err := chainHeight(txn)
if err != nil {
Expand Down Expand Up @@ -874,7 +891,6 @@ func (b *Blockchain) revertHead(txn db.Transaction) error {
}

// Revert chain height and pending.

if genesisBlock {
if err = txn.Delete(db.Pending.Key()); err != nil {
return err
Expand Down
94 changes: 80 additions & 14 deletions cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/spf13/cobra"
)

const (
dbRevertToBlockF = "to-block"
)

type DBInfo struct {
Network string `json:"network"`
ChainHeight uint64 `json:"chain_height"`
Expand All @@ -33,7 +37,7 @@ func DBCmd(defaultDBPath string) *cobra.Command {
}

dbCmd.PersistentFlags().String(dbPathF, defaultDBPath, dbPathUsage)
dbCmd.AddCommand(DBInfoCmd(), DBSizeCmd())
dbCmd.AddCommand(DBInfoCmd(), DBSizeCmd(), DBRevertCmd())
return dbCmd
}

Expand All @@ -55,21 +59,29 @@ func DBSizeCmd() *cobra.Command {
}
}

func DBRevertCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "revert",
Short: "Revert current head to given position",
Long: `This subcommand revert all data related to all blocks till given so it becomes new head.`,
RunE: dbRevert,
}
cmd.Flags().Uint64(dbRevertToBlockF, 0, "New head (this block won't be reverted)")

return cmd
}

func dbInfo(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err
}

if _, err = os.Stat(dbPath); os.IsNotExist(err) {
fmt.Fprintln(cmd.OutOrStdout(), "Database path does not exist")
return nil
}

database, err := pebble.New(dbPath)
database, err := openDB(dbPath)
if err != nil {
return fmt.Errorf("open DB: %w", err)
return err
}
defer database.Close()

chain := blockchain.New(database, nil)
info := DBInfo{}
Expand Down Expand Up @@ -110,6 +122,50 @@ func dbInfo(cmd *cobra.Command, args []string) error {
return nil
}

func dbRevert(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
return err
}

revertToBlock, err := cmd.Flags().GetUint64(dbRevertToBlockF)
if err != nil {
return err
}

if revertToBlock == 0 {
return fmt.Errorf("--%v cannot be 0", dbRevertToBlockF)
}

database, err := openDB(dbPath)
if err != nil {
return err
}
defer database.Close()

for {
chain := blockchain.New(database, nil)
head, err := chain.Head()
if err != nil {
return fmt.Errorf("failed to get the latest block information: %v", err)
}

if head.Number == revertToBlock {
fmt.Fprintf(cmd.OutOrStdout(), "Successfully reverted all blocks to %d\n", revertToBlock)
break
}

err = chain.RevertHead()
if err != nil {
return fmt.Errorf("failed to revert head at block %d: %v", head.Number, err)
}

fmt.Fprintf(cmd.OutOrStdout(), "Reverted head at block %d\n", head.Number)
}

return nil
}

func dbSize(cmd *cobra.Command, args []string) error {
dbPath, err := cmd.Flags().GetString(dbPathF)
if err != nil {
Expand All @@ -120,15 +176,11 @@ func dbSize(cmd *cobra.Command, args []string) error {
return fmt.Errorf("--%v cannot be empty", dbPathF)
}

if _, err = os.Stat(dbPath); os.IsNotExist(err) {
fmt.Fprintln(cmd.OutOrStdout(), "Database path does not exist")
return nil
}

pebbleDB, err := pebble.New(dbPath)
pebbleDB, err := openDB(dbPath)
if err != nil {
return err
}
defer pebbleDB.Close()

var (
totalSize utils.DataSize
Expand Down Expand Up @@ -201,3 +253,17 @@ func getNetwork(head *core.Block, stateDiff *core.StateDiff) string {

return "unknown"
}

func openDB(path string) (db.DB, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil, fmt.Errorf("database path does not exist")
}

database, err := pebble.New(path)
if err != nil {
return nil, fmt.Errorf("failed to open db: %w", err)
}

return database, nil
}
66 changes: 55 additions & 11 deletions cmd/juno/dbcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main_test

import (
"context"
"strconv"
"testing"

"github.com/NethermindEth/juno/blockchain"
Expand All @@ -12,6 +13,7 @@ import (
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,27 +29,69 @@ func TestDBCmd(t *testing.T) {
cmd := juno.DBSizeCmd()
executeCmdInDB(t, cmd)
})

t.Run("revert db by 1 block", func(t *testing.T) {
network := utils.Mainnet

const (
syncToBlock = uint64(2)
revertToBlock = syncToBlock - 1
)

cmd := juno.DBRevertCmd()
cmd.Flags().String("db-path", "", "")

dbPath := prepareDB(t, &network, syncToBlock)

require.NoError(t, cmd.Flags().Set("db-path", dbPath))
require.NoError(t, cmd.Flags().Set("to-block", strconv.Itoa(int(revertToBlock))))
require.NoError(t, cmd.Execute())

// unfortunately we cannot use blockchain from prepareDB because
// inside revert cmd another pebble instance is used which will panic if there are other instances
// that use the same db path
db, err := pebble.New(dbPath)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})

chain := blockchain.New(db, &network)
block, err := chain.Head()
require.NoError(t, err)
assert.Equal(t, revertToBlock, block.Number)
})
}

func executeCmdInDB(t *testing.T, cmd *cobra.Command) {
cmd.Flags().String("db-path", "", "")

client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
block0, err := gw.BlockByNumber(context.Background(), 0)
require.NoError(t, err)
dbPath := prepareDB(t, &utils.Mainnet, 0)

stateUpdate0, err := gw.StateUpdate(context.Background(), 0)
require.NoError(t, err)
require.NoError(t, cmd.Flags().Set("db-path", dbPath))
require.NoError(t, cmd.Execute())
}

func prepareDB(t *testing.T, network *utils.Network, syncToBlock uint64) string {
client := feeder.NewTestClient(t, network)
gw := adaptfeeder.New(client)

dbPath := t.TempDir()
testDB, err := pebble.New(dbPath)
require.NoError(t, err)

chain := blockchain.New(testDB, &utils.Mainnet)
require.NoError(t, chain.Store(block0, &emptyCommitments, stateUpdate0, nil))
testDB.Close()
chain := blockchain.New(testDB, network)

require.NoError(t, cmd.Flags().Set("db-path", dbPath))
require.NoError(t, cmd.Execute())
for blockNumber := uint64(0); blockNumber <= syncToBlock; blockNumber++ {
block, err := gw.BlockByNumber(context.Background(), blockNumber)
require.NoError(t, err)

stateUpdate, err := gw.StateUpdate(context.Background(), blockNumber)
require.NoError(t, err)

require.NoError(t, chain.Store(block, &emptyCommitments, stateUpdate, nil))
}
require.NoError(t, testDB.Close())

return dbPath
}
4 changes: 4 additions & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
callMaxStepsF = "rpc-call-max-steps"
corsEnableF = "rpc-cors-enable"
versionedConstantsFileF = "versioned-constants-file"
pluginPathF = "plugin-path"

defaultConfig = ""
defaulHost = "localhost"
Expand Down Expand Up @@ -119,6 +120,7 @@ const (
defaultGwTimeout = 5 * time.Second
defaultCorsEnable = false
defaultVersionedConstantsFile = ""
defaultPluginPath = ""

configFlagUsage = "The YAML configuration file."
logLevelFlagUsage = "Options: trace, debug, info, warn, error."
Expand Down Expand Up @@ -170,6 +172,7 @@ const (
"The upper limit is 4 million steps, and any higher value will still be capped at 4 million."
corsEnableUsage = "Enable CORS on RPC endpoints"
versionedConstantsFileUsage = "Use custom versioned constants from provided file"
pluginPathUsage = "Path to the plugin .so file"
)

var Version string
Expand Down Expand Up @@ -355,6 +358,7 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.Flags().Bool(corsEnableF, defaultCorsEnable, corsEnableUsage)
junoCmd.Flags().String(versionedConstantsFileF, defaultVersionedConstantsFile, versionedConstantsFileUsage)
junoCmd.MarkFlagsMutuallyExclusive(p2pFeederNodeF, p2pPeersF)
junoCmd.Flags().String(pluginPathF, defaultPluginPath, pluginPathUsage)

junoCmd.AddCommand(GenP2PKeyPair(), DBCmd(defaultDBPath))

Expand Down
Loading

0 comments on commit e750af4

Please sign in to comment.