36 lines
592 B
Go
36 lines
592 B
Go
package main
|
|
|
|
import (
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
maxNonce = math.MaxInt64
|
|
)
|
|
|
|
type Block struct {
|
|
Timestamp int64
|
|
Data []byte
|
|
PrevBlockHash []byte
|
|
Hash []byte
|
|
Nonce int
|
|
}
|
|
|
|
// NewBlock returns a new block, ready to be added to the chain
|
|
func NewBlock(data string, prevBlockHash []byte) *Block {
|
|
block := &Block{
|
|
Timestamp: time.Now().Unix(),
|
|
Data: []byte(data),
|
|
PrevBlockHash: prevBlockHash,
|
|
Hash: []byte{},
|
|
}
|
|
pow := NewProofOfWork(block)
|
|
nonce, hash := pow.Run()
|
|
|
|
block.Hash = hash[:]
|
|
block.Nonce = nonce
|
|
|
|
return block
|
|
}
|