初识Geth

安装geth

linux下执行命令

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

私网搭建

配置创世块文件

{
    "config": {
        "chainID" :18,
        "homesteadBlock": 0,
          "eip150Block" : 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "alloc":   {},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "difficulty": "0x2",
    "extraData": "",
    "gasLimit": "0xffffffff",
    "nonce": "0x0000000000000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp": "0x00"
}

保存为genesis.json

其中:

Coinbase:挖矿后获得奖励的账户地址

Difficulty:挖矿难度

gasLimit:一个区块能容纳的gas上限

nonce:随机值

mixhash:一个256位hash证明

extraData:附加信息

parentHash:前一块的hash值

数据初始化

geth init genesis.json --datadir ./data

创建账户

geth account new --datadir data

并输入密码

启动geth节点

geth --datadir ./data --networkid 18  --port 30303  --http --http.addr 0.0.0.0 --http.port 8545 --miner.gasprice 0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock  console 2> 1.log

其中参数含义如下:

networkid:编号,代表加入哪个网络,和配置文件内相同

datadir:数据目录

port:p2p端口

http:http-rpc服务

httpapi:http-rpc所提供的api

http.port:远程服务的端口,默认是8545

allow-insecure-unlock:允许在geth命令窗口解锁账户

http.corsdomain:指定可以接收请求来源的域名列表(浏览器访问,必须开启)

miner.gasprice:付给矿工的gas价格,这里为了方便设为0

常用命令       

查看存在账户

eth.accounts

新建用户(密码自己指定)

personal.newAccount("passwd")
personal.newAccount("passwd2")

启动挖矿(传入线程数)

miner.start(1)

查看账户余额

eth.getBalance(eth.accounts[0])

给账户取别名

testName=eth.accounts[0]
testName2=eth.accounts[1

解锁账户

personal.unlockAccount(testName)

转账10个货币

 eth.sendTransaction({from:testName,to:testName1,value:web3.toWei(10)})

 

                                                                                                                                                 

 
posted @ 2022-04-05 14:59  Yu_so1dier0n  阅读(175)  评论(0编辑  收藏  举报