Skip to content

Commit

Permalink
refactor(bmr): Clean up bsc and snow chains from unused code (#813) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-kl authored Nov 22, 2022
1 parent 4944321 commit b3d5a90
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 799 deletions.
24 changes: 12 additions & 12 deletions cmd/iconbridge/chain/bsc/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"sync"
"time"

ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain"
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
"github.com/icon-project/icon-bridge/common/log"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -141,7 +141,7 @@ func (r *receiver) syncVerifier(ctx context.Context, vr IVerifier, height int64)

type res struct {
Height int64
Header *types.Header
Header *ethTypes.Header
}

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

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

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

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

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

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

// last unverified block notification
var lbn *BlockNotification
var lbn *types.BlockNotification
// start monitor loop

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

type bnq struct {
h uint64
v *BlockNotification
v *types.BlockNotification
err error
retry int
}
Expand All @@ -355,7 +355,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
r.log.Error("Fatal: Zero length of query channel. Avoiding deadlock")
continue
}
bns := make([]*BlockNotification, 0, len(qch))
bns := make([]*types.BlockNotification, 0, len(qch))
for q := range qch {
switch {
case q.err != nil:
Expand Down Expand Up @@ -384,7 +384,7 @@ func (r *receiver) receiveLoop(ctx context.Context, opts *BnOptions, callback fu
}()

if q.v == nil {
q.v = &BlockNotification{}
q.v = &types.BlockNotification{}
}

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

if v.Height.Uint64() != lastHeight+1 {
Expand Down Expand Up @@ -516,7 +516,7 @@ func (r *receiver) Subscribe(
return _errCh, nil
}

func (r *receiver) getRelayReceipts(v *BlockNotification) []*chain.Receipt {
func (r *receiver) getRelayReceipts(v *types.BlockNotification) []*chain.Receipt {
sc := common.HexToAddress(r.src.ContractAddress())
var receipts []*chain.Receipt
var events []*chain.Event
Expand Down
10 changes: 4 additions & 6 deletions cmd/iconbridge/chain/bsc/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum"

ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -19,7 +19,7 @@ import (
"github.com/icon-project/icon-bridge/cmd/e2etest/chain/bsc/abi/bmcperiphery"
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain"
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/mocks"
bscTypes "github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
"github.com/icon-project/icon-bridge/cmd/iconbridge/chain/bsc/types"
"github.com/icon-project/icon-bridge/common/intconv"
"github.com/icon-project/icon-bridge/common/log"
"github.com/icon-project/icon-bridge/common/wallet"
Expand All @@ -29,9 +29,7 @@ import (
)

const (
ICON_BMC = "btp://0x7.icon/cx8a6606d526b96a16e6764aee5d9abecf926689df"
BSC_BMC_PERIPHERY = "btp://0x61.bsc/0xB4fC4b3b4e3157448B7D279f06BC8e340d63e2a9"
BlockHeight = 21447824
)

func newTestReceiver(t *testing.T, src, dst chain.BTPAddress) chain.Receiver {
Expand Down Expand Up @@ -503,7 +501,7 @@ func Test_MockReceiveLoop(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
err := rx.receiveLoop(ctx, &BnOptions{StartHeight: 23033451, Concurrency: 1},
func(v *BlockNotification) error {
func(v *types.BlockNotification) error {
if v.HasBTPMessage != nil && *v.HasBTPMessage {
if !blockHasBTPMessage[int(v.Height.Int64())] {
return fmt.Errorf("Expected hasBTPMessage %v Got %v", blockHasBTPMessage[int(v.Height.Int64())], *v.HasBTPMessage)
Expand Down Expand Up @@ -721,7 +719,7 @@ func TestClient_MockMedianGasPrice(t *testing.T) {
func TestClient_MockBlockReceipts(t *testing.T) {
blkHash := ethCommon.HexToHash("0x941bf8efb2664191d52fdc4745ea07129aa6032097c0a434ac0e652f592ad00f")
//blkNumber := 23033400
blk := &bscTypes.Block{
blk := &types.Block{
Transactions: []string{"0x4d2c7826773b5e9a74eebff4fa6cba796faf6e318c4710a45b4afba7830de5da", "0x33f061ca51140df25c48d52e867df338b3771b431035d7b8bd2045346fec0372"},
GasUsed: "0x203c27",
}
Expand Down
157 changes: 0 additions & 157 deletions cmd/iconbridge/chain/bsc/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,6 @@ const (
DefaultGasLimit = 25000000
)

/*
type sender struct {
c *Client
src module.BtpAddress
dst module.BtpAddress
w Wallet
l log.Logger
opt struct {
}
bmc *binding.BMC
evtLogRawFilter struct {
addr []byte
signature []byte
next []byte
seq []byte
}
evtReq *BlockRequest
isFoundOffsetBySeq bool
cb module.ReceiveCallback
mutex sync.Mutex
}
*/

type senderOptions struct {
GasLimit uint64 `json:"gas_limit"`
Expand Down Expand Up @@ -360,135 +335,3 @@ func revertReason(data []byte) string {
length := binary.BigEndian.Uint64(data[24:32])
return string(data[32 : 32+length])
}

/*
func (s *sender) newTransactionParam(prev string, rm *RelayMessage) (*TransactionParam, error) {
b, err := codec.RLP.MarshalToBytes(rm)
if err != nil {
return nil, err
}
rmp := BMCRelayMethodParams{
Prev: prev,
//Messages: base64.URLEncoding.EncodeToString(b[:]),
Messages: string(b[:]),
}
s.l.Debugf("HandleRelayMessage msg: %s", base64.URLEncoding.EncodeToString(b))
p := &TransactionParam{
Params: rmp,
}
return p, nil
}
func (s *sender) Relay(segment *module.Segment) (module.GetResultParam, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
p, ok := segment.TransactionParam.(*TransactionParam)
if !ok {
return nil, fmt.Errorf("casting failure")
}
t, err := s.c.newTransactOpts(s.w)
if err != nil {
return nil, err
}
rmp := p.Params.(BMCRelayMethodParams)
var tx *types.Transaction
tx, err = s.bmc.HandleRelayMessage(t, rmp.Prev, rmp.Messages)
if err != nil {
s.l.Errorf("handleRelayMessage: ", err.Error())
return nil, err
}
thp := &TransactionHashParam{}
thp.Hash = tx.Hash()
s.l.Debugf("HandleRelayMessage tx hash:%s, prev %s, msg: %s", thp.Hash, rmp.Prev, base64.URLEncoding.EncodeToString([]byte(rmp.Messages)))
return thp, nil
}
func (s *sender) GetResult(p module.GetResultParam) (module.TransactionResult, error) {
if txh, ok := p.(*TransactionHashParam); ok {
for {
_, pending, err := s.c.GetTransaction(txh.Hash)
if err != nil {
return nil, err
}
if pending {
<-time.After(DefaultGetRelayResultInterval)
continue
}
tx, err := s.c.GetTransactionReceipt(txh.Hash)
if err != nil {
return nil, err
}
return tx, nil //mapErrorWithTransactionResult(&types.Receipt{}, err) // TODO: map transaction.js result error
}
} else {
return nil, fmt.Errorf("fail to casting TransactionHashParam %T", p)
}
}
func (s *sender) GetStatus() (*module.BMCLinkStatus, error) {
var status binding.TypesLinkStats
status, err := s.bmc.GetStatus(nil, s.src.String())
if err != nil {
s.l.Errorf("Error retrieving relay status from BMC")
return nil, err
}
ls := &module.BMCLinkStatus{}
ls.TxSeq = status.TxSeq.Int64()
ls.RxSeq = status.RxSeq.Int64()
ls.BMRIndex = int(status.RelayIdx.Int64())
ls.RotateHeight = status.RotateHeight.Int64()
ls.RotateTerm = int(status.RotateTerm.Int64())
ls.DelayLimit = int(status.DelayLimit.Int64())
ls.MaxAggregation = int(status.MaxAggregation.Int64())
ls.CurrentHeight = status.CurrentHeight.Int64()
ls.RxHeight = status.RxHeight.Int64()
ls.RxHeightSrc = status.RxHeightSrc.Int64()
return ls, nil
}
func (s *sender) isOverLimit(size int) bool {
return txSizeLimit < float64(size)
}
func (s *sender) MonitorLoop(height int64, cb module.MonitorCallback, scb func()) error {
s.l.Debugf("MonitorLoop (sender) connected")
br := &BlockRequest{
Height: big.NewInt(height),
}
return s.c.MonitorBlock(br,
func(v *BlockNotification) error {
return cb(v.Height.Int64())
})
}
func (s *sender) StopMonitorLoop() {
s.c.CloseAllMonitor()
}
func (s *sender) FinalizeLatency() int {
//on-the-next
return 1
}
func NewSender(src, dst module.BtpAddress, w Wallet, endpoints []string, opt map[string]interface{}, l log.Logger) module.Sender {
s := &sender{
src: src,
dst: dst,
w: w,
l: l,
}
b, err := json.Marshal(opt)
if err != nil {
l.Panicf("fail to marshal opt:%#v err:%+v", opt, err)
}
if err = json.Unmarshal(b, &s.opt); err != nil {
l.Panicf("fail to unmarshal opt:%#v err:%+v", opt, err)
}
s.c = NewClient(endpoints, l)
s.bmc, _ = binding.NewBMC(HexToAddress(s.dst.ContractAddress()), s.c.ethcl)
return s
}
*/
Loading

0 comments on commit b3d5a90

Please sign in to comment.