当前位置: 首页>>代码示例>>Golang>>正文


Golang Ethereum.BlockChain方法代码示例

本文整理汇总了Golang中github.com/ethereum/go-ethereum/eth.Ethereum.BlockChain方法的典型用法代码示例。如果您正苦于以下问题:Golang Ethereum.BlockChain方法的具体用法?Golang Ethereum.BlockChain怎么用?Golang Ethereum.BlockChain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/ethereum/go-ethereum/eth.Ethereum的用法示例。


在下文中一共展示了Ethereum.BlockChain方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: New

// New creates a new Ether APIs instance, connects with it to the Ethereum network
// via an embedded Geth instance and attaches an RPC in-process endpoint to it.
func New(datadir string, network geth.EthereumNetwork, address common.Address) (*EtherAPIs, error) {
	// Create a Geth instance and boot it up
	client, err := geth.New(datadir, network)
	if err != nil {
		return nil, err
	}
	if err := client.Start(); err != nil {
		return nil, err
	}
	// Retrieve the underlying ethereum service and attach global RPC interface
	var ethereum *eth.Ethereum
	if err := client.Stack().Service(&ethereum); err != nil {
		return nil, err
	}
	api, err := client.Attach()
	if err != nil {
		return nil, err
	}
	// Assemble an interface around the consensus contract
	contract, err := contract.New(ethereum.ChainDb(), ethereum.EventMux(), ethereum.BlockChain(), ethereum.Miner().PendingState)
	if err != nil {
		return nil, err
	}
	// Assemble and return the Ether APIs instance
	return &EtherAPIs{
		client:   client,
		ethereum: ethereum,
		eventmux: client.Stack().EventMux(),
		rpcapi:   api,
		contract: contract,
	}, nil
}
开发者ID:karalabe,项目名称:etherapis,代码行数:34,代码来源:etherapis.go

示例2: TestBlockChain

func TestBlockChain(t *testing.T) {
	tmp, repl, node := testJEthRE(t)
	defer node.Stop()
	defer os.RemoveAll(tmp)
	// get current block dump before export/import.
	val, err := repl.re.Run("JSON.stringify(debug.dumpBlock(eth.blockNumber))")
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
	beforeExport := val.String()

	// do the export
	extmp, err := ioutil.TempDir("", "geth-test-export")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(extmp)
	tmpfile := filepath.Join(extmp, "export.chain")
	tmpfileq := strconv.Quote(tmpfile)

	var ethereum *eth.Ethereum
	node.Service(&ethereum)
	ethereum.BlockChain().Reset()

	checkEvalJSON(t, repl, `admin.exportChain(`+tmpfileq+`)`, `true`)
	if _, err := os.Stat(tmpfile); err != nil {
		t.Fatal(err)
	}

	// check import, verify that dumpBlock gives the same result.
	checkEvalJSON(t, repl, `admin.importChain(`+tmpfileq+`)`, `true`)
	checkEvalJSON(t, repl, `debug.dumpBlock(eth.blockNumber)`, beforeExport)
}
开发者ID:Xiaoyang-Zhu,项目名称:go-ethereum,代码行数:33,代码来源:js_test.go

示例3: processTxs

func processTxs(repl *testjethre, t *testing.T, expTxc int) bool {
	var txc int64
	var err error
	for i := 0; i < 50; i++ {
		txc, err = pendingTransactions(repl, t)
		if err != nil {
			t.Errorf("unexpected error checking pending transactions: %v", err)
			return false
		}
		if expTxc < int(txc) {
			t.Errorf("too many pending transactions: expected %v, got %v", expTxc, txc)
			return false
		} else if expTxc == int(txc) {
			break
		}
		time.Sleep(100 * time.Millisecond)
	}
	if int(txc) != expTxc {
		t.Errorf("incorrect number of pending transactions, expected %v, got %v", expTxc, txc)
		return false
	}
	var ethereum *eth.Ethereum
	repl.stack.Service(&ethereum)

	err = ethereum.StartMining(runtime.NumCPU(), "")
	if err != nil {
		t.Errorf("unexpected error mining: %v", err)
		return false
	}
	defer ethereum.StopMining()

	timer := time.NewTimer(100 * time.Second)
	blockNr := ethereum.BlockChain().CurrentBlock().Number()
	height := new(big.Int).Add(blockNr, big.NewInt(1))
	repl.wait <- height
	select {
	case <-timer.C:
		// if times out make sure the xeth loop does not block
		go func() {
			select {
			case repl.wait <- nil:
			case <-repl.wait:
			}
		}()
	case <-repl.wait:
	}
	txc, err = pendingTransactions(repl, t)
	if err != nil {
		t.Errorf("unexpected error checking pending transactions: %v", err)
		return false
	}
	if txc != 0 {
		t.Errorf("%d trasactions were not mined", txc)
		return false
	}
	return true
}
开发者ID:Xiaoyang-Zhu,项目名称:go-ethereum,代码行数:57,代码来源:js_test.go

示例4: RunTest

// RunTest executes the specified test against an already pre-configured protocol
// stack to ensure basic checks pass before running RPC tests.
func RunTest(stack *node.Node, test *tests.BlockTest) error {
	var ethereum *eth.Ethereum
	stack.Service(&ethereum)
	blockchain := ethereum.BlockChain()

	// Process the blocks and verify the imported headers
	blocks, err := test.TryBlocksInsert(blockchain)
	if err != nil {
		return err
	}
	if err := test.ValidateImportedHeaders(blockchain, blocks); err != nil {
		return err
	}
	// Retrieve the assembled state and validate it
	stateDb, err := blockchain.State()
	if err != nil {
		return err
	}
	if err := test.ValidatePostState(stateDb); err != nil {
		return err
	}
	return nil
}
开发者ID:Raskal8,项目名称:go-ethereum,代码行数:25,代码来源:main.go


注:本文中的github.com/ethereum/go-ethereum/eth.Ethereum.BlockChain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。