go-pulse/block.go

65 lines
1.2 KiB
Go
Raw Normal View History

2013-12-26 11:45:52 +00:00
package main
import (
_"fmt"
2013-12-27 20:22:57 +00:00
"time"
2013-12-26 11:45:52 +00:00
)
type Block struct {
2013-12-27 20:22:57 +00:00
RlpSerializer
number uint32
prevHash string
uncles []*Block
coinbase string
// state xxx
difficulty int
time time.Time
nonce int
transactions []*Transaction
2013-12-26 11:45:52 +00:00
}
func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
2013-12-27 20:22:57 +00:00
time: time.Now(),
2013-12-26 11:45:52 +00:00
}
return block
}
func (block *Block) Update() {
}
2013-12-27 20:22:57 +00:00
func (block *Block) Hash() string {
return Sha256Hex(block.MarshalRlp())
}
func (block *Block) MarshalRlp() []byte {
// Encoding method requires []interface{} type. It's actual a slice of strings
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
encTx[i] = string(tx.MarshalRlp())
}
enc := RlpEncode([]interface{}{
block.number,
block.prevHash,
// Sha of uncles
block.coinbase,
// root state
Sha256Bin([]byte(RlpEncode(encTx))),
block.difficulty,
block.time,
block.nonce,
// extra?
})
return []byte(enc)
}
func (block *Block) UnmarshalRlp(data []byte) {
}