Skip to content

Commit

Permalink
Merge pull request #15 from bnb-chain/develop
Browse files Browse the repository at this point in the history
Prepare for release v0.0.3
  • Loading branch information
unclezoro authored Mar 14, 2023
2 parents f4e9b90 + a0f22d4 commit be752dc
Show file tree
Hide file tree
Showing 38 changed files with 793 additions and 715 deletions.
17 changes: 17 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Description

add a description of your changes here...

### Rationale

tell us why we need these changes...

### Example

add an example CLI or API response...

### Changes

Notable changes:
* add each change in a bullet point here
* ...
30 changes: 30 additions & 0 deletions .github/workflows/lint-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Lint PR"

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: amannn/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
with:
types: |
feat
fix
docs
style
refactor
perf
test
build
ci
chore
revert
release
52 changes: 52 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Lint
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
- uses: actions/checkout@v3

- run: go env -w GOPRIVATE="github.com/bnb-chain/*"
- run: git config --global url."https://${{ secrets.GH_TOKEN }}@github.com".insteadOf "https://github.com"

- uses: actions/cache@v3
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: |
go mod tidy
go mod download
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.0
skip-pkg-cache: true
args: --timeout=99m
16 changes: 7 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,24 @@ func NewApp(cfg *config.Config) *App {
greenfieldExecutor.SetBSCExecutor(bscExecutor)
bscExecutor.SetGreenfieldExecutor(greenfieldExecutor)

votePoolExecutor := vote.NewVotePoolExecutor(cfg)

// listeners
greenfieldListener := listener.NewGreenfieldListener(cfg, greenfieldExecutor, bscExecutor, daoManager)
bscListener := listener.NewBSCListener(cfg, bscExecutor, greenfieldExecutor, daoManager)

// vote signer
signer := vote.NewVoteSigner(ethcommon.Hex2Bytes(cfg.VotePoolConfig.BlsPrivateKey))
signer := vote.NewVoteSigner(ethcommon.Hex2Bytes(cfg.GreenfieldConfig.BlsPrivateKey))

// voteProcessors
greenfieldVoteProcessor := vote.NewGreenfieldVoteProcessor(cfg, daoManager, signer, greenfieldExecutor, votePoolExecutor)
bscVoteProcessor := vote.NewBSCVoteProcessor(cfg, daoManager, signer, bscExecutor, votePoolExecutor)
greenfieldVoteProcessor := vote.NewGreenfieldVoteProcessor(cfg, daoManager, signer, greenfieldExecutor)
bscVoteProcessor := vote.NewBSCVoteProcessor(cfg, daoManager, signer, bscExecutor)

// assemblers
greenfieldAssembler := assembler.NewGreenfieldAssembler(cfg, greenfieldExecutor, daoManager, bscExecutor, votePoolExecutor)
bscAssembler := assembler.NewBSCAssembler(cfg, bscExecutor, daoManager, votePoolExecutor, greenfieldExecutor)
greenfieldAssembler := assembler.NewGreenfieldAssembler(cfg, greenfieldExecutor, daoManager, bscExecutor)
bscAssembler := assembler.NewBSCAssembler(cfg, bscExecutor, daoManager, greenfieldExecutor)

// relayers
gnfdRelayer := relayer.NewGreenfieldRelayer(greenfieldListener, greenfieldExecutor, bscExecutor, votePoolExecutor, greenfieldVoteProcessor, greenfieldAssembler)
bscRelayer := relayer.NewBSCRelayer(bscListener, greenfieldExecutor, bscExecutor, votePoolExecutor, bscVoteProcessor, bscAssembler)
gnfdRelayer := relayer.NewGreenfieldRelayer(greenfieldListener, greenfieldExecutor, bscExecutor, greenfieldVoteProcessor, greenfieldAssembler)
bscRelayer := relayer.NewBSCRelayer(bscListener, greenfieldExecutor, bscExecutor, bscVoteProcessor, bscAssembler)

return &App{
BSCRelayer: bscRelayer,
Expand Down
34 changes: 17 additions & 17 deletions assembler/bsc_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assembler

import (
"encoding/hex"
"errors"
"time"

"github.com/bnb-chain/greenfield-relayer/common"
Expand All @@ -20,16 +21,16 @@ type BSCAssembler struct {
greenfieldExecutor *executor.GreenfieldExecutor
bscExecutor *executor.BSCExecutor
daoManager *dao.DaoManager
votePoolExecutor *vote.VotePoolExecutor
blsPubKey string
}

func NewBSCAssembler(cfg *config.Config, executor *executor.BSCExecutor, dao *dao.DaoManager, votePoolExecutor *vote.VotePoolExecutor, greenfieldExecutor *executor.GreenfieldExecutor) *BSCAssembler {
func NewBSCAssembler(cfg *config.Config, executor *executor.BSCExecutor, dao *dao.DaoManager, greenfieldExecutor *executor.GreenfieldExecutor) *BSCAssembler {
return &BSCAssembler{
config: cfg,
bscExecutor: executor,
daoManager: dao,
votePoolExecutor: votePoolExecutor,
greenfieldExecutor: greenfieldExecutor,
blsPubKey: hex.EncodeToString(util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey)),
}
}

Expand All @@ -40,8 +41,7 @@ func (a *BSCAssembler) AssemblePackagesAndClaimLoop() {

func (a *BSCAssembler) assemblePackagesAndClaimForOracleChannel(channelId types.ChannelId) {
for {
err := a.process(channelId)
if err != nil {
if err := a.process(channelId); err != nil {
logging.Logger.Errorf("encounter error when relaying packages, err=%s ", err.Error())
time.Sleep(common.RetryInterval)
}
Expand All @@ -53,15 +53,17 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error {
if err != nil {
return err
}
var pkgIds []int64
pkgs, err := a.daoManager.BSCDao.GetAllVotedPackages(nextSequence)

pkgs, err := a.daoManager.BSCDao.GetPackagesByOracleSequenceAndStatus(nextSequence, db.AllVoted)
if err != nil {
logging.Logger.Errorf("failed to get all validator voted tx with channel id %d and sequence : %d", channelId, nextSequence)
return err
}
if len(pkgs) == 0 {
return nil
}

var pkgIds []int64
for _, p := range pkgs {
pkgIds = append(pkgIds, p.Id)
}
Expand Down Expand Up @@ -89,8 +91,11 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error {
// packages for same oracle sequence share one timestamp
pkgTs := pkgs[0].TxTime

relayerPubKey := util.BlsPubKeyFromPrivKeyStr(a.getBlsPrivateKey())
relayerIdx := util.IndexOf(hex.EncodeToString(relayerPubKey), relayerPubKeys)
relayerIdx := util.IndexOf(a.blsPubKey, relayerPubKeys)
if relayerIdx == -1 {
return errors.New(" relayer's bls pub key not found. ")
}

firstInturnRelayerIdx := int(pkgTs) % len(relayerPubKeys)
packagesRelayStartTime := pkgTs + a.config.RelayConfig.BSCToGreenfieldRelayingDelayTime
logging.Logger.Infof("packages will be relayed starting at %d", packagesRelayStartTime)
Expand Down Expand Up @@ -133,19 +138,18 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error {
return err
}
logging.Logger.Infof("claimed transaction with txHash %s", txHash)
err = a.daoManager.BSCDao.UpdateBatchPackagesStatusAndClaimedTxHash(pkgIds, db.Delivered, txHash)
if err != nil {
logging.Logger.Errorf("failed to update packages to 'Delivered', error=%s", err.Error())
if err = a.daoManager.BSCDao.UpdateBatchPackagesClaimedTxHash(pkgIds, txHash); err != nil {
return err
}
time.Sleep(executor.GnfdSequenceUpdateLatency)
return nil
}
}
}
}

func (a *BSCAssembler) validateSequenceFilled(filled chan struct{}, errC chan error, sequence uint64) {
ticker := time.NewTicker(common.RetryInterval)
ticker := time.NewTicker(common.RetrieveSequenceInterval)
defer ticker.Stop()
for range ticker.C {
nextDeliverySequence, err := a.bscExecutor.GetNextDeliveryOracleSequence()
Expand All @@ -158,7 +162,3 @@ func (a *BSCAssembler) validateSequenceFilled(filled chan struct{}, errC chan er
}
}
}

func (a *BSCAssembler) getBlsPrivateKey() string {
return a.config.VotePoolConfig.BlsPrivateKey
}
23 changes: 12 additions & 11 deletions assembler/greenfield_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assembler

import (
"encoding/hex"
"errors"
"time"

"github.com/bnb-chain/greenfield-relayer/common"
Expand All @@ -21,16 +22,16 @@ type GreenfieldAssembler struct {
bscExecutor *executor.BSCExecutor
greenfieldExecutor *executor.GreenfieldExecutor
daoManager *dao.DaoManager
votePoolExecutor *vote.VotePoolExecutor
blsPubKey string
}

func NewGreenfieldAssembler(cfg *config.Config, executor *executor.GreenfieldExecutor, dao *dao.DaoManager, bscExecutor *executor.BSCExecutor, votePoolExecutor *vote.VotePoolExecutor) *GreenfieldAssembler {
func NewGreenfieldAssembler(cfg *config.Config, executor *executor.GreenfieldExecutor, dao *dao.DaoManager, bscExecutor *executor.BSCExecutor) *GreenfieldAssembler {
return &GreenfieldAssembler{
config: cfg,
greenfieldExecutor: executor,
daoManager: dao,
bscExecutor: bscExecutor,
votePoolExecutor: votePoolExecutor,
blsPubKey: hex.EncodeToString(util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey)),
}
}

Expand All @@ -43,8 +44,7 @@ func (a *GreenfieldAssembler) AssembleTransactionsLoop() {

func (a *GreenfieldAssembler) assembleTransactionAndSendForChannel(channelId types.ChannelId) {
for {
err := a.process(channelId)
if err != nil {
if err := a.process(channelId); err != nil {
logging.Logger.Errorf("encounter error when relaying tx, err=%s ", err.Error())
time.Sleep(common.RetryInterval)
}
Expand Down Expand Up @@ -83,9 +83,11 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error {
if err != nil {
return err
}
relayerIdx := util.IndexOf(a.blsPubKey, relayerBlsPubKeys)
if relayerIdx == -1 {
return errors.New(" not a relayer. ")
}

relayerPubKey := util.BlsPubKeyFromPrivKeyStr(a.votePoolExecutor.GetBlsPrivateKey())
relayerIdx := util.IndexOf(hex.EncodeToString(relayerPubKey), relayerBlsPubKeys)
firstInturnRelayerIdx := int(tx.TxTime) % len(relayerBlsPubKeys)
txRelayStartTime := tx.TxTime + a.config.RelayConfig.GreenfieldToBSCRelayingDelayTime
logging.Logger.Infof("tx will be relayed starting at %d", txRelayStartTime)
Expand Down Expand Up @@ -128,19 +130,18 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error {
return err
}
logging.Logger.Infof("delivered transaction to BSC with txHash %s", txHash.String())
err = a.daoManager.GreenfieldDao.UpdateTransactionStatusAndClaimedTxHash(tx.Id, db.Delivered, txHash.String())
if err != nil {
logging.Logger.Errorf("failed to update Tx with channel id %d and sequence %d to status 'Delivered', error=%s", tx.ChannelId, tx.Sequence, err.Error())
if err = a.daoManager.GreenfieldDao.UpdateTransactionClaimedTxHash(tx.Id, txHash.String()); err != nil {
return err
}
time.Sleep(executor.BSCSequenceUpdateLatency)
return nil
}
}
}
}

func (a *GreenfieldAssembler) validateSequenceFilled(filled chan struct{}, errC chan error, sequence uint64, channelID types.ChannelId) {
ticker := time.NewTicker(common.RetryInterval)
ticker := time.NewTicker(common.RetrieveSequenceInterval)
defer ticker.Stop()
for range ticker.C {
nextDeliverySequence, err := a.greenfieldExecutor.GetNextDeliverySequenceForChannel(channelID)
Expand Down
11 changes: 6 additions & 5 deletions common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
)

var (
RtyAttNum = uint(5)
RtyAttem = retry.Attempts(RtyAttNum)
RtyDelay = retry.Delay(time.Millisecond * 500)
RtyErr = retry.LastErrorOnly(true)
RetryInterval = 1 * time.Second
RtyAttNum = uint(5)
RtyAttem = retry.Attempts(RtyAttNum)
RtyDelay = retry.Delay(time.Millisecond * 500)
RtyErr = retry.LastErrorOnly(true)
RetryInterval = 2 * time.Second
RetrieveSequenceInterval = 1 * time.Second
)

const (
Expand Down
16 changes: 5 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
type Config struct {
GreenfieldConfig GreenfieldConfig `json:"greenfield_config"`
BSCConfig BSCConfig `json:"bsc_config"`
VotePoolConfig VotePoolConfig `json:"vote_pool_config"`
RelayConfig RelayConfig `json:"relay_config"`
LogConfig LogConfig `json:"log_config"`
AdminConfig AdminConfig `json:"admin_config"`
Expand All @@ -28,18 +27,14 @@ func (cfg *AdminConfig) Validate() {
}
}

type VotePoolConfig struct {
RPCAddr string `json:"rpc_addr"`
BlsPrivateKey string `json:"bls_private_key"`
}

type GreenfieldConfig struct {
KeyType string `json:"key_type"`
AWSRegion string `json:"aws_region"`
AWSSecretName string `json:"aws_secret_name"`
RPCAddrs []string `json:"rpc_addrs"`
GRPCAddrs []string `json:"grpc_addrs"`
PrivateKey string `json:"private_key"`
BlsPrivateKey string `json:"bls_private_key"`
NumberOfBlocksForFinality uint64 `json:"number_of_blocks_for_finality"`
ChainId uint16 `json:"chain_id"`
StartHeight uint64 `json:"start_height"`
Expand All @@ -62,11 +57,10 @@ type BSCConfig struct {
}

type RelayConfig struct {
BSCToGreenfieldRelayingDelayTime int64 `json:"bsc_to_greenfield_relaying_delay_time"` // in second
GreenfieldToBSCRelayingDelayTime int64 `json:"greenfield_to_bsc_relaying_delay_time"` // in second
FirstInTurnRelayerRelayingWindow int64 `json:"first_in_turn_relayer_relaying_window"` // in second
InTurnRelayerRelayingWindow int64 `json:"in_turn_relayer_relaying_window"` // in second

BSCToGreenfieldRelayingDelayTime int64 `json:"bsc_to_greenfield_relaying_delay_time"` // in second
GreenfieldToBSCRelayingDelayTime int64 `json:"greenfield_to_bsc_relaying_delay_time"` // in second
FirstInTurnRelayerRelayingWindow int64 `json:"first_in_turn_relayer_relaying_window"` // in second
InTurnRelayerRelayingWindow int64 `json:"in_turn_relayer_relaying_window"` // in second
GreenfieldEventTypeCrossChain string `json:"greenfield_event_type_cross_chain"`
BSCCrossChainPackageEventName string `json:"bsc_cross_chain_package_event_name"`
CrossChainPackageEventHex string `json:"cross_chain_package_event_hex"`
Expand Down
Loading

0 comments on commit be752dc

Please sign in to comment.