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: start bee node without a connected chain backend #2783

Merged
merged 23 commits into from
Feb 24, 2022
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
2 changes: 2 additions & 0 deletions cmd/bee/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
optionNameSwapInitialDeposit = "swap-initial-deposit"
optionNameSwapEnable = "swap-enable"
optionNameChequebookEnable = "chequebook-enable"
optionNameChainEnable = "chain-enable"
optionNameTransactionHash = "transaction"
optionNameBlockHash = "block-hash"
optionNameSwapDeploymentGasPrice = "swap-deployment-gas-price"
Expand Down Expand Up @@ -253,6 +254,7 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().String(optionNameSwapInitialDeposit, "10000000000000000", "initial deposit if deploying a new chequebook")
cmd.Flags().Bool(optionNameSwapEnable, true, "enable swap")
cmd.Flags().Bool(optionNameChequebookEnable, true, "enable chequebook")
cmd.Flags().Bool(optionNameChainEnable, true, "use a blockchain backend")
cmd.Flags().Bool(optionNameFullNode, false, "cause the node to start in full mode")
cmd.Flags().String(optionNamePostageContractAddress, "", "postage stamp contract address")
cmd.Flags().String(optionNamePriceOracleAddress, "", "price oracle contract address")
Expand Down
3 changes: 3 additions & 0 deletions cmd/bee/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (c *command) initDeployCmd() error {
logger,
stateStore,
swapEndpoint,
0,
signer,
blocktime,
true,
)
if err != nil {
return err
Expand Down Expand Up @@ -91,6 +93,7 @@ func (c *command) initDeployCmd() error {
chequebookFactory,
swapInitialDeposit,
deployGasPrice,
true,
)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (c *command) initStartCmd() (err error) {
SwapInitialDeposit: c.config.GetString(optionNameSwapInitialDeposit),
SwapEnable: c.config.GetBool(optionNameSwapEnable),
ChequebookEnable: c.config.GetBool(optionNameChequebookEnable),
ChainEnable: c.config.GetBool(optionNameChainEnable),
FullNodeMode: fullNode,
Transaction: c.config.GetString(optionNameTransactionHash),
BlockHash: c.config.GetString(optionNameBlockHash),
Expand Down
2 changes: 2 additions & 0 deletions packaging/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ password-file: /var/lib/bee/password
# postage-stamp-address: ""
## ENS compatible API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url
# resolver-options: []
## use a blockchain backend (default true)
# chain-enable: true
## enable swap (default true)
# swap-enable: true
## swap ethereum blockchain endpoint (default "ws://localhost:8546")
Expand Down
2 changes: 2 additions & 0 deletions packaging/homebrew-amd64/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ password-file: /usr/local/var/lib/swarm-bee/password
# postage-stamp-address: ""
## ENS compatible API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url
# resolver-options: []
## use a blockchain backend (default true)
# chain-enable: true
## enable swap (default true)
# swap-enable: true
## swap ethereum blockchain endpoint (default "ws://localhost:8546")
Expand Down
2 changes: 2 additions & 0 deletions packaging/homebrew-arm64/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ password-file: /opt/homebrew/var/lib/swarm-bee/password
# postage-stamp-address: ""
## ENS compatible API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url
# resolver-options: []
## use a blockchain backend (default true)
# chain-enable: true
## enable swap (default true)
# swap-enable: true
## swap ethereum blockchain endpoint (default "ws://localhost:8546")
Expand Down
2 changes: 2 additions & 0 deletions packaging/scoop/bee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ password-file: ./password
# postage-stamp-address: ""
## ENS compatible API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url
# resolver-options: []
## use a blockchain backend (default true)
# chain-enable: true
## enable swap (default true)
# swap-enable: true
## swap ethereum blockchain endpoint (default "ws://localhost:8546")
Expand Down
16 changes: 9 additions & 7 deletions pkg/bzz/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,20 @@ func NewAddress(signer crypto.Signer, underlay ma.Multiaddr, overlay swarm.Addre
}, nil
}

func ParseAddress(underlay, overlay, signature, trxHash, blockHash []byte, networkID uint64) (*Address, error) {
func ParseAddress(underlay, overlay, signature, trxHash, blockHash []byte, validateOverlay bool, networkID uint64) (*Address, error) {
recoveredPK, err := crypto.Recover(signature, generateSignData(underlay, overlay, networkID))
if err != nil {
return nil, ErrInvalidAddress
}

recoveredOverlay, err := crypto.NewOverlayAddress(*recoveredPK, networkID, blockHash)
if err != nil {
return nil, ErrInvalidAddress
}
if !bytes.Equal(recoveredOverlay.Bytes(), overlay) {
return nil, ErrInvalidAddress
if validateOverlay {
recoveredOverlay, err := crypto.NewOverlayAddress(*recoveredPK, networkID, blockHash)
if err != nil {
return nil, ErrInvalidAddress
}
if !bytes.Equal(recoveredOverlay.Bytes(), overlay) {
return nil, ErrInvalidAddress
}
}

multiUnderlay, err := ma.NewMultiaddrBytes(underlay)
Expand Down
2 changes: 1 addition & 1 deletion pkg/bzz/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestBzzAddress(t *testing.T) {
t.Fatal(err)
}

bzzAddress2, err := bzz.ParseAddress(node1ma.Bytes(), overlay.Bytes(), bzzAddress.Signature, trxHash, blockHash, 3)
bzzAddress2, err := bzz.ParseAddress(node1ma.Bytes(), overlay.Bytes(), bzzAddress.Signature, trxHash, blockHash, true, 3)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/debugapi/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
LightMode BeeNodeMode = iota
FullMode
DevMode
UltraLightMode
)

type nodeResponse struct {
Expand All @@ -33,6 +34,8 @@ func (b BeeNodeMode) String() string {
return "full"
case DevMode:
return "dev"
case UltraLightMode:
return "ultra-light"
}
return "unknown"
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/debugapi/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (s *Service) postageCreateHandler(w http.ResponseWriter, r *http.Request) {

batchID, err := s.postageContract.CreateBatch(ctx, amount, uint8(depth), immutable, label)
if err != nil {
if errors.Is(err, postagecontract.ErrChainDisabled) {
s.logger.Debugf("create batch: no chain backend: %v", err)
s.logger.Error("create batch: no chain backend")
jsonhttp.MethodNotAllowed(w, "no chain backend")
return
}
if errors.Is(err, postagecontract.ErrInsufficientFunds) {
s.logger.Debugf("create batch: out of funds: %v", err)
s.logger.Error("create batch: out of funds")
Expand Down Expand Up @@ -185,6 +191,7 @@ func (s *Service) postageGetStampsHandler(w http.ResponseWriter, r *http.Request
BatchTTL: batchTTL,
})
}

jsonhttp.OK(w, resp)
}

Expand Down
129 changes: 117 additions & 12 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethersphere/bee/pkg/config"
Expand All @@ -29,6 +31,8 @@ import (
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/bee/pkg/transaction/wrapped"
"github.com/ethersphere/go-sw3-abi/sw3abi"
"github.com/prometheus/client_golang/prometheus"
)

const (
Expand All @@ -44,25 +48,33 @@ func InitChain(
logger logging.Logger,
stateStore storage.StateStorer,
endpoint string,
oChainID int64,
signer crypto.Signer,
pollingInterval time.Duration,
chainEnabled bool,
) (transaction.Backend, common.Address, int64, transaction.Monitor, transaction.Service, error) {
var backend transaction.Backend
rpcClient, err := rpc.DialContext(ctx, endpoint)
if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("dial eth client: %w", err)
var backend transaction.Backend = &noOpChainBackend{
chainID: oChainID,
}

var versionString string
err = rpcClient.CallContext(ctx, &versionString, "web3_clientVersion")
if err != nil {
logger.Infof("could not connect to backend at %v. In a swap-enabled network a working blockchain node (for xdai network in production, goerli in testnet) is required. Check your node or specify another node using --swap-endpoint.", endpoint)
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("eth client get version: %w", err)
}
if chainEnabled {
// connect to the real one
rpcClient, err := rpc.DialContext(ctx, endpoint)
if err != nil {
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("dial eth client: %w", err)
}

logger.Infof("connected to ethereum backend: %s", versionString)
var versionString string
err = rpcClient.CallContext(ctx, &versionString, "web3_clientVersion")
if err != nil {
logger.Infof("could not connect to backend at %v. In a swap-enabled network a working blockchain node (for xdai network in production, goerli in testnet) is required. Check your node or specify another node using --swap-endpoint.", endpoint)
return nil, common.Address{}, 0, nil, nil, fmt.Errorf("eth client get version: %w", err)
}

logger.Infof("connected to ethereum backend: %s", versionString)

backend = wrapped.NewBackend(ethclient.NewClient(rpcClient))
backend = wrapped.NewBackend(ethclient.NewClient(rpcClient))
}

chainID, err := backend.ChainID(ctx)
if err != nil {
Expand Down Expand Up @@ -148,7 +160,12 @@ func InitChequebookService(
chequebookFactory chequebook.Factory,
initialDeposit string,
deployGasPrice string,
chainEnabled bool,
) (chequebook.Service, error) {
if !chainEnabled {
return new(noOpChequebookService), nil
}

chequeSigner := chequebook.NewChequeSigner(signer, chainID)

deposit, ok := new(big.Int).SetString(initialDeposit, 10)
Expand Down Expand Up @@ -325,3 +342,91 @@ func GetTxNextBlock(ctx context.Context, logger logging.Logger, backend transact

return hashBytes, nil
}

// noOpChequebookService is a noOp implementation for chequebook.Service interface.
type noOpChequebookService struct{}

func (m *noOpChequebookService) Deposit(context.Context, *big.Int) (hash common.Hash, err error) {
return hash, errors.New("chain disabled")
}
func (m *noOpChequebookService) Withdraw(context.Context, *big.Int) (hash common.Hash, err error) {
return hash, errors.New("chain disabled")
}
func (m *noOpChequebookService) WaitForDeposit(context.Context, common.Hash) error {
return errors.New("chain disabled")
}
func (m *noOpChequebookService) Balance(context.Context) (*big.Int, error) {
return nil, errors.New("chain disabled")
}
func (m *noOpChequebookService) AvailableBalance(context.Context) (*big.Int, error) {
return nil, errors.New("chain disabled")
}
func (m *noOpChequebookService) Address() common.Address {
return common.Address{}
}
func (m *noOpChequebookService) Issue(context.Context, common.Address, *big.Int, chequebook.SendChequeFunc) (*big.Int, error) {
return nil, errors.New("chain disabled")
}
func (m *noOpChequebookService) LastCheque(common.Address) (*chequebook.SignedCheque, error) {
return nil, errors.New("chain disabled")
}
func (m *noOpChequebookService) LastCheques() (map[common.Address]*chequebook.SignedCheque, error) {
return nil, errors.New("chain disabled")
}

// noOpChainBackend is a noOp implementation for transaction.Backend interface.
type noOpChainBackend struct {
chainID int64
}

func (m noOpChainBackend) Metrics() []prometheus.Collector {
return nil
}

func (m noOpChainBackend) CodeAt(context.Context, common.Address, *big.Int) ([]byte, error) {
return common.FromHex(sw3abi.SimpleSwapFactoryDeployedBinv0_4_0), nil
}
func (m noOpChainBackend) CallContract(context.Context, ethereum.CallMsg, *big.Int) ([]byte, error) {
panic("chain no op: CallContract")
}
func (m noOpChainBackend) HeaderByNumber(context.Context, *big.Int) (*types.Header, error) {
h := new(types.Header)
h.Time = uint64(time.Now().Unix())
return h, nil
}
func (m noOpChainBackend) PendingNonceAt(context.Context, common.Address) (uint64, error) {
panic("chain no op: PendingNonceAt")
}
func (m noOpChainBackend) SuggestGasPrice(context.Context) (*big.Int, error) {
panic("chain no op: SuggestGasPrice")
}
func (m noOpChainBackend) EstimateGas(context.Context, ethereum.CallMsg) (uint64, error) {
panic("chain no op: EstimateGas")
}
func (m noOpChainBackend) SendTransaction(context.Context, *types.Transaction) error {
panic("chain no op: SendTransaction")
}
func (m noOpChainBackend) TransactionReceipt(context.Context, common.Hash) (*types.Receipt, error) {
r := new(types.Receipt)
r.BlockNumber = big.NewInt(1)
return r, nil
}
func (m noOpChainBackend) TransactionByHash(context.Context, common.Hash) (tx *types.Transaction, isPending bool, err error) {
panic("chain no op: TransactionByHash")
}
func (m noOpChainBackend) BlockNumber(context.Context) (uint64, error) {
return 4, nil
}
func (m noOpChainBackend) BalanceAt(context.Context, common.Address, *big.Int) (*big.Int, error) {
panic("chain no op: BalanceAt")
}
func (m noOpChainBackend) NonceAt(context.Context, common.Address, *big.Int) (uint64, error) {
panic("chain no op: NonceAt")
}
func (m noOpChainBackend) FilterLogs(context.Context, ethereum.FilterQuery) ([]types.Log, error) {
panic("chain no op: FilterLogs")
}
func (m noOpChainBackend) ChainID(context.Context) (*big.Int, error) {
return big.NewInt(m.chainID), nil
}
func (m noOpChainBackend) Close() {}
Loading