# ETH源码学习(3)getBlock **Published by:** [point](https://paragraph.com/@point/) **Published on:** 2022-05-12 **URL:** https://paragraph.com/@point/eth-3-getblock ## Content 通过上篇getBalance和这个getBlock,理论上eth也能变成一个中心化的服务,因为用户和交易最终也都是保存到了数据库,那就和我们平时开发的项目没什么区别。这还蛮有意思的,eth实际上是区块链+传统项目的结合体。源码入口//命令 eth.getBlock(1,true) //跳过了前面几层 func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { //用block number换block的hash hash := rawdb.ReadCanonicalHash(bc.db, number) if hash == (common.Hash{}) { return nil } //根据hash和number获取block return bc.GetBlock(hash, number) } //获取hash func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash { //这个还没弄明白 data, _ := db.Ancient(freezerHashTable, number) if len(data) == 0 { //从db中读 data, _ = db.Get(headerHashKey(number)) if len(data) == 0 { data, _ = db.Ancient(freezerHashTable, number) } } if len(data) == 0 { return common.Hash{} } return common.BytesToHash(data) } //获取区块 func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { //从缓存中读,这里用到了lur if block, ok := bc.blockCache.Get(hash); ok { return block.(*types.Block) } //从db中读,内容和getbalance一样,里面是先获取header,再获取body,然后创建区块,将header和body copy到block返回 block := rawdb.ReadBlock(bc.db, hash, number) if block == nil { return nil } //缓存区块 bc.blockCache.Add(block.Hash(), block) return block } 两篇get方法都是大同小异,进一步了解到eth是可以很简单的实现的,那些很牛逼的东西,都是为了满足区块链、性能这些需求。 另外这篇内容的主要代码都在blockchain内,又一次出现了这个,说明很重要,而statedb却没有出现,目前可以猜测statedb和账户有关,和block无关,需要后续验证。 也能知道leveldb里大致存的内容用户数据headernumber→hashbody,body里存了tx,uncle可以想象还有receipt//block的例子 { difficulty: 1461115576915, extraData: "0x476574682f76312e302e312f6c696e75782f676f312e342e32", gasLimit: 22150, gasUsed: 21000, hash: "0xeecb25ce31fb7a212500443a31d463b663a2c5f65e7838c5364bb8110a0750f1", logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", miner: "0x47ff6576639c2e94762ea5443978d7681c0e78dc", mixHash: "0xaaad7bf5c87f1b2fc08ed73a1519dae6832b458a112891cd7c5a0a86125a8321", nonce: "0x4e164418632effd2", number: 46251, parentHash: "0x3c93a7ff6fb6f39e63456e9ea6fb069c7783f3c00b1012e36f0a8a02bdae604a", receiptsRoot: "0x5add83c7805c02ab079871e3a2379b6a764d34e557d758ddce00b6a1bf5cc79f", sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", size: 652, stateRoot: "0x7692ecfeac33453620701bad9eae23f89c01e845274f0e5adec78a5ea96f5083", timestamp: 1438919990, totalDifficulty: 42835839136947252, transactions: [{ blockHash: "0xeecb25ce31fb7a212500443a31d463b663a2c5f65e7838c5364bb8110a0750f1", blockNumber: 46251, from: "0xfd2605a2bf58fdbb90db1da55df61628b47f9e8c", gas: 21000, gasPrice: 88893746891, hash: "0x304346de678ddca5b1311970e7e8d3f26a42d2924aaa2f2d73b8781a9a9a2c73", input: "0x", nonce: 1, r: "0x26f3e5c541f526b549d9f003c8e7de442d3cf4bbf9c66934027672693a88275f", s: "0x7bd40d54be85b58444d6f0a28f22591474a84380fc8a6a7515bda42b5ebb0e2b", to: "0x073f70b5bfade6409e4951ef72bc8f4157677729", transactionIndex: 0, v: "0x1c", value: 10000000000000000 }], transactionsRoot: "0xf642520ac0ce7008292d2c835b8d2e6eb90822f8a68b8ae368b0774229371b5d", uncles: [] } ## Publication Information - [point](https://paragraph.com/@point/): Publication homepage - [All Posts](https://paragraph.com/@point/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@point): Subscribe to updates