以太坊测试网络rinkeby交易测试

操作系统centos7.6

概述

Geth(go-ethereum)是由以太坊基金会提供的官方以太坊协议实现,用Go编程语言编写的。Geth提供了一个交互式命令控制台,通过命令控制台中包含了以太坊的各种功能(API)。

1. 本地编译geth

cd ~/go/src/github.com/
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
git branch -a 
git checkout 
git checkout release/1.9
make all
cp ./build/bin/geth /usr/bin
  • geth 最常用的CLI客户端
  • abigen 源代码生成器,用于将以太坊智能合约定义转换为易于使用的,编译时类型安全的Go软件包
  • bootnode 以太坊客户端实现的精简版本,仅参与网络节点发现协议,但不运行任何更高级别的应用程序协议
  • evm 能够在可配置的环境和执行模式下运行字节码
  • rlpdump 用于将二进制RLP转储(以太坊协议使用的数据编码,无论是网络还是共识方式)转换为用户友好的层次表示形式(例如rlpdump --hex CE0183FFFFFFC4C304050583616263)。

2. 利用docker启动节点

编写节点启动脚本

mkdir -p /root/ethereum_node
cd /root/ethereum_node
vi start-node.sh

start-node.sh内容如下:

#!/bin/bash
docker rm -f eth_node || true
DATADIR=/root/ethereum_node/chain
mkdir -p $DATADIR
docker run -it -d --name eth_node -v $DATADIR:/root/.ethereum  -p 8545:8545  -p 30303:30303 ethereum/client-go   --ws --rpc --rpcaddr 0.0.0.0 --rpccorsdomain '*' --rpcapi "db,eth,net,web3,personal" --allow-insecure-unlock --wsapi "personal,web3" --rinkeby   console

执行脚本

chmod 777 start-node.sh
  ./start-node.sh

漫长的等待,待区块数据全部同步。

3. 登录到控制台

登录 javascript控制台,在控制台里可执行web3.js的api。

web3.js是一个javascript库,你可以使用HTTP或IPC连接本地或远程以太它节点进行交互。
web3的JavaScript库能够与以太坊区块链交互。 它可以检索用户帐户,发送交易,与智能合约交互等
api参考:https://web3js.readthedocs.io/en/v1.2.4/

geth attach rpc:http://xx.xx.xx.xx:8545

or

geth attach ipc://root/ethereum_node/chain/rinkeby/geth.ipc

查看eth所有的方法

eth

4. 创建账户

personal.listAccounts
personal.newAccount('pld123')
0xe7c5b662c719fe2a99fe20327fb2bf1aa8c0fdb2
personal.newAccount('pld123')
0x4fced1c852abcab76a9d2761c1db8d59e53a310c

去水龙头获取测试币:https://faucet.rinkeby.io/
需要 tweet或facebook账号,发送指定内容的消息,最多可获最多18.75个eth。

5. 构造一笔交易

方式一:使用sendTransaction方法

personal.unlockAccount(web3.eth.accounts[0], 'pld123', 300)
src = web3.eth.accounts[0];
dst = web3.eth.accounts[1];
#获取余额
web3.fromWei(eth.getBalance(eth.accounts[0]), 'ether')
web3.fromWei(eth.getBalance(eth.accounts[1]), 'ether')
# 转账
web3.eth.sendTransaction({from: src, to: dst, value: web3.toWei(0.01, "ether"), data: ""});

方式一:使用signTransactionsendRawTransaction方法

personal.unlockAccount(web3.eth.accounts[0], 'pld123', 300)
web3.eth.signTransaction({
    from: eth.accounts[0],
    gasPrice: "20000000000",
    gas: "21000",
    nonce: web3.eth.getTransactionCount(eth.accounts[0])+1,
    to: eth.accounts[1],
    value: web3.toWei(0.01, "ether"),
    data: ""
})
eth.sendRawTransaction()
发布了31 篇原创文章 · 获赞 1 · 访问量 2803

猜你喜欢

转载自blog.csdn.net/kk3909/article/details/105082704