aboutsummaryrefslogtreecommitdiffstats
path: root/block.go
diff options
context:
space:
mode:
Diffstat (limited to 'block.go')
-rw-r--r--block.go38
1 files changed, 32 insertions, 6 deletions
diff --git a/block.go b/block.go
index 9aa3c8bb5..146328471 100644
--- a/block.go
+++ b/block.go
@@ -15,6 +15,7 @@ type Block struct {
uncles []*Block
coinbase string
// state xxx
+ state *Trie
difficulty uint32
// Creation time
time int64
@@ -34,7 +35,7 @@ func NewBlock(raw []byte) *Block {
}
// Creates a new block. This is currently for testing
-func CreateBlock(/* TODO use raw data */transactions []*Transaction) *Block {
+func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
@@ -49,12 +50,32 @@ func CreateBlock(/* TODO use raw data */transactions []*Transaction) *Block {
return block
}
+func CreateBlock(root string, num int, prevHash string, base string, difficulty int, nonce int, extra string, txes []*Transaction) *Block {
+ block := &Block{
+ // Slice of transactions to include in this block
+ transactions: txes,
+ number: uint32(num),
+ prevHash: prevHash,
+ coinbase: base,
+ difficulty: uint32(difficulty),
+ nonce: uint32(nonce),
+ time: time.Now().Unix(),
+ extra: extra,
+ }
+ block.state = NewTrie(Db, root)
+ for _, tx := range txes {
+ block.state.Update(tx.recipient, string(tx.MarshalRlp()))
+ }
+
+ return block
+}
+
func (block *Block) Update() {
}
// Returns a hash of the block
-func (block *Block) Hash() string {
- return Sha256Hex(block.MarshalRlp())
+func (block *Block) Hash() []byte {
+ return Sha256Bin(block.MarshalRlp())
}
func (block *Block) MarshalRlp() []byte {
@@ -73,7 +94,7 @@ func (block *Block) MarshalRlp() []byte {
"",
block.coinbase,
// root state
- "",
+ block.state.root,
// Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
block.difficulty,
@@ -99,7 +120,7 @@ func (block *Block) UnmarshalRlp(data []byte) {
block.number = uint32(number)
}
- if prevHash, ok := header[1].([]byte); ok {
+ if prevHash, ok := header[1].([]uint8); ok {
block.prevHash = string(prevHash)
}
@@ -109,7 +130,12 @@ func (block *Block) UnmarshalRlp(data []byte) {
block.coinbase = string(coinbase)
}
- // state is header[header[4]
+ if state, ok := header[4].([]uint8); ok {
+ // XXX The database is currently a global variable defined in testing.go
+ // This will eventually go away and the database will grabbed from the public server
+ // interface
+ block.state = NewTrie(Db, string(state))
+ }
// sha is header[5]