简体   繁体   English

如何使用 web3 获取以太坊区块的数据

[英]How to get the data of an ethereum block using web3

This is my contract:这是我的合同:

pragma solidity ^0.4.18;
contract Signup {
   string fStudentId;
   string fLocation;

   function setInfo(string _fStudentId, string _fLocation) public  {
       fStudentId = _fStudentId;
       fLocation = _fLocation;
   }
   function getInfo() public constant returns (string, string)    {
       return (fStudentId, fLocation);
   }
}

I put studentId & location in to block, and how do I get this info by using web3?我将 studentId 和 location 放入阻止,如何使用 web3 获取此信息? I try to use: web3.eth.getBlock(7).then(console.log);我尝试使用: web3.eth.getBlock(7).then(console.log);

but get:但得到: 在此处输入图像描述

Where is my studentId & location?我的学生 ID 和位置在哪里? thanks!!!!!谢谢!!!!!

Not sure what you mean when you say "I put studentId & location in to block" but I will try to answer.当您说“我将 studentId 和位置放入阻止”时,不确定您的意思,但我会尝试回答。

You are probably setting state by doing something like您可能正在通过执行类似的操作来设置 state

contract.functions.setInfo(1, "here")

To retrieve that data you will do要检索该数据,您将执行

contract.functions.getInfo()


The actual state of the ethereum blockchain is composed into a merkle trie and referenced by stateRoot .以太坊区块链的实际 state 组成一个 merkle trie 并由stateRoot引用。 So as you can see, there is not an easy way to find this data based on the block header you have.如您所见,根据您拥有的块 header 找到这些数据并不容易。

To retrieve the data from your contract you have a few options:要从合同中检索数据,您有几个选择:

  1. Use your getInfo method as I described above如上所述使用您的 getInfo 方法
  2. Look up the transaction where you execute setInfo and parse the inputs from the transaction receipt查找执行setInfo的交易并解析交易收据中的输入
  3. Emit an event from your contract whenever setInfo is called and set up a listener for a filter that will be called any time a transaction that emits your contract's event occurs每当setInfo时从您的合约中发出一个事件,并为一个过滤器设置一个侦听器,该过滤器将在任何时候发出您的合约事件的事务发生时调用
  4. Run a node and get the raw data from the block instead of just the block header运行一个节点并从块中获取原始数据,而不仅仅是块 header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM