皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

admin 2022年7月6日09:41:37评论21 views字数 3924阅读13分4秒阅读模式

皮蛋厂的学习日记系列为山东警察学院网安社成员日常学习分享,希望能与大家共同学习、共同进步~

  • 2021级 Mu.Chen |区块链初探

    • 前言

    • 环境搭建

    • 基础知识

    • 刷题


2021级 Mu.Chen |区块链初探

前言

越来越多的ctf比赛在这个方向出题了,认识了个东北林业专区块链的师傅,属实是吓了一跳,想起来之前玩比特币赔了半个月生活费,就想把这个方向给学起来

目前blockchain方面的题目只在eth方面,主要是合约安全

环境搭建

在Microsoft里下载一个叫做matemask的插件

皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

就这个小狐狸

装完了按照教程弄一个钱包,玩过币的应该都懂,那几个英语单词一定要自己记好藏起来

然后显示测试网络

皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

切换到Ropsten测试网络里

环境搭建完成!

测试网络里的币是没有意义的,只能自娱自乐,所以欢迎大家给0x74dfC68e1a005eFfFCbD198EebC1f6844fdec62D这个地址打钱

皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

不过测试网络的交易记录也可以在https://www.etherscan.io/上查到

还有就是,区块链的合约语言需要单独学,有c语言和python的基础应该很快就能上手

基础知识

整个区块链网络架构事实上和暗网的架构差不多,想要连接这个网络有两种形式,一种是自己做一个节点连接上去,另一种就是利用已经接入网络的服务器暴露的RPC,小狐狸就是这样一种轻微节点接入区块链网络的。

根据实际经验,一般跨节点交易是需要手续费的,而在同一个节点内部进行交易,交易内容不会写在链上,所以不用花手续费,可以自己转着玩。

货币

以太坊的货币单位被称为以太(ether,也可以表示为ETH或者符号Ξ

挖矿产出(幽灵协议)

每产生一个新的区块都会有一笔固定的奖励给矿工,这个奖励包含三部分,它们是区块奖励,交易费(也就是手续费)和打包叔伯块的奖励(这一部分比特币中没有)。

区块奖励

每个区块奖励是5以太

blockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block   5ether

拜占庭硬分叉以后,每个区块奖励成了3以太

ByzantiumBlockReward   *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium

由于挖矿需要的算力极高,现在市面上有很多矿池,矿工们可以连进去集合算力一同挖,最后共享奖励,自己挖矿一般情况下是挖不到的

叔伯块(uncle)及叔伯块奖励分发机制

每一个区块最多纳入两个叔伯块。

maxUncles            = 2                 // Maximum number of uncles allowed in a single block

有些区块被挖的稍晚一些,因此不能作为主区块链的组成部分。比特币称这类区块为"孤块",并且完全舍弃它们。但是,以太币称它们为"叔块",并且在这之后的区块中,可以引用它们,但根据以太坊设计指南中的叔伯块激励章节所述,叔伯块至多只能被其兄弟区块的第七代子孙引用。如果叔块在之后的区块链中作为叔块被引用,每个叔块会为挖矿者产出的区块链奖励的7/8,这些称之为叔块奖励。而产生孤块的矿工是没有奖励的。

验证叔伯块的关键代码

/consensus/ethash/consensus.go
func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
//...
// Gather the set of past uncles and ancestors
uncles, ancestors := set.New(), make(map[common.Hash]*types.Header)
number, parent := block.NumberU64()-1, block.ParentHash()
//从当前区块回溯7层(或7代),搜集祖先(主链上的块)和叔伯块
for i := 0; i < 7; i++ {
ancestor := chain.GetBlock(parent, number)
if ancestor == nil {
break
}
ancestors[ancestor.Hash()] = ancestor.Header()
for _, uncle := range ancestor.Uncles() {
uncles.Add(uncle.Hash())
}
parent, number = ancestor.ParentHash(), number-1
}
// Verify each of the uncles that it's recent, but not an ancestor 验证叔伯块不是祖先(即不能是主链的块)
for _, uncle := range block.Uncles() {
// Make sure every uncle is rewarded only once 每一个叔伯块只能被奖励一次,否则就“多重奖励”了
hash := uncle.Hash()
if uncles.Has(hash) {
return errDuplicateUncle
}
uncles.Add(hash)

// Make sure the uncle has a valid ancestry 该叔伯块为祖先区块,即为主链上的块
if ancestors[hash] != nil {
return errUncleIsAncestor
}
//叔伯块的祖先不在7层祖先之中 或者 叔伯块和当前区块在同一区块高度
if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
return errDanglingUncle
}
//验证叔伯块头部的有效性
if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
return err
}
//...
}

后续区块对每一个叔伯块的引用(将叔伯块的区块摘要值纳入自己区块头部的相应字段中)会给给自己带来5/32的奖励。

刷题

以上的知识显然是不够用的,区块链师傅发给我好多熟肉文章,还在啃。与此同时可以在https://ethernaut.openzeppelin.com/上刷题,用实际操作去理解。

0.Hello Ethernaut

首先得先切换网络,连接到Rinkeby测试网络里,之后平台会自动跟账户交互

之后要搞一点点eth,因为无论是生成实例,还是提交合约都是需要花费eth的,根据提示,从水管里能搞到0.1eth,完全够用了

由于还不太熟悉合约,我完全是根据提示走的,打开开发者工具之后,从控制台输入合约就行

皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

代码如上,

完整源码

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Instance {

string public password;
uint8 public infoNum = 42;
string public theMethodName = 'The method name is method7123949.';
bool private cleared = false;

// constructor
constructor(string memory _password) public {
password = _password;
}

function info() public pure returns (string memory) {
return 'You will find what you need in info1().';
}

function info1() public pure returns (string memory) {
return 'Try info2(), but with "hello" as a parameter.';
}

function info2(string memory param) public pure returns (string memory) {
if(keccak256(abi.encodePacked(param)) == keccak256(abi.encodePacked('hello'))) {
return 'The property infoNum holds the number of the next info method to call.';
}
return 'Wrong parameter.';
}

function info42() public pure returns (string memory) {
return 'theMethodName is the name of the next method.';
}

function method7123949() public pure returns (string memory) {
return 'If you know the password, submit it to authenticate().';
}

function authenticate(string memory passkey) public {
if(keccak256(abi.encodePacked(passkey)) == keccak256(abi.encodePacked(password))) {
cleared = true;
}
}

function getCleared() public view returns (bool) {
return cleared;
}
}

有些吃力,还是先学合约去了


原文始发于微信公众号(山警网络空间安全实验室):皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年7月6日09:41:37
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   皮蛋厂的学习日记 7.5 BLOCKCHAIN|区块链初探https://cn-sec.com/archives/1160022.html

发表评论

匿名网友 填写信息