Skip to content

Commit a2ca89b

Browse files
committed
Add configurations
- max_gas_limit: manually tx gas limit - estimate_gas_factor: increase ratio of estimated gas limit - block_checkpoint_interval
1 parent b84cb9c commit a2ca89b

File tree

5 files changed

+98
-57
lines changed

5 files changed

+98
-57
lines changed

chain/bsc2/bsc2factory.go

+30-13
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ func NewReceiver(srcCfg link.ChainConfig, dstAddr types.BtpAddress, baseDir stri
4242
src := srcCfg.(chain.BaseConfig)
4343

4444
return newReceiver(RecvConfig{
45-
ChainID: ChainID(src.Endpoint),
46-
Epoch: uint64(Epoch),
47-
StartNumber: convToUint64(src.Options, "start_number", 0),
48-
SrcAddress: src.Address,
49-
DstAddress: dstAddr,
50-
Endpoint: src.Endpoint,
51-
DBType: fmt.Sprintf("%v", src.Options["db_type"]),
52-
DBPath: fmt.Sprintf("%v", src.Options["db_path"]),
45+
ChainID: ChainID(src.Endpoint),
46+
Epoch: uint64(Epoch),
47+
StartNumber: convToUint64(src.Options, "start_number", 0),
48+
SrcAddress: src.Address,
49+
DstAddress: dstAddr,
50+
Endpoint: src.Endpoint,
51+
DBType: fmt.Sprintf("%v", src.Options["db_type"]),
52+
DBPath: fmt.Sprintf("%v", src.Options["db_path"]),
53+
BlockCheckpointInterval: convToUint64(src.Options, "block_checkpoint_interval", 1024),
5354
}, l), nil
5455
}
5556

@@ -62,11 +63,15 @@ func NewSender(srcAddr types.BtpAddress, dstCfg link.ChainConfig, baseDir string
6263
}
6364

6465
return newSender(SenderConfig{
65-
SrcAddress: srcAddr,
66-
DstAddress: dst.Address,
67-
Endpoint: dst.Endpoint,
68-
ChainID: ChainID(dst.Endpoint),
69-
Epoch: uint64(Epoch)}, w, l), nil
66+
SrcAddress: srcAddr,
67+
DstAddress: dst.Address,
68+
Endpoint: dst.Endpoint,
69+
ChainID: ChainID(dst.Endpoint),
70+
Epoch: uint64(Epoch),
71+
MaxGasLimit: convToUint64(dst.Options, "max_gas_limit", 0),
72+
EstimateGasFactor: convToFloat64(dst.Options, "estimate_gas_factor", 1.5),
73+
BlockCheckpointInterval: convToUint64(dst.Options, "block_checkpoint_interval", 1024),
74+
}, w, l), nil
7075
}
7176

7277
func newWallet(passwd, secret string, keyStorePath string) (types.Wallet, error) {
@@ -103,3 +108,15 @@ func convToUint64(m map[string]interface{}, k string, def uint64) uint64 {
103108
}
104109
}
105110
}
111+
112+
func convToFloat64(m map[string]interface{}, k string, def float64) float64 {
113+
if val, ok := m[k]; !ok {
114+
return def
115+
} else {
116+
if val, err := strconv.ParseFloat(fmt.Sprintf("%v", val), 64); err != nil {
117+
panic(err)
118+
} else {
119+
return val
120+
}
121+
}
122+
}

chain/bsc2/receiver.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ const (
4040
var EmptyHash = common.Hash{}
4141

4242
type RecvConfig struct {
43-
ChainID uint64 `json:"chain_id"`
44-
Epoch uint64 `json:"epoch"`
45-
StartNumber uint64 `json:"start_height"`
46-
Endpoint string `json:"endpoint"`
47-
DBType string `json:"db_type"`
48-
DBPath string `json:"db_path"`
49-
SrcAddress btp.BtpAddress
50-
DstAddress btp.BtpAddress
43+
ChainID uint64 `json:"chain_id"`
44+
Epoch uint64 `json:"epoch"`
45+
StartNumber uint64 `json:"start_height"`
46+
Endpoint string `json:"endpoint"`
47+
DBType string `json:"db_type"`
48+
DBPath string `json:"db_path"`
49+
SrcAddress btp.BtpAddress
50+
DstAddress btp.BtpAddress
51+
BlockCheckpointInterval uint64
5152
}
5253

5354
type receiver struct {
55+
cfg RecvConfig
5456
chainId *big.Int
5557
epoch uint64
5658
startnumber uint64
@@ -70,6 +72,7 @@ type receiver struct {
7072

7173
func newReceiver(config RecvConfig, log log.Logger) *receiver {
7274
o := &receiver{
75+
cfg: config,
7376
chainId: new(big.Int).SetUint64(config.ChainID),
7477
epoch: config.Epoch,
7578
startnumber: config.StartNumber,
@@ -115,7 +118,7 @@ func newReceiver(config RecvConfig, log log.Logger) *receiver {
115118
o.accumulator.Height(), o.accumulator.Offset())
116119
}
117120
}
118-
o.snapshots = newSnapshots(o.chainId, o.client.Client, CacheSize, o.database, o.log)
121+
o.snapshots = newSnapshots(o.chainId, o.client.Client, o.cfg.BlockCheckpointInterval, CacheSize, o.database, o.log)
119122
return o
120123
}
121124

chain/bsc2/sender.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type relayResult struct {
3131
}
3232

3333
type sender struct {
34+
cfg SenderConfig
3435
src, dst btp.BtpAddress
3536
chainId *big.Int
3637
epoch uint64
@@ -43,15 +44,19 @@ type sender struct {
4344
}
4445

4546
type SenderConfig struct {
46-
SrcAddress btp.BtpAddress
47-
DstAddress btp.BtpAddress
48-
Endpoint string
49-
ChainID uint64
50-
Epoch uint64
47+
SrcAddress btp.BtpAddress
48+
DstAddress btp.BtpAddress
49+
Endpoint string
50+
ChainID uint64
51+
Epoch uint64
52+
MaxGasLimit uint64
53+
EstimateGasFactor float64
54+
BlockCheckpointInterval uint64
5155
}
5256

5357
func newSender(config SenderConfig, wallet btp.Wallet, log log.Logger) btp.Sender {
5458
o := &sender{
59+
cfg: config,
5560
src: config.SrcAddress,
5661
dst: config.DstAddress,
5762
chainId: new(big.Int).SetUint64(config.ChainID),
@@ -60,8 +65,11 @@ func newSender(config SenderConfig, wallet btp.Wallet, log log.Logger) btp.Sende
6065
log: log,
6166
client: NewClient(config.Endpoint, config.DstAddress, config.SrcAddress, log),
6267
}
63-
o.snapshots = newSnapshots(o.chainId, o.client.Client, CacheSize, nil, log)
64-
o.transactor = newMessageTransactor(o.snapshots, o.log)
68+
o.snapshots = newSnapshots(o.chainId, o.client.Client, o.cfg.BlockCheckpointInterval, CacheSize, nil, log)
69+
o.transactor = newMessageTransactor(MessageTransactorConfig{
70+
MaxGasLimit: o.cfg.MaxGasLimit,
71+
EstimateGasFactor: o.cfg.EstimateGasFactor,
72+
}, o.snapshots, o.log)
6573
return o
6674
}
6775

@@ -175,7 +183,7 @@ func (o *sender) GetStatus() (*btp.BMCLinkStatus, error) {
175183
BlockNumber: new(big.Int).SetUint64(o.finality.Number),
176184
Context: context.Background(),
177185
}, o.src.String()); err != nil {
178-
o.log.Errorf("fail to retrieve bmc status - err(%s)\n", err.Error())
186+
o.log.Errorf("fail to retrieve bmc status - %d %s err(%s)\n", o.finality.Number, o.src.String(), err.Error())
179187
return nil, err
180188
} else {
181189
return &btp.BMCLinkStatus{

chain/bsc2/snapshots.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@ import (
1212
"github.com/icon-project/btp2/common/log"
1313
)
1414

15-
const (
16-
SnapCheckPoint = 1024
17-
)
18-
1915
type Snapshots struct {
20-
chainId *big.Int
21-
database db.Database
22-
cache *lru.ARCCache
23-
client *ethclient.Client
24-
log log.Logger
16+
chainId *big.Int
17+
database db.Database
18+
cache *lru.ARCCache
19+
client *ethclient.Client
20+
checkpoint uint64
21+
log log.Logger
2522
}
2623

27-
func newSnapshots(chainId *big.Int, client *ethclient.Client, cacheSize int, database db.Database, log log.Logger) *Snapshots {
24+
func newSnapshots(chainId *big.Int, client *ethclient.Client, checkpoint uint64, cacheSize int, database db.Database, log log.Logger) *Snapshots {
2825
snaps := &Snapshots{
29-
chainId: chainId,
30-
client: client,
31-
database: database,
32-
log: log,
26+
chainId: chainId,
27+
client: client,
28+
database: database,
29+
checkpoint: checkpoint,
30+
log: log,
3331
}
3432

3533
if cache, err := lru.NewARC(cacheSize); err != nil {
@@ -73,7 +71,7 @@ func (o *Snapshots) get(id common.Hash) (*Snapshot, error) {
7371

7472
func (o *Snapshots) add(snap *Snapshot) error {
7573
o.cache.Add(snap.Hash, snap)
76-
if o.database != nil && snap.Number%SnapCheckPoint == 0 {
74+
if o.database != nil && snap.Number%o.checkpoint == 0 {
7775
if err := snap.store(o.database); err != nil {
7876
return err
7977
}

chain/bsc2/transactor.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ func (o MessageType) String() string {
5454
}
5555
}
5656

57+
type MessageTransactorConfig struct {
58+
MaxGasLimit uint64
59+
EstimateGasFactor float64
60+
}
61+
5762
type MessageTransactor struct {
63+
cfg MessageTransactorConfig
5864
snapshots *Snapshots
5965
replies chan<- *btp.RelayResult
6066
finalities chan common.Hash
@@ -64,8 +70,9 @@ type MessageTransactor struct {
6470
log log.Logger
6571
}
6672

67-
func newMessageTransactor(snapshots *Snapshots, log log.Logger) *MessageTransactor {
73+
func newMessageTransactor(cfg MessageTransactorConfig, snapshots *Snapshots, log log.Logger) *MessageTransactor {
6874
return &MessageTransactor{
75+
cfg: cfg,
6976
snapshots: snapshots,
7077
log: log,
7178
finalities: make(chan common.Hash),
@@ -96,7 +103,7 @@ func (o *MessageTransactor) Run(replies chan<- *btp.RelayResult) {
96103
select {
97104
case msg := <-o.transition:
98105
go func() {
99-
if msg = msg.Transit(); msg != nil {
106+
if msg = msg.Transit(o.cfg); msg != nil {
100107
o.Send(msg)
101108
}
102109
}()
@@ -167,12 +174,12 @@ func (o *MessageTransactor) tryFinalizeInLock(finality common.Hash, msg MessageT
167174
log: o.log,
168175
}
169176
}
170-
return exec.Transit()
177+
return exec.Transit(o.cfg)
171178
}
172179

173180
type MessageTx interface {
174181
Type() MessageType
175-
Transit() MessageTx
182+
Transit(MessageTransactorConfig) MessageTx
176183
Raw() *btp.RelayResult
177184
}
178185

@@ -201,8 +208,16 @@ func (o *CreatedMessage) Type() MessageType {
201208
return Created
202209
}
203210

204-
func (o *CreatedMessage) Transit() MessageTx {
211+
func (o *CreatedMessage) Transit(cfg MessageTransactorConfig) MessageTx {
212+
if cfg.MaxGasLimit > 0 {
213+
o.opts.GasLimit = cfg.MaxGasLimit
214+
}
215+
205216
if o.opts.GasLimit == uint64(0) {
217+
if cfg.MaxGasLimit > 0 {
218+
o.opts.GasLimit = cfg.MaxGasLimit
219+
}
220+
206221
o.opts.NoSend = true
207222
for ErrCounter := 0; ; ErrCounter++ {
208223
if tx, err := o.client.BTPMessageCenter.HandleRelayMessage(o.opts, o.from, o.blob); err != nil {
@@ -220,7 +235,7 @@ func (o *CreatedMessage) Transit() MessageTx {
220235
continue
221236
} else {
222237
o.opts.NoSend = false
223-
o.opts.GasLimit = uint64(float64(tx.Gas()) * 1.3)
238+
o.opts.GasLimit = uint64(float64(tx.Gas()) * cfg.EstimateGasFactor)
224239
o.log.Debugf("Original GasLimit(%d), Enough GasLimit(%d)", tx.Gas(), o.opts.GasLimit)
225240
break
226241
}
@@ -268,7 +283,7 @@ func (o *PendingMessage) Raw() *btp.RelayResult {
268283
return nil
269284
}
270285

271-
func (o *PendingMessage) Transit() MessageTx {
286+
func (o *PendingMessage) Transit(_ MessageTransactorConfig) MessageTx {
272287
var err error
273288
pending := true
274289
attempt := int64(0)
@@ -336,7 +351,7 @@ func (o *ExecutingMessage) Type() MessageType {
336351
return Executing
337352
}
338353

339-
func (o *ExecutingMessage) Transit() MessageTx {
354+
func (o *ExecutingMessage) Transit(_ MessageTransactorConfig) MessageTx {
340355
if o.receipt.Status == types.ReceiptStatusSuccessful {
341356
o.log.Infof("MessageTransition(E->EE) ID(%s) Tx(%s)", o.id, o.tx.Hash().Hex())
342357
return &ExecutedMessage{
@@ -410,7 +425,7 @@ func (o *ExecutedMessage) Type() MessageType {
410425
return Executed
411426
}
412427

413-
func (o *ExecutedMessage) Transit() MessageTx {
428+
func (o *ExecutedMessage) Transit(_ MessageTransactorConfig) MessageTx {
414429
o.log.Infof("MessageTransition(EE->FN) ID(%s) Tx(%s)", o.id, o.tx.Hex())
415430
return &FinalizedMessage{o}
416431
}
@@ -435,7 +450,7 @@ func (o *FinalizedMessage) Type() MessageType {
435450
return Finalized
436451
}
437452

438-
func (o *FinalizedMessage) Transit() MessageTx {
453+
func (o *FinalizedMessage) Transit(_ MessageTransactorConfig) MessageTx {
439454
o.log.Debugf("MessageTransition(FN->Nil)")
440455
return nil
441456
}
@@ -457,7 +472,7 @@ func (o *DroppedMessage) Type() MessageType {
457472
return Dropped
458473
}
459474

460-
func (o *DroppedMessage) Transit() MessageTx {
475+
func (o *DroppedMessage) Transit(_ MessageTransactorConfig) MessageTx {
461476
o.log.Debugf("MessageTransition(D->Nil)")
462477
return nil
463478
}
@@ -481,7 +496,7 @@ func (o *FaultedMessage) Type() MessageType {
481496
return Faulted
482497
}
483498

484-
func (o *FaultedMessage) Transit() MessageTx {
499+
func (o *FaultedMessage) Transit(_ MessageTransactorConfig) MessageTx {
485500
o.log.Debugf("MessageTransition(F->Nil)")
486501
return nil
487502
}

0 commit comments

Comments
 (0)