Skip to content

Commit b3d5a90

Browse files
authored
refactor(bmr): Clean up bsc and snow chains from unused code (#813) (#814)
1 parent 4944321 commit b3d5a90

File tree

12 files changed

+70
-799
lines changed

12 files changed

+70
-799
lines changed

cmd/iconbridge/chain/bsc/receiver.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"sync"
1414
"time"
1515

16-
ethereum "github.com/ethereum/go-ethereum"
16+
"github.com/ethereum/go-ethereum"
1717
"github.com/ethereum/go-ethereum/common"
1818
ethCommon "github.com/ethereum/go-ethereum/common"
19-
"github.com/ethereum/go-ethereum/core/types"
2019
ethTypes "github.com/ethereum/go-ethereum/core/types"
2120
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain"
21+
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
2222
"github.com/icon-project/icon-bridge/common/log"
2323
"github.com/pkg/errors"
2424
)
@@ -141,7 +141,7 @@ func (r *receiver) syncVerifier(ctx context.Context, vr IVerifier, height int64)
141141

142142
type res struct {
143143
Height int64
144-
Header *types.Header
144+
Header *ethTypes.Header
145145
}
146146

147147
type req struct {
@@ -153,7 +153,7 @@ func (r *receiver) syncVerifier(ctx context.Context, vr IVerifier, height int64)
153153

154154
r.log.WithFields(log.Fields{"height": vr.Next().String(), "target": height}).Info("syncVerifier: start")
155155

156-
var prevHeader *types.Header
156+
var prevHeader *ethTypes.Header
157157
cursor := vr.Next().Int64()
158158
for cursor <= height {
159159
rqch := make(chan *req, r.opts.SyncConcurrency)
@@ -238,7 +238,7 @@ func (r *receiver) syncVerifier(ctx context.Context, vr IVerifier, height int64)
238238
return nil
239239
}
240240

241-
func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback func(v *BlockNotification) error) (err error) {
241+
func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback func(v *types.BlockNotification) error) (err error) {
242242

243243
if opts == nil {
244244
return errors.New("receiveLoop: invalid options: <nil>")
@@ -259,7 +259,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
259259
// block notification channel
260260
// (buffered: to avoid deadlock)
261261
// increase concurrency parameter for faster sync
262-
bnch := make(chan *BlockNotification, r.opts.SyncConcurrency)
262+
bnch := make(chan *types.BlockNotification, r.opts.SyncConcurrency)
263263

264264
heightTicker := time.NewTicker(BlockInterval)
265265
defer heightTicker.Stop()
@@ -278,7 +278,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
278278
next, latest := opts.StartHeight, latestHeight()
279279

280280
// last unverified block notification
281-
var lbn *BlockNotification
281+
var lbn *types.BlockNotification
282282
// start monitor loop
283283

284284
for {
@@ -342,7 +342,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
342342

343343
type bnq struct {
344344
h uint64
345-
v *BlockNotification
345+
v *types.BlockNotification
346346
err error
347347
retry int
348348
}
@@ -355,7 +355,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
355355
r.log.Error("Fatal: Zero length of query channel. Avoiding deadlock")
356356
continue
357357
}
358-
bns := make([]*BlockNotification, 0, len(qch))
358+
bns := make([]*types.BlockNotification, 0, len(qch))
359359
for q := range qch {
360360
switch {
361361
case q.err != nil:
@@ -384,7 +384,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
384384
}()
385385

386386
if q.v == nil {
387-
q.v = &BlockNotification{}
387+
q.v = &types.BlockNotification{}
388388
}
389389

390390
q.v.Height = (&big.Int{}).SetUint64(q.h)
@@ -475,7 +475,7 @@ func (r *receiver) Subscribe(
475475
StartHeight: opts.Height,
476476
Concurrency: r.opts.SyncConcurrency,
477477
},
478-
func(v *BlockNotification) error {
478+
func(v *types.BlockNotification) error {
479479
r.log.WithFields(log.Fields{"height": v.Height}).Debug("block notification")
480480

481481
if v.Height.Uint64() != lastHeight+1 {
@@ -516,7 +516,7 @@ func (r *receiver) Subscribe(
516516
return _errCh, nil
517517
}
518518

519-
func (r *receiver) getRelayReceipts(v *BlockNotification) []*chain.Receipt {
519+
func (r *receiver) getRelayReceipts(v *types.BlockNotification) []*chain.Receipt {
520520
sc := common.HexToAddress(r.src.ContractAddress())
521521
var receipts []*chain.Receipt
522522
var events []*chain.Event

cmd/iconbridge/chain/bsc/receiver_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13-
ethereum "github.com/ethereum/go-ethereum"
13+
"github.com/ethereum/go-ethereum"
1414

1515
ethCommon "github.com/ethereum/go-ethereum/common"
1616
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -19,7 +19,7 @@ import (
1919
"github.com/icon-project/icon-bridge/cmd/e2etest/chain/bsc/abi/bmcperiphery"
2020
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain"
2121
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/mocks"
22-
bscTypes "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
22+
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
2323
"github.com/icon-project/icon-bridge/common/intconv"
2424
"github.com/icon-project/icon-bridge/common/log"
2525
"github.com/icon-project/icon-bridge/common/wallet"
@@ -29,9 +29,7 @@ import (
2929
)
3030

3131
const (
32-
ICON_BMC = "btp://0x7.icon/cx8a6606d526b96a16e6764aee5d9abecf926689df"
3332
BSC_BMC_PERIPHERY = "btp://0x61.bsc/0xB4fC4b3b4e3157448B7D279f06BC8e340d63e2a9"
34-
BlockHeight = 21447824
3533
)
3634

3735
func newTestReceiver(t *testing.T, src, dst chain.BTPAddress) chain.Receiver {
@@ -503,7 +501,7 @@ func Test_MockReceiveLoop(t *testing.T) {
503501
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
504502
defer cancel()
505503
err := rx.receiveLoop(ctx, &BnOptions{StartHeight: 23033451, Concurrency: 1},
506-
func(v *BlockNotification) error {
504+
func(v *types.BlockNotification) error {
507505
if v.HasBTPMessage != nil && *v.HasBTPMessage {
508506
if !blockHasBTPMessage[int(v.Height.Int64())] {
509507
return fmt.Errorf("Expected hasBTPMessage %v Got %v", blockHasBTPMessage[int(v.Height.Int64())], *v.HasBTPMessage)
@@ -721,7 +719,7 @@ func TestClient_MockMedianGasPrice(t *testing.T) {
721719
func TestClient_MockBlockReceipts(t *testing.T) {
722720
blkHash := ethCommon.HexToHash("0x941bf8efb2664191d52fdc4745ea07129aa6032097c0a434ac0e652f592ad00f")
723721
//blkNumber := 23033400
724-
blk := &bscTypes.Block{
722+
blk := &types.Block{
725723
Transactions: []string{"0x4d2c7826773b5e9a74eebff4fa6cba796faf6e318c4710a45b4afba7830de5da", "0x33f061ca51140df25c48d52e867df338b3771b431035d7b8bd2045346fec0372"},
726724
GasUsed: "0x203c27",
727725
}

cmd/iconbridge/chain/bsc/sender.go

-157
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,6 @@ const (
4747
DefaultGasLimit = 25000000
4848
)
4949

50-
/*
51-
type sender struct {
52-
c *Client
53-
src module.BtpAddress
54-
dst module.BtpAddress
55-
w Wallet
56-
l log.Logger
57-
opt struct {
58-
}
59-
60-
bmc *binding.BMC
61-
62-
evtLogRawFilter struct {
63-
addr []byte
64-
signature []byte
65-
next []byte
66-
seq []byte
67-
}
68-
evtReq *BlockRequest
69-
isFoundOffsetBySeq bool
70-
cb module.ReceiveCallback
71-
72-
mutex sync.Mutex
73-
}
74-
*/
7550

7651
type senderOptions struct {
7752
GasLimit uint64 `json:"gas_limit"`
@@ -360,135 +335,3 @@ func revertReason(data []byte) string {
360335
length := binary.BigEndian.Uint64(data[24:32])
361336
return string(data[32 : 32+length])
362337
}
363-
364-
/*
365-
func (s *sender) newTransactionParam(prev string, rm *RelayMessage) (*TransactionParam, error) {
366-
b, err := codec.RLP.MarshalToBytes(rm)
367-
if err != nil {
368-
return nil, err
369-
}
370-
rmp := BMCRelayMethodParams{
371-
Prev: prev,
372-
//Messages: base64.URLEncoding.EncodeToString(b[:]),
373-
Messages: string(b[:]),
374-
}
375-
s.l.Debugf("HandleRelayMessage msg: %s", base64.URLEncoding.EncodeToString(b))
376-
p := &TransactionParam{
377-
Params: rmp,
378-
}
379-
return p, nil
380-
}
381-
382-
func (s *sender) Relay(segment *module.Segment) (module.GetResultParam, error) {
383-
s.mutex.Lock()
384-
defer s.mutex.Unlock()
385-
p, ok := segment.TransactionParam.(*TransactionParam)
386-
if !ok {
387-
return nil, fmt.Errorf("casting failure")
388-
}
389-
t, err := s.c.newTransactOpts(s.w)
390-
if err != nil {
391-
return nil, err
392-
}
393-
rmp := p.Params.(BMCRelayMethodParams)
394-
var tx *types.Transaction
395-
tx, err = s.bmc.HandleRelayMessage(t, rmp.Prev, rmp.Messages)
396-
if err != nil {
397-
s.l.Errorf("handleRelayMessage: ", err.Error())
398-
return nil, err
399-
}
400-
thp := &TransactionHashParam{}
401-
thp.Hash = tx.Hash()
402-
s.l.Debugf("HandleRelayMessage tx hash:%s, prev %s, msg: %s", thp.Hash, rmp.Prev, base64.URLEncoding.EncodeToString([]byte(rmp.Messages)))
403-
return thp, nil
404-
}
405-
406-
func (s *sender) GetResult(p module.GetResultParam) (module.TransactionResult, error) {
407-
if txh, ok := p.(*TransactionHashParam); ok {
408-
for {
409-
_, pending, err := s.c.GetTransaction(txh.Hash)
410-
if err != nil {
411-
return nil, err
412-
}
413-
if pending {
414-
<-time.After(DefaultGetRelayResultInterval)
415-
continue
416-
}
417-
tx, err := s.c.GetTransactionReceipt(txh.Hash)
418-
if err != nil {
419-
return nil, err
420-
}
421-
return tx, nil //mapErrorWithTransactionResult(&types.Receipt{}, err) // TODO: map transaction.js result error
422-
}
423-
} else {
424-
return nil, fmt.Errorf("fail to casting TransactionHashParam %T", p)
425-
}
426-
}
427-
428-
func (s *sender) GetStatus() (*module.BMCLinkStatus, error) {
429-
var status binding.TypesLinkStats
430-
status, err := s.bmc.GetStatus(nil, s.src.String())
431-
432-
if err != nil {
433-
s.l.Errorf("Error retrieving relay status from BMC")
434-
return nil, err
435-
}
436-
437-
ls := &module.BMCLinkStatus{}
438-
ls.TxSeq = status.TxSeq.Int64()
439-
ls.RxSeq = status.RxSeq.Int64()
440-
ls.BMRIndex = int(status.RelayIdx.Int64())
441-
ls.RotateHeight = status.RotateHeight.Int64()
442-
ls.RotateTerm = int(status.RotateTerm.Int64())
443-
ls.DelayLimit = int(status.DelayLimit.Int64())
444-
ls.MaxAggregation = int(status.MaxAggregation.Int64())
445-
ls.CurrentHeight = status.CurrentHeight.Int64()
446-
ls.RxHeight = status.RxHeight.Int64()
447-
ls.RxHeightSrc = status.RxHeightSrc.Int64()
448-
return ls, nil
449-
}
450-
451-
func (s *sender) isOverLimit(size int) bool {
452-
return txSizeLimit < float64(size)
453-
}
454-
455-
func (s *sender) MonitorLoop(height int64, cb module.MonitorCallback, scb func()) error {
456-
s.l.Debugf("MonitorLoop (sender) connected")
457-
br := &BlockRequest{
458-
Height: big.NewInt(height),
459-
}
460-
return s.c.MonitorBlock(br,
461-
func(v *BlockNotification) error {
462-
return cb(v.Height.Int64())
463-
})
464-
}
465-
466-
func (s *sender) StopMonitorLoop() {
467-
s.c.CloseAllMonitor()
468-
}
469-
func (s *sender) FinalizeLatency() int {
470-
//on-the-next
471-
return 1
472-
}
473-
474-
func NewSender(src, dst module.BtpAddress, w Wallet, endpoints []string, opt map[string]interface{}, l log.Logger) module.Sender {
475-
s := &sender{
476-
src: src,
477-
dst: dst,
478-
w: w,
479-
l: l,
480-
}
481-
b, err := json.Marshal(opt)
482-
if err != nil {
483-
l.Panicf("fail to marshal opt:%#v err:%+v", opt, err)
484-
}
485-
if err = json.Unmarshal(b, &s.opt); err != nil {
486-
l.Panicf("fail to unmarshal opt:%#v err:%+v", opt, err)
487-
}
488-
s.c = NewClient(endpoints, l)
489-
490-
s.bmc, _ = binding.NewBMC(HexToAddress(s.dst.ContractAddress()), s.c.ethcl)
491-
492-
return s
493-
}
494-
*/

0 commit comments

Comments
 (0)