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

Juno @task-5 #1870

Closed
Closed
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
787942a
fix::> Basic str ready
PsychoPunkSage May 7, 2024
9fe3466
fix::> parsing error reverting back to org
PsychoPunkSage May 7, 2024
8df2f60
fix::> RPC export properly working
PsychoPunkSage May 7, 2024
aafed1c
fix::> block with receipt output
PsychoPunkSage May 7, 2024
bf778fb
fix::> block with txns output
PsychoPunkSage May 7, 2024
b0cd907
fix::> remoaninig push
PsychoPunkSage May 7, 2024
a41b802
fix::> removed rpc response
PsychoPunkSage May 7, 2024
e09c801
add::> added response file
PsychoPunkSage May 7, 2024
301b7b9
fix::> New handler added
PsychoPunkSage May 8, 2024
7b66a6f
fix::> Test basic str ready
PsychoPunkSage May 8, 2024
07a205f
Merge branch 'NethermindEth:main' into pps-sandbox
PsychoPunkSage May 8, 2024
ba7e1b0
fix::> Test ready
PsychoPunkSage May 8, 2024
6380352
fix::> Implemented fn to get StateTrie
PsychoPunkSage May 10, 2024
c67cfd4
fix::> Expose NodeFromRoot fn
PsychoPunkSage May 10, 2024
399b283
fix::> impl fn to get nodes from root and a node parser
PsychoPunkSage May 10, 2024
d472131
fix::> impl handler JunoGetNodesFromRoot
PsychoPunkSage May 10, 2024
fc257c7
fix::> template
PsychoPunkSage May 10, 2024
2e7918b
fix::> Changed the fn name
PsychoPunkSage May 10, 2024
3fea2be
fix::> added new RPC_meth to methods
PsychoPunkSage May 10, 2024
94fc3f8
fix::> modified the code
PsychoPunkSage May 10, 2024
4a1a516
fix::> Handler test written
PsychoPunkSage May 11, 2024
893d38e
fix::> removed Juno
PsychoPunkSage May 11, 2024
b49f2f5
fix::> removed unnecessary files
PsychoPunkSage May 11, 2024
368ae2d
fix::> Test Written
PsychoPunkSage May 11, 2024
c2ada93
fix::> mock_state Node from Root partially implemented
PsychoPunkSage May 17, 2024
c921837
fix::> mock_state Node from Root partially implemented 1
PsychoPunkSage May 17, 2024
248ef83
fix::> Transfering task 5 code
PsychoPunkSage May 19, 2024
f5a1e6b
Merge branch 'NethermindEth:main' into @intern/task_five
PsychoPunkSage May 19, 2024
3bb8344
Merge branch 'NethermindEth:main' into @intern/task_five
PsychoPunkSage May 23, 2024
4e10777
fix::> JunoGetNodesFromRoot is returning Actual errors
PsychoPunkSage May 23, 2024
55c94b5
fix::> Added the required test <Key exist> in the test script
PsychoPunkSage May 23, 2024
59bcea5
fix::> change the error return way
PsychoPunkSage May 28, 2024
eb55b94
fix::> Changed the impl of element
PsychoPunkSage May 28, 2024
6e6ae5e
Merge branch 'NethermindEth:main' into @intern/task_five
PsychoPunkSage May 28, 2024
547ee7b
Merge branch 'NethermindEth:main' into @intern/task_five
PsychoPunkSage Jun 17, 2024
d112f04
add::> added response file
PsychoPunkSage May 7, 2024
80bfba9
fix::> New handler added
PsychoPunkSage May 8, 2024
4dee1c3
fix::> Test basic str ready
PsychoPunkSage May 8, 2024
be65415
fix::> Test ready
PsychoPunkSage May 8, 2024
c59c985
fix::> Implemented fn to get StateTrie
PsychoPunkSage May 10, 2024
bf4c8e1
fix::> Expose NodeFromRoot fn
PsychoPunkSage May 10, 2024
65aa53e
fix::> impl handler JunoGetNodesFromRoot
PsychoPunkSage May 10, 2024
75f44c2
fix::> template
PsychoPunkSage May 10, 2024
28f5516
fix::> Changed the fn name
PsychoPunkSage May 10, 2024
26a8a03
fix::> added new RPC_meth to methods
PsychoPunkSage May 10, 2024
fabef6a
fix::> modified the code
PsychoPunkSage May 10, 2024
05e983b
fix::> Handler test written
PsychoPunkSage May 11, 2024
33a6d10
fix::> removed Juno
PsychoPunkSage May 11, 2024
d4603b7
fix::> removed unnecessary files
PsychoPunkSage May 11, 2024
93c0612
fix::> Merge conflict issue
PsychoPunkSage Jun 18, 2024
372c7fd
fix::> Merge conflict II
PsychoPunkSage Jun 18, 2024
3693d6f
fix::> Copied mock_state.go file
PsychoPunkSage Jun 18, 2024
29243b4
fix::> Removed unwanted file
PsychoPunkSage Jun 18, 2024
3d5a796
fix::> Removed all conflicts
PsychoPunkSage Jun 18, 2024
47642f5
fix::> Removed all conflicts II
PsychoPunkSage Jun 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ courtney/
__pycache__/
config/
.envrc
juno/

# pyenv
.python-version
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 Down Expand Up @@ -65,3 +66,7 @@ func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error)

return p.head.Class(classHash)
}

func (p *PendingState) GetGlobalTrie() (*trie.Trie, func() error, error) {
return p.head.GetGlobalTrie()
}
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)
GetGlobalTrie() (*trie.Trie, func() error, error)
}

type State struct {
Expand Down Expand Up @@ -122,6 +123,10 @@ func (s *State) Root() (*felt.Felt, error) {
return crypto.PoseidonArray(stateVersion, storageRoot, classesRoot), nil
}

func (s *State) GetGlobalTrie() (*trie.Trie, func() error, error) {
return s.globalTrie(db.StateTrie, trie.NewTriePedersen)
}

// storage returns a [core.Trie] that represents the Starknet global state in the given Txn context.
func (s *State) storage() (*trie.Trie, func() error, error) {
return s.globalTrie(db.StateTrie, trie.NewTriePedersen)
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 Down Expand Up @@ -87,3 +88,7 @@ func (s *stateSnapshot) Class(classHash *felt.Felt) (*DeclaredClass, error) {
}
return declaredClass, nil
}

func (s *stateSnapshot) GetGlobalTrie() (*trie.Trie, func() error, error) {
return s.state.GetGlobalTrie()
}
25 changes: 25 additions & 0 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
return NewKey(t.height, kBytes[:])
}

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

// 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 Expand Up @@ -135,6 +139,27 @@
node *Node
}

type ParsedNodes struct {
k string
v string
}

func (t *Trie) NodeParser(nodes []storageNode) []ParsedNodes {
var parsedNodes []ParsedNodes
for _, node := range nodes {
parsableNode := ParsedNodes{
k: fmt.Sprintf("%v", node.key), // Assuming node.key can be stringified

Check failure on line 151 in core/trie/trie.go

View workflow job for this annotation

GitHub Actions / lint

redundantSprint: use node.key.String() instead (gocritic)
v: fmt.Sprintf("%v", node.node),
}
parsedNodes = append(parsedNodes, parsableNode)
}
return parsedNodes
}

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

// nodesFromRoot enumerates the set of [Node] objects that need to be traversed from the root
// of the Trie to the node which is given by the key.
// The [storageNode]s are returned in descending order beginning with the root.
Expand Down
61 changes: 61 additions & 0 deletions mocks/mock_state.go

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

53 changes: 53 additions & 0 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
ErrUnsupportedTxVersion = &jsonrpc.Error{Code: 61, Message: "the transaction version is not supported"}
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
ErrParsingError = &jsonrpc.Error{Code: 64, Message: "Failed to parse"}

// These errors can be only be returned by Juno-specific methods.
ErrSubscriptionNotFound = &jsonrpc.Error{Code: 100, Message: "Subscription not found"}
Expand Down Expand Up @@ -330,6 +331,11 @@
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithReceipts,
},
{
Name: "juno_getNodesFromRoot",
Params: []jsonrpc.Parameter{{Name: "key"}},
Handler: h.JunoGetNodesFromRoot,
},
}, "/v0_7"
}

Expand Down Expand Up @@ -483,5 +489,52 @@
Params: []jsonrpc.Parameter{{Name: "id"}},
Handler: h.Unsubscribe,
},
{
Name: "juno_getNodesFromRoot",
Params: []jsonrpc.Parameter{{Name: "key"}},
Handler: h.JunoGetNodesFromRoot,
},
}, "/v0_6"
}

func (h *Handler) JunoGetNodesFromRoot(key felt.Felt) (string, *jsonrpc.Error) {
stateReader, _, error := h.bcReader.HeadState()

Check failure on line 501 in rpc/handlers.go

View workflow job for this annotation

GitHub Actions / lint

builtinShadow: shadowing of predeclared identifier: error (gocritic)
if error != nil {
return "", &jsonrpc.Error{
Code: 11111,
Message: "Failed to retrieve head state",
Data: error.Error(),
}
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
}

try, _, errTry := stateReader.GetGlobalTrie()
if errTry != nil {
return "", &jsonrpc.Error{
Code: 22222,
Message: "Failed to get global trie",
Data: errTry.Error(),
}
}

k := try.FeltToKeyConverter(&key)
storageNodes, err := try.GetNodesFromRoot(&k)
if err != nil {
return "", &jsonrpc.Error{
Code: 33333,
Message: "Failed to get nodes from root",
Data: err.Error(),
}
}

parsedNodes := try.NodeParser(storageNodes)

jsonBytes, err := json.Marshal(parsedNodes)
if err != nil {
return "", &jsonrpc.Error{
Code: 44444,
Message: "Failed to parse nodes",
Data: err.Error(),
}
}
return string(jsonBytes), nil
}
28 changes: 28 additions & 0 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/NethermindEth/juno/node"
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/utils"
fp1 "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -99,3 +100,30 @@ func TestThrottledVMError(t *testing.T) {
assert.Equal(t, throttledErr, rpcErr.Data)
})
}

func TestJunoGetNodesFromRoot(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
mockReader := mocks.NewMockReader(mockCtrl)
mockReader.EXPECT().Network().Return(&utils.Mainnet).AnyTimes()

log := utils.NewNopZapLogger()
mockState := mocks.NewMockStateHistoryReader(mockCtrl)

handler := rpc.New(mockReader, nil, nil, "", utils.Ptr(utils.Mainnet), log)

t.Run("Key DNE", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add another test where nodeStorage is not nil

Copy link
Author

Choose a reason for hiding this comment

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

Resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't seem to run

Copy link
Author

Choose a reason for hiding this comment

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

The main issue is with mocks
I am facing difficulty implementing:

func (m *MockStateHistoryReader) NodeFromRoot() (*trie.Trie, func() error, error) {
	panic("not implemented")

Like I have to write this function right? It won't be auto-generated, right?

Please Have a look at this message:: Message

mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
nodeStorage, err := handler.JunoGetNodesFromRoot(felt.Zero)
require.Nil(t, nodeStorage)
require.NotEqual(t, err, nil)
})

t.Run("Key Exist", func(t *testing.T) {
element := fp1.NewElement(1)
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
nodeStorage, err := handler.JunoGetNodesFromRoot(*felt.NewFelt(&element))
require.Nil(t, err)
assert.NotEqual(t, nodeStorage, nil)
})
}
Loading