ARTS w05- 使用谷歌 BigQuery 分析以太坊数据
Tips
谷歌 BigQuery 提供了以太坊的数据集,使用 python 的话,可以调用 google.cloud 库做以太坊的数据分析,比如分析以太坊交易所花费的 gas 费用,如下:
from google.cloud import bigquery
import pandas as pd
client = bigquery.Client()
# Query by Allen Day, GooglCloud Developer Advocate (https://medium.com/@allenday)
query = """
SELECT
DATE(timestamp) AS tx_date,
AVG(gas_price*(receipt_gas_used/POWER(10,18))) AS avg_tx_gas_cost,
SUM(value/POWER(10,18)) AS sum_tx_ether
FROM
`bigquery-public-data.ethereum_blockchain.transactions` AS transactions,
`bigquery-public-data.ethereum_blockchain.blocks` AS blocks
WHERE TRUE
AND transactions.block_number = blocks.number
AND receipt_status = 1
AND value > 0
GROUP BY tx_date
HAVING tx_date >= '2018-01-01' AND tx_date <= '2018-12-31'
ORDER BY tx_date
"""
query_job = client.query(query)
iterator = query_job.result(timeout=30)
rows = list(iterator)
# Transform the rows into a nice pandas dataframe
columns = ['tx_date', 'avg_tx_gas_cost', 'sum_tx_ether']
df = pd.DataFrame(data=[list(x.values()) for x in rows], columns=columns)
# Look at the first 10
df.head(10)
输出
将以上的查询结果,图形化输出:
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.style.use('ggplot')
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
f, g = plt.subplots(figsize=(12, 9))
g = sns.lineplot(x="tx_date", y="avg_tx_gas_cost", data=df, palette="Blues_d")
plt.title("Average Ether transaction cost over time")
plt.show(g)
结果如下图:
Algorithm
leetcode 257. Binary Tree Paths,输入一棵二叉树,返回所有根节点到叶节点的路径.
Input:
1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Explanation: All root-to-leaf paths are: 1->2->5, 1->3
代码:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
res = []
self.recurse_tree(root, [], res)
result= []
for item in res:
result.append('->'.join(str(v) for v in item))
return result
def recurse_tree(self, root, res, res_):
if not root:
return res
res.append(root.val)
if root.right:
self.recurse_tree(root.right, res[:], res_)
if root.left:
self.recurse_tree(root.left, res[:], res_)
if not root.right and not root.left:
res_.append(res)
Review
How does Ethereum work, anyway? 这篇文章介绍了以太坊的工作原理,作者多次阅读以太坊黄皮书的总结,是了解以太坊和区块链的入门文章。作者 Preethi Kasireddy 之前是 coinbase 的前端工程师,目前自己创业,成立 TruStory,使命是为数字和去中心化世界带来真实性。
这篇文章主要介绍了以太坊的重要概念和原理:
- account:账户地址,分为外部账户和合约账户。向他人发起一笔转账操作,走的是外部账户,向合约转账,走的是合约账户。
- state:状态,全局共享。
- gas 和 fees:每一笔交易都要花费 gas,是矿工收取的手续费。
- transactions:每一笔转账都是一笔交易。
- blocks:区块包含零到多笔交易。
- transaction execution:交易的执行在以太坊的虚拟机 EVM 中进行。
- mining:挖矿,矿工打包交易的过程称为挖矿。
- proof of work:矿工之间争夺记账权。
文章还介绍以太坊 EVM 的工作原理,深入浅出,面面俱到,认真研读这篇文章能够对以太坊有一个较为全面的认识。
Share
互联网从 web 1.0 发展到了 web 2.0,web 1.0 只是静态的只读页面,信息的传播是单向的,而以 twitter 为代表的 2.0 时代,普通用户可以主动参与产生与分享信息,网络成为沟通渠道实现进行人与人沟通。web 3.0又会是什么样的?这篇文章 Understanding Web 3 — A User Controlled Internet 提出了 web 3.0 一种架构,以区块链为底层的架构,可以存储用户状态,用户能够管理和维护自己的数据和状态,而不依赖于第三方的中心机构。
这个架构中协议层是目前网络中还不存在的,包括 Trading、Lending、Derivatives等,这些基于区块链带来的 token 属性。当越来越多的用户在这个网络中流通 token,当他们逐渐将资产转移到网络中,就会对现有的金融体系形成冲击。
参考