Skip to content

Commit

Permalink
Chaos testing (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Sep 11, 2023
1 parent 2348e59 commit c029478
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
12 changes: 12 additions & 0 deletions internal/controller/l1watcher/l1_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ func (l1 *l1Contracts) ParseL1Events(ctx context.Context, db *gorm.DB, start, en
return 0, err
}

// Store l1 confirm.
var l1Confirms = make([]orm.L1ChainConfirm, 0, end-start+1)
for number := start; number <= end; number++ {
l1Confirms = append(l1Confirms, orm.L1ChainConfirm{
Number: number,
})
}
if err = l1.tx.Save(l1Confirms).Error; err != nil {
l1.tx.Rollback()
return 0, err
}

// store the latest l1 block numbers
err = l1.tx.Save(&orm.L1Block{
Number: end,
Expand Down
12 changes: 2 additions & 10 deletions internal/controller/l1watcher/l1_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (l1 *l1Contracts) storeGatewayEvents(ignore uint64) error {
event := l1.ethEvents[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
event.Amount = big.NewInt(0).SetUint64(ignore)
}
if msgHash, exist := l1.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
Expand All @@ -94,7 +94,7 @@ func (l1 *l1Contracts) storeGatewayEvents(ignore uint64) error {
event := l1.erc20Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
event.Amount = big.NewInt(0).SetUint64(ignore)
}
if msgHash, exist := l1.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
Expand All @@ -109,10 +109,6 @@ func (l1 *l1Contracts) storeGatewayEvents(ignore uint64) error {
// store l1 err721 events.
for i := 0; i < len(l1.erc721Events); i++ {
event := l1.erc721Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
}
if msgHash, exist := l1.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
}
Expand All @@ -126,10 +122,6 @@ func (l1 *l1Contracts) storeGatewayEvents(ignore uint64) error {
// store l1 erc1155 events.
for i := 0; i < len(l1.erc1155Events); i++ {
event := l1.erc1155Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
}
if msgHash, exist := l1.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/l2watcher/l2_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (l2 *l2Contracts) ParseL2Events(ctx context.Context, db *gorm.DB, start, en
ignore, _ := rand.Int(rand.Reader, big.NewInt(0).SetUint64((end-start+1)*2))

// store l2Messenger sentMessenger events.
if err = l2.storeMessengerEvents(ctx, start, end, ignore.Uint64()+start); err != nil {
if err = l2.storeMessengerEvents(ctx, start, end); err != nil {
l2.tx.Rollback()
return 0, err
}
Expand Down
12 changes: 2 additions & 10 deletions internal/controller/l2watcher/l2_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (l2 *l2Contracts) storeGatewayEvents(ignore uint64) error {
event := l2.ethEvents[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
event.Amount = big.NewInt(0).SetUint64(ignore)
}
if msgHash, exist := l2.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
Expand All @@ -95,7 +95,7 @@ func (l2 *l2Contracts) storeGatewayEvents(ignore uint64) error {
event := l2.erc20Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
event.Amount = big.NewInt(0).SetUint64(ignore)
}
if msgHash, exist := l2.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
Expand All @@ -110,10 +110,6 @@ func (l2 *l2Contracts) storeGatewayEvents(ignore uint64) error {
// store l2 err721 events.
for i := 0; i < len(l2.erc721Events); i++ {
event := l2.erc721Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
}
if msgHash, exist := l2.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
}
Expand All @@ -127,10 +123,6 @@ func (l2 *l2Contracts) storeGatewayEvents(ignore uint64) error {
// store l2 erc1155 events.
for i := 0; i < len(l2.erc1155Events); i++ {
event := l2.erc1155Events[i]
// The event should be ignored for test if event number equal to ignore.
if event.Number == ignore {
continue
}
if msgHash, exist := l2.txHashMsgHash[event.TxHash]; exist {
event.MsgHash = msgHash.String()
}
Expand Down
19 changes: 8 additions & 11 deletions internal/controller/l2watcher/l2_messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (l2 *l2Contracts) registerMessengerHandlers() {
})
}

func (l2 *l2Contracts) storeMessengerEvents(ctx context.Context, start, end, ignore uint64) error {
func (l2 *l2Contracts) storeMessengerEvents(ctx context.Context, start, end uint64) error {
if len(l2.msgSentEvents) == 0 {
return nil
}
Expand All @@ -54,17 +54,14 @@ func (l2 *l2Contracts) storeMessengerEvents(ctx context.Context, start, end, ign
})
continue
}
// If the number equal to ignore number, don't update withdraw root.
if number != ignore {
msgs := l2.msgSentEvents[number]
for i, msg := range msgs {
proofs := l2.withdraw.AppendMessages([]common.Hash{common.HexToHash(msg.MsgHash)})
// Store the latest one for every block.
if i == len(msgs)-1 {
msg.MsgProof = common.Bytes2Hex(proofs[0])
}
msgSentEvents = append(msgSentEvents, msgs[i])
msgs := l2.msgSentEvents[number]
for i, msg := range msgs {
proofs := l2.withdraw.AppendMessages([]common.Hash{common.HexToHash(msg.MsgHash)})
// Store the latest one for every block.
if i == len(msgs)-1 {
msg.MsgProof = common.Bytes2Hex(proofs[0])
}
msgSentEvents = append(msgSentEvents, msgs[i])
}
chainMonitors = append(chainMonitors, &orm.L2ChainConfirm{
Number: number,
Expand Down
13 changes: 9 additions & 4 deletions internal/controller/monitor/deposit_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -110,7 +111,8 @@ func (ch *ChainMonitor) confirmDepositEvents(ctx context.Context, start, end uin
failedNumbers = append(failedNumbers, msg.L2Number)
}
// If eth msg don't match, alert it.
go ch.SlackNotify(fmt.Sprintf("deposit eth don't match, message: %v", msg))
data, _ := json.Marshal(msg)
go ch.SlackNotify(fmt.Sprintf("deposit eth don't match, message: %s", string(data)))
log.Error("the eth deposit count or amount don't match", "start", start, "end", end, "event_type", orm.L2FinalizeDepositETH, "l1_tx_hash", msg.L1TxHash, "l2_tx_hash", msg.L2TxHash)
}
}
Expand All @@ -134,7 +136,8 @@ func (ch *ChainMonitor) confirmDepositEvents(ctx context.Context, start, end uin
failedNumbers = append(failedNumbers, msg.L2Number)
}
// If erc20 msg don't match, alert it.
go ch.SlackNotify(fmt.Sprintf("erc20 deposit don't match, message: %v", msg))
data, _ := json.Marshal(msg)
go ch.SlackNotify(fmt.Sprintf("erc20 deposit don't match, message: %s", string(data)))
log.Error(
"the erc20 deposit count or amount doesn't match",
"start", start,
Expand All @@ -160,7 +163,8 @@ func (ch *ChainMonitor) confirmDepositEvents(ctx context.Context, start, end uin
failedNumbers = append(failedNumbers, msg.L2Number)
}
// If erc721 event don't match, alert it.
go ch.SlackNotify(fmt.Sprintf("erc721 event don't match, message: %v", msg))
data, _ := json.Marshal(msg)
go ch.SlackNotify(fmt.Sprintf("erc721 event don't match, message: %s", string(data)))
log.Error("the erc721 deposit count or amount doesn't match", "start", start, "end", end, "event_type", orm.L2FinalizeDepositERC721, "l1_tx_hash", msg.L1TxHash, "l2_tx_hash", msg.L2TxHash)
}
}
Expand All @@ -179,7 +183,8 @@ func (ch *ChainMonitor) confirmDepositEvents(ctx context.Context, start, end uin
failedNumbers = append(failedNumbers, msg.L2Number)
}
// If erc1155 event don't match, alert it.
go ch.SlackNotify(fmt.Sprintf("erc1155 event don't match, message: %v", msg))
data, _ := json.Marshal(msg)
go ch.SlackNotify(fmt.Sprintf("erc1155 event don't match, message: %s", string(data)))
log.Error("the erc1155 deposit count or amount doesn't match", "start", start, "end", end, "event_type", orm.L2FinalizeDepositERC1155, "l1_tx_hash", msg.L1TxHash, "l2_tx_hash", msg.L2TxHash)
}
}
Expand Down

0 comments on commit c029478

Please sign in to comment.