func main() {
genesisBlock := block{"Genesis Block", "", ""}
hash := sha256.Sum256([]byte(genesisBlock.data + genesisBlock.prevHash))
hexHash := fmt.Sprintf("%x", hash)
genesisBlock.hash = hexHash
secondBlocks := block{"Second blocks", "", genesisBlock.hash}
}
type blockchain struct{
blocks []block
}
func main() {
chain := blockchain{}
}
func main() {
chain := blockchain{}
chain.addBlock("Genesis Block")
chain.addBlock("Second Block")
chain.addBlock("Third Block")
chain.listblocks()
}
func (b *blockchain) addBlock(data string) {
}
func (b *blockchain) listBlocks() {
}
func (b *blockchain) addBlock(data string) {
newBlock := block{data, "", ""}
if len(b.blocks) > 0 {
newBlock.prevHash = b.blocks[len(b.blocks)-1].hash
}
}
func (b *blockchain) getLastHash() string{
if len(b.blocks) > 0 {
return b.blocks[len(b.blocks)-1].hash
}
return ""
}
func (b *blockchain) addBlock(data string) {
newBlock := block{data, "", b.getLastHash()}
}
func (b *blockchain) addBlock(data string) {
newBlock := block{data, "", b.getLastHash()}
hash := sha256.Sum256([]byte(newBlock.data + newBlock.prevHash))
newBlock.hash = fmt.Sprintf("%x", hash)
b.blocks = append(b.blocks, newBlock)
}
func (b *blockchain) listBlocks() {
for _, block := range b.blocks{
fmt.Printf("Data: %s\n", block.data)
fmt.Printf("Hash: %s\n", block.hash)
fmt.Printf("Prev Hash: %s\n", block.prevHash)
}
}
Data: Genesis Block
Hash: 89eb0ac031a63d2421cd05a2fbe41f3ea35f5c3712ca839cbf6b85c4ee07b7a3
Prev Hash:
Data: Second Block
Hash: ec6f43cf27c78760cc38a4855b36e83a7f054f5205a751628591d3f505599c08
Prev Hash: 89eb0ac031a63d2421cd05a2fbe41f3ea35f5c3712ca839cbf6b85c4ee07b7a3
Data: Third Block
Hash: d90e93ce55d3ef3b137b01112c8aad9f304efde1f970c41035c9c7658ad5e8dd
Prev Hash: ec6f43cf27c78760cc38a4855b36e83a7f054f5205a751628591d3f505599c08
https://go.dev/play/p/8as9Eg1xftJ
[Nomad Coin] Blockchain - #4.4 Refactoring part One (0) | 2022.07.16 |
---|---|
[Nomad Coin] Blockchain - #4.3 Singleton Pattern (1) | 2022.07.15 |
[Nomad Coin] Blockchain - #4.1 Our First Block (0) | 2022.07.14 |
[Nomad Coin] 블록체인 (0) | 2022.07.13 |
[Nomad Coin] 비트코인 떡상을 보고 개발자가 느낀 것 (0) | 2022.07.13 |
댓글 영역