Skip to content

Commit 0443d3f

Browse files
committed
sc: transfer bridge data to NillRollup
1 parent a89096f commit 0443d3f

File tree

17 files changed

+234
-1669
lines changed

17 files changed

+234
-1669
lines changed

nil/services/synccommittee/Makefile.inc

+20-11
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ root_contracts = rollup-bridge-contracts/contracts
33

44
.PHONY: sync_committee_targets
55
sync_committee_targets: \
6-
sync_committee_contract_abi \
76
sync_committee_types_stringer \
87
sync_committee_generate_mocks \
98
sync_committee_protobuf \
109
sync_committee_tracer_constants \
11-
$(root_sc)/generate_fee_updater_abi
10+
$(root_sc)/generate_rollup_contract_abi \
11+
$(root_sc)/generate_fee_updater_abi \
12+
$(root_sc)/compile_l2_bridge_state_getter_abi
1213

1314
.PHONY: $(root_sc)/compile_fee_updater_abi
1415
$(root_sc)/compile_fee_updater_abi:
@@ -18,13 +19,21 @@ $(root_sc)/compile_fee_updater_abi:
1819
$(root_sc)/generate_fee_updater_abi: $(root_sc)/compile_fee_updater_abi
1920
cd $(root_sc)/core/feeupdater && go run github.com/ethereum/go-ethereum/cmd/abigen --abi IFeeStorage.abi --pkg=feeupdater --out=./i_fee_storage_contract_abi_generated.go
2021

21-
.PHONY: sync_committee_contract_abi
22-
sync_committee_contract_abi: \
23-
$(root_sc)/core/rollupcontract/rollupcontract_abi_generated.go
22+
.PHONY: $(root_sc)/compile_rollup_contract_abi
23+
$(root_sc)/compile_rollup_contract_abi:
24+
solc $(root_contracts)/interfaces/INilRollup.sol --abi --overwrite -o $(root_sc)/core/rollupcontract --allow-paths .,$(root_contracts)/common/libraries --no-cbor-metadata --metadata-hash none --pretty-json
25+
26+
.PHONY: $(root_sc)/generate_rollup_contract_abi
27+
$(root_sc)/generate_rollup_contract_abi: $(root_sc)/compile_rollup_contract_abi
28+
cd $(root_sc)/core/rollupcontract && go run github.com/ethereum/go-ethereum/cmd/abigen --abi INilRollup.abi --pkg=rollupcontract --out=./i_rollup_contract_abi_generated.go
2429

25-
$(root_sc)/core/rollupcontract/rollupcontract_abi_generated.go: \
26-
$(root_sc)/core/rollupcontract/abi.json
27-
go generate $(root_sc)/core/rollupcontract/generate.go
30+
.PHONY: $(root_sc)/compile_l2_bridge_state_getter_abi
31+
$(root_sc)/compile_l2_bridge_state_getter_abi:
32+
solc $(root_contracts)/bridge/l2/interfaces/IL2BridgeStateGetter.sol --abi --overwrite -o $(root_sc)/core/bridgecontract --allow-paths .,$(root_contracts)/common/libraries --no-cbor-metadata --metadata-hash none --pretty-json
33+
34+
$(root_sc)/core/rollupcontract/wrapper_generated_mock.go: \
35+
$(root_sc)/generate_rollup_contract_abi
36+
cd $(root_sc)/core/rollupcontract && ../../internal/scripts/generate_mock.sh Wrapper
2837

2938
$(root_sc)/internal/l1client/eth_client_generated_mock.go:
3039
cd $(root_sc)/internal/l1client && bash ../../internal/scripts/generate_mock.sh EthClient
@@ -84,7 +93,7 @@ $(root_sc)/internal/api/task_state_change_handler_generated_mock.go: \
8493
go generate $(root_sc)/internal/api/task_state_change_handler.go
8594

8695
$(root_sc)/core/state_reset_launcher_generated_mock.go: \
87-
sync_committee_contract_abi \
96+
$(root_sc)/generate_rollup_contract_abi \
8897
$(root_sc)/core/task_state_change_handler.go
8998
go generate $(root_sc)/core/task_state_change_handler.go
9099

@@ -107,9 +116,9 @@ $(root_sc)/prover/tracer/storage_getter_setter_generated_mock.go: \
107116
go generate $(root_sc)/prover/tracer
108117

109118
$(root_sc)/core/rollupcontract/wrapper_generated_mock.go: \
110-
sync_committee_contract_abi \
119+
$(root_sc)/generate_rollup_contract_abi \
111120
$(root_sc)/core/rollupcontract/wrapper.go
112-
go generate $(root_sc)/core/rollupcontract/generate.go
121+
cd $(root_sc)/core/rollupcontract && bash ../../internal/scripts/generate_mock.sh Wrapper
113122

114123
$(root_sc)/core/feeupdater/contract_wrapper_generated_mock.go: \
115124
$(root_sc)/core/feeupdater/contract_wrapper.go \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package bridgecontract
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/big"
7+
8+
"github.com/NilFoundation/nil/nil/client"
9+
"github.com/NilFoundation/nil/nil/common/hexutil"
10+
"github.com/NilFoundation/nil/nil/internal/abi"
11+
"github.com/NilFoundation/nil/nil/internal/types"
12+
"github.com/NilFoundation/nil/nil/services/rpc/jsonrpc"
13+
"golang.org/x/sync/errgroup"
14+
)
15+
16+
type Bytes32 [32]byte
17+
18+
type BridgeState struct {
19+
L2toL1Root Bytes32
20+
DepositNonce *big.Int
21+
L1MessageHash Bytes32
22+
}
23+
24+
type BridgeStateGetter interface {
25+
GetBridgeState(ctx context.Context, blockId any) (*BridgeState, error)
26+
}
27+
28+
type bridgeStateGetter struct {
29+
nilClient client.Client
30+
contractAddr types.Address
31+
abi *abi.ABI
32+
}
33+
34+
func NewBridgeStateGetter(
35+
nilClient client.Client,
36+
l2ContractAddr types.Address,
37+
) *bridgeStateGetter {
38+
return &bridgeStateGetter{
39+
nilClient: nilClient,
40+
contractAddr: l2ContractAddr,
41+
abi: GetL2BridgeStateGetterABI(),
42+
}
43+
}
44+
45+
func (b *bridgeStateGetter) GetBridgeState(ctx context.Context, blockId any) (*BridgeState, error) {
46+
eg, gCtx := errgroup.WithContext(ctx)
47+
48+
var l1MessageHash Bytes32
49+
eg.Go(func() error {
50+
ret, err := callContract[Bytes32](gCtx, b.nilClient, blockId, b.contractAddr, b.abi, "l1MessageHash")
51+
if err != nil {
52+
return err
53+
}
54+
l1MessageHash = ret
55+
return nil
56+
})
57+
58+
var l2ToL1Root Bytes32
59+
eg.Go(func() error {
60+
ret, err := callContract[Bytes32](gCtx, b.nilClient, blockId, b.contractAddr, b.abi, "getL2ToL1Root")
61+
if err != nil {
62+
return err
63+
}
64+
l2ToL1Root = ret
65+
return nil
66+
})
67+
68+
var depositNonce Bytes32
69+
eg.Go(func() error {
70+
ret, err := callContract[Bytes32](gCtx, b.nilClient, blockId, b.contractAddr, b.abi, "getDepositNonce")
71+
if err != nil {
72+
return err
73+
}
74+
depositNonce = ret
75+
return nil
76+
})
77+
78+
if err := eg.Wait(); err != nil {
79+
return nil, err
80+
}
81+
82+
ret := &BridgeState{
83+
L2toL1Root: l2ToL1Root,
84+
DepositNonce: new(big.Int),
85+
L1MessageHash: l1MessageHash,
86+
}
87+
ret.DepositNonce.SetBytes(depositNonce[:])
88+
return ret, nil
89+
}
90+
91+
func callContract[Ret any](
92+
ctx context.Context,
93+
c client.Client,
94+
blockId any,
95+
addr types.Address,
96+
abi *abi.ABI,
97+
method string,
98+
args ...any,
99+
) (Ret, error) {
100+
var ret Ret
101+
calldata, err := abi.Pack(method, args...)
102+
if err != nil {
103+
return ret, err
104+
}
105+
106+
callArgs := &jsonrpc.CallArgs{
107+
Data: (*hexutil.Bytes)(&calldata),
108+
To: addr,
109+
Fee: types.NewFeePackFromGas(100_000), // TODO(oclaw) which value to use here?
110+
}
111+
112+
callRes, err := c.Call(ctx, callArgs, blockId, nil)
113+
if err != nil {
114+
return ret, err
115+
}
116+
117+
res, err := abi.Unpack(method, callRes.Data)
118+
if err != nil {
119+
return ret, err
120+
}
121+
122+
if len(res) != 1 {
123+
return ret, fmt.Errorf("expected single return value, got %d", len(res))
124+
}
125+
126+
var ok bool
127+
ret, ok = res[0].(Ret)
128+
if !ok {
129+
return ret, fmt.Errorf("type mismatch: expected %T got %T", ret, res[0])
130+
}
131+
132+
return ret, nil
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package bridgecontract
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
7+
"github.com/NilFoundation/nil/nil/common/check"
8+
"github.com/NilFoundation/nil/nil/internal/abi"
9+
)
10+
11+
//go:embed IL2BridgeStateGetter.abi
12+
var l2BridgeStateGetterContractABIData []byte
13+
14+
var l2BridgeStateGetterContractABI *abi.ABI
15+
16+
func init() {
17+
abi, err := abi.JSON(bytes.NewReader(l2BridgeStateGetterContractABIData))
18+
check.PanicIfErr(err)
19+
if err != nil {
20+
panic(err)
21+
}
22+
l2BridgeStateGetterContractABI = &abi
23+
}
24+
25+
func GetL2BridgeStateGetterABI() *abi.ABI {
26+
return l2BridgeStateGetterContractABI
27+
}

nil/services/synccommittee/core/proposer.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/NilFoundation/nil/nil/common"
1010
"github.com/NilFoundation/nil/nil/common/concurrent"
1111
"github.com/NilFoundation/nil/nil/common/logging"
12+
"github.com/NilFoundation/nil/nil/services/synccommittee/core/bridgecontract"
1213
"github.com/NilFoundation/nil/nil/services/synccommittee/core/reset"
1314
"github.com/NilFoundation/nil/nil/services/synccommittee/core/rollupcontract"
1415
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/log"
@@ -35,6 +36,7 @@ type proposer struct {
3536

3637
storage ProposerStorage
3738
resetter *reset.StateResetLauncher
39+
bridgeStateGetter bridgecontract.BridgeStateGetter
3840
rollupContractWrapper rollupcontract.Wrapper
3941
metrics ProposerMetrics
4042
logger logging.Logger
@@ -56,13 +58,15 @@ func NewDefaultProposerConfig() ProposerConfig {
5658
func NewProposer(
5759
config ProposerConfig,
5860
storage ProposerStorage,
61+
bridgeStateGetter bridgecontract.BridgeStateGetter,
5962
contractWrapper rollupcontract.Wrapper,
6063
resetter *reset.StateResetLauncher,
6164
metrics ProposerMetrics,
6265
logger logging.Logger,
6366
) (*proposer, error) {
6467
p := &proposer{
6568
storage: storage,
69+
bridgeStateGetter: bridgeStateGetter,
6670
rollupContractWrapper: contractWrapper,
6771
resetter: resetter,
6872
metrics: metrics,
@@ -110,14 +114,24 @@ func (p *proposer) updateState(
110114
ctx context.Context,
111115
proposalData *scTypes.ProposalData,
112116
) error {
113-
// TODO: populate with actual data
117+
// NOTE(oclaw) current impl assumes that L2BridgeMessenger is deployed at the main shard
118+
// if it is not - we need to expilicitly set shard id and use latest block ref from it
119+
lastestMainBlockRef := proposalData.NewProvedStateRoot
120+
if lastestMainBlockRef == common.EmptyHash {
121+
return errors.New("latest main block ref is empty")
122+
}
123+
124+
bridgeData, err := p.bridgeStateGetter.GetBridgeState(ctx, lastestMainBlockRef)
125+
if err != nil {
126+
return fmt.Errorf("failed to get bridge state: %w", err)
127+
}
114128

115129
updateStateData := scTypes.NewUpdateStateData(
116130
proposalData,
117131
[]byte{0x0A, 0x0B, 0x0C},
118-
common.EmptyHash,
119-
0,
120-
common.EmptyHash,
132+
common.Hash(bridgeData.L2toL1Root),
133+
common.Hash(bridgeData.L1MessageHash),
134+
bridgeData.DepositNonce,
121135
)
122136

123137
log.NewStateUpdateEvent(p.logger, zerolog.InfoLevel, updateStateData).Msg("calling UpdateState L1 method")

nil/services/synccommittee/core/proposer_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (s *ProposerTestSuite) SetupSuite() {
135135
s.proposer, err = NewProposer(
136136
s.params,
137137
s.storage,
138+
nil, // TODO oclaw
138139
contractWrapper,
139140
resetLauncher,
140141
metricsHandler,

0 commit comments

Comments
 (0)