您正在查看: Ethereum-新手教程 分类下的文章

以太坊的几种同步模式

查看geth命令行参数geth --help

--syncmode value                    Blockchain sync mode ("snap", "full" or "light") (default: snap)

参数支持"full", "snap", "light" 三种模式「原先的"fast"已被 "snap"替代」

查看官方文档

同步模式
--syncmode <mode> 您可以使用确定网络中节点类型的参数以三种不同同步模式之一启动 Geth 。

这些是:

  • Full : 下载所有区块(包括标题、交易和收据)并通过执行每个区块增量生成区块链的状态。
  • Snap(默认):与快速相同的功能,但具有更快的算法。
  • Light:下载所有区块头、区块数据,并随机验证一些。

官方推荐使用快照同步,简单说就是新的算法替代原先的fast模式,

请注意,快照同步已在 Geth v1.10.0 中提供,但尚未启用。
原因是提供快照同步需要节点已经生成快照加速结构,目前还没有,因为它也在 v1.10.0 中提供。
您可以通过 手动启用快照同步--syncmode snap,
但请注意,我们预计它要在柏林之后的几周内才能找到合适的对等方。
当我们觉得有足够的对等点可以依赖它时,我们将默认启用它。

此时已设置为默认模式

以太坊存档节点有什么用?

举个例子,如果你想知道4,000,000区块的以太坊账户余额,那么就需要运行存档节点,然后查询这个数字。这个节点依赖于一些专门的用例,但是对区块链的安全性和信任模型来说其实并没有影响。

内容引用

https://www.ccvalue.cn/article/2011.html

Transaction gasPrice (0) is too low for the next block, which has a baseFeePerGas of

问题描述

为了对比数据方便,排除发起交易gas消耗引起的账户余额差别,会将hardhat test 脚本中设置发起交易,设置gasPrice:0

await contract.withdraw({from: accounts[15], gasPrice: 0})

执行hardhat test时报以下错误

Transaction gasPrice (0) is too low for the next block, which has a baseFeePerGas of 8

解决方案

hardhat-config.js中设置initialBaseFeePerGas: 0

 networks: {
        hardhat: {
            ...
            initialBaseFeePerGas: 0
        }
    }

参考

https://github.com/nomiclabs/hardhat/issues/1216
https://github.com/nomiclabs/hardhat/blob/8e219bfc4112488953508eddd826d537bc71e803/docs/hardhat-network/reference/README.md

block.Hash()计算得到的hash 与 eth_getBlockByNumber获取到的值不一致

1.10.8版本出现的一个bug

rpc returndata 使用一个字段miner,但不包含coinbase. 因此,如果调用者想要在远程端计算头/块哈希,这是不可能的,因为我们不再提供正确的值coinbase。

通过revert提交解决
https://github.com/ethereum/go-ethereum/pull/23466/files

等待 1.10.9解决,或者使用master分支,文章此时已合入

SHA-1: 62ad17fb0046243255048fbf8cb0882f48d8d850

* Revert "eth, internal/ethapi: make RPC block miner field show block sealer correctly (#23312)" (#23466)

https://github.com/ethereum/go-ethereum/issues/23523
https://github.com/ethereum/go-ethereum/issues/23463
https://github.com/ethereum/go-ethereum/issues/23512
https://github.com/ethereum/go-ethereum/pull/23466

合约内执行ERC20转账

假设有个合约ERC20充值方法,方法接收msg.sender发送的ERC20代币,并增加合约中的余额统计

import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
...
function rechargeMain(address mainChainErc20Addr, uint256 amount) payable external notContract returns (bool){
    ...
    require(amount > 0, "The recharge amount is too small");
    require(IERC20(mainChainErc20Addr).balanceOf(msg.sender) >= amount, "Insufficient contract account balance");

    IERC20(mainChainErc20Addr).transfer(address(this), amount);
    ...
    return true;
}

执行后提示

execution reverted: ERC20: transfer amount exceeds balance

经测试(Github Code

function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
    _transfer(_msgSender(), recipient, amount);
    return true;
}

_msgSender()的地址为合约地址,测试交易(跳转地址

所以我们需要使用transferFrom,代码修改为

import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
...
function rechargeMain(address mainChainErc20Addr, uint256 amount) payable external notContract returns (bool){
    ...
    require(amount > 0, "The recharge amount is too small");
    require(IERC20(mainChainErc20Addr).balanceOf(msg.sender) >= amount, "Insufficient contract account balance");

    IERC20(mainChainErc20Addr).transferFrom(msg.sender, address(this), amount);
    ...
    return true;
}

执行完,提示

execution reverted: ERC20: transfer amount exceeds allowance

查看代码(Github code

function transferFrom(
address sender,
 address recipient,
 uint256 amount
) public virtual override returns (bool) {
    _transfer(sender, recipient, amount);

    uint256 currentAllowance = _allowances[sender][_msgSender()];
    require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
    unchecked {
        _approve(sender, _msgSender(), currentAllowance - amount);
    }

    return true;
}

需要先批准交易金额(津贴)(Github Code

 function approve(address spender, uint256 amount) public virtual override returns (bool) {
     _approve(_msgSender(), spender, amount);
     return true;
 }

查看当前剩余允许值,(Github Code

function allowance(address owner, address spender) public view virtual override returns (uint256) {
    return _allowances[owner][spender];
}

也可以执行increaseAllowancedecreaseAllowance增量增减调整

对于此次需求,执行rechargeMain前,需要先执行津贴充值

increaseAllowance(address spender, uint256 addedValue)

执行increaseAllowance的地址为当前充值转出地址
spender为当前转入合约地址
addedValue为当前充值金额
测试交易:https://ropsten.etherscan.io/tx/0x0f65cb2a7ca2bf0d6b1fdd65e943afb5c6305573dc3c3a960c2f86f71757c897