Dapp-Development

獲取事件歷史記錄需要很長時間或根本不起作用

  • October 23, 2016

我在獲取事件歷史記錄時遇到了麻煩。我不確定我是否誤解了事件是如何工作的,我是否編碼錯誤,或者它是否與最近的乙太坊攻擊減慢了網路速度有關。我的工作流程是我部署了一個名為“KnowledgeExchangeManager”的契約,當您送出問題時,它又會創建“KnowledgeExchange”契約。創建 KnowledgeExchange 契約時,它還會創建一個 KnowledgeExchangeEvent,以便我以後可以查找已生成的契約。

契約:

pragma solidity ^0.4.2;

contract KnowledgeExchangeManager {

   address owner;

   event KnowledgeExchangeEvent(
       address indexed _from,
       address _exchange
   );

   function KnowledgeExchangeManager() {

       owner = msg.sender;

   }

   function submitQuestion(string _title, string _body) payable {

       KnowledgeExchange exchangeContract = new KnowledgeExchange(msg.sender, _title, _body);
       KnowledgeExchangeEvent(msg.sender, exchangeContract);

   }

   function kill() { 
       if (msg.sender == owner) selfdestruct(owner); 
   }

}

contract KnowledgeExchange {

   event BountyClaimed(
       address indexed _owner
   );

   struct Question {
       address owner;
       string title;
       string body;
   }

   struct Answer {
       address owner;
       string body;
   }

   Question question;
   Answer answer;
   bool public answered;

   function KnowledgeExchange(address questioner, string _title, string _body) payable {

       question = Question(questioner, _title, _body);
       answered = false;

   }

   function getQuestionDetails() returns (address, string, string, uint) {

       return (question.owner, question.title, question.body, this.balance);
   } 

   function submitAnswer(string _body) {

       bool success = msg.sender.send(this.balance);
       if (!success) {
           throw;
       }
       answered = true;
       answer = Answer(msg.sender, _body);
       BountyClaimed(msg.sender);

   }

   function getAnswerDetails() returns (address, string) {

       return (answer.owner, answer.body);
   }

   function getBounty() returns (uint256) {
       return this.balance;
   } 

   function kill() { 
       if (msg.sender == question.owner) selfdestruct(question.owner); 
   }

}

Javascript 使用者界面:

var accounts;
var account;
var knowledgeManager;

function refreshQuestions() {

console.log("Refreshing Questions...");
  var exchangeEvent = knowledgeManager.KnowledgeExchangeEvent({fromBlock: 0, toBlock: 'latest'});
  exchangeEvent.watch(function(error, events) {
    console.log("Got Questions."); 
    if (!error) {
      console.log(events);
    } else {
      console.log(error);
    }

    exchangeEvent.stopWatching();
  });

};

function submitQuestion() {

 knowledgeManager.submitQuestion("Test Question", "Lorem ipsum dolor amet", {from: account, gas: 900000}, function(error, txId) {
   if (error) {
     console.log(error);
   } else {
     console.log(txId);
   }
 });

};

window.onload = function() {

 console.log("Loading App");

 web3.eth.getAccounts(function(err, accs) {
   if (err != null) {
     alert("There was an error fetching your accounts.");
     return;
   }

   if (accs.length == 0) {
     alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
     return;
   }

   accounts = accs;
   account = accounts[0];
   knowledgeManager = web3.eth.contract(JSON.parse('[ { "constant": false, "inputs": [ { "name": "_title", "type": "string" }, { "name": "_body", "type": "string" } ], "name": "submitQuestion", "outputs": [], "payable": true, "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "type": "function" }, { "inputs": [], "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "_from", "type": "address" }, { "indexed": false, "name": "_exchange", "type": "address" } ], "name": "KnowledgeExchangeEvent", "type": "event" } ]'))
           .at("0xb52e407F7Feb3C2D14b1721584dd3ea34166C716");

   refreshQuestions();
 });


}

我曾假設 javascript 應該在頁面載入時獲取所有事件,但它很少這樣做。如果它確實有效,它將在相當長的一段時間後(5 分鐘)。我已經在執行 Metamask 的 Chrome 和 Mist 瀏覽器中測試了我的應用程序。我正在使用現代測試網路。

嘗試添加{}之前{fromBlock...}

var exchangeEvent = knowledgeManager.KnowledgeExchangeEvent({}, {fromBlock: 0, toBlock: 'latest'});

請參閱如何從 The DAO 檢索已投票的事件以{}獲取更多詳細資訊,包括在此參數中添加更多過濾器。

引用自:https://ethereum.stackexchange.com/questions/9532