Skip to content

Commit

Permalink
reslove: conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
turingczz committed Sep 28, 2023
2 parents b2edeb1 + 01451fe commit 5330e80
Show file tree
Hide file tree
Showing 93 changed files with 14,364 additions and 626 deletions.
5 changes: 3 additions & 2 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/bittorrent/go-btfs/chain/tokencfg"
"io"
"math/big"
"strings"
"time"

"github.com/bittorrent/go-btfs/chain/tokencfg"

"github.com/bittorrent/go-btfs/accounting"
"github.com/bittorrent/go-btfs/chain/config"
"github.com/bittorrent/go-btfs/settlement"
Expand Down Expand Up @@ -348,7 +349,7 @@ func initSwap(
priceOracle := priceoracle.New(currentPriceOracleAddress, transactionService)
_, err := priceOracle.CheckNewPrice(tokencfg.GetWbttToken()) // CheckNewPrice when node starts
if err != nil {
return nil, nil, errors.New("CheckNewPrice " + err.Error())
return nil, nil, errors.New("CheckNewPrice error, it may happens when contract call failed if bttc chain rpc is down, please try again")
}

swapProtocol := swapprotocol.New(overlayEthAddress, priceOracle)
Expand Down
32 changes: 32 additions & 0 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
_ "expvar"
"fmt"
"github.com/bittorrent/go-btfs/s3"
"github.com/bittorrent/go-btfs/s3/api/services/accesskey"
"io/ioutil"
"math/rand"
"net"
Expand Down Expand Up @@ -100,6 +102,7 @@ const (
chainID = "chain-id"
// apiAddrKwd = "address-api"
// swarmAddrKwd = "address-swarm"
enableS3CompatibleAPIKwd = "s3-compatible-api"
)

// BTFS daemon test exit error code
Expand Down Expand Up @@ -227,6 +230,7 @@ Headers.
// TODO: add way to override addresses. tricky part: updating the config if also --init.
// cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),
// cmds.StringOption(swarmAddrKwd, "Address for the swarm socket (overrides config)"),
cmds.BoolOption(enableS3CompatibleAPIKwd, "Enable s3-compatible-api server"),
},
Subcommands: map[string]*cmds.Command{},
NoRemote: true,
Expand Down Expand Up @@ -713,6 +717,33 @@ If the user need to start multiple nodes on the same machine, the configuration
functest(cfg.Services.OnlineServerDomain, cfg.Identity.PeerID, hValue)
}

// Init s3 providers
err = s3.InitProviders(statestore)
if err != nil {
return err
}

// Init access-key
accesskey.InitService(s3.GetProviders())

// Start s3-compatible-api server
s3OptEnable, s3Opt := req.Options[enableS3CompatibleAPIKwd].(bool)
if s3OptEnable || (!s3Opt && cfg.S3CompatibleAPI.Enable) {
s3Server := s3.NewServer(cfg.S3CompatibleAPI)
err = s3Server.Start()
if err != nil {
fmt.Printf("S3-Compatible-API server: %v\n", err)
return
}
fmt.Printf("S3-Compatible-API server started, endpoint-url: http://%s\n", cfg.S3CompatibleAPI.Address)
defer func() {
err = s3Server.Stop()
if err != nil {
fmt.Printf("S3-Compatible-API server: %v\n", err)
}
}()
}

if SimpleMode == false {
// set Analytics flag if specified
if dc, ok := req.Options[enableDataCollection]; ok == true {
Expand All @@ -728,6 +759,7 @@ If the user need to start multiple nodes on the same machine, the configuration
spin.Analytics(api, cctx.ConfigRoot, node, version.CurrentVersionNumber, hValue)
spin.Hosts(node, env)
spin.Contracts(node, req, env, nodepb.ContractStat_HOST.String())
spin.RestartFixChequeCashOut()
}

// Give the user some immediate feedback when they hit C-c
Expand Down
26 changes: 26 additions & 0 deletions cmd/btfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/bittorrent/go-btfs/assets"
"github.com/bittorrent/go-btfs/chain"
Expand All @@ -36,6 +37,7 @@ const (
rmOnUnpinOptionName = "rm-on-unpin"
seedOptionName = "seed"
simpleMode = "simple-mode"
recoveryOptionName = "recovery"
/*
passWordOptionName = "password"
passwordFileoptionName = "password-file"
Expand Down Expand Up @@ -72,6 +74,7 @@ environment variable:
cmds.BoolOption(rmOnUnpinOptionName, "r", "Remove unpinned files.").WithDefault(false),
cmds.StringOption(seedOptionName, "s", "Import seed phrase"),
cmds.BoolOption(simpleMode, "sm", "init with simple mode or not."),
cmds.StringOption(recoveryOptionName, "Recovery data from a backup"),
/*
cmds.StringOption(passWordOptionName, "", "password for decrypting keys."),
cmds.StringOption(passwordFileoptionName, "", "path to a file that contains password for decrypting keys"),
Expand Down Expand Up @@ -140,7 +143,30 @@ environment variable:
password, _ := req.Options[passWordOptionName].(string)
passwordFile, _ := req.Options[passwordFileoptionName].(string)
*/
backupPath, ok := req.Options[recoveryOptionName].(string)
if ok {
btfsPath := env.(*oldcmds.Context).ConfigRoot
dstPath := filepath.Dir(btfsPath)
if fsrepo.IsInitialized(btfsPath) {
newPath := filepath.Join(dstPath, fmt.Sprintf(".btfs_backup_%d", time.Now().Unix()))
// newPath := filepath.Join(filepath.Dir(btfsPath), backup)
err := os.Rename(btfsPath, newPath)
if err != nil {
return err
}
fmt.Println("btfs configuration file already exists!")
fmt.Println("We have renamed it to ", newPath)
}

if err := commands.UnTar(backupPath, dstPath); err != nil {
err = commands.UnZip(backupPath, dstPath)
if err != nil {
return errors.New("your file format is not tar.gz or zip, please check again")
}
}
fmt.Println("Recovery successful!")
return nil
}
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profile, conf, keyType, importKey, seedPhrase, rmOnUnpin, simpleModeIn)
},
}
Expand Down
175 changes: 175 additions & 0 deletions core/commands/accesskey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package commands

import (
"errors"
cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/core/commands/cmdenv"
"github.com/bittorrent/go-btfs/s3/api/services/accesskey"
)

var AccessKeyCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Manage S3-Compatible-API access-keys.",
ShortDescription: "Commands for generate, update, get and list access-keys stored in this node.",
},
Subcommands: map[string]*cmds.Command{
"generate": accessKeyGenerateCmd,
"enable": accessKeyEnableCmd,
"disable": accessKeyDisableCmd,
"reset": accessKeyResetCmd,
"delete": accessKeyDeleteCmd,
"get": accessKeyGetCmd,
"list": accessKeyListCmd,
},
NoLocal: true,
}

func checkDaemon(env cmds.Environment) (err error) {
node, err := cmdenv.GetNode(env)
if err != nil {
return
}
if !node.IsDaemon {
err = errors.New("please start the node first")
}
return
}

var accessKeyGenerateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Generate a new access-key record.",
ShortDescription: "Outputs the new created access-key record.",
},
Arguments: []cmds.Argument{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
ack, err := accesskey.Generate()
if err != nil {
return
}
err = cmds.EmitOnce(res, ack)
return
},
}

var accessKeyEnableCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Set status of the specified access-key to enable.",
ShortDescription: "Outputs empty if the access-key has been set to enable or it was already enabled.",
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, true, "The key").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
key := req.Arguments[0]
err = accesskey.Enable(key)
return
},
}

var accessKeyDisableCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Set status of the specified access-key to enable.",
ShortDescription: "Outputs empty if the access-key has been set to disable or it was already disabled.",
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, true, "The key").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
key := req.Arguments[0]
err = accesskey.Disable(key)
return
},
}

var accessKeyResetCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Rest secret of the specified access-key.",
ShortDescription: "Outputs the updated access-key record if it's secret has been reset.",
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, true, "The key").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
key := req.Arguments[0]
err = accesskey.Reset(key)
return
},
}

var accessKeyDeleteCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Delete the specified access-key",
ShortDescription: "Outputs empty if access-key record has been deleted.",
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, true, "The key").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
key := req.Arguments[0]
err = accesskey.Delete(key)
return
},
}

var accessKeyGetCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get an access-key detail info.",
ShortDescription: "Outputs access-key record for the specified key.",
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, true, "The key").EnableStdin(),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
key := req.Arguments[0]
ack, err := accesskey.Get(key)
if err != nil {
return
}
err = cmds.EmitOnce(res, ack)
return
},
}

var accessKeyListCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List all access-keys.",
ShortDescription: "Outputs all non-deleted access-keys stored in current node.",
},
Arguments: []cmds.Argument{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
err = checkDaemon(env)
if err != nil {
return
}
list, err := accesskey.List()
if err != nil {
return
}
err = cmds.EmitOnce(res, list)
return
},
}
Loading

0 comments on commit 5330e80

Please sign in to comment.