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

Onboarding exercises #1909

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions blockchain/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockchain
import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/core/trie"
)

type Pending struct {
Expand All @@ -17,6 +18,10 @@ type PendingState struct {
head core.StateReader
}

func (p *PendingState) GetClassesTrie() (*trie.Trie, func() error, error) {
return p.head.GetClassesTrie()
}

func NewPendingState(stateDiff *core.StateDiff, newClasses map[felt.Felt]core.Class, head core.StateReader) *PendingState {
return &PendingState{
stateDiff: stateDiff,
Expand Down
5 changes: 5 additions & 0 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type StateReader interface {
ContractNonce(addr *felt.Felt) (*felt.Felt, error)
ContractStorage(addr, key *felt.Felt) (*felt.Felt, error)
Class(classHash *felt.Felt) (*DeclaredClass, error)
GetClassesTrie() (*trie.Trie, func() error, error)
}

type State struct {
Expand Down Expand Up @@ -131,6 +132,10 @@ func (s *State) classesTrie() (*trie.Trie, func() error, error) {
return s.globalTrie(db.ClassesTrie, trie.NewTriePoseidon)
}

func (s *State) GetClassesTrie() (*trie.Trie, func() error, error) {
return s.classesTrie()
}

func (s *State) globalTrie(bucket db.Bucket, newTrie trie.NewTrieFunc) (*trie.Trie, func() error, error) {
dbPrefix := bucket.Key()
tTxn := trie.NewStorage(s.txn, dbPrefix)
Expand Down
5 changes: 5 additions & 0 deletions core/state_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/core/trie"
"github.com/NethermindEth/juno/db"
)

Expand All @@ -12,6 +13,10 @@ type stateSnapshot struct {
state StateHistoryReader
}

func (s *stateSnapshot) GetClassesTrie() (*trie.Trie, func() error, error) {
return s.state.GetClassesTrie()
}

func NewStateSnapshot(state StateHistoryReader, blockNumber uint64) StateReader {
return &stateSnapshot{
blockNumber: blockNumber,
Expand Down
20 changes: 20 additions & 0 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ func (t *Trie) feltToKey(k *felt.Felt) Key {
return NewKey(t.height, kBytes[:])
}

func (t *Trie) ConvertFeltToKey(key *felt.Felt) Key {
return t.feltToKey(key)
}

func (t *Trie) GetNodesFromRoot(key *Key) ([]StorageNode, error) {
return t.nodesFromRoot(key)
}

func (t *Trie) ParseNodes(nodes []StorageNode) ([]map[string]string, error) {
result := make([]map[string]string, len(nodes))
for i, node := range nodes {
result[i] = map[string]string{
"key": node.key.String(),
"value": node.node.Value.String(),
}
}

return result, nil
}

// findCommonKey finds the set of common MSB bits in two key bitsets.
func findCommonKey(longerKey, shorterKey *Key) (Key, bool) {
divergentBit := findDivergentBit(longerKey, shorterKey)
Expand Down
17 changes: 17 additions & 0 deletions mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 0 additions & 47 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import (
"testing"
"time"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/node"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -46,45 +41,3 @@ func TestNewNode(t *testing.T) {
cancel()
n.Run(ctx)
}

func TestNetworkVerificationOnNonEmptyDB(t *testing.T) {
network := utils.Integration
tests := map[string]struct {
network utils.Network
errString string
}{
"same network": {
network: network,
errString: "",
},
"different network": {
network: utils.Mainnet,
errString: "unable to verify latest block hash; are the database and --network option compatible?",
},
}

for description, test := range tests {
t.Run(description, func(t *testing.T) {
dbPath := t.TempDir()
log := utils.NewNopZapLogger()
database, err := pebble.New(dbPath, 1, 1, log)
require.NoError(t, err)
chain := blockchain.New(database, &network)
syncer := sync.New(chain, adaptfeeder.New(feeder.NewTestClient(t, &network)), log, 0, false)
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
require.NoError(t, syncer.Run(ctx))
cancel()
require.NoError(t, database.Close())

_, err = node.New(&node.Config{
DatabasePath: dbPath,
Network: test.network,
}, "v0.1")
if test.errString == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, test.errString)
}
})
}
}
39 changes: 39 additions & 0 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,45 @@ func (h *Handler) BlockWithTxs(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
}, nil
}

type BlockWithTxsAndReceipts struct {
Status BlockStatus
BlockHeader
Transactions []*Transaction
Receipts []*TransactionReceipt
}

func (h *Handler) BlockWithTxsAndReceipts(id BlockID) (*BlockWithTxsAndReceipts, *jsonrpc.Error) {
block, rpcErr := h.blockByID(&id)
if rpcErr != nil {
return nil, rpcErr
}

blockStatus, rpcErr := h.blockStatus(id, block)
if rpcErr != nil {
return nil, rpcErr
}

finalityStatus := TxnAcceptedOnL2
if blockStatus == BlockAcceptedL1 {
finalityStatus = TxnAcceptedOnL1
}

txs := make([]*Transaction, len(block.Transactions))
receipts := make([]*TransactionReceipt, len(block.Transactions))
for index, txn := range block.Transactions {
txs[index] = AdaptTransaction(txn)
r := block.Receipts[index]
receipts[index] = AdaptReceipt(r, txn, finalityStatus, nil, 0, false)
}

return &BlockWithTxsAndReceipts{
Status: blockStatus,
BlockHeader: adaptBlockHeader(block.Header),
Transactions: txs,
Receipts: receipts,
}, nil
}

func (h *Handler) BlockWithTxsV0_6(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
resp, err := h.BlockWithTxs(id)
if err != nil {
Expand Down
Loading
Loading