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

Blockchain 如何使用web3获取以太坊块的数据,blockchain,ethereum,web3,Blockchain,Ethereum,Web3,这是我的合同: pragma solidity ^0.4.18; contract Signup { string fStudentId; string fLocation; function setInfo(string _fStudentId, string _fLocation) public { fStudentId = _fStudentId; fLocation = _fLocation; } function getIn

这是我的合同:

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);
   }
}
我把studentId和location放在block中,如何使用web3获取这些信息? 我尝试使用:
web3.eth.getBlock(7).then(console.log)

但是你可以得到:

我的学生ID和位置在哪里?
谢谢

我不知道你说“我把studentId和location放在块中”是什么意思,但我会尽力回答

您可能通过执行以下操作来设置状态

contract.functions.setInfo(1,“此处”)

要检索该数据,您将执行以下操作

contract.functions.getInfo()


以太坊区块链的实际状态由merkle trie组成,并由
stateRoot
引用。因此,正如您所看到的,没有一种简单的方法可以根据您拥有的块头来查找这些数据

要从合同中检索数据,您有几个选项:

  • 使用我上面描述的getInfo方法
  • 查找执行
    setInfo
    的交易,并解析交易凭证中的输入
  • 每当调用
    setInfo
    时,从合同中发出事件,并为过滤器设置侦听器,该过滤器将在发出合同事件的事务发生时调用
  • 运行一个节点并从块中获取原始数据,而不仅仅是块头

  • 要获得这些值,您应该使用web3的契约方法。