aboutsummaryrefslogtreecommitdiffstats
path: root/block.go
blob: f8bf2ce3bfd9bb28e254cc4cda0d13c6b6558138 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
  _"fmt"
  "time"
)

type Block struct {
  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
}

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) {
}