VUE练习2 - 使用axios模块

1 安装axios

1
2
项目根目录下执行如下指令
npm install -S axios --registry https://registry.npm.taobao.org

2 修改模块代码

在开发过程中,我们可能经常会在前端项目的业务里面使用到某些变量,我们可以添加到配置文件中,比如我们在src目录下创建一个settings.js文件:

/settings.js

1
2
3
export default { // 注意,对象要抛出后,其他文件中才能引入使用
host: 'http://api.yunminitools.cn:8000' // 我们的后台项目将来就通过这个域名和端口来启动
}
More...

VUE练习1 - 添加页面

1 Vue 的目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
src/         主开发目录,要开发的客户端代码文件(单文件组件,样式、工具函数等等)全部在这个目录下
static/ 静态资源目录,项目中的静态资源(css,js,图片等文件)放在这个文件夹
dist/ 项目打包发布文件夹,目前没有这个文件夹,最后要上线单文件项目文件都在这个文件夹中
后面使用npm build 打包项目,让项目中的vue组件经过编译变成js 代码以后,dist就出现了

node_modules/ node的包目录,项目运行的依赖包存储目录,
package.json和package-lock.json文件中会自动记录了这个目录下所有的包以及包的版本信息,
如果node_modules没有,但是有package.json,则可以在终端下,通过npm install进行恢复。

config/ 配置目录,是环境配置目录与项目无关。
build/ 项目打包时依赖的目录
src/router/ 路由,是我们创建项目的时候,如果选择安装vue-router,就自动会生成这个目录。
src/assets/ 静态资源存储目录,与static目录作用类似。
src/components/ 组件存储目录,就是浏览器中用户看到的页面的一部分内容。
src/views/ 组件存储目录,就是浏览器中用户看到的页面内容,views往往会加载并包含components中的组件进来

2 执行流程图

graph TD
A(index.html 全局唯一入口)-->B(main.js VUE项目初始化入口)
B-->C(App.vue 根组件/路由)
C-->D1(首页页面组件)
C-->D2(登录页面组件)
C-->D3(商品页面组件)
D1-->E1(头部子组件)
D2-->E1
D3-->E1
D2-->E2(脚部子组件)
D1-->E2
D3-->E2
F(router/index.js 配置路由)-->C
More...

Javascript 箭头函数

Javascript 箭头函数

1
2
var arr = [11,22,33,44]
var res = arr.filter((item,index)=> item>30)

其等价形式为:

1
2
3
4
var arr = [11,22,33,44]
var res = arr.filter(function(item,index){
return item>30
})

在 vue 中常常可以看到箭头函数的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script>
// give each todo a unique id
let id = 0

export default {
data() {
return {
newTodo: '',
todos: [
{ id: id++, text: 'Learn HTML' },
{ id: id++, text: 'Learn JavaScript' },
{ id: id++, text: 'Learn Vue' }
]
}
},
methods: {
addTodo() {
// ...
this.todos.push({id:id++,text:this.newTodo})
this.newTodo = ''
},
removeTodo(todo) {
// ...
this.todos = this.todos.filter((t)=>t!=todo)
}
}
}
</script>

<template>
<form @submit.prevent="addTodo">
<input v-model="newTodo">
<button>Add Todo</button>
</form>
<ul>
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
<button @click="removeTodo(todo)">X</button>
</li>
</ul>
</template>

其中的 this.todos = this.todos.filter ((t)=>t!=todo) 就是箭头函数。 可以实现列表的删除效果。
原理就是把不等于todo的元素筛选出来,也就是去除了todo。

论文学习 - Bitcoin:A Peer-to-Peer Electronic Cash System(7)

比特币:一个点对点的电子货币系统

9 合并和分割货币

9 Combining and Splitting Value
Although it would be possible to handle coins individually, it would be unwieldy to make a separate transaction for every cent in a transfer. To allow value to be split and combined, transactions contain multiple inputs and outputs. Normally there will be either a single input from a larger previous transaction or multiple inputs combining smaller amounts, and at most two outputs: one for the payment, and one returning the change, if any, back to the sender.

It should be noted that fan-out, where a transaction depends on several transactions, and those transactions depend on many more, is not a problem here. There is never the need to extract a complete standalone copy of a transaction’s history.

尽管单独处理每个货币是可能的,但将一次转账按每一分拆成多次交易太笨拙。为允许交易额被分割和合并,交易将包含多个输入值和输出值。通常是一个从之前交易而得的较大输入值或多个较小输入值的组合,以及最多两个输出值:一个作为支付,另一个作为找零,如果有的话,退还给支付发送方。

需要注意的是,这里的扇出(fan-out),即一笔交易依赖数笔交易,这数笔交易又依赖更多的交易,形成了一个树状结构。在比特币系统中,这种情况不会成为问题,因为不需要获取一笔交易历史的完整独立副本。

【关注点】:

  • 扇出(fan-out), 是一个比较形象的说法,后面直接给出了定义。
  • 不需要获取一笔交易历史的完整独立副本,并没有解释原因,为什么呢?

论文学习 - Bitcoin:A Peer-to-Peer Electronic Cash System(6)

比特币:一个点对点的电子货币系统

8.简化的支付验证

8.Simplified Payment Verification
It is possible to verify payments without running a full network node. A user only needs to keep a copy of the block headers of the longest proof-of-work chain, which he can get by querying network nodes until he’s convinced he has the longest chain, and obtain the Merkle branch linking the transaction to the block it’s timestamped in. He can’t check the transaction for himself, but by linking it to a place in the chain, he can see that a network node has accepted it, and blocks added after it further confirm the network has accepted it.

As such, the verification is reliable as long as honest nodes control the network, but is more vulnerable if the network is overpowered by an attacker. While network nodes can verify transactions for themselves, the simplified method can be fooled by an attacker’s fabricated transactions for as long as the attacker can continue to overpower the network. One strategy to protect against this would be to accept alerts from network nodes when they detect an invalid block, prompting the user’s software to download the full block and alerted transactions to
confirm the inconsistency. Businesses that receive frequent payments will probably still want to run their own nodes for more independent security and quicker verification.

不运行一个完整的网络节点也是可以进行支付验证的。用户只需拥有一个最长工作量证明链的区块头副本,他可以通过向其他网络节点查询以确认他拥有了最长的链,并获取链接交易到给交易打时间戳区块的默克尔分支。虽然他自己不能核实这个交易,但如果交易已经链接到链中的某个位置,就说明一个网络节点已经接受了此交易,而其后追加的区块进一步确认网络已经接受了它。

同样地,只要诚实节点控制着网络,这种简化验证就是可靠的;如果网络被攻击者控制,简化验证会变得比较脆弱。虽然网络节点可以验证他们自己的交易,但只要攻击者持续控制网络,那么这种简化的方法就可能被攻击者的伪造交易欺骗。一种对策是接受其他网络节点发现一个无效区块时发出的警告,提醒用户软件下载整个区块和被警告的交易来检查一致性。为了更加独立的安全性以及更快的支付确认,收款频繁的公司可能仍需运行他们自己的节点。

【关注点】:

  • 向其他网络节点查询以确认他拥有了最长的链, 如何做到?
  • 仍需运行他们自己的节点,是指要运行全节点吗?

论文学习 - Bitcoin:A Peer-to-Peer Electronic Cash System(5)

比特币:一个点对点的电子货币系统

6. 激励

6 Incentive
By convention, the first transaction in a block is a special transaction that starts a new coin owned by the creator of the block. This adds an incentive for nodes to support the network, and provides a way to initially distribute coins into circulation, since there is no central authority to issue them.
The steady addition of a constant of amount of new coins is analogous to gold miners expending resources to add gold to circulation. In our case, it is CPU time and electricity that is expended. The incentive can also be funded with transaction fees. If the output value of a transaction is less than its input value, the difference is a transaction fee that is added to the incentive value of the block containing the transaction. Once a predetermined number of coins have entered circulation, the incentive can transition entirely to transaction fees and be completely inflation free.
The incentive may help encourage nodes to stay honest. If a greedy attacker is able to assemble more CPU power than all the honest nodes, he would have to choose between using it to defraud people by stealing back his payments, or using it to generate new coins. He ought to find it more profitable to play by the rules, such rules that favour him with more new coins than everyone else combined, than to undermine the system and the validity of his own wealth.

我们约定,区块中的第一笔交易是区块创建者开创一枚属于他的新货币的特殊的交易。这就加入了对支持网络的节点的激励,并提供了一种初始分发货币到流通领域的方法,因为这里没有中央机构来发行货币。新货币按固定量稳定地增加就像金矿矿工消耗资源并增加黄金到流通领域一样。对我们而言,消耗的是 CPU 时间和电力激励也可以由交易费充当。如果交易的输出值小于其输入值,差价就作为交易费被加到包含此交易的区块的激励中。一旦预定量的货币进入了流通领域,激励将变为只含有交易费,这样可以完全避免通货膨胀。
激励会有助于鼓励节点保持诚实。如果一个贪心的攻击者有能力聚集比所有诚实节点更多的 CPU 算力,他将面临是以骗回已付款的方式欺诈别人还是使用这些算力生成新货币的抉择。他将发现遵守规则比破坏系统和他自己财产的有效性更有利,因为这些规则准许他获得比所有其他人都多的新货币。

7. 回收磁盘空间

  1. Reclaiming Disk Space
    Once the latest transaction in a coin is buried under enough blocks, the spent transactions before it can be discarded to save disk space. To facilitate this without breaking the block’s hash, transactions are hashed in a Merkle Tree [7][2][5], with only the root included in the block’s hash. Old blocks can then be compacted by stubbing off branches of the tree. The interior hashes do not need to be stored.

    A block header with no transactions would be about 80 bytes. If we suppose blocks are generated every 10 minutes, 80 bytes * 6 * 24 * 365 = 4.2MB per year. With computer systems typically selling with 2GB of RAM as of 2008, and Moore’s Law predicting current growth of 1.2GB per year, storage should not be a problem even if the block headers must be kept in memory.

一旦某个货币的最新交易已经被足够多的区块覆盖,这之前的支付交易就可以被丢弃以节省磁盘空间。为便于此而又不破坏区块的哈希值,交易将被哈希进默克尔树 [7][2][5],只有根节点被纳入到区块的哈希值。老的区块可通过剪除树枝的方式被压缩。树枝内部的哈希不需要被保存。

每个不包含交易的区块头大约是 80 bytes。如果每 10 分钟生成一个区块,每年生成80 bytes * 6 * 24 * 365 = 4.2 MB,2008 年在售的典型计算机有 2 GB 内存,并且摩尔定律预测目前每年内存增加 1.2 GB,所以就算区块头一定要存在内存里,存储也不是问题。

【关注点】:

  • If the output value of a transaction is less than its input value,这句话不是太理解,什么情况下会出现?
  • favour him with more new coins,翻译为 有利于他 获得更多的新币。问题是当到后期新币越来越少的时候,做这个事情是否是就有利可图了?
  • enough blocks ,多少算足够?
  • transactions are hashed in a Merkle Tree,为什么这样就可以不改变Hash值?

论文学习 - Bitcoin:A Peer-to-Peer Electronic Cash System(4)

比特币:一个点对点的电子货币系统

5. 网络

5 Network
The steps to run the network are as follows:

  1. New transactions are broadcast to all nodes.
  2. Each node collects new transactions into a block.
  3. Each node works on finding a difficult proof-of-work for its block.
  4. When a node finds a proof-of-work, it broadcasts the block to all nodes.
  5. Nodes accept the block only if all transactions in it are valid and not already spent.
  6. Nodes express their acceptance of the block by working on creating the next block in the
    chain, using the hash of the accepted block as the previous hash.

Nodes always consider the longest chain to be the correct one and will keep working on extending it. If two nodes broadcast different versions of the next block simultaneously, some nodes may receive one or the other first. In that case, they work on the first one they received, but save the other branch in case it becomes longer. The tie will be broken when the next proof-of-work is found and one branch becomes longer; the nodes that were working on the other branch will then switch to the longer one.

New transaction broadcasts do not necessarily need to reach all nodes. As long as they reach many nodes, they will get into a block before long. Block broadcasts are also tolerant of dropped messages. If a node does not receive a block, it will request it when it receives the next block and realizes it missed one.

运行网络的步骤如下:

  1. 新交易向所有节点广播。
  2. 每个节点将新交易收集到一个区块。
  3. 每个节点为它的区块寻找工作量证明(困难)。
  4. 当一个节点找到了工作量证明,就向所有节点广播这个区块。
  5. 节点只有在区块内所有交易都是有效的且之前没有被支付的情况下接收这个区块。
  6. 节点通过使用这个区块的哈希值作为上一个哈希值,在链中创建下一个区块的方式表示对这个区块的接受。

节点总是认为最长的链为正确的并持续致力于延长它。如果两个节点同时广播了不同的下一个区块,有些节点可能先收到其中一个而其他节点先收到另一个。这种情况,节点基于他们收到的第一个区块工作,但是也保存另一个分支以防它变为更长的链。当下一个工作量证明被找到后僵局就会被打破,从而其中一个分支变得更长;在另一个分支上工作的节点将切换到更长的链上来。

新交易的广播不必到达所有的节点。只要到达一些节点,不久就会进入到一个区块。区块广播也是能容忍消息丢失的。如果一个节点没有收到某个区块,它将在收到下一个区块时发现它丢失了一个区块然后去请求这个区块。

【关注点】:

  • 另一个分支上工作的节点将切换到更长的链,也就意味着有些交易会失效,对于这些失效的交易如何处理?
  • 不久就会进入到一个区块,这个是指这笔交易会被保存到区块里。那一个区块会包含多少笔交易呢?

论文学习 - Bitcoin:A Peer-to-Peer Electronic Cash System(3)

比特币:一个点对点的电子货币系统

3. 时间戳服务器

3 Timestamp Server
The solution we propose begins with a timestamp server. A timestamp server works by taking a hash of a block of items to be timestamped and widely publishing the hash, such as in a newspaper or Usenet post [2-5]. The timestamp proves that the data must have existed at the time, obviously, in order to get into the hash. Each timestamp includes the previous timestamp in its hash, forming a chain, with each additional timestamp reinforcing the ones before it.

我们提出的方案从时间戳服务器开始。时间戳服务器计算包含多个需要被打时间戳的数据项的区块的哈希值并广泛地发布这个哈希值,就像在报纸或新闻组帖子里 [2-5]。时间戳能证明要得到这个哈希值,显然这些数据当时一定是存在的。每个时间戳的哈希值都纳入了上一个时间戳,形成一条链,后面的时间戳进一步增强前一个时间戳。

More...

请我喝杯咖啡吧~

支付宝
微信