📅  最后修改于: 2023-12-03 15:00:54.473000             🧑  作者: Mango
Geth是一款以太坊客户端,它能够与以太坊网络进行交互,并且可以用于部署和运行智能合约。
在Linux平台上,我们可以使用以下命令来安装和运行Geth:
在Linux上安装Geth非常简单,我们可以使用包管理工具来完成安装,例如在Ubuntu上使用apt-get命令:
sudo apt-get update
sudo apt-get install ethereum
安装完成后,我们可以通过以下命令来启动Geth:
geth --syncmode "fast" --cache=1024 console
命令参数说明:
--syncmode "fast"
:设置同步模式为快速同步模式。--cache=1024
:设置缓存大小为1024 MB。console
:启动Geth的JavaScript控制台。在控制台中,我们可以通过JavaScript代码与以太坊进行交互,查询区块链信息,部署和运行智能合约等。
以下是一些常用的Geth JavaScript API:
eth.getBlock()
获取最新的区块信息:
> eth.getBlock("latest")
{
difficulty: 1127072749735362,
extraData: "0x",
gasLimit: 5000,
gasUsed: 0,
hash: "0xe381a960637926b4e3ed081db78ac6de4fe06236f6874f178d5ea445b827013e",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000000",
number: 13081839,
parentHash: "0x0c160aa3b10b6f78a1e2c86c0f52e08c93683c16adf76b0ab4cba37a7c6dc33a",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e36350a5",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 555,
stateRoot: "0xc9ecabd4c7bf4fa9decc7f1d257c91b4c05d20ee7c0af302b78a728c89c32085",
timestamp: 1648348595,
totalDifficulty: 14706402069028026389620,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e36350a5",
uncles: []
}
eth.getBlockNumber()
获取最新的区块高度:
> eth.getBlockNumber()
13081840
eth.getTransaction()
获取交易信息:
> eth.getTransaction("0x58b0892a71053c29e05c4ad8da764b5ac3ad43167ec081d9f8fbf4e60ab06320")
{
blockHash: "0xa3501f91a6400d1c683c5e392a2d6472b5e0fb5737562dc67599a0107d0d2c41",
blockNumber: 13081751,
from: "0x4c5f400298e06ca969af1151932564e04c862178",
gas: 21000,
gasPrice: 50000000000,
hash: "0x58b0892a71053c29e05c4ad8da764b5ac3ad43167ec081d9f8fbf4e60ab06320",
input: "0x",
nonce: 105,
r: "0x57e4a77115e5cee4e17a0f592339a9db63dc8d6e4628aa755931eec4c2469f3d",
s: "0x5a70c43d19b6687f793188345ee56f2d7c04feefa4a1dca2a3a9d8ead308cb22",
to: "0xfFfFffC8Ddd8b8E774444bA8F16Bf9c4d6Bf0760",
transactionIndex: 0,
v: "0x1c",
value: 1000000000000000000
}
eth.sendTransaction()
发送一笔以太币交易:
> eth.sendTransaction({from: '0x4c5f400298e06ca969af1151932564e04c862178', to: '0x2b55',
value: web3.toWei(1, "ether"), gasPrice: web3.toWei(4, "gwei")})
"0x5d2f734ad2d7f9a0d357d5e71f372d5c73ec67ba22d651f527ca870e11444938"
更多Geth JavaScript API请参考Geth官方文档。