Skip to content

Commit ad325e0

Browse files
committed
Support bohr upgrade
1 parent 757f34a commit ad325e0

File tree

11 files changed

+660
-4666
lines changed

11 files changed

+660
-4666
lines changed

chain/bsc2/bmc.go

+358-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/bsc2/calculator.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import (
44
"bytes"
55

66
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/params"
78
"github.com/icon-project/btp2/common/log"
89
)
910

1011
type BlockFinalityCalculator struct {
12+
config *params.ChainConfig
1113
checkpoint common.Hash
1214
feeds []common.Hash
1315
snaps *Snapshots
1416
log log.Logger
1517
}
1618

17-
func newBlockFinalityCalculator(checkpoint common.Hash, feeds []common.Hash, snaps *Snapshots, log log.Logger) *BlockFinalityCalculator {
19+
func newBlockFinalityCalculator(config *params.ChainConfig, checkpoint common.Hash, feeds []common.Hash, snaps *Snapshots, log log.Logger) *BlockFinalityCalculator {
1820
return &BlockFinalityCalculator{
21+
config: config,
1922
checkpoint: checkpoint,
2023
feeds: feeds,
2124
snaps: snaps,
@@ -44,12 +47,12 @@ func (o *BlockFinalityCalculator) feed(feed common.Hash) ([]common.Hash, error)
4447
}
4548

4649
func (o *BlockFinalityCalculator) calculate() (common.Hash, error) {
47-
snap, err := o.snaps.get(o.feeds[len(o.feeds)-1])
50+
snap, err := o.snaps.get(o.config, o.feeds[len(o.feeds)-1])
4851
if err != nil {
4952
return common.Hash{}, err
5053
}
5154

52-
checkpoint, err := o.snaps.get(o.checkpoint)
55+
checkpoint, err := o.snaps.get(o.config, o.checkpoint)
5356
if err != nil {
5457
return common.Hash{}, err
5558
}
@@ -65,7 +68,7 @@ func (o *BlockFinalityCalculator) calculate() (common.Hash, error) {
6568
break
6669
}
6770

68-
snap, err = o.snaps.get(snap.ParentHash)
71+
snap, err = o.snaps.get(o.config, snap.ParentHash)
6972
if err != nil {
7073
return common.Hash{}, err
7174
}

chain/bsc2/receiver.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/ethereum/go-ethereum/accounts/abi/bind"
15+
"github.com/ethereum/go-ethereum/params"
1516

1617
lru "github.com/hashicorp/golang-lru"
1718

@@ -53,6 +54,7 @@ type RecvConfig struct {
5354

5455
type receiver struct {
5556
cfg RecvConfig
57+
config *params.ChainConfig
5658
chainId *big.Int
5759
epoch uint64
5860
startnumber uint64
@@ -83,6 +85,13 @@ func newReceiver(config RecvConfig, log log.Logger) *receiver {
8385
log: log,
8486
}
8587

88+
switch config.ChainID {
89+
case 56:
90+
o.config = params.BSCChainConfig
91+
case 97:
92+
o.config = params.ChapelChainConfig
93+
}
94+
8695
if cache, err := lru.NewARC(CacheSize); err != nil {
8796
o.log.Panicf("fail to create lru head cache - size(%d) err(%s)", CacheSize, err.Error())
8897
} else {
@@ -209,7 +218,7 @@ func (o *receiver) recoverable(err error) bool {
209218
}
210219

211220
func (o *receiver) applyAndCache(snap *Snapshot, head *types.Header) (*Snapshot, error) {
212-
next, err := snap.apply(head, o.chainId)
221+
next, err := snap.apply(o.config, head, o.chainId)
213222
if err != nil {
214223
o.log.Warnf("fail to apply snapshot - err(%s)", err)
215224
return nil, err
@@ -239,9 +248,9 @@ var ErrInconsistentCount = 0
239248
func (o *receiver) loop(hash common.Hash, och chan<- interface{}) error {
240249
o.log.Tracef("StartReceiverLoop")
241250
headCh := make(chan *types.Header)
242-
calc := newBlockFinalityCalculator(hash, make([]common.Hash, 0), o.snapshots, o.log)
251+
calc := newBlockFinalityCalculator(o.config, hash, make([]common.Hash, 0), o.snapshots, o.log)
243252

244-
snap, err := o.snapshots.get(hash)
253+
snap, err := o.snapshots.get(o.config, hash)
245254
if err != nil {
246255
o.log.Panicf("NoSnapshot(%s)", hash)
247256
}
@@ -271,7 +280,7 @@ func (o *receiver) loop(hash common.Hash, och chan<- interface{}) error {
271280
final := fnzs[len(fnzs)-1]
272281
var number uint64
273282
var hash common.Hash
274-
if snap, err := o.snapshots.get(final); err != nil {
283+
if snap, err := o.snapshots.get(o.config, final); err != nil {
275284
o.log.Panicln(err.Error())
276285
} else {
277286
// latest finalized block number
@@ -323,7 +332,7 @@ func (o *receiver) updateStatus(number uint64, hash common.Hash, sequence uint64
323332
func (o *receiver) queryAndCacheMessages(child, ancestor common.Hash) (uint64, error) {
324333
var sequence uint64
325334
for child != ancestor {
326-
snap, _ := o.snapshots.get(child)
335+
snap, _ := o.snapshots.get(o.config, child)
327336
o.log.Debugf("Query Message BlockHash(%d:%s)", snap.Number, snap.Hash)
328337
ms, err := o.client.MessagesByBlockHash(context.Background(), child)
329338
if err != nil {
@@ -340,7 +349,7 @@ func (o *receiver) queryAndCacheMessages(child, ancestor common.Hash) (uint64, e
340349
}
341350
}
342351

343-
if snap, err := o.snapshots.get(child); err != nil {
352+
if snap, err := o.snapshots.get(o.config, child); err != nil {
344353
return sequence, err
345354
} else {
346355
child = snap.ParentHash
@@ -402,7 +411,7 @@ func (o *receiver) selectFork(from common.Hash, blocks *BlockTree) []common.Hash
402411

403412
var hash common.Hash
404413
for _, child := range children {
405-
snap, err := o.snapshots.get(child)
414+
snap, err := o.snapshots.get(o.config, child)
406415
if err == nil {
407416
hash = snap.Hash
408417
break
@@ -440,14 +449,14 @@ func (o *receiver) BuildBlockUpdate(status *btp.BMCLinkStatus, limit int64) ([]l
440449
o.log.Debugf("Build block updates - P(%d:%.8s) L(%d:%d:%.8s)",
441450
peer.number, peer.blocks.Root().Hex(), o.local.number, o.local.cache, o.local.hash)
442451
defer o.log.Debugf("Done - Build block updates")
443-
if _, err := o.snapshots.get(peer.hash); err != nil {
452+
if _, err := o.snapshots.get(o.config, peer.hash); err != nil {
444453
o.log.Errorf("No header for finalized block number(%d)", peer.number)
445454
return nil, errors.New("NoCachedHeader")
446455
}
447456

448457
canonical := o.selectFork(peer.hash, peer.blocks)
449458
o.log.Debugf("Canonical(%v)", canonical)
450-
calc := newBlockFinalityCalculator(peer.hash, canonical, o.snapshots, o.log)
459+
calc := newBlockFinalityCalculator(o.config, peer.hash, canonical, o.snapshots, o.log)
451460

452461
heads := make([]*types.Header, 0)
453462
parent := peer.hash
@@ -749,7 +758,7 @@ func (o *receiver) prepare() error {
749758
}
750759

751760
if !ok {
752-
snap, err := BootSnapshot(o.epoch, head, o.client.Client, o.log)
761+
snap, err := BootSnapshot(o.config, o.epoch, head, o.client.Client, o.log)
753762
if err != nil {
754763
return err
755764
}
@@ -773,7 +782,7 @@ func (o *receiver) synchronize(until *big.Int) error {
773782

774783
// synchronize snapshots
775784
o.log.Infof("synchronize block snapshots - until(%s)", hash)
776-
if err := o.snapshots.ensure(hash); err != nil {
785+
if err := o.snapshots.ensure(o.config, hash); err != nil {
777786
o.log.Errorf("fail to load past snapshots - err(%+v)", err)
778787
return err
779788
}

chain/bsc2/sender.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1111
"github.com/ethereum/go-ethereum/core/types"
12+
"github.com/ethereum/go-ethereum/params"
1213
"github.com/icon-project/btp2/common/errors"
1314
"github.com/icon-project/btp2/common/log"
1415
btp "github.com/icon-project/btp2/common/types"
@@ -32,6 +33,7 @@ type relayResult struct {
3233

3334
type sender struct {
3435
cfg SenderConfig
36+
config *params.ChainConfig
3537
src, dst btp.BtpAddress
3638
chainId *big.Int
3739
epoch uint64
@@ -65,11 +67,17 @@ func newSender(config SenderConfig, wallet btp.Wallet, log log.Logger) btp.Sende
6567
log: log,
6668
client: NewClient(config.Endpoint, config.DstAddress, config.SrcAddress, log),
6769
}
70+
switch config.ChainID {
71+
case 56:
72+
o.config = params.BSCChainConfig
73+
case 97:
74+
o.config = params.ChapelChainConfig
75+
}
6876
o.snapshots = newSnapshots(o.chainId, o.client.Client, o.cfg.BlockCheckpointInterval, CacheSize, nil, log)
6977
o.transactor = newMessageTransactor(MessageTransactorConfig{
7078
MaxGasLimit: o.cfg.MaxGasLimit,
7179
EstimateGasFactor: o.cfg.EstimateGasFactor,
72-
}, o.snapshots, o.log)
80+
}, o.config, o.snapshots, o.log)
7381
return o
7482
}
7583

@@ -111,7 +119,7 @@ func (o *sender) prepare() error {
111119
}
112120

113121
// check block finality by the nearest epoch block
114-
if snap, err := BootSnapshot(o.epoch, head, o.client.Client, o.log); err != nil {
122+
if snap, err := BootSnapshot(o.config, o.epoch, head, o.client.Client, o.log); err != nil {
115123
return err
116124
} else {
117125
o.log.Debugf("make initial snapshot - number(%d) hash(%s)", snap.Number, snap.Hash.Hex())
@@ -127,7 +135,7 @@ func (o *sender) watchBlockFinalities() error {
127135
headCh := make(chan *types.Header)
128136
number := new(big.Int).SetUint64(o.finality.Number + uint64(1))
129137
snap := o.finality
130-
calc := newBlockFinalityCalculator(o.finality.Hash, make([]common.Hash, 0), o.snapshots, o.log)
138+
calc := newBlockFinalityCalculator(o.config, o.finality.Hash, make([]common.Hash, 0), o.snapshots, o.log)
131139
o.log.Tracef("new calculator - addr(%p) number(%d) hash(%s)", calc, o.finality.Number, o.finality.Hash.Hex())
132140
var err error
133141
sub := o.client.WatchHeader(context.Background(), number, headCh)
@@ -137,7 +145,7 @@ func (o *sender) watchBlockFinalities() error {
137145
o.log.Errorf("Watcher error(%+v)", err)
138146
return err
139147
case head := <-headCh:
140-
snap, err = snap.apply(head, o.chainId)
148+
snap, err = snap.apply(o.config, head, o.chainId)
141149
if err != nil {
142150
o.log.Errorf("fail to apply snapshot - err(%+v)", err)
143151
sub.Unsubscribe()
@@ -158,7 +166,7 @@ func (o *sender) watchBlockFinalities() error {
158166
if len(fnzs) <= 0 {
159167
break
160168
}
161-
fn, err := o.snapshots.get(fnzs[len(fnzs)-1])
169+
fn, err := o.snapshots.get(o.config, fnzs[len(fnzs)-1])
162170
if err != nil {
163171
o.log.Errorf("fail to get snapshot - err(%+v)", err)
164172
sub.Unsubscribe()

0 commit comments

Comments
 (0)