// handleGetBlock implements the getblock command. func handleGetBlock(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { c := cmd.(*btcjson.GetBlockCmd) sha, err := btcwire.NewShaHashFromStr(c.Hash) if err != nil { rpcsLog.Errorf("Error generating sha: %v", err) return nil, btcjson.ErrBlockNotFound } blk, err := s.server.db.FetchBlockBySha(sha) if err != nil { rpcsLog.Errorf("Error fetching sha: %v", err) return nil, btcjson.ErrBlockNotFound } // When the verbose flag isn't set, simply return the network-serialized // block as a hex-encoded string. if !c.Verbose { blkHex, err := messageToHex(blk.MsgBlock()) if err != nil { return nil, err } return blkHex, nil } // The verbose flag is set, so generate the JSON object and return it. buf, err := blk.Bytes() if err != nil { rpcsLog.Errorf("Error fetching block: %v", err) return nil, btcjson.Error{ Code: btcjson.ErrInternal.Code, Message: err.Error(), } } idx := blk.Height() _, maxidx, err := s.server.db.NewestSha() if err != nil { rpcsLog.Errorf("Cannot get newest sha: %v", err) return nil, btcjson.ErrBlockNotFound } blockHeader := &blk.MsgBlock().Header blockReply := btcjson.BlockResult{ Hash: c.Hash, Version: blockHeader.Version, MerkleRoot: blockHeader.MerkleRoot.String(), PreviousHash: blockHeader.PrevBlock.String(), Nonce: blockHeader.Nonce, Time: blockHeader.Timestamp.Unix(), Confirmations: uint64(1 + maxidx - idx), Height: idx, Size: len(buf), Bits: strconv.FormatInt(int64(blockHeader.Bits), 16), Difficulty: getDifficultyRatio(blockHeader.Bits), } if !c.VerboseTx { txList, _ := blk.TxShas() txNames := make([]string, len(txList)) for i, v := range txList { txNames[i] = v.String() } blockReply.Tx = txNames } else { txns := blk.Transactions() rawTxns := make([]btcjson.TxRawResult, len(txns)) for i, tx := range txns { txSha := tx.Sha().String() mtx := tx.MsgTx() rawTxn, err := createTxRawResult(s.server.btcnet, txSha, mtx, blk, maxidx, sha) if err != nil { rpcsLog.Errorf("Cannot create TxRawResult for "+ "transaction %s: %v", txSha, err) return nil, err } rawTxns[i] = *rawTxn } blockReply.RawTx = rawTxns } // Get next block unless we are already at the top. if idx < maxidx { var shaNext *btcwire.ShaHash shaNext, err = s.server.db.FetchBlockShaByHeight(int64(idx + 1)) if err != nil { rpcsLog.Errorf("No next block: %v", err) return nil, btcjson.ErrBlockNotFound } blockReply.NextHash = shaNext.String() } return blockReply, nil }