Skip to content

Commit 5b457a3

Browse files
authored
Merge pull request #18 from icon-project/fix-historical-summary-index-calc
Fix historical summary index calculation
2 parents aa6d529 + 8f9432a commit 5b457a3

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

chain/eth2/beacon.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2023 ICON Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package eth2
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/icon-project/btp2-eth2/chain/eth2/client"
23+
)
24+
25+
type BeaconNetwork int
26+
27+
const (
28+
Mainnet BeaconNetwork = iota + 1
29+
Sepolia
30+
)
31+
32+
var networkNames = []string{
33+
"",
34+
"mainnet",
35+
"sepolia",
36+
}
37+
38+
func (b BeaconNetwork) String() string {
39+
return networkNames[b]
40+
}
41+
42+
const (
43+
mainnetGenesisValidatorsRoot = "0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"
44+
sepoliaGenesisValidatorsRoot = "0xd8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078"
45+
)
46+
47+
func getBeaconNetwork(c *client.ConsensusLayer) (BeaconNetwork, error) {
48+
genesis, err := c.Genesis()
49+
if err != nil {
50+
return 0, err
51+
}
52+
53+
switch fmt.Sprintf("%#x", genesis.GenesisValidatorsRoot) {
54+
case mainnetGenesisValidatorsRoot:
55+
return Mainnet, nil
56+
case sepoliaGenesisValidatorsRoot:
57+
return Sepolia, nil
58+
default:
59+
return 0, fmt.Errorf("invalid target network")
60+
}
61+
}

chain/eth2/receiver.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type receiver struct {
8383
l log.Logger
8484
src types.BtpAddress
8585
dst types.BtpAddress
86+
bn BeaconNetwork
8687
cl *client.ConsensusLayer
8788
el *client.ExecutionLayer
8889
bmc *client.BMC
@@ -114,14 +115,21 @@ func newReceiver(src, dst types.BtpAddress, endpoint string, opt map[string]inte
114115
ht: make(map[int64]*ssz.Node),
115116
cp: make(map[int64]*phase0.BeaconBlockHeader),
116117
}
117-
r.el, err = client.NewExecutionLayer(endpoint, l)
118-
if err != nil {
119-
l.Panicf("failed to connect to %s, %v", endpoint, err)
120-
}
118+
121119
r.cl, err = client.NewConsensusLayer(opt["consensus_endpoint"].(string), l)
122120
if err != nil {
123121
l.Panicf("failed to connect to %s, %v", opt["consensus_endpoint"].(string), err)
124122
}
123+
r.bn, err = getBeaconNetwork(r.cl)
124+
if err != nil {
125+
l.Panicf("fail to get beacon network, %v", err)
126+
}
127+
r.l.Debugf("Detected beacon network : %s", r.bn.String())
128+
129+
r.el, err = client.NewExecutionLayer(endpoint, l)
130+
if err != nil {
131+
l.Panicf("failed to connect to %s, %v", endpoint, err)
132+
}
125133
r.bmc, err = client.NewBMC(common.HexToAddress(r.src.ContractAddress()), r.el.GetBackend())
126134
if err != nil {
127135
l.Panicf("fail to get instance of BMC %s, %v", r.src.ContractAddress(), err)
@@ -332,7 +340,7 @@ func (r *receiver) blockProofDataViaBlockRoots(bls *types.BMCLinkStatus, mp *mes
332340

333341
func (r *receiver) blockProofDataViaHistoricalSummaries(bls *types.BMCLinkStatus, mp *messageProofData) (*blockProofData, error) {
334342
header := mp.Header.Beacon
335-
gindex := proof.HistoricalSummariesIdxToGIndex(SlotToHistoricalSummariesIndex(header.Slot))
343+
gindex := proof.HistoricalSummariesIdxToGIndex(SlotToHistoricalSummariesIndex(r.bn, header.Slot))
336344
r.l.Debugf("make blockProofData with historicalSummaries. finalized: %d, slot:%d, gIndex:%d",
337345
bls.Verifier.Height, header.Slot, gindex)
338346
bp, err := r.cl.GetStateProofWithGIndex(strconv.FormatInt(bls.Verifier.Height, 10), gindex)
@@ -363,7 +371,7 @@ func (r *receiver) blockProofDataViaHistoricalSummaries(bls *types.BMCLinkStatus
363371

364372
func (r *receiver) getHistoricalSummariesTrie(slot int64) (*ssz.Node, error) {
365373
roots := make([][]byte, SlotPerHistoricalRoot)
366-
start := int64(HistoricalSummariesStartSlot(phase0.Slot(slot)))
374+
start := int64(HistoricalSummariesStartSlot(r.bn, phase0.Slot(slot)))
367375
r.l.Debugf("getHistoricalSummariesTrie slot:%d start:%d", slot, start)
368376
if ht, ok := r.ht[start]; !ok {
369377
var prevRoot []byte

chain/eth2/receiver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func TestReceiver_MessageProof(t *testing.T) {
230230
}
231231

232232
var mp *messageProofData
233-
mp, err = r.makeMessageProofData(header)
233+
mp, err = r.makeMessageProofData(header, nil)
234234
assert.NoError(t, err)
235235

236236
// verify receiptsRoot

chain/eth2/time.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const (
1313

1414
ForkEpochCapellaSepolia = 56832
1515
ForkEpochCapellaMainnet = 194048
16-
ForkEpochCapella = ForkEpochCapellaSepolia
1716
)
1817

1918
// SlotToEpoch returns the epoch number of the input slot.
@@ -41,10 +40,21 @@ func SlotToBlockRootsIndex(s phase0.Slot) uint64 {
4140
return uint64(s % SlotPerHistoricalRoot)
4241
}
4342

44-
func SlotToHistoricalSummariesIndex(s phase0.Slot) uint64 {
45-
return uint64(s-ForkEpochCapella*SlotPerEpoch) / SlotPerHistoricalRoot
43+
func forkEpochCapella(bn BeaconNetwork) uint64 {
44+
switch bn {
45+
case Mainnet:
46+
return ForkEpochCapellaMainnet
47+
case Sepolia:
48+
return ForkEpochCapellaSepolia
49+
default:
50+
return 0
51+
}
4652
}
4753

48-
func HistoricalSummariesStartSlot(s phase0.Slot) phase0.Slot {
49-
return phase0.Slot((ForkEpochCapella * SlotPerEpoch) + SlotToHistoricalSummariesIndex(s)*SlotPerHistoricalRoot)
54+
func SlotToHistoricalSummariesIndex(bn BeaconNetwork, s phase0.Slot) uint64 {
55+
return (uint64(s) - forkEpochCapella(bn)*SlotPerEpoch) / SlotPerHistoricalRoot
56+
}
57+
58+
func HistoricalSummariesStartSlot(bn BeaconNetwork, s phase0.Slot) phase0.Slot {
59+
return phase0.Slot((forkEpochCapella(bn) * SlotPerEpoch) + SlotToHistoricalSummariesIndex(bn, s)*SlotPerHistoricalRoot)
5060
}

0 commit comments

Comments
 (0)