aboutsummaryrefslogtreecommitdiffstats
path: root/block.go
blob: 00317573c51cae8d8439c344361d563e39f66cfc (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main

import (
  "fmt"
  "time"
  _"bytes"
  _"encoding/hex"
)

type Block struct {
  // The number of this block
  number        uint32
  // Hash to the previous block
  prevHash      string
  // Uncles of this block
  uncles        []*Block
  coinbase      string
  // state xxx
  state         *Trie
  difficulty    uint32
  // Creation time
  time          int64
  nonce         uint32
  // List of transactions and/or contracts
  transactions  []*Transaction

  extra         string
}

// New block takes a raw encoded string
func NewBlock(raw []byte) *Block {
  block := &Block{}
  block.UnmarshalRlp(raw)

  return block
}

// Creates a new block. This is currently for testing
func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
  block := &Block{
    // Slice of transactions to include in this block
    transactions: transactions,
    number: 1,
    prevHash: "1234",
    coinbase: "me",
    difficulty: 10,
    nonce: 0,
    time: time.Now().Unix(),
  }

  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 {
    // Create contract if there's no recipient
    if tx.recipient == "" {
      addr := tx.Hash()

      contract := NewContract(tx.value, []byte(""))
      block.state.Update(string(addr), string(contract.MarshalRlp()))
      for i, val := range tx.data {
        contract.state.Update(string(NumberToBytes(uint64(i), 32)), val)
        //contract.state.Update(string(Encode(uint32(i))), val)
      }
      block.UpdateContract(addr, contract)
    }
  }

  return block
}

func (block *Block) GetContract(addr []byte) *Contract {
  data := block.state.Get(string(addr))
  if data == "" {
    return nil
  }

  contract := &Contract{}
  contract.UnmarshalRlp([]byte(data))

  return contract
}

func (block *Block) UpdateContract(addr []byte, contract *Contract) {
  block.state.Update(string(addr), string(contract.MarshalRlp()))
}


func (block *Block) PayFee(addr []byte, fee uint64) bool {
  contract := block.GetContract(addr)
  // If we can't pay the fee return
  if contract == nil || contract.amount < fee {
    fmt.Println("Contract has insufficient funds", contract.amount, fee)
    return false
  }

  contract.amount -= fee
  block.state.Update(string(addr), string(contract.MarshalRlp()))

  data := block.state.Get(string(block.coinbase))
  println(data)
  // Get the ether (coinbase) and add the fee (gief fee to miner)
  ether := NewEtherFromData([]byte(data))
  ether.amount += fee

  block.state.Update(string(block.coinbase), string(ether.MarshalRlp()))

  return true
}

// Returns a hash of the block
func (block *Block) Hash() []byte {
  return Sha256Bin(block.MarshalRlp())
}

func (block *Block) MarshalRlp() []byte {
  // Marshal the transactions of this block
  encTx := make([]string, len(block.transactions))
  for i, tx := range block.transactions {
    // Cast it to a string (safe)
    encTx[i] = string(tx.MarshalRlp())
  }

  /* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
  header := []interface{}{
    block.number,
    block.prevHash,
    // Sha of uncles
    "",
    block.coinbase,
    // root state
    block.state.root,
    // Sha of tx
    string(Sha256Bin([]byte(Encode(encTx)))),
    block.difficulty,
    uint64(block.time),
    block.nonce,
    block.extra,
  }

  // TODO
  uncles := []interface{}{}
  // Encode a slice interface which contains the header and the list of transactions.
  return Encode([]interface{}{header, encTx, uncles})
}

func (block *Block) UnmarshalRlp(data []byte) {
  t, _ := Decode(data,0)

  // interface slice assertion
  if slice, ok := t.([]interface{}); ok {
    // interface slice assertion
    if header, ok := slice[0].([]interface{}); ok {
      if number, ok := header[0].(uint8); ok {
        block.number = uint32(number)
      }

      if prevHash, ok := header[1].([]uint8); ok {
        block.prevHash = string(prevHash)
      }

      // sha of uncles is header[2]

      if coinbase, ok := header[3].([]byte); ok {
        block.coinbase = string(coinbase)
      }

      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]

      // It's either 8bit or 64
      if difficulty, ok := header[6].(uint8); ok {
        block.difficulty = uint32(difficulty)
      }
      if difficulty, ok := header[6].(uint64); ok {
        block.difficulty = uint32(difficulty)
      }

      // It's either 8bit or 64
      if time, ok := header[7].(uint8); ok {
        block.time = int64(time)
      }
      if time, ok := header[7].(uint64); ok {
        block.time = int64(time)
      }

      if nonce, ok := header[8].(uint8); ok {
        block.nonce = uint32(nonce)
      }

      if extra, ok := header[9].([]byte); ok {
        block.extra = string(extra)
      }
    }

    if txSlice, ok := slice[1].([]interface{}); ok {
      // Create transaction slice equal to decoded tx interface slice
      block.transactions = make([]*Transaction, len(txSlice))

      // Unmarshal transactions
      for i, tx := range txSlice {
        if t, ok := tx.([]byte); ok {
          tx := &Transaction{}
          // Use the unmarshaled data to unmarshal the transaction
          // t is still decoded.
          tx.UnmarshalRlp(t)

          block.transactions[i] = tx
        }
      }
    }
  }
}