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

以太坊部署合约时的地址是怎么计算的

先看代码 core/state_processor.go

func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, modOptions ...ModifyProcessOptionFunc) (*types.Receipt, error) {
...
    // If the transaction created a contract, store the creation address in the receipt.
    if msg.To() == nil {
        receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
    }

其中的evm.TxContext.Origin 为当前部署合约交易发起地址
core/evm.go

// NewEVMTxContext creates a new transaction context for a single transaction.
func NewEVMTxContext(msg Message) vm.TxContext {
    return vm.TxContext{
        Origin:   msg.From(),
        GasPrice: new(big.Int).Set(msg.GasPrice()),
    }
}

结论

合约地址ContractAddress通过部署合约发起地址和当前发起交易所在nonce计算所得

所以如果知道合约部署地址,就可以猜出对应的合约部署时地址了,可以用于一些提前抢单操作。。

以太坊的几种同步模式

查看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