Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate tx onchain #112

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assembler/bsc_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"encoding/hex"
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/cosmos/cosmos-sdk/x/oracle/types"
"time"

"github.com/bnb-chain/greenfield-relayer/common"
"github.com/bnb-chain/greenfield-relayer/config"
Expand Down
3 changes: 2 additions & 1 deletion assembler/greenfield_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"encoding/hex"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"sync"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield-relayer/common"
"github.com/bnb-chain/greenfield-relayer/config"
"github.com/bnb-chain/greenfield-relayer/db"
Expand Down
2 changes: 1 addition & 1 deletion db/dao/bsc_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dao

import (
"database/sql"
"github.com/cometbft/cometbft/votepool"
"time"

"github.com/cometbft/cometbft/votepool"
"gorm.io/gorm"

"github.com/bnb-chain/greenfield-relayer/db"
Expand Down
14 changes: 14 additions & 0 deletions executor/greenfield_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,17 @@ func (e *GreenfieldExecutor) getGasLimitAndFeeAmount(msg *oracletypes.MsgClaim)
}
return e.config.GreenfieldConfig.GasLimit, e.config.GreenfieldConfig.FeeAmount, nil
}

func (e *GreenfieldExecutor) GetCrossTxPack(destChainID sdk.ChainID, channelID types.ChannelId, sequence uint64) (pack []byte, err error) {
return pack, retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
defer cancel()
pack, err = e.GetGnfdClient().GetCrossChainPackage(ctx, destChainID, uint32(channelID), sequence)
return err
}, relayercommon.RtyAttem,
relayercommon.RtyDelay,
relayercommon.RtyErr,
retry.OnRetry(func(n uint, err error) {
logging.Logger.Errorf("failed to query crosschain tx for channel %d, seq %d, attempt: %d times, max_attempts: %d", channelID, n+1, relayercommon.RtyAttNum)
}))
}
3 changes: 1 addition & 2 deletions listener/bsc_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cometbft/cometbft/votepool"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down
2 changes: 1 addition & 1 deletion listener/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package listener
import (
"encoding/hex"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"

Expand Down
48 changes: 48 additions & 0 deletions listener/greenfield_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"sync"
"time"

"github.com/bnb-chain/greenfield-relayer/types"
abci "github.com/cometbft/cometbft/abci/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cometbft/cometbft/votepool"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield-relayer/common"
"github.com/bnb-chain/greenfield-relayer/config"
Expand Down Expand Up @@ -84,11 +86,23 @@ func (l *GreenfieldListener) poll() error {
case tx := <-relayTxCh:
txs = append(txs, tx)
case <-waitCh:
// validate tx against Chain
for _, tx := range txs {
onchainPack, err := l.greenfieldExecutor.GetCrossTxPack(sdk.ChainID(tx.DestChainId), types.ChannelId(tx.ChannelId), tx.Sequence)
if err != nil {
return fmt.Errorf("failed to get on chain tx, err=%s", err.Error())
}
if err := l.validateTx(onchainPack, tx); err != nil {
return fmt.Errorf("failed to validate tx, err=%s", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest panic if it does not match, because it is a very serious bug.

}
}

b := &model.GreenfieldBlock{
Chain: block.ChainID,
Height: uint64(block.Height),
BlockTime: block.Time.Unix(),
}

if err := l.DaoManager.GreenfieldDao.SaveBlockAndBatchTransactions(b, txs); err != nil {
return fmt.Errorf("failed to persist block and tx to DB, err=%s", err.Error())
}
Expand All @@ -98,6 +112,40 @@ func (l *GreenfieldListener) poll() error {
}
}

func (l *GreenfieldListener) validateTx(expectedPack []byte, tx *model.GreenfieldRelayTransaction) error {
relayerFee, err := util.StrToBigInt(tx.RelayerFee)
if err != nil {
return err
}
ackRelayerFee, err := util.StrToBigInt(tx.AckRelayerFee)
if err != nil {
return err
}
// package
packBz := make([]byte, 0)

// package header
packageHeader := sdk.EncodePackageHeader(sdk.PackageHeader{
PackageType: sdk.CrossChainPackageType(tx.PackageType),
Timestamp: uint64(tx.TxTime),
RelayerFee: relayerFee,
AckRelayerFee: ackRelayerFee,
})
packBz = append(packBz, packageHeader...)

// package payload
payloadBz, err := hex.DecodeString(tx.PayLoad)
if err != nil {
return err
}
packBz = append(packBz, payloadBz...)

if !bytes.Equal(expectedPack, packBz) {
return fmt.Errorf("package not match")
}
return nil
}

func (l *GreenfieldListener) getLatestPolledBlock() (*model.GreenfieldBlock, error) {
return l.DaoManager.GreenfieldDao.GetLatestBlock()
}
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/bnb-chain/greenfield-relayer/app"
"github.com/bnb-chain/greenfield-relayer/config"
"github.com/bnb-chain/greenfield-relayer/logging"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
)

func initFlags() {
Expand Down
10 changes: 10 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"encoding/binary"
"fmt"
"math/big"
"strconv"

Expand Down Expand Up @@ -41,3 +42,12 @@ func Uint64ToBytes(num uint64) []byte {
binary.BigEndian.PutUint64(bt, num)
return bt
}

func StrToBigInt(str string) (*big.Int, error) {
var r big.Int
num, ok := r.SetString(str, 10)
if !ok {
return &big.Int{}, fmt.Errorf("convetion failed, %s", str)
}
return num, nil
}
Loading