aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <obscuren@obscura.com>2013-12-28 04:22:57 +0800
committerobscuren <obscuren@obscura.com>2013-12-28 04:22:57 +0800
commit8301d154ae4bb6082d588bd42c6e913be63618ef (patch)
tree1a793c51bcdafa8b5da54097c7822b85c753434d
parentd8c0b0c89906edada0a30d144ebeb116388b45b1 (diff)
downloadgo-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar.gz
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar.bz2
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar.lz
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar.xz
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.tar.zst
go-tangerine-8301d154ae4bb6082d588bd42c6e913be63618ef.zip
Serializing block
-rw-r--r--block.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/block.go b/block.go
index a0bcf0bd0..f8bf2ce3b 100644
--- a/block.go
+++ b/block.go
@@ -2,16 +2,29 @@ package main
import (
_"fmt"
+ "time"
)
type Block struct {
- transactions []*Transaction
+ RlpSerializer
+
+ number uint32
+ prevHash string
+ uncles []*Block
+ coinbase string
+ // state xxx
+ difficulty int
+ time time.Time
+ nonce int
+ transactions []*Transaction
}
func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
+
+ time: time.Now(),
}
return block
@@ -19,3 +32,33 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block {
func (block *Block) Update() {
}
+
+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) {
+}