Skip to content

Commit

Permalink
validate tx onchain (bnb-chain#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Jul 15, 2024
1 parent 13b8990 commit 63f039d
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 9 deletions.
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 {
panic(fmt.Sprintf("failed to validate tx, err=%s", err.Error()))
}
}

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
}

0 comments on commit 63f039d

Please sign in to comment.