-
Notifications
You must be signed in to change notification settings - Fork 193
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
Pps sandbox #1860
Pps sandbox #1860
Changes from 24 commits
787942a
9fe3466
8df2f60
aafed1c
bf778fb
b0cd907
a41b802
e09c801
301b7b9
7b66a6f
07a205f
ba7e1b0
6380352
c67cfd4
399b283
d472131
fc257c7
2e7918b
3fea2be
94fc3f8
4a1a516
893d38e
b49f2f5
368ae2d
c2ada93
c921837
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ type Reader interface { | |
Pending() (Pending, error) | ||
|
||
Network() *utils.Network | ||
// JunoGetNodesFromRoot(key *felt.Felt) (core.StateReader /*, StateCloser*/, error) | ||
} | ||
|
||
var ( | ||
|
@@ -109,6 +110,17 @@ func (b *Blockchain) StateCommitment() (*felt.Felt, error) { | |
}) | ||
} | ||
|
||
// func (b *Blockchain) JunoGetNodesFromRoot(key *felt.Felt) (core.StateReader /*, StateCloser*/, error) { | ||
// b.listener.OnRead("JunoGetNodesFromRoot") | ||
// var state core.StateReader | ||
// // var closer StateCloser | ||
// return state, b.database.View(func(txn db.Transaction) error { | ||
// var err error | ||
// state, _, err = core.NewState(txn).NodeFromRoot(key) | ||
// return err | ||
// }) | ||
// } | ||
|
||
Comment on lines
+113
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mentioned about writing some codes in |
||
// Height returns the latest block height. If blockchain is empty nil is returned. | ||
func (b *Blockchain) Height() (uint64, error) { | ||
b.listener.OnRead("Height") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
NodeFromRoot() (*trie.Trie, func() error, error) | ||
} | ||
|
||
type State struct { | ||
|
@@ -122,6 +123,10 @@ func (s *State) Root() (*felt.Felt, error) { | |
return crypto.PoseidonArray(stateVersion, storageRoot, classesRoot), nil | ||
} | ||
|
||
func (s *State) NodeFromRoot() (*trie.Trie, func() error, error) { | ||
return s.globalTrie(db.StateTrie, trie.NewTriePedersen) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are returning the state trie here, not the nodes from the root to a leaf |
||
} | ||
|
||
// 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) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -702,3 +702,115 @@ func TestRpcBlockAdaptation(t *testing.T) { | |
require.Equal(t, &felt.Zero, blockWithTxHashes.BlockHeader.SequencerAddress) | ||
}) | ||
} | ||
|
||
func TestJunoGetBlockWithTxsAndReceipts(t *testing.T) { | ||
blockId := map[string]rpc.BlockID{ | ||
"latest": {Latest: true}, | ||
"pending": {Pending: true}, | ||
"hash": {Hash: new(felt.Felt).SetUint64(1)}, | ||
"number": {Number: 1}, | ||
} | ||
|
||
log := utils.NewNopZapLogger() | ||
n := utils.Ptr(utils.Mainnet) | ||
chain := blockchain.New(pebble.NewMemTest(t), n) | ||
handler := rpc.New(chain, nil, nil, "", n, log) | ||
for desc, id := range blockId { | ||
t.Run(desc, func(t *testing.T) { | ||
block, blockReceipt, rpcErr := handler.JunoGetBlockWithTxsAndReceipts(id) | ||
assert.Nil(t, block) | ||
assert.Nil(t, blockReceipt) | ||
assert.Equal(t, rpc.ErrBlockNotFound, rpcErr) | ||
}) | ||
} | ||
|
||
mockCtrl := gomock.NewController(t) | ||
t.Cleanup(mockCtrl.Finish) | ||
mockReader := mocks.NewMockReader(mockCtrl) | ||
|
||
t.Run("transaction DNE", func(t *testing.T) { | ||
rianhughes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
blockID := rpc.BlockID{Number: 420} | ||
|
||
mockReader.EXPECT().BlockByNumber(blockID.Number).Return(nil, db.ErrKeyNotFound) // Expecting BlockID DNE | ||
|
||
respTxn, respRecp, rpcErr := handler.JunoGetBlockWithTxsAndReceipts(blockID) | ||
assert.Nil(t, respTxn) | ||
assert.Nil(t, respRecp) | ||
assert.Equal(t, rpc.ErrBlockNotFound, rpcErr) | ||
}) | ||
|
||
t.Run("l1head failure", func(t *testing.T) { | ||
blockID := rpc.BlockID{Number: 420} | ||
block := &core.Block{ | ||
Header: &core.Header{}, | ||
} | ||
|
||
err := errors.New("l1 failure") | ||
mockReader.EXPECT().BlockByNumber(blockID.Number).Return(block, nil) // Expecting BlockID to be valid | ||
mockReader.EXPECT().L1Head().Return(nil, err) // Expecting L1head to fail | ||
|
||
respTxn, respRecp, rpcErr := handler.JunoGetBlockWithTxsAndReceipts(blockID) | ||
assert.Nil(t, respTxn) | ||
assert.Nil(t, respRecp) | ||
assert.Equal(t, rpc.ErrInternal.CloneWithData(err.Error()), rpcErr) | ||
}) | ||
|
||
client := feeder.NewTestClient(t, n) | ||
mainnetGw := adaptfeeder.New(client) | ||
|
||
t.Run("pending block", func(t *testing.T) { | ||
t.Skip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. t.Skip()? |
||
block0, err := mainnetGw.BlockByNumber(context.Background(), 0) | ||
require.NoError(t, err) | ||
|
||
blockID := rpc.BlockID{Pending: true} | ||
|
||
mockReader.EXPECT().Pending().Return(blockchain.Pending{Block: block0}, nil) | ||
mockReader.EXPECT().L1Head().Return(&core.L1Head{}, nil) | ||
|
||
respTxn, respRecp, rpcErr := handler.JunoGetBlockWithTxsAndReceipts(blockID) | ||
header := respRecp.BlockHeader | ||
|
||
var txsWithReceipt []rpc.TransactionWithReceipt | ||
var txns []*rpc.Transaction | ||
for i, tx := range block0.Transactions { | ||
receipt := block0.Receipts[i] | ||
adaptedTx := rpc.AdaptTransaction(tx) | ||
txns = append(txns, adaptedTx) | ||
txsWithReceipt = append(txsWithReceipt, rpc.TransactionWithReceipt{ | ||
Transaction: adaptedTx, | ||
Receipt: rpc.AdaptReceipt(receipt, tx, rpc.TxnAcceptedOnL2, nil, 0, true), | ||
}) | ||
} | ||
|
||
assert.Nil(t, rpcErr) | ||
assert.Equal(t, &rpc.BlockWithTxs{ | ||
Status: rpc.BlockPending, | ||
BlockHeader: rpc.BlockHeader{ | ||
Hash: header.Hash, | ||
ParentHash: header.ParentHash, | ||
Number: header.Number, | ||
NewRoot: header.NewRoot, | ||
Timestamp: header.Timestamp, | ||
SequencerAddress: header.SequencerAddress, | ||
L1GasPrice: header.L1GasPrice, | ||
StarknetVersion: header.StarknetVersion, | ||
}, | ||
Transactions: txns, | ||
}, respTxn) | ||
assert.Equal(t, &rpc.BlockWithReceipts{ | ||
Status: rpc.BlockPending, | ||
BlockHeader: rpc.BlockHeader{ | ||
Hash: header.Hash, | ||
ParentHash: header.ParentHash, | ||
Number: header.Number, | ||
NewRoot: header.NewRoot, | ||
Timestamp: header.Timestamp, | ||
SequencerAddress: header.SequencerAddress, | ||
L1GasPrice: header.L1GasPrice, | ||
StarknetVersion: header.StarknetVersion, | ||
}, | ||
Transactions: txsWithReceipt, | ||
}, respRecp) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ var ( | |
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"} | ||
|
@@ -330,6 +331,11 @@ func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen | |
Params: []jsonrpc.Parameter{{Name: "block_id"}}, | ||
Handler: h.BlockWithReceipts, | ||
}, | ||
{ | ||
Name: "juno_getBlockWithTxsAndReceipts", | ||
Params: []jsonrpc.Parameter{{Name: "block_id"}}, | ||
Handler: h.JunoGetBlockWithTxsAndReceipts, | ||
}, | ||
}, "/v0_7" | ||
} | ||
|
||
|
@@ -483,5 +489,35 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen | |
Params: []jsonrpc.Parameter{{Name: "id"}}, | ||
Handler: h.Unsubscribe, | ||
}, | ||
{ | ||
Name: "juno_getBlockWithTxsAndReceipts", | ||
Params: []jsonrpc.Parameter{{Name: "block_id"}}, | ||
Handler: h.JunoGetBlockWithTxsAndReceipts, | ||
}, | ||
}, "/v0_6" | ||
} | ||
|
||
func (h *Handler) JunoGetNodesFromRoot(key felt.Felt) (string, *jsonrpc.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be easier to review if you created a new PR for this :) |
||
stateReader, _, error := h.bcReader.HeadState() | ||
if error != nil { | ||
return "", ErrBlockNotFound | ||
} | ||
try, _, errTry := stateReader.NodeFromRoot() | ||
if errTry != nil { | ||
return "", ErrBlockNotFound | ||
} | ||
|
||
k := try.FeltToKeyConverter(&key) | ||
storageNodes, err := try.GetNodesFromRoot(&k) | ||
if err != nil { | ||
return "", ErrBlockNotFound | ||
} | ||
|
||
parsedNodes := try.NodeParser(storageNodes) | ||
|
||
jsonBytes, err := json.Marshal(parsedNodes) | ||
if err != nil { | ||
return "", ErrParsingError | ||
} | ||
return string(jsonBytes), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,21 @@ func TestThrottledVMError(t *testing.T) { | |
assert.Equal(t, throttledErr, rpcErr.Data) | ||
}) | ||
} | ||
|
||
func TestJunoGetBlockFromRoot(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is panicking because you are calling |
||
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil) | ||
nodeStorage, _ := handler.JunoGetNodesFromRoot(felt.Zero) | ||
require.Nil(t, nodeStorage) | ||
// assert.Equal(t, rpc.ErrBlockNotFound, err) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fyi, in a real PR these shouldn't be here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I just wanted to keep previous code intact (I'm experimenting with it).
I thought this PR won't be merged so I kept it this way.