Skip to content

Commit

Permalink
fix compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
rockca committed Sep 29, 2021
1 parent b875f40 commit edebc13
Show file tree
Hide file tree
Showing 18 changed files with 713 additions and 584 deletions.
91 changes: 46 additions & 45 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/TRON-US/go-btfs/accounting"
"github.com/TRON-US/go-btfs/chain/config"
"github.com/TRON-US/go-btfs/settlement"
"github.com/TRON-US/go-btfs/settlement/swap"
Expand All @@ -26,14 +27,11 @@ import (
)

var (
var log = logging.Logger("chain")
erc20ABI = transaction.ParseABIUnchecked(sw3abi.ERC20ABIv0_3_1)
errDecodeABI = errors.New("could not decode abi data")
ChainObject = ChainInfo{}
SettleObject = SettleInfo{}
log = logging.Logger("chain")
ChainObject ChainInfo
SettleObject SettleInfo
)


const (
MaxDelay = 1 * time.Minute
CancellationDepth = 6
Expand Down Expand Up @@ -78,28 +76,30 @@ func InitChain(
return nil, fmt.Errorf("get chain id: %w", err)
}

chainconfig, _ := config.GetChainConfig(chainID)
chainconfig, _ := config.GetChainConfig(chainID.Int64())

overlayEthAddress, err := signer.EthereumAddress()
if err != nil {
return nil, fmt.Errorf("eth address: %w", err)
}

transactionMonitor := transaction.NewMonitor(backend, overlayEthAddress, pollingInterval, cancellationDepth)
transactionMonitor := transaction.NewMonitor(backend, overlayEthAddress, pollingInterval, CancellationDepth)

transactionService, err := transaction.NewService(backend, signer, stateStore, chainID, transactionMonitor)
if err != nil {
return nil, fmt.Errorf("new transaction service: %w", err)
}

return &ChainInfo{
Chainconfig: chainconfig,
ChainObject = ChainInfo{
Chainconfig: *chainconfig,
Backend: backend,
OverlayAddress: overlayEthAddress,
ChainID: chainID.Int64(),
OverlayAddress: overlayEthAddress,
ChainID: chainID.Int64(),
TransactionMonitor: transactionMonitor,
TransactionService: transactionService,
}, nil
}

return &ChainObject, nil
}

func InitSettlement(
Expand All @@ -111,25 +111,25 @@ func InitSettlement(
networkID uint64,
) (*SettleInfo, error) {
//InitChequebookFactory
factory, err := InitChequebookFactory(chaininfo.backend, chaininfo.chainID, chaininfo.transactionService, chaininfo.chainconfig.CurrentFactory)
factory, err := initChequebookFactory(chaininfo.Backend, chaininfo.ChainID, chaininfo.TransactionService, chaininfo.Chainconfig.CurrentFactory.String())

if err != nil {
fmt.Printf("init chequebook factory error")
return nil, errors.New("init chequebook factory error")
}

//InitChequebookService
chequebookService, err := InitChequebookService(
chequebookService, err := initChequebookService(
ctx,
stateStore,
chaininfo.signer,
chaininfo.chainID,
chaininfo.backend,
chaininfo.overlayAddress,
chaininfo.transactionService,
chaininfo.Signer,
chaininfo.ChainID,
chaininfo.Backend,
chaininfo.OverlayAddress,
chaininfo.TransactionService,
factory,
initialDeposit,
deployGasPrice
deployGasPrice,
)

if err != nil {
Expand All @@ -139,12 +139,12 @@ func InitSettlement(

//initChequeStoreCashout
chequeStore, cashoutService := initChequeStoreCashout(
stateStore,
chaininfo.backend,
factory,
chaininfo.chainID,
chaininfo.overlayAddress,
chaininfo.transactionService
stateStore,
chaininfo.Backend,
factory,
chaininfo.ChainID,
chaininfo.OverlayAddress,
chaininfo.TransactionService,
)

//new accounting
Expand All @@ -157,31 +157,33 @@ func InitSettlement(

//InitSwap
swapService, priceOracleService, err := initSwap(
stateStore,
networkID,
chaininfo.overlayAddress,
chequebookService,
chequeStore,
cashoutService,
accounting,
chaininfo.chainconfig.PriceOracleAddress,
chaininfo.chainID,
chaininfo.transactionService
stateStore,
networkID,
chaininfo.OverlayAddress,
chequebookService,
chequeStore,
cashoutService,
accounting,
chaininfo.Chainconfig.PriceOracleAddress.String(),
chaininfo.ChainID,
chaininfo.TransactionService,
)

if err != nil {
fmt.Printf("init swap service error")
return nil, errors.New("init swap service error")
}

return &SettleInfo{
Factory: factory,
SettleObject = SettleInfo{
Factory: factory,
ChequebookService: chequebookService,
ChequeStore: chequeStore,
CashoutService: cashoutService,
SwapService: swapService,
OracleService: priceOracleService,
}, nil
ChequeStore: chequeStore,
CashoutService: cashoutService,
SwapService: swapService,
OracleService: priceOracleService,
}

return &SettleObject, nil
}

// InitChequebookFactory will initialize the chequebook factory with the given
Expand All @@ -193,7 +195,6 @@ func initChequebookFactory(
factoryAddress string,
) (chequebook.Factory, error) {
var currentFactory common.Address
var legacyFactories []common.Address

chainCfg, found := config.GetChainConfig(chainID)

Expand Down
6 changes: 2 additions & 4 deletions chain/statestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"github.com/TRON-US/go-btfs/statestore/leveldb"
"github.com/TRON-US/go-btfs/statestore/mock"
"github.com/TRON-US/go-btfs/transaction/storage"
logging "github.com/ipfs/go-log"
)

func InitStateStore(dataDir string) (ret storage.StateStorer, err error) {
if dataDir == "" {
ret = mock.NewStateStore()
log.Warning("using in-mem state store, no node state will be persisted")
log.Warn("using in-mem state store, no node state will be persisted")
return ret, nil
}

var log = logging.Logger("chain:statestore")
return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"), log)
return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"))
}
25 changes: 14 additions & 11 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
_ "expvar"
"fmt"
Expand Down Expand Up @@ -362,41 +363,43 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
singer := crypto.NewDefaultSigner(pk)

//chain init --- dir hardcode
statestore := chain.InitStateStore("/Users/yangsai/go/work/testData")
statestore, err := chain.InitStateStore("/Users/yangsai/go/work/testData")
if err != nil {
fmt.Println("init statestore err: ", err)
return err
}
//endpoint 先hardcode
chainInfo, err := chain.InitChain(cctx, statestore, "http://18.144.29.246:8110", singer, time.Duration(10))
chainInfo, err := chain.InitChain(context.Background(), statestore, "http://18.144.29.246:8110", singer, time.Duration(10))
if err != nil {
fmt.Printf("init chain err: ", err)
fmt.Println("init chain err: ", err)
return err
}

// Sync the with the given Ethereum backend:
isSynced, _, err := transaction.IsSynced(cctx, chainInfo.Backend, chain.MaxDelay)
isSynced, _, err := transaction.IsSynced(context.Background(), chainInfo.Backend, chain.MaxDelay)
if err != nil {
return fmt.Errorf("is synced: %w", err)
}
if !isSynced {
log.Infof("waiting to sync with the Ethereum backend")

err := transaction.WaitSynced(p2pCtx, logger, swapBackend, maxDelay)
err := transaction.WaitSynced(context.Background(), chainInfo.Backend, chain.MaxDelay)
if err != nil {
return fmt.Errorf("waiting backend sync: %w", err)
}
}

var initialDeposit = "100"
var deployGasPrice = "0"
var networkID = 10
var networkID = uint64(10)

settleinfo, err := chain.InitSettlement(cctx, statestore, chainInfo, initialDeposit, deployGasPrice, networkID)
/*settleinfo*/
_, err = chain.InitSettlement(context.Background(), statestore, chainInfo, initialDeposit, deployGasPrice, networkID)
if err != nil {
fmt.Printf("init settlement err: ", err)
fmt.Println("init settlement err: ", err)
return err
}

chain.ChainObject = chainInfo
chain.SettleObject = settleinfo

hValue, hasHval := req.Options[hValueKwd].(string)

migrated := config.MigrateConfig(cfg, inited, hasHval)
Expand Down
6 changes: 3 additions & 3 deletions cmd/btfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ environment variable:
passwordFile, _ := req.Options[passwordFileoptionName].(string)
*/

return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profile, conf, keyType /* importKey,*/, seedPhrase, rmOnUnpin)
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profile, conf, keyType, "", seedPhrase, rmOnUnpin)
},
}

Expand All @@ -142,9 +142,9 @@ Reinitializing would overwrite your keys.
`)

func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config,
keyType string /*importKey string,*/, mnemonic string, rmOnUnpin bool) error {
keyType string, importKey string, mnemonic string, rmOnUnpin bool) error {

importKey, mnemonic, err := util.GenerateKey(keyType, mnemonic)
importKey, mnemonic, err := util.GenerateKey(importKey, keyType, mnemonic)
if err != nil {
return err
}
Expand Down
34 changes: 9 additions & 25 deletions cmd/btfs/util/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,12 @@ const (
mnemonicLength = 12
)

func GenerateKey( /*importKey string, */ keyType string, seedPhrase string) (string, string, error) {
func GenerateKey(importKey string, keyType string, seedPhrase string) (string, string, error) {
mnemonicLen := len(strings.Split(seedPhrase, ","))
mnemonic := strings.ReplaceAll(seedPhrase, ",", " ")
/*
if importKey != "" && keyType != "" && strings.ToLower(keyType) != "secp256k1" {
return "", "", fmt.Errorf("cannot specify key type and import TRON private key at the same time")
} else if seedPhrase != "" {
if mnemonicLen != mnemonicLength {
return "", "", fmt.Errorf("The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has %v words.", mnemonicLen)
}
if err := !bip39.IsMnemonicValid(mnemonic); err {
return "", "", fmt.Errorf("Entered seed phrase is not valid")
}
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39(mnemonic)
} else if (keyType == "" && importKey == "") || keyType == "BIP39" {
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39("")
} else {
return importKey, mnemonic, nil
}
*/

if seedPhrase != "" {
if importKey != "" && keyType != "" && strings.ToLower(keyType) != "secp256k1" {
return "", "", fmt.Errorf("cannot specify key type and import TRON private key at the same time")
} else if seedPhrase != "" {
if mnemonicLen != mnemonicLength {
return "", "", fmt.Errorf("The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has %v words.", mnemonicLen)
}
Expand All @@ -47,11 +29,13 @@ func GenerateKey( /*importKey string, */ keyType string, seedPhrase string) (str
}
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39(mnemonic)
} else if (keyType == "" && importKey == "") || keyType == "BIP39" {
fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39("")
} else {
return importKey, mnemonic, nil
}

fmt.Println("Generating TRON key with BIP39 seed phrase...")
return GeneratePrivKeyUsingBIP39("")

}

func GeneratePrivKeyUsingBIP39(mnemonic string) (string, string, error) {
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ require (
github.com/jbenet/go-temp-err-catcher v0.1.0
github.com/jbenet/goprocess v0.1.4
github.com/klauspost/reedsolomon v1.9.9
github.com/libp2p/go-libp2p v0.14.3
github.com/libp2p/go-libp2p v0.11.0
github.com/libp2p/go-libp2p-circuit v0.4.0
github.com/libp2p/go-libp2p-connmgr v0.2.4
github.com/libp2p/go-libp2p-core v0.9.0
Expand Down Expand Up @@ -164,4 +164,16 @@ replace github.com/ipld/go-car => github.com/TRON-US/go-car v0.3.0

replace github.com/ipld/go-ipld-prime-proto => github.com/TRON-US/go-ipld-prime-proto v0.1.0

replace github.com/libp2p/go-libp2p-yamux => github.com/libp2p/go-libp2p-yamux v0.2.8

replace github.com/libp2p/go-libp2p-swarm => github.com/libp2p/go-libp2p-swarm v0.2.8

replace github.com/libp2p/go-libp2p-mplex => github.com/libp2p/go-libp2p-mplex v0.2.4

replace github.com/libp2p/go-libp2p => github.com/libp2p/go-libp2p v0.11.0

replace github.com/libp2p/go-libp2p-circuit => github.com/libp2p/go-libp2p-circuit v0.3.1

replace github.com/libp2p/go-libp2p-quic-transport => github.com/libp2p/go-libp2p-quic-transport v0.8.0
go 1.14

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ github.com/libp2p/go-nat v0.0.5/go.mod h1:B7NxsVNPZmRLvMOwiEO1scOSyjA56zxYAGv1yQ
github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk=
github.com/libp2p/go-netroute v0.1.3 h1:1ngWRx61us/EpaKkdqkMjKk/ufr/JlIFYQAxV2XX8Ig=
github.com/libp2p/go-netroute v0.1.3/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk=
github.com/libp2p/go-netroute v0.1.5 h1:Qj8GE/6Cxmw9aQUYTA9BrS+TELTqU7kKKspWBR4ieVw=
github.com/libp2p/go-netroute v0.1.5/go.mod h1:V1SR3AaECRkEQCoFFzYwVYWvYIEtlxx89+O3qcpCl4A=
github.com/libp2p/go-netroute v0.1.6 h1:ruPJStbYyXVYGQ81uzEDzuvbYRLKRrLvTYd33yomC38=
github.com/libp2p/go-netroute v0.1.6/go.mod h1:AqhkMh0VuWmfgtxKPp3Oc1LdU5QSWS7wl0QLhSZqXxQ=
Expand All @@ -1182,6 +1183,7 @@ github.com/libp2p/go-reuseport-transport v0.0.4 h1:OZGz0RB620QDGpv300n1zaOcKGGAo
github.com/libp2p/go-reuseport-transport v0.0.4/go.mod h1:trPa7r/7TJK/d+0hdBLOCGvpQQVOU74OXbNCIMkufGw=
github.com/libp2p/go-sockaddr v0.0.2 h1:tCuXfpA9rq7llM/v834RKc/Xvovy/AqM9kHvTV/jY/Q=
github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k=
github.com/libp2p/go-sockaddr v0.1.0 h1:Y4s3/jNoryVRKEBrkJ576F17CPOaMIzUeCsg7dlTDj0=
github.com/libp2p/go-sockaddr v0.1.0/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k=
github.com/libp2p/go-sockaddr v0.1.1 h1:yD80l2ZOdGksnOyHrhxDdTDFrf7Oy+v3FMVArIRgZxQ=
github.com/libp2p/go-sockaddr v0.1.1/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k=
Expand Down
Loading

0 comments on commit edebc13

Please sign in to comment.