// blockNode represents a block within the DAG type blockNode struct { // NOTE: Additions, deletions, or modifications to the order of the // definitions in this struct should not be changed without considering // how it affects alignment on 64-bit platforms. The current order is // specifically crafted to result in minimal padding. There will be // hundreds of thousands of these in memory, so a few extra bytes of // padding adds up.
// parents are the parent block for this node. parents []*blockNode
// parent metadata like version and extra data parentMetadata []*parentInfo
parentVersion int32
// hash is the double sha 256 of the block. hash chainhash.Hash
// workSum is the total amount of work in the chain up to and including // this node. workSum *big.Int
// height is parentsMaxHeight + 1 height int32
// Some fields from block headers to // reconstruct headers from memory. These must be treated as // immutable and are intentionally ordered to avoid padding on 64-bit // platforms. version int32 bits uint32 nonce uint32 timestamp int64 merkleRoot chainhash.Hash
}
type parentInfo struct { hash chainhash.Hash data [32]byte }
block 的:
1 2 3 4 5 6 7
type Block struct { Index int Timestamp string BPM int Hash string PrevHash string }
Index 是这个块在整个链中的位置
Timestamp 显而易见就是块生成时的时间戳
Hash 是这个块通过 SHA256 算法生成的散列值
PrevHash 代表前一个块的 SHA256 散列值
BPM 每分钟心跳数,也就是心率。–不太清楚干什么用的
接着,我们再定义一个结构表示整个链,最简单的表示形式就是一个 Block 的数组: var ``Blockchain`` []``Block 可以看出来,在 go 里面,是先写变量名,再写类型(比如*,[]),然后是最终类型。
# nc -v -p 5000 localhost 6000 nc: connect to localhost port 6000 (tcp) failed: Connection refused Connection to localhost 6000 port [tcp/x11] succeeded! Hi, server Hi, client
Server:
1 2 3 4 5
# nc -v -l 127.0.0.1 6000 Listening on [127.0.0.1] (family 0, port 6000) Connection from [127.0.0.1] port 6000 [tcp/x11] accepted (family 2, sport 5000) Hi, server Hi, client