Skip to content

Commit 003c644

Browse files
authored
Add bundle basic types for RPC (#780)
* Add bundle basic types for RPC
1 parent b8f6bdd commit 003c644

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

api/sonicapi/api.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@
1515
// along with Sonic. If not, see <http://www.gnu.org/licenses/>.
1616

1717
package sonicapi
18+
19+
import "github.com/0xsoniclabs/sonic/api/ethapi"
20+
21+
type PublicBundleAPI struct {
22+
b ethapi.Backend
23+
}
24+
25+
// NewPublicBundleAPI creates a new instance of the PublicBundleAPI with the given backend.
26+
func NewPublicBundleAPI(b ethapi.Backend) *PublicBundleAPI {
27+
return &PublicBundleAPI{
28+
b: b,
29+
}
30+
}

api/sonicapi/bundle_api.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2026 Sonic Operations Ltd
2+
// This file is part of the Sonic Client
3+
//
4+
// Sonic is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Sonic is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with Sonic. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package sonicapi
18+
19+
import (
20+
"github.com/0xsoniclabs/sonic/gossip/blockproc/bundle"
21+
"github.com/ethereum/go-ethereum/common"
22+
"github.com/ethereum/go-ethereum/rpc"
23+
)
24+
25+
type RPCExecutionStep struct {
26+
From common.Address `json:"from"`
27+
Hash common.Hash `json:"hash"`
28+
}
29+
30+
type RPCExecutionPlan struct {
31+
Flags bundle.ExecutionFlags `json:"flags"`
32+
Steps []RPCExecutionStep `json:"steps"`
33+
Earliest rpc.BlockNumber `json:"earliest"`
34+
Latest rpc.BlockNumber `json:"latest"`
35+
}
36+
37+
// NewRPCExecutionPlan converts a bundle.ExecutionPlan to an RPCExecutionPlan for JSON-RPC responses.
38+
func NewRPCExecutionPlan(plan bundle.ExecutionPlan) RPCExecutionPlan {
39+
steps := make([]RPCExecutionStep, len(plan.Steps))
40+
for i, step := range plan.Steps {
41+
steps[i] = RPCExecutionStep{
42+
From: step.From,
43+
Hash: step.Hash,
44+
}
45+
}
46+
47+
return RPCExecutionPlan{
48+
Flags: plan.Flags,
49+
Steps: steps,
50+
Earliest: rpc.BlockNumber(plan.Range.Earliest),
51+
Latest: rpc.BlockNumber(plan.Range.Latest),
52+
}
53+
}

api/sonicapi/bundle_api_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2026 Sonic Operations Ltd
2+
// This file is part of the Sonic Client
3+
//
4+
// Sonic is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Sonic is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with Sonic. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package sonicapi
18+
19+
import (
20+
"testing"
21+
22+
"github.com/0xsoniclabs/sonic/gossip/blockproc/bundle"
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/rpc"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestNewRPCExecutionPlan(t *testing.T) {
29+
plan := bundle.ExecutionPlan{
30+
Steps: []bundle.ExecutionStep{
31+
{
32+
From: common.HexToAddress("0x1111111111111111111111111111111111111111"),
33+
Hash: common.HexToHash("0x2222222222222222222222222222222222222222222222222222222222222222"),
34+
},
35+
{
36+
From: common.HexToAddress("0x3333333333333333333333333333333333333333"),
37+
Hash: common.HexToHash("0x4444444444444444444444444444444444444444444444444444444444444444"),
38+
},
39+
},
40+
Flags: bundle.EF_TolerateInvalid | bundle.EF_OneOf,
41+
Range: bundle.BlockRange{
42+
Earliest: 100,
43+
Latest: 200,
44+
},
45+
}
46+
47+
rpcPlan := NewRPCExecutionPlan(plan)
48+
49+
require.Equal(t, plan.Flags, rpcPlan.Flags)
50+
require.Equal(t, rpc.BlockNumber(plan.Range.Earliest), rpcPlan.Earliest)
51+
require.Equal(t, rpc.BlockNumber(plan.Range.Latest), rpcPlan.Latest)
52+
require.Len(t, rpcPlan.Steps, len(plan.Steps))
53+
for i, step := range plan.Steps {
54+
require.Equal(t, step.From, rpcPlan.Steps[i].From)
55+
require.Equal(t, step.Hash, rpcPlan.Steps[i].Hash)
56+
}
57+
}

0 commit comments

Comments
 (0)