Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/b2network/b2-node
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcc committed Dec 22, 2023
2 parents 2e46832 + 38335f5 commit 8df4909
Show file tree
Hide file tree
Showing 97 changed files with 369 additions and 4,675 deletions.
2 changes: 1 addition & 1 deletion .env.bitcoin
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BITCOIN_ENABLE_INDEXER="false"
BITCOIN_INDEXER_LISTEN_ADDRESS="tb1qgm39cu009lyvq93afx47pp4h9wxq5x92lxxgnz"
BITCOIN_BRIDGE_ETH_RPC_URL="127.0.0.1:8545"
BITCOIN_BRIDGE_CONTRACT_ADDRESS="0xB457BF68D71a17Fa5030269Fb895e29e6cD2DF22"
BITCOIN_BRIDGE_ETH_PRIV_KEY="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
BITCOIN_BRIDGE_ETH_PRIV_KEY=""
BITCOIN_BRIDGE_ABI="aaa.abi"
BITCOIN_BRIDGE_GAS_LIMIT="23333"
BITCOIN_ENABLE_COMMITTER=false
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
pull_request:
branches:
- main
- dev

jobs:
cleanup-runs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [main]
branches: [main, dev]
pull_request:
# The branches below must be a subset of the branches above
branches: [main]
branches: [main, dev]
schedule:
- cron: '37 21 * * 4'

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy-contract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
pull_request:
branches:
- main
- dev

jobs:
cleanup-runs:
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ on:
push:
branches:
- "main"
- "dev"
tags:
- "v*.*.*"
pull_request:
branches:
- "main"
- "dev"

jobs:
docker:
Expand All @@ -23,12 +25,6 @@ jobs:
-
name: Git fetch everything
run: git fetch --prune --unshallow
-
name: Prepare
id: prepare
run: |
COMMIT_DATE=$(date +%Y%m%d-%H%M%S)
echo commit_date=${COMMIT_DATE} >> $GITHUB_OUTPUT
-
name: Docker meta
id: meta
Expand All @@ -43,7 +39,7 @@ jobs:
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=sha,prefix=${{ steps.prepare.outputs.commit_date }}-
type=sha
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- dev

jobs:
triage:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- main
- dev
jobs:
golangci:
name: Run golangci-lint
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/markdown-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
push:
branches:
- main
- dev
paths:
- '**.md'

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- dev

jobs:
Gosec:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- main
- dev
paths:
- .github/workflows/semgrep.yml
schedule:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name: Lint Code Base

on:
push:
branches: ["main"]
branches: ["main", "dev"]
pull_request:
branches: ["main"]
branches: ["main", "dev"]
jobs:
run-lint:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- dev
- release/**

jobs:
Expand Down
42 changes: 0 additions & 42 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ import (
"github.com/evmos/ethermint/ethereum/eip712"
srvflags "github.com/evmos/ethermint/server/flags"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/bitcoincommiter"
bitcoincommiterkeeper "github.com/evmos/ethermint/x/bitcoincommiter/keeper"
bitcoincommitertypes "github.com/evmos/ethermint/x/bitcoincommiter/types"
"github.com/evmos/ethermint/x/bitcoinindexer"
bitcoinindexerkeeper "github.com/evmos/ethermint/x/bitcoinindexer/keeper"
bitcoinindexertypes "github.com/evmos/ethermint/x/bitcoinindexer/types"
"github.com/evmos/ethermint/x/evm"
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
evmtypes "github.com/evmos/ethermint/x/evm/types"
Expand Down Expand Up @@ -184,9 +178,6 @@ var (
// Ethermint modules
evm.AppModuleBasic{},
feemarket.AppModuleBasic{},
// bitcoin modules
bitcoinindexer.AppModuleBasic{},
bitcoincommiter.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -252,10 +243,6 @@ type EthermintApp struct {
EvmKeeper *evmkeeper.Keeper
FeeMarketKeeper feemarketkeeper.Keeper

// Bitcoin keepers
BitcoinindexerKeeper bitcoinindexerkeeper.Keeper
BitcoincommiterKeeper bitcoincommiterkeeper.Keeper

// the module manager
mm *module.Manager

Expand Down Expand Up @@ -487,20 +474,6 @@ func NewEthermintApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

// Create bitcoinindexer keeper
app.BitcoinindexerKeeper = *bitcoinindexerkeeper.NewKeeper(
appCodec,
keys[bitcoinindexertypes.MemStoreKey],
app.GetSubspace(bitcoinindexertypes.ModuleName),
)

app.BitcoincommiterKeeper = *bitcoincommiterkeeper.NewKeeper(
appCodec,
keys[bitcoincommitertypes.MemStoreKey],
app.GetSubspace(bitcoincommitertypes.ModuleName),
homePath,
)

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -537,10 +510,6 @@ func NewEthermintApp(
// Ethermint app modules
feemarket.NewAppModule(app.FeeMarketKeeper, feeMarketSs),
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper, evmSs),

// bitcoin modules
bitcoinindexer.NewAppModule(appCodec, app.BitcoinindexerKeeper),
bitcoincommiter.NewAppModule(appCodec, app.BitcoincommiterKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -571,8 +540,6 @@ func NewEthermintApp(
feegrant.ModuleName,
paramstypes.ModuleName,
vestingtypes.ModuleName,
bitcoinindexertypes.ModuleName,
bitcoincommitertypes.ModuleName,
)

// NOTE: fee market module must go last in order to retrieve the block gas used.
Expand All @@ -598,8 +565,6 @@ func NewEthermintApp(
paramstypes.ModuleName,
upgradetypes.ModuleName,
vestingtypes.ModuleName,
bitcoinindexertypes.ModuleName,
bitcoincommitertypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -634,10 +599,6 @@ func NewEthermintApp(
vestingtypes.ModuleName,
// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,

// bitcoin modules
bitcoinindexertypes.ModuleName,
bitcoincommitertypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -907,8 +868,5 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
// ethermint subspaces
paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(evmtypes.ParamKeyTable()) //nolint: staticcheck
paramsKeeper.Subspace(feemarkettypes.ModuleName).WithKeyTable(feemarkettypes.ParamKeyTable())
// bitcoin subspaces
paramsKeeper.Subspace(bitcoinindexertypes.ModuleName)
paramsKeeper.Subspace(bitcoincommitertypes.ModuleName)
return paramsKeeper
}
70 changes: 23 additions & 47 deletions bitcoin/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import (
"bytes"
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"net/url"
"os"
"path"

b2aa "github.com/b2network/b2-go-aa-utils"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)

var ErrBrdigeDepositTxIDExist = errors.New("non-repeatable processing")

// Bridge bridge
// TODO: only L1 -> L2, More calls may be supported later
type Bridge struct {
Expand Down Expand Up @@ -61,35 +64,36 @@ func NewBridge(bridgeCfg BridgeConfig, abiFileDir string) (*Bridge, error) {
}

// Deposit to ethereum
func (b *Bridge) Deposit(bitcoinAddress string, amount int64) error {
func (b *Bridge) Deposit(hash string, bitcoinAddress string, amount int64) ([]byte, error) {
if bitcoinAddress == "" {
return fmt.Errorf("bitcoin address is empty")
return nil, fmt.Errorf("bitcoin address is empty")
}

if hash == "" {
return nil, fmt.Errorf("tx id is empty")
}

ctx := context.Background()

toAddress, err := b.BitcoinAddressToEthAddress(bitcoinAddress)
if err != nil {
return err
return nil, fmt.Errorf("btc address to eth address err:%w", err)
}

data, err := b.ABIPack(b.ABI, "deposit", common.HexToAddress(toAddress), new(big.Int).SetInt64(amount))
txHash, err := chainhash.NewHashFromStr(hash)
if err != nil {
return err
return nil, err
}

receipt, err := b.ethContractCall(ctx, b.EthPrivKey, data)
data, err := b.ABIPack(b.ABI, "depositV2", txHash, common.HexToAddress(toAddress), new(big.Int).SetInt64(amount))
if err != nil {
return err
return nil, fmt.Errorf("abi pack err:%w", err)
}

if receipt.Status != 1 {
return fmt.Errorf("tx failed, receipt:%v", receipt)
}
return nil
return b.ethContractCall(ctx, b.EthPrivKey, data)
}

func (b *Bridge) ethContractCall(ctx context.Context, priv *ecdsa.PrivateKey, data []byte) (*types.Receipt, error) {
func (b *Bridge) ethContractCall(ctx context.Context, priv *ecdsa.PrivateKey, data []byte) ([]byte, error) {
client, err := ethclient.Dial(b.EthRPCURL)
if err != nil {
return nil, err
Expand All @@ -100,42 +104,14 @@ func (b *Bridge) ethContractCall(ctx context.Context, priv *ecdsa.PrivateKey, da
if !ok {
return nil, fmt.Errorf("error casting public key to ECDSA")
}
nonce, err := client.PendingNonceAt(ctx, crypto.PubkeyToAddress(*publicKeyECDSA))
if err != nil {
return nil, err
}
gasPrice, err := client.SuggestGasPrice(ctx)
if err != nil {
return nil, err
}

tx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &b.ContractAddress,
Value: big.NewInt(0),
Gas: b.GasLimit,
GasPrice: gasPrice,
Data: data,
})

chainID, err := client.ChainID(ctx)
if err != nil {
return nil, err
}
// sign tx
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), priv)
if err != nil {
return nil, err
}

// send tx
err = client.SendTransaction(ctx, signedTx)
if err != nil {
return nil, err
callMsg := ethereum.CallMsg{
From: crypto.PubkeyToAddress(*publicKeyECDSA),
To: &b.ContractAddress,
Data: data,
}

// wait tx confirm
return bind.WaitMined(ctx, client, signedTx)
return client.CallContract(ctx, callMsg, nil)
}

// ABIPack the given method name to conform the ABI. Method call's data
Expand Down
Loading

0 comments on commit 8df4909

Please sign in to comment.