Skip to content

Commit

Permalink
Fix parameters provided to the zkevm_getBatchDataByNumbers (#111)
Browse files Browse the repository at this point in the history
* Provide correct params to zkevm_getBatchDataByNumbers function

* Provide capacities to slices

* Update json rpc md

* Address comment (use FilterBatch struct instead ofmap)

* Unit test
  • Loading branch information
Stefan-Ethernal authored Apr 1, 2024
1 parent dc85fb7 commit 60a3e2e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions dataavailability/dataavailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (d *DataAvailability) trustedSequencerData(batchNums []uint64, expectedHash
return nil, fmt.Errorf("invalid arguments, len of batch numbers does not equal length of expected hashes: %d != %d",
len(batchNums), len(expectedHashes))
}
var nums []*big.Int
nums := make([]*big.Int, 0, len(batchNums))
for _, n := range batchNums {
nums = append(nums, new(big.Int).SetUint64(n))
}
Expand All @@ -124,7 +124,7 @@ func (d *DataAvailability) trustedSequencerData(batchNums []uint64, expectedHash
if len(batchData) != len(batchNums) {
return nil, fmt.Errorf("missing batch data, expected %d, got %d", len(batchNums), len(batchData))
}
var result [][]byte
result := make([][]byte, 0, len(batchNums))
for i := 0; i < len(batchNums); i++ {
number := batchNums[i]
batch := batchData[i]
Expand Down
1 change: 1 addition & 0 deletions docs/json-rpc-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ If the endpoint is not in the list below, it means this specific endpoint is not
- `zkevm_estimateGasPrice`
- `zkevm_estimateCounters`
- `zkevm_getBatchByNumber`
- `zkevm_getBatchDataByNumbers`
- `zkevm_getExitRootsByGER`
- `zkevm_getFullBlockByHash`
- `zkevm_getFullBlockByNumber`
Expand Down
15 changes: 5 additions & 10 deletions jsonrpc/client/zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,15 @@ func (c *Client) BatchByNumber(ctx context.Context, number *big.Int) (*types.Bat
// BatchesByNumbers returns batches from the current canonical chain by batch numbers. If the list is empty, the last
// known batch is returned as a list.
func (c *Client) BatchesByNumbers(_ context.Context, numbers []*big.Int) ([]*types.BatchData, error) {
var list []types.BatchNumber
batchNumbers := make([]types.BatchNumber, 0, len(numbers))
for _, n := range numbers {
list = append(list, types.BatchNumber(n.Int64()))
batchNumbers = append(batchNumbers, types.BatchNumber(n.Int64()))
}
if len(list) == 0 {
list = append(list, types.LatestBatchNumber)
if len(batchNumbers) == 0 {
batchNumbers = append(batchNumbers, types.LatestBatchNumber)
}

var batchNumbers []string
for _, n := range list {
batchNumbers = append(batchNumbers, n.StringOrHex())
}

response, err := JSONRPCCall(c.url, "zkevm_getBatchDataByNumbers", batchNumbers, true)
response, err := JSONRPCCall(c.url, "zkevm_getBatchDataByNumbers", &types.BatchFilter{Numbers: batchNumbers})
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (z *ZKEVMEndpoints) GetBatchByNumber(batchNumber types.BatchNumber, fullTx
// GetBatchDataByNumbers returns the batch data for batches by numbers
func (z *ZKEVMEndpoints) GetBatchDataByNumbers(filter types.BatchFilter) (interface{}, types.Error) {
return z.txMan.NewDbTxScope(z.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
var batchNumbers []uint64
batchNumbers := make([]uint64, 0, len(filter.Numbers))
for _, bn := range filter.Numbers {
n, rpcErr := bn.GetNumericBatchNumber(ctx, z.state, z.etherman, dbTx)
if rpcErr != nil {
Expand All @@ -224,7 +224,7 @@ func (z *ZKEVMEndpoints) GetBatchDataByNumbers(filter types.BatchFilter) (interf
fmt.Sprintf("couldn't load batch data from state by numbers %v", filter.Numbers), err, true)
}

var ret []*types.BatchData
ret := make([]*types.BatchData, 0, len(batchNumbers))
for _, n := range batchNumbers {
data := &types.BatchData{Number: types.ArgUint64(n)}
if b, ok := batchesData[n]; ok {
Expand Down
29 changes: 29 additions & 0 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2705,3 +2705,32 @@ func TestGetLatestGlobalExitRoot(t *testing.T) {
})
}
}

func TestClient_BatchesByNumbers(t *testing.T) {
const batchesCount = 6

s, m, _ := newSequencerMockedServer(t)
defer s.Stop()

batchesDataMap := make(map[uint64][]byte, batchesCount)
for i := 0; i < batchesCount; i++ {
batchesDataMap[uint64(i+1)] = []byte(fmt.Sprintf("batch %d data", i+1))
}

m.State.On("GetBatchL2DataByNumbers", mock.Anything, mock.Anything, mock.Anything).
Return(batchesDataMap, nil).Once()

m.State.On("BeginStateTransaction", context.Background()).
Return(m.DbTx, nil).Once()

m.DbTx.On("Commit", context.Background()).Return(nil).Once()

zkEVMClient := client.NewClient(s.ServerURL)
reqBatchesNum := []*big.Int{big.NewInt(1), big.NewInt(3), big.NewInt(4)}
result, err := zkEVMClient.BatchesByNumbers(context.Background(), reqBatchesNum)
require.NoError(t, err)
require.Len(t, result, len(reqBatchesNum))
for i, batchNum := range reqBatchesNum {
require.Equal(t, hex.EncodeToHex(batchesDataMap[batchNum.Uint64()]), result[i].BatchL2Data.Hex())
}
}

0 comments on commit 60a3e2e

Please sign in to comment.