社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
type Tx struct {
Hash utils.Hash //如果改成指针型会更好一些
Version uint32
Ins []*TxIn
Outs []*TxOut
}
其实比特币中最重要的是交易(Transaction)的结构,这里简化了一个交易的结构。
utils.Hash其实就是一个[]byte数组。
func (tx *Tx) AddTxIn(txIn *TxIn) {
tx.Ins = append(tx.Ins, txIn)
}
func (tx *Tx) AddTxOut(txOut *TxOut) {
tx.Outs = append(tx.Outs, txOut)
}
这样就可以添加交易输入和输出。
type TxIn struct {
PrevOutPoint *OutPoint
ScripSig *Script
}
type OutPoint struct {
Hash utils.Hash //Tx的Hash
Index uint32 //Tx Outs中的第几笔
}
type TxOut struct {
Value int64
ScriptPubKey *Script
}
可见,在这里交易其实并不需要地址,只要满足条件就可以
type Script struct {
bytes []byte
//这里的Scirpt对外公开的只有ParsedOpCodes
//在其他版本的实现中,Script类只有bytes这一个成员
//由于我们其实只关注解析后的bytes,即ParsedOpCodes
//所以这里这样设计
ParsedOpCodes []ParsedOpCodes
}
type ParsedOpCode struct {
opValue byte
length int
data []byte
}
这里实现了由原来的操作与数据混合在一起的模型解析为一个操作及它所对应的操作数模型。
一开始Satoshi Nakamoto就将这里的脚本系统作为一个单一的模块设置出来,显示了他对这一方面超前的认识。而后的智能合约正是在这里收到启发从而完善(以以太坊为代表)。
智能合约就是链上代码,需要执行的时候就向这个地址发送一笔交易,交易的验证必然就会执行Script脚本,从而使得代码得到执行。
The bitcoin transaction script language contains many operators, but is deliberately limited in one important way - there are no loops or complex flow control capabilities other than conditional flow control. This ensures that the language is not Turing Complete, meaning that scripts have limited complexity and predictable execution times. Script is not a general-purpose language. These limitations ensure that the language cannot be used to create an infinite loop or other form of “logic bomb” that could be embedded in a transaction in a way that causes a denial-of-service attach against the bitcoin network. Remember, every transaction is validated by every full node on the bitcoin network. A limited language prevents the transaction validation mechanism from being used as a vulnerability.
比特币的脚本语言不具备循环和复杂的条件控制语句,这样可以使得编写出来的代码不会陷入不可预知的死循环从而使整个比特币网络不稳定。
而以太坊进一步修改了这里,提供了循环同时加入了Gas这一概念,脚本的执行每一步都需要消耗Gas,如果Gas不足,整个脚本的执行都会回退到初始,Gas的上限也确保了一个脚本不可能无限循环。
type Address struct {
key *crypto.PrivateKey
version byte
publicKey []byte
addressStr string
hash160 [20]byte
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!