本文整理匯總了Golang中github.com/NebulousLabs/Sia/modules/consensus.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestExplorerGenesisHeight
// TestExplorerGenesisHeight checks that when the explorer is initialized and given the
// genesis block, the result has the correct height.
func TestExplorerGenesisHeight(t *testing.T) {
// Create the dependencies.
testdir := build.TempDir(modules.HostDir, "TestExplorerGenesisHeight")
g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir))
if err != nil {
t.Fatal(err)
}
cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
t.Fatal(err)
}
// Create the explorer - from the subscription only the genesis block will
// be received.
e, err := New(cs, testdir)
if err != nil {
t.Fatal(err)
}
block, height, exists := e.Block(types.GenesisID)
if !exists {
t.Error("explorer missing genesis block after initialization")
}
if block.ID() != types.GenesisID {
t.Error("explorer returned wrong genesis block")
}
if height != 0 {
t.Errorf("genesis block hash wrong height: expected 0, got %v", height)
}
}
示例2: 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
}
示例3: 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
}
示例4: createMinerTester
// createMinerTester creates a minerTester that's ready for use.
func createMinerTester(name string) (*minerTester, error) {
testdir := build.TempDir(modules.MinerDir, name)
// Create the modules.
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
}
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 := New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble the minerTester.
mt := &minerTester{
gateway: g,
cs: cs,
tpool: tp,
wallet: w,
walletKey: key,
miner: m,
}
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := m.FindBlock()
err = cs.AcceptBlock(b)
if err != nil {
return nil, err
}
}
return mt, nil
}
示例5: TestIntegrationWalletBlankEncrypt
// TestIntegrationWalletBlankEncrypt tries to encrypt and unlock the wallet
// through the api using a blank encryption key - meaning that the wallet seed
// returned by the encryption call can be used as the encryption key.
func TestIntegrationWalletBlankEncrypt(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// Create a server object without encrypting or unlocking the wallet.
testdir := build.TempDir("api", "TestIntegrationWalletBlankEncrypt")
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)
}
srv, err := NewServer(":0", cs, g, nil, nil, nil, nil, tp, w, nil)
if err != nil {
t.Fatal(err)
}
// Assemble the serverTester.
st := &serverTester{
cs: cs,
gateway: g,
tpool: tp,
wallet: w,
server: srv,
}
go func() {
listenErr := srv.Serve()
if listenErr != nil {
panic(listenErr)
}
}()
// Make a call to /wallet/encrypt and get the seed. Provide no encryption
// key so that the encryption key is the seed that gets returned.
var wep WalletEncryptPOST
err = st.postAPI("/wallet/encrypt", url.Values{}, &wep)
if err != nil {
t.Fatal(err)
}
// Use the seed to call /wallet/unlock.
unlockValues := url.Values{}
unlockValues.Set("encryptionpassword", wep.PrimarySeed)
err = st.stdPostAPI("/wallet/unlock", unlockValues)
if err != nil {
t.Fatal(err)
}
// Check that the wallet actually unlocked.
if !w.Unlocked() {
t.Error("wallet is not unlocked")
}
}
示例6: 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
}
示例7: TestIntegrationNewNilInputs
// TestIntegrationNewNilInputs tries to trigger a panic with nil inputs.
func TestIntegrationNewNilInputs(t *testing.T) {
// Create a gateway and consensus set.
testdir := build.TempDir(modules.TransactionPoolDir, "TestNewNilInputs")
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)
}
// Try all combinations of nil inputs.
_, err = New(nil, nil)
if err == nil {
t.Error(err)
}
_, err = New(nil, g)
if err != errNilCS {
t.Error(err)
}
_, err = New(cs, nil)
if err != errNilGateway {
t.Error(err)
}
_, err = New(cs, g)
if err != nil {
t.Error(err)
}
}
示例8: TestNilInputs
// TestNilInputs tries starting the wallet using nil inputs.
func TestNilInputs(t *testing.T) {
testdir := build.TempDir(modules.WalletDir, "TestNilInputs")
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)
}
wdir := filepath.Join(testdir, modules.WalletDir)
_, err = New(cs, nil, wdir)
if err != errNilTpool {
t.Error(err)
}
_, err = New(nil, tp, wdir)
if err != errNilConsensusSet {
t.Error(err)
}
_, err = New(nil, nil, wdir)
if err != errNilConsensusSet {
t.Error(err)
}
}
示例9: 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
}
示例10: TestCloseWallet
// TestCloseWallet tries to close the wallet.
func TestCloseWallet(t *testing.T) {
if testing.Short() {
t.Skip()
}
testdir := build.TempDir(modules.WalletDir, "TestCloseWallet")
g, err := gateway.New("localhost: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, filepath.Join(testdir, modules.TransactionPoolDir))
if err != nil {
t.Fatal(err)
}
wdir := filepath.Join(testdir, modules.WalletDir)
w, err := New(cs, tp, wdir)
if err != nil {
t.Fatal(err)
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
}
示例11: 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
}
示例12: TestIntegrationWalletGETEncrypted
// TestIntegrationWalletGETEncrypted probes the GET call to /wallet when the
// wallet has never been encrypted.
func TestIntegrationWalletGETEncrypted(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// Check a wallet that has never been encrypted.
testdir := build.TempDir("api", "TestIntegrationWalletGETEncrypted")
g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir))
if err != nil {
t.Fatal("Failed to create gateway:", err)
}
cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
if err != nil {
t.Fatal("Failed to create consensus set:", err)
}
tp, err := transactionpool.New(cs, g)
if err != nil {
t.Fatal("Failed to create tpool:", err)
}
w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
if err != nil {
t.Fatal("Failed to create wallet:", err)
}
srv, err := NewServer(":0", "Sia-Agent", cs, nil, g, nil, nil, nil, tp, w)
if err != nil {
t.Fatal(err)
}
// Assemble the serverTester and start listening for api requests.
st := &serverTester{
cs: cs,
gateway: g,
tpool: tp,
wallet: w,
server: srv,
}
go func() {
listenErr := srv.Serve()
if listenErr != nil {
t.Fatal("API server quit:", listenErr)
}
}()
defer st.server.Close()
var wg WalletGET
err = st.getAPI("/wallet", &wg)
if err != nil {
t.Fatal(err)
}
if wg.Encrypted {
t.Error("Wallet has never been unlocked")
}
if wg.Unlocked {
t.Error("Wallet has never been unlocked")
}
}
示例13: createMinerTester
// createMinerTester creates a minerTester that's ready for use.
func createMinerTester(name string) (*minerTester, error) {
testdir := build.TempDir(modules.MinerDir, name)
// Create the modules.
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
}
m, err := New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
if err != nil {
return nil, err
}
// Assemble the minerTester.
mt := &minerTester{
gateway: g,
cs: cs,
tpool: tp,
wallet: w,
miner: m,
csUpdateChan: cs.ConsensusSetNotify(),
minerUpdateChan: m.MinerNotify(),
tpoolUpdateChan: tp.TransactionPoolNotify(),
walletUpdateChan: w.WalletNotify(),
}
mt.csUpdateWait()
// Mine until the wallet has money.
for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
b, _ := m.FindBlock()
err = cs.AcceptBlock(b)
if err != nil {
return nil, err
}
mt.csUpdateWait()
}
return mt, nil
}
示例14: 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
}
示例15: 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
}