本文整理匯總了Golang中github.com/NebulousLabs/Sia/modules/miner.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createWalletTester
// createWalletTester takes a testing.T and creates a WalletTester.
func createWalletTester(name string) (*walletTester, error) {
// Create the modules
testdir := build.TempDir(modules.WalletDir, name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
var masterKey crypto.TwofishKey
_, err = rand.Read(masterKey[:])
if err != nil {
return nil, err
}
_, err = w.Encrypt(masterKey)
if err != nil {
return nil, err
}
err = w.Unlock(masterKey)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
// Assemble all componenets into a wallet tester.
wt := &walletTester{
cs: cs,
gateway: g,
tpool: tp,
miner: m,
wallet: w,
walletMasterKey: masterKey,
persistDir: testdir,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := wt.miner.FindBlock()
err := wt.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return wt, nil
}
示例2: newHostDBTester
// newHostDBTester creates a ready-to-use hostdb tester with money in the
// wallet.
func newHostDBTester(name string) (*hostdbTester, error) {
// Create the modules.
testdir := build.TempDir("hostdb", name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := modWallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
hdb, err := New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all pieces into a hostdb tester.
ht := &hostdbTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
hostdb: hdb,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
_, err := ht.miner.AddBlock()
if err != nil {
return nil, err
}
}
return ht, nil
}
示例3: createConsensusSetTester
// createConsensusSetTester creates a consensusSetTester that's ready for use.
func createConsensusSetTester(name string) (*consensusSetTester, error) {
testdir := build.TempDir(modules.ConsensusDir, name)
// Create modules.
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all objects into a consensusSetTester.
cst := &consensusSetTester{
gateway: g,
miner: m,
tpool: tp,
wallet: w,
walletKey: key,
cs: cs,
persistDir: testdir,
}
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := cst.miner.FindBlock()
err = cst.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return cst, nil
}
示例4: createTpoolTester
// createTpoolTester returns a ready-to-use tpool tester, with all modules
// initialized.
func createTpoolTester(name string) (*tpoolTester, error) {
// Initialize the modules.
testdir := build.TempDir(modules.TransactionPoolDir, name)
g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
var key crypto.TwofishKey
_, err = rand.Read(key[:])
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all of the objects into a tpoolTester
tpt := &tpoolTester{
cs: cs,
gateway: g,
tpool: tp,
miner: m,
wallet: w,
walletKey: key,
persistDir: testdir,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := tpt.miner.FindBlock()
err = tpt.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return tpt, nil
}
示例5: newRenterTester
// newRenterTester creates a ready-to-use renter tester with money in the
// wallet.
func newRenterTester(name string) (*renterTester, error) {
// Create the modules.
testdir := build.TempDir("renter", name)
g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
r, err := New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all pieces into a renter tester.
rt := &renterTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
renter: r,
}
// Mine blocks until there is money in the wallet.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
_, err := rt.miner.AddBlock()
if err != nil {
return nil, err
}
}
return rt, nil
}
示例6: newTestingWallet
// newTestingWallet is a helper function that creates a ready-to-use wallet
// and mines some coins into it.
func newTestingWallet(testdir string, cs modules.ConsensusSet, tp modules.TransactionPool) (modules.Wallet, error) {
w, err := modWallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
if !w.Encrypted() {
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
// give it some money
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
_, err := m.AddBlock()
if err != nil {
return nil, err
}
}
return w, nil
}
示例7: createExplorerTester
// createExplorerTester creates a tester object for the explorer module.
func createExplorerTester(name string) (*explorerTester, error) {
// Create and assemble the dependencies.
testdir := build.TempDir(modules.HostDir, name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
e, err := New(cs, filepath.Join(testdir, modules.ExplorerDir))
if err != nil {
return nil, err
}
et := &explorerTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
walletKey: key,
explorer: e,
}
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := et.miner.FindBlock()
err = et.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return et, nil
}
示例8: createConsensusSetTester
// createConsensusSetTester creates a consensusSetTester that's ready for use.
func createConsensusSetTester(name string) (*consensusSetTester, error) {
testdir := build.TempDir(modules.ConsensusDir, name)
// Create modules.
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all objects into a consensusSetTester.
cst := &consensusSetTester{
gateway: g,
miner: m,
tpool: tp,
wallet: w,
cs: cs,
persistDir: testdir,
csUpdateChan: cs.ConsensusSetNotify(),
minerUpdateChan: m.MinerNotify(),
tpoolUpdateChan: tp.TransactionPoolNotify(),
walletUpdateChan: w.WalletNotify(),
}
cst.csUpdateWait()
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := cst.miner.FindBlock()
err = cst.cs.AcceptBlock(b)
if err != nil {
return nil, err
}
cst.csUpdateWait()
}
return cst, nil
}
示例9: createConsensusSetTester
// createConsensusSetTester creates a consensusSetTester that's ready for use.
func createConsensusSetTester(name string) (*consensusSetTester, error) {
testdir := build.TempDir(modules.ConsensusDir, name)
// Create modules.
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, err
}
_, err = w.Encrypt(key)
if err != nil {
return nil, err
}
err = w.Unlock(key)
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble all objects into a consensusSetTester.
cst := &consensusSetTester{
gateway: g,
miner: m,
tpool: tp,
wallet: w,
walletKey: key,
cs: cs,
persistDir: testdir,
}
cst.addSiafunds()
cst.mineSiacoins()
return cst, nil
}
示例10: blankHostTester
// blankHostTester creates a host tester where the modules are created but no
// extra initialization has been done, for example no blocks have been mined
// and the wallet keys have not been created.
func blankHostTester(name string) (*hostTester, error) {
testdir := build.TempDir(modules.HostDir, name)
// Create the modules.
g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
return nil, err
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
h, err := New(cs, tp, w, "localhost:0", filepath.Join(testdir, modules.HostDir))
if err != nil {
return nil, err
}
/*
r, err := renter.New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
if err != nil {
return nil, err
}
*/
// Assemble all objects into a hostTester
ht := &hostTester{
cs: cs,
gateway: g,
miner: m,
// renter: r,
tpool: tp,
wallet: w,
host: h,
persistDir: testdir,
}
return ht, nil
}
示例11: reorgToBlank
// reorgToBlank creates a bunch of empty blocks on top of the genesis block
// that reorgs the explorer to a state of all blank blocks.
func (et *explorerTester) reorgToBlank() error {
// Get a unique directory name to house the persistence of the miner
// dependencies.
dir := et.testdir + " - " + persist.RandomSuffix()
// Create a miner and all dependencies to create an alternate chain.
g, err := gateway.New("localhost:0", false, filepath.Join(dir, modules.GatewayDir))
if err != nil {
return err
}
cs, err := consensus.New(g, false, filepath.Join(dir, modules.ConsensusDir))
if err != nil {
return err
}
tp, err := transactionpool.New(cs, g, filepath.Join(dir, modules.TransactionPoolDir))
if err != nil {
return err
}
w, err := wallet.New(cs, tp, filepath.Join(dir, modules.WalletDir))
if err != nil {
return err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return err
}
_, err = w.Encrypt(key)
if err != nil {
return err
}
err = w.Unlock(key)
if err != nil {
return err
}
m, err := miner.New(cs, tp, w, filepath.Join(dir, modules.RenterDir))
if err != nil {
return err
}
// Mine blocks until the height is higher than the existing consensus,
// submitting each block to the explorerTester.
currentHeight := cs.Height()
for i := types.BlockHeight(0); i <= currentHeight+1; i++ {
block, err := m.AddBlock()
if err != nil {
return err
}
et.cs.AcceptBlock(block) // error is not checked, will not always be nil
}
return nil
}
示例12: createExplorerTester
func createExplorerTester(name string, t *testing.T) *explorerTester {
testdir := build.TempDir(modules.HostDir, name)
// Create the modules
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
t.Fatal(err)
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
t.Fatal(err)
}
tp, err := transactionpool.New(cs, g)
if err != nil {
t.Fatal(err)
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
t.Fatal(err)
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.RenterDir))
if err != nil {
t.Fatal(err)
}
be, err := New(cs)
if err != nil {
t.Fatal(err)
}
et := &explorerTester{
cs: cs,
gateway: g,
miner: m,
tpool: tp,
wallet: w,
explorer: be,
csUpdateChan: cs.ConsensusSetNotify(),
beUpdateChan: be.BlockExplorerNotify(),
tpoolUpdateChan: tp.TransactionPoolNotify(),
minerUpdateChan: m.MinerNotify(),
walletUpdateChan: w.WalletNotify(),
t: t,
}
et.csUpdateWait()
return et
}
示例13: createBlankWalletTester
// createBlankWalletTester creates a wallet tester that has not mined any
// blocks or encrypted the wallet.
func createBlankWalletTester(name string) (*walletTester, error) {
// Create the modules
testdir := build.TempDir(modules.WalletDir, name)
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, err
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, err
}
tp, err := transactionpool.New(cs, g)
if err != nil {
return nil, err
}
w, err := New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, err
}
// Assemble all componenets into a wallet tester.
wt := &walletTester{
gateway: g,
cs: cs,
tpool: tp,
miner: m,
wallet: w,
persistDir: testdir,
}
return wt, nil
}
示例14: startDaemon
// startDaemon uses the config parameters to initialize Sia modules and start
// siad.
func startDaemon(config Config) (err error) {
// Prompt user for API password.
if config.Siad.AuthenticateAPI {
config.APIPassword, err = speakeasy.Ask("Enter API password: ")
if err != nil {
return err
}
if config.APIPassword == "" {
return errors.New("password cannot be blank")
}
}
// Process the config variables after they are parsed by cobra.
config, err = processConfig(config)
if err != nil {
return err
}
// Print a startup message.
fmt.Println("Loading...")
loadStart := time.Now()
// Create the server and start serving daemon routes immediately.
fmt.Printf("(0/%d) Loading siad...\n", len(config.Siad.Modules))
srv, err := NewServer(config.Siad.APIaddr, config.Siad.RequiredUserAgent, config.APIPassword)
if err != nil {
return err
}
servErrs := make(chan error)
go func() {
servErrs <- srv.Serve()
}()
// Initialize the Sia modules
i := 0
var g modules.Gateway
if strings.Contains(config.Siad.Modules, "g") {
i++
fmt.Printf("(%d/%d) Loading gateway...\n", i, len(config.Siad.Modules))
g, err = gateway.New(config.Siad.RPCaddr, !config.Siad.NoBootstrap, filepath.Join(config.Siad.SiaDir, modules.GatewayDir))
if err != nil {
return err
}
defer g.Close()
}
var cs modules.ConsensusSet
if strings.Contains(config.Siad.Modules, "c") {
i++
fmt.Printf("(%d/%d) Loading consensus...\n", i, len(config.Siad.Modules))
cs, err = consensus.New(g, !config.Siad.NoBootstrap, filepath.Join(config.Siad.SiaDir, modules.ConsensusDir))
if err != nil {
return err
}
defer cs.Close()
}
var e modules.Explorer
if strings.Contains(config.Siad.Modules, "e") {
i++
fmt.Printf("(%d/%d) Loading explorer...\n", i, len(config.Siad.Modules))
e, err = explorer.New(cs, filepath.Join(config.Siad.SiaDir, modules.ExplorerDir))
if err != nil {
return err
}
defer e.Close()
}
var tpool modules.TransactionPool
if strings.Contains(config.Siad.Modules, "t") {
i++
fmt.Printf("(%d/%d) Loading transaction pool...\n", i, len(config.Siad.Modules))
tpool, err = transactionpool.New(cs, g, filepath.Join(config.Siad.SiaDir, modules.TransactionPoolDir))
if err != nil {
return err
}
defer tpool.Close()
}
var w modules.Wallet
if strings.Contains(config.Siad.Modules, "w") {
i++
fmt.Printf("(%d/%d) Loading wallet...\n", i, len(config.Siad.Modules))
w, err = wallet.New(cs, tpool, filepath.Join(config.Siad.SiaDir, modules.WalletDir))
if err != nil {
return err
}
defer w.Close()
}
var m modules.Miner
if strings.Contains(config.Siad.Modules, "m") {
i++
fmt.Printf("(%d/%d) Loading miner...\n", i, len(config.Siad.Modules))
m, err = miner.New(cs, tpool, w, filepath.Join(config.Siad.SiaDir, modules.MinerDir))
if err != nil {
return err
}
defer m.Close()
}
var h modules.Host
if strings.Contains(config.Siad.Modules, "h") {
//.........這裏部分代碼省略.........
示例15: newTestingTrio
// newTestingTrio creates a Host, Contractor, and TestMiner that can be used
// for testing host/renter interactions.
func newTestingTrio(name string) (modules.Host, *Contractor, modules.TestMiner, error) {
testdir := build.TempDir("contractor", name)
// create miner
g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
if err != nil {
return nil, nil, nil, err
}
cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
return nil, nil, nil, err
}
tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
return nil, nil, nil, err
}
w, err := modWallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
return nil, nil, nil, err
}
key, err := crypto.GenerateTwofishKey()
if err != nil {
return nil, nil, nil, err
}
if !w.Encrypted() {
_, err = w.Encrypt(key)
if err != nil {
return nil, nil, nil, err
}
}
err = w.Unlock(key)
if err != nil {
return nil, nil, nil, err
}
m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, nil, nil, err
}
// create host and contractor, using same consensus set and gateway
h, err := newTestingHost(filepath.Join(testdir, "Host"), cs, tp)
if err != nil {
return nil, nil, nil, err
}
c, err := newTestingContractor(filepath.Join(testdir, "Contractor"), cs, tp)
if err != nil {
return nil, nil, nil, err
}
// announce the host
err = h.Announce()
if err != nil {
return nil, nil, nil, err
}
// mine a block, processing the announcement
m.AddBlock()
// wait for hostdb to scan host
for i := 0; i < 500 && len(c.hdb.RandomHosts(1, nil)) == 0; i++ {
time.Sleep(time.Millisecond)
}
return h, c, m, nil
}