Skip to content

Commit

Permalink
Updated core packages to BTFS 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbad Marino authored and Simbad Marino committed Aug 10, 2022
1 parent 75eae09 commit f00b942
Show file tree
Hide file tree
Showing 48 changed files with 2,467 additions and 842 deletions.
5 changes: 5 additions & 0 deletions chain/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ var (
bttcTestBatchAddress = common.HexToAddress("0x0c9de531dcb38b758fe8a2c163444a5e54ee0db2")
bttcTestVaultLogicAddressV1 = common.HexToAddress("0x212324b18255593AdE87597Fa37C2c582aD72d24")
bttcTestVaultLogicAddress = common.HexToAddress("0x73bcbE03999913dB7229FD5dC485cf23247c58B5") // https://testnet.bttcscan.com/address/0x73bcbE03999913dB7229FD5dC485cf23247c58B5
bttcTestStatusAddress = common.HexToAddress("0x8226b334C441095215Ae58eD9396a55a9D80bFD5")

bttcFactoryAddressV1 = common.HexToAddress("0x9AF4bEc1A30BeC47756Ecef4cf43B91592121bC9")
bttcFactoryAddress = common.HexToAddress("0x763d7858287B9a33F4bE5bb3df0241dACc59BCc7") // https://bttcscan.com/address/0x763d7858287B9a33F4bE5bb3df0241dACc59BCc7
bttcOracleAddress = common.HexToAddress("0x0064d80C42b6E2cE3aC92eaD445B3D83C510c7AA")
bttcBatchAddress = common.HexToAddress("0x0c9de531dcb38b758fe8a2c163444a5e54ee0db2")
bttcVaultLogicAddressV1 = common.HexToAddress("0x102dbCe01394C4a44Da3a1DF1De418e3fC225077") // https://bttcscan.com/address/0x102dbce01394c4a44da3a1df1de418e3fc225077
bttcVaultLogicAddress = common.HexToAddress("0x11a91B7270ea000768F7A2C543547e832b5cb031") // https://bttcscan.com/address/0x11a91B7270ea000768F7A2C543547e832b5cb031
bttcStatusAddress = common.HexToAddress("0x6DBAd4Bd16C15AE6dDEaA640626e5A3E151F02fC")

// deploy gas
ethDeploymentGas = "10"
Expand Down Expand Up @@ -75,6 +77,7 @@ type ChainConfig struct {
VaultLogicAddress common.Address
DeploymentGas string
Endpoint string
StatusAddress common.Address
}

func GetChainConfig(chainID int64) (*ChainConfig, bool) {
Expand Down Expand Up @@ -104,6 +107,7 @@ func GetChainConfig(chainID int64) (*ChainConfig, bool) {
cfg.DeploymentGas = bttcDeploymentGas
cfg.Endpoint = bttcEndpoint
cfg.BatchAddress = bttcBatchAddress
cfg.StatusAddress = bttcStatusAddress
return &cfg, true
case bttcTestChainID:
cfg.StartBlock = bttcStartBlock
Expand All @@ -113,6 +117,7 @@ func GetChainConfig(chainID int64) (*ChainConfig, bool) {
cfg.Endpoint = bttcTestEndpoint
cfg.BatchAddress = bttcTestBatchAddress
cfg.VaultLogicAddress = bttcTestVaultLogicAddress
cfg.StatusAddress = bttcTestStatusAddress
return &cfg, true
case testChainID:
cfg.StartBlock = ethStartBlock
Expand Down
149 changes: 149 additions & 0 deletions chain/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package chain

import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"

cmds "github.com/bittorrent/go-btfs-cmds"
oldcmds "github.com/bittorrent/go-btfs/commands"
Expand All @@ -14,6 +17,7 @@ import (
"github.com/bittorrent/go-btfs/transaction/storage"
"github.com/ethereum/go-ethereum/common"
"github.com/tron-us/go-btfs-common/crypto"
onlinePb "github.com/tron-us/go-btfs-common/protos/online"
)

// after btfs init
Expand Down Expand Up @@ -158,3 +162,148 @@ func StoreChainIdIfNotExists(chainID int64, statestore storage.StateStorer) erro

return nil
}

// GetReportStatus from leveldb
var keyReportStatus = "keyReportStatus"

type ReportStatusInfo struct {
ReportStatusSeconds int64
LastReportTime time.Time
}

func GetReportStatus() (*ReportStatusInfo, error) {
if StateStore == nil {
return nil, errors.New("please start btfs node, at first! ")
}

var reportStatusInfo ReportStatusInfo
err := StateStore.Get(keyReportStatus, &reportStatusInfo)
if err != nil {
if err == storage.ErrNotFound {
reportStatusInfo = ReportStatusInfo{ReportStatusSeconds: int64(rand.Intn(100000000) % 86400), LastReportTime: time.Time{}}
err := StateStore.Put(keyReportStatus, reportStatusInfo)
if err != nil {
fmt.Println("StoreChainIdIfNotExists: init StoreChainId err: ", err)
return nil, err
}
}
return nil, err
}
return &reportStatusInfo, nil
}

func SetReportStatusOK() (*ReportStatusInfo, error) {
var reportStatusInfo ReportStatusInfo
err := StateStore.Get(keyReportStatus, &reportStatusInfo)
if err != nil {
return nil, err
}
reportStatusInfo.LastReportTime = time.Now()
err = StateStore.Put(keyReportStatus, reportStatusInfo)
if err != nil {
return nil, err
}
//fmt.Println("... ReportStatus, SetReportStatus: ok! ")
return &reportStatusInfo, nil
}

// GetReportStatus from leveldb
var keyReportStatusList = "keyReportStatusList"

type LevelDbReportStatusInfo struct {
Peer string `json:"peer"`
BttcAddress string `json:"bttc_addr"`
StatusContract string `json:"status_contract"`
Nonce uint32 `json:"nonce"`
TxHash string `json:"tx_hash"`
GasSpend string `json:"gas_spend"`
ReportTime time.Time `json:"report_time"`
IncreaseNonce uint32 `json:"increase_nonce"`
}

// SetReportStatusListOK store tx list
func SetReportStatusListOK(r *LevelDbReportStatusInfo) ([]*LevelDbReportStatusInfo, error) {
if StateStore == nil {
return nil, errors.New("please start btfs node, at first! ")
}

init := false

rList := make([]*LevelDbReportStatusInfo, 0)
err := StateStore.Get(keyReportStatusList, &rList)
if err != nil {
if err.Error() == "storage: not found" {
init = true
// continue
} else {
return nil, err
}
}

if init {
r.IncreaseNonce = r.Nonce
} else {
r.IncreaseNonce = r.Nonce - rList[len(rList)-1].Nonce
}

rList = append(rList, r)
err = StateStore.Put(keyReportStatusList, rList)
if err != nil {
return nil, err
}
//fmt.Println("... ReportStatus, SetReportStatusListOK: ok! rList = ", rList)
return rList, nil
}

// GetReportStatusListOK store tx list
func GetReportStatusListOK() ([]*LevelDbReportStatusInfo, error) {
if StateStore == nil {
return nil, errors.New("please start btfs node, at first! ")
}

rList := make([]*LevelDbReportStatusInfo, 0)
err := StateStore.Get(keyReportStatusList, &rList)
if err != nil {
if err.Error() == "storage: not found" {
return nil, nil
} else {
return nil, err
}
}

return rList, nil
}

// GetLastOnline from leveldb
var keyLastOnline = "keyLastOnline"

type LastOnlineInfo struct {
LastSignedInfo onlinePb.SignedInfo
LastSignature string
LastTime time.Time
}

func GetLastOnline() (*LastOnlineInfo, error) {
if StateStore == nil {
return nil, errors.New("please start btfs node, at first! ")
}

var lastOnlineInfo LastOnlineInfo
err := StateStore.Get(keyLastOnline, &lastOnlineInfo)
if err != nil {
if err == storage.ErrNotFound {
return nil, nil
}
return nil, err
}
return &lastOnlineInfo, nil
}
func StoreOnline(lastOnlineInfo *LastOnlineInfo) error {
err := StateStore.Put(keyLastOnline, *lastOnlineInfo)
if err != nil {
fmt.Println("StoreOnline: init StoreChainId err: ", err)
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/btfs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
btfs
btfs-test-cover
btfs.ex
btfs.exe
*.a
8 changes: 8 additions & 0 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
nodeMount "github.com/bittorrent/go-btfs/fuse/node"
"github.com/bittorrent/go-btfs/repo"
fsrepo "github.com/bittorrent/go-btfs/repo/fsrepo"
"github.com/bittorrent/go-btfs/reportstatus"
"github.com/bittorrent/go-btfs/settlement/swap/vault"
"github.com/bittorrent/go-btfs/spin"
"github.com/bittorrent/go-btfs/transaction"
Expand Down Expand Up @@ -470,6 +471,13 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
return err
}

// init report status contract
err = reportstatus.Init(chainInfo.TransactionService, cfg, configRoot, chainCfg.StatusAddress, chainInfo.ChainID)
if err != nil {
fmt.Println("init report status, err: ", err)
return err
}

// init ip2location db
if err := bindata.Init(); err != nil {
// log init ip2location err
Expand Down
2 changes: 0 additions & 2 deletions cmd/btfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
return err
}

fmt.Printf("initializing BTFS node at %s\n", repoRoot)
if _, err := fmt.Fprintf(out, "initializing BTFS node at %s\n", repoRoot); err != nil {
return err
}
Expand All @@ -170,7 +169,6 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
if err != nil {
return err
}

if rmOnUnpin {
raw := json.RawMessage(`{"rmOnUnpin":"` + strconv.FormatBool(rmOnUnpin) + `"}`)
conf.Datastore.Params = &raw
Expand Down
36 changes: 0 additions & 36 deletions cmd/btfs/print.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/btfs/util/print.go

This file was deleted.

5 changes: 5 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ func TestCommands(t *testing.T) {
"/wallet/transactions",
//"/wallet/keys",
"/wallet/withdraw",
"/statuscontract",
"/statuscontract/total",
"/statuscontract/reportlist",
"/statuscontract/lastinfo",
"/statuscontract/config",
}

cmdSet := make(map[string]struct{})
Expand Down
24 changes: 23 additions & 1 deletion core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Set the value of the 'Datastore.Path' key:
"show": configShowCmd,
"edit": configEditCmd,
"replace": configReplaceCmd,
//"profile": configProfileCmd,
//"profile": configProfileCmd,
"storage-host-enable": storageHostEnableCmd,
"sync-chain-info": SyncChainInfoCmd,
"optin": optInCmd,
Expand Down Expand Up @@ -670,6 +670,28 @@ func SyncConfigChainInfoV2(configRoot string, chainid int64, endpoint string, cu
return nil
}

func SyncConfigOnlineCfg(configRoot string, onlineServerDomain string, reportOnline, reportStatusContract bool) error {
r, err := fsrepo.Open(configRoot)
if err != nil {
return err
}
defer r.Close()

cfg, err := r.Config()
if err != nil {
return err
}
cfg.Services.OnlineServerDomain = onlineServerDomain
cfg.Experimental.ReportOnline = reportOnline
cfg.Experimental.ReportStatusContract = reportStatusContract

err = r.SetConfig(cfg)
if err != nil {
return err
}
return nil
}

func SetConfigStorageHostEnable(configRoot string, enable bool) error {
r, err := fsrepo.Open(configRoot)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ var rootSubcommands = map[string]*cmds.Command{
"bttc": bttc.BttcCmd,
"settlement": settlement.SettlementCmd,
//"update": ExternalBinary(),
"network": NetworkCmd,
"wallet": WalletCmd,
"network": NetworkCmd,
"wallet": WalletCmd,
"statuscontract": StatusContractCmd,
}

// RootRO is the readonly version of Root
Expand Down
Loading

0 comments on commit f00b942

Please sign in to comment.