Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
irrun committed Mar 27, 2024
1 parent e5b2373 commit b3bb32d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
BSC-MEV-Sentry serves as the proxy service for BSC MEV architecture, It has the following features:

1. Forward RPC requests under mev namespace to validators/builders.
2. Pay builders for their bid.
2. Pay builders on behalf of validators for their bids.
3. Monitor validators' status and health.

See also: https://github.com/bnb-chain/BEPs/pull/322
Expand Down
22 changes: 1 addition & 21 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ import (
type Mode string

const (
privateKeyMode Mode = "privateKey"
privateKeyMode Mode = "privatekey"
keystoreMode Mode = "keystore"
)

type Account interface {
Address() common.Address
SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
SetBalance(balance *big.Int)
GetBalance() *big.Int
SetNonce(nonce uint64)
GetNonce() uint64
}

func New(config *Config) (Account, error) {
Expand Down Expand Up @@ -64,22 +60,6 @@ func (a *baseAccount) Address() common.Address {
return a.address
}

func (a *baseAccount) SetBalance(balance *big.Int) {
a.balance = balance
}

func (a *baseAccount) GetBalance() *big.Int {
return a.balance
}

func (a *baseAccount) SetNonce(nonce uint64) {
a.nonce = nonce
}

func (a *baseAccount) GetNonce() uint64 {
return a.nonce
}

type keystoreAccount struct {
keystore *keystore.KeyStore
account accounts.Account
Expand Down
8 changes: 1 addition & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ func main() {

log.Infow("bsc mev-sentry start", "configPath", *configPath, "config", cfg)

validators := make(map[string]node.Validator)
for _, v := range cfg.Validators {
validator := node.NewValidator(&v)
if validator != nil {
validators[v.PublicHostName] = validator
}
}
validators := node.NewValidators(cfg.Validators)

builders := make(map[common.Address]node.Builder)
for _, b := range cfg.Builders {
Expand Down
19 changes: 13 additions & 6 deletions configs/config-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ HTTPListenAddr = "localhost:8555" # The address to listen on for HTTP requests.
RPCConcurrency = 100 # The maximum number of concurrent requests.
RPCTimeout = "10s" # The timeout for RPC requests.

[[Validators]] # A list of validators to forward requests to.
PrivateURL = "https://127.0.0.1:8545" # The private rpc url of the validator, it can only been accessed in the local network.
PublicHostName = "127.0.0.1" # The domain name of the validator, if a request's HOST info is same with this, it will be forwarded to the validator.
BidFeeCeil = 100000
AccountMode = "privateKey" # The unlock mode of the account.
PrivateKey = "59ba8068eb256d520179e903f43dacf6d8d57d72bd306e1bd603fdb8c8da10e8"
[[Validators]]
PrivateURL = "http://10.200.31.36:8545"
PublicHostName = "bsc-testnet-elbrus.bnbchain.org"
BidFeeCeil = 0
PayAccountMode = "privateKey"
PrivateKey = "b1fed931ad503ba9d029e86c2bd957cccbeb54f5995441f34796ddbee68a53cf"

[[Validators]]
PrivateURL = "http://10.200.33.92:8545"
PublicHostName = "bsc-testnet-ararat.bnbchain.org"
BidFeeCeil = 1000000000000000 # 0.0001BNB
PayAccountMode = "privateKey"
PrivateKey = "ce3f1b7573842f80764ec12573a049612bc49c8f940c00fb71d755f66f647503"

[[Builders]]
Address = "0x980A75eCd1309eA12fa2ED87A8744fBfc9b863D5" # The address of the builder.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ require (
replace (
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.23.0
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-tendermint v0.0.0-20230417032003-4cda1f296fb2
github.com/ethereum/go-ethereum => github.com/bnb-chain/bsc-builder v0.0.0-20240320091307-8943e3fd7c83
github.com/ethereum/go-ethereum => github.com/bnb-chain/bsc v1.4.3-alpha
github.com/grpc-ecosystem/grpc-gateway/v2 => github.com/prysmaticlabs/grpc-gateway/v2 v2.3.1-0.20210702154020-550e1cd83ec1
github.com/syndtr/goleveldb v1.0.1 => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint => github.com/bnb-chain/tendermint v0.31.16
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,8 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/blizzy78/varnamelen v0.8.0/go.mod h1:V9TzQZ4fLJ1DSrjVDfl89H7aMnTvKkApdHeyESmyR7k=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bnb-chain/bsc-builder v0.0.0-20240320091307-8943e3fd7c83 h1:w1Ex51i0Q1DSWvTPmP7JdVSp4MFEvEGGv82W/dh6VgE=
github.com/bnb-chain/bsc-builder v0.0.0-20240320091307-8943e3fd7c83/go.mod h1:r9/LuIlntgKKPG0ztTHdX2Env53Vci323MQivBOSR3Q=
github.com/bnb-chain/bsc v1.4.3-alpha h1:tY5oYYzF9o1FHkCPKU4ZjftxN3kbUuM2u6akB2HtZhk=
github.com/bnb-chain/bsc v1.4.3-alpha/go.mod h1:r9/LuIlntgKKPG0ztTHdX2Env53Vci323MQivBOSR3Q=
github.com/bnb-chain/fastssz v0.1.2 h1:vTcXw5SwCtRYnl/BEclujiml7GXiVOZ74tub4GHpvlM=
github.com/bnb-chain/fastssz v0.1.2/go.mod h1:KcabV+OEw2QwgyY8Fc88ZG79CKYkFdu0kKWyfA3dI6o=
github.com/bnb-chain/greenfield-tendermint v0.0.0-20230417032003-4cda1f296fb2 h1:jubavYCs/mCFj/g6Utl+l4SfpykdBdWJFPsvb9FcEXU=
Expand Down
87 changes: 57 additions & 30 deletions node/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/big"
"net"
"net/http"
"os"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -41,6 +43,26 @@ var (
}
)

func NewValidators(cfg []ValidatorConfig) map[string]Validator {
validators := make(map[string]Validator)
for _, v := range cfg {
val := NewValidator(&v)
if val != nil {
validators[v.PublicHostName] = val
}
}

// remove password file
for _, v := range cfg {
_, err := os.Lstat(v.PasswordFilePath)
if !os.IsNotExist(err) {
os.Remove(v.PasswordFilePath)
}
}

return validators
}

type Validator interface {
SendBid(context.Context, types.BidArgs) (common.Hash, error)
MevRunning() bool
Expand Down Expand Up @@ -80,17 +102,17 @@ func NewValidator(config *ValidatorConfig) Validator {
PasswordFilePath: config.PasswordFilePath,
Address: config.PayAccountAddress})
if err != nil {
log.Panicw("failed to create account", "err", err)
log.Panicw("failed to create payAccount", "err", err)
}

v := &validator{
cfg: config,
client: cli,
scheduler: gocron.NewScheduler(time.UTC),
account: acc,
cfg: config,
client: cli,
scheduler: gocron.NewScheduler(time.UTC),
payAccount: acc,
}

if _, err := v.scheduler.Every(30).Second().Do(func() {
if _, err := v.scheduler.Every(10).Second().Do(func() {
v.refresh()
}); err != nil {
log.Debugw("error while setting up scheduler", "err", err)
Expand All @@ -102,20 +124,22 @@ func NewValidator(config *ValidatorConfig) Validator {
}

type validator struct {
cfg *ValidatorConfig
client *ethclient.Client
account account.Account

scheduler *gocron.Scheduler
mevRunning bool
cfg *ValidatorConfig
client *ethclient.Client
payAccount account.Account

scheduler *gocron.Scheduler
mevRunning uint32
payAccountBalance uint64
payAccountNonce uint64
}

func (n *validator) SendBid(ctx context.Context, args types.BidArgs) (common.Hash, error) {
return n.client.SendBid(ctx, args)
}

func (n *validator) MevRunning() bool {
return n.mevRunning
return atomic.LoadUint32(&n.mevRunning) == 1
}

func (n *validator) refresh() {
Expand All @@ -125,23 +149,29 @@ func (n *validator) refresh() {
log.Errorw("failed to fetch mev running status", "url", n.cfg.PrivateURL, "err", err)
}

n.mevRunning = mevRunning
if mevRunning {
atomic.StoreUint32(&n.mevRunning, 1)
} else {
atomic.StoreUint32(&n.mevRunning, 0)
}

balance, err := n.client.BalanceAt(context.Background(), n.account.Address(), nil)
balance, err := n.client.BalanceAt(context.Background(), n.payAccount.Address(), nil)
if err != nil {
metrics.ChainError.Inc()
log.Errorw("failed to fetch validator account balance", "err", err)
log.Errorw("failed to fetch validator payAccount balance", "err", err)
}

n.account.SetBalance(balance)
if balance != nil {
atomic.StoreUint64(&n.payAccountBalance, balance.Uint64())
}

nonce, err := n.client.PendingNonceAt(context.Background(), n.account.Address())
nonce, err := n.client.PendingNonceAt(context.Background(), n.payAccount.Address())
if err != nil {
metrics.ChainError.Inc()
log.Errorw("failed to fetch validator account nonce", "err", err)
log.Errorw("failed to fetch validator payAccount nonce", "err", err)
}

n.account.SetNonce(nonce)
atomic.StoreUint64(&n.payAccountNonce, nonce)
}

func (n *validator) BestBidGasFee(ctx context.Context, parentHash common.Hash) (*big.Int, error) {
Expand All @@ -158,31 +188,28 @@ func (n *validator) BidFeeCeil() uint64 {

func (n *validator) GeneratePayBidTx(_ context.Context, builder common.Address, builderFee *big.Int) (hexutil.Bytes, error) {
// take pay bid tx as block tag
var (
amount = big.NewInt(0)
balance = n.account.GetBalance()
nonce = n.account.GetNonce()
)
var amount = big.NewInt(0)

if builderFee != nil {
amount = builderFee
}

if balance.Cmp(amount) < 0 {
metrics.AccountError.WithLabelValues(n.account.Address().String(), "insufficient_balance").Inc()
log.Errorw("insufficient balance", "balance", balance.Uint64(), "builderFee", builderFee.Uint64())
if atomic.LoadUint64(&n.payAccountBalance) < amount.Uint64() {
metrics.AccountError.WithLabelValues(n.payAccount.Address().String(), "insufficient_balance").Inc()
log.Errorw("insufficient balance", "balance", atomic.LoadUint64(&n.payAccountBalance),
"builderFee", builderFee.Uint64())
return nil, errors.New("insufficient balance")
}

tx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
Nonce: atomic.LoadUint64(&n.payAccountNonce),
GasPrice: big.NewInt(0),
Gas: 25000,
To: &builder,
Value: amount,
})

signedTx, err := n.account.SignTx(tx, amount)
signedTx, err := n.payAccount.SignTx(tx, amount)
if err != nil {
log.Errorw("failed to sign pay bid tx", "err", err)
return nil, err
Expand Down

0 comments on commit b3bb32d

Please sign in to comment.