erigon-pulse/block.go

185 lines
4.5 KiB
Go
Raw Normal View History

2013-12-26 11:45:52 +00:00
package main
import (
2013-12-29 22:53:12 +00:00
_"fmt"
2013-12-27 20:22:57 +00:00
"time"
2013-12-28 14:18:08 +00:00
_"bytes"
2013-12-26 11:45:52 +00:00
)
type Block struct {
2013-12-29 00:36:59 +00:00
// The number of this block
2013-12-27 20:22:57 +00:00
number uint32
2013-12-29 00:36:59 +00:00
// Hash to the previous block
2013-12-27 20:22:57 +00:00
prevHash string
2013-12-29 00:36:59 +00:00
// Uncles of this block
2013-12-27 20:22:57 +00:00
uncles []*Block
coinbase string
// state xxx
2014-01-02 22:02:24 +00:00
state *Trie
2013-12-28 14:18:08 +00:00
difficulty uint32
2013-12-29 00:36:59 +00:00
// Creation time
2013-12-29 22:53:12 +00:00
time int64
2013-12-28 14:18:08 +00:00
nonce uint32
2013-12-29 00:36:59 +00:00
// List of transactions and/or contracts
2013-12-27 20:22:57 +00:00
transactions []*Transaction
2013-12-29 22:53:12 +00:00
extra string
}
// New block takes a raw encoded string
func NewBlock(raw []byte) *Block {
block := &Block{}
block.UnmarshalRlp(raw)
return block
2013-12-26 11:45:52 +00:00
}
2013-12-29 00:36:59 +00:00
// Creates a new block. This is currently for testing
2014-01-02 22:02:24 +00:00
func CreateTestBlock(/* TODO use raw data */transactions []*Transaction) *Block {
2013-12-26 11:45:52 +00:00
block := &Block{
// Slice of transactions to include in this block
transactions: transactions,
2013-12-28 14:18:08 +00:00
number: 1,
prevHash: "1234",
coinbase: "me",
difficulty: 10,
nonce: 0,
2013-12-29 22:53:12 +00:00
time: time.Now().Unix(),
2013-12-26 11:45:52 +00:00
}
return block
}
2014-01-02 22:02:24 +00:00
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
}
2013-12-26 11:45:52 +00:00
func (block *Block) Update() {
}
2013-12-27 20:22:57 +00:00
2013-12-29 00:36:59 +00:00
// Returns a hash of the block
2014-01-02 22:02:24 +00:00
func (block *Block) Hash() []byte {
return Sha256Bin(block.MarshalRlp())
2013-12-27 20:22:57 +00:00
}
func (block *Block) MarshalRlp() []byte {
2013-12-29 00:36:59 +00:00
// Marshal the transactions of this block
2013-12-27 20:22:57 +00:00
encTx := make([]string, len(block.transactions))
for i, tx := range block.transactions {
2013-12-29 00:36:59 +00:00
// Cast it to a string (safe)
2013-12-27 20:22:57 +00:00
encTx[i] = string(tx.MarshalRlp())
}
2013-12-28 14:18:08 +00:00
/* I made up the block. It should probably contain different data or types. It sole purpose now is testing */
2013-12-28 01:24:16 +00:00
header := []interface{}{
2013-12-27 20:22:57 +00:00
block.number,
2013-12-28 14:18:08 +00:00
block.prevHash,
2013-12-27 20:22:57 +00:00
// Sha of uncles
2013-12-28 14:18:08 +00:00
"",
block.coinbase,
2013-12-27 20:22:57 +00:00
// root state
2014-01-02 22:02:24 +00:00
block.state.root,
2013-12-29 22:53:12 +00:00
// Sha of tx
string(Sha256Bin([]byte(Encode(encTx)))),
2013-12-28 14:18:08 +00:00
block.difficulty,
2013-12-29 22:53:12 +00:00
uint64(block.time),
2013-12-28 14:18:08 +00:00
block.nonce,
2013-12-29 22:53:12 +00:00
block.extra,
2013-12-28 01:24:16 +00:00
}
2013-12-27 20:22:57 +00:00
2013-12-29 22:53:12 +00:00
// TODO
uncles := []interface{}{}
2013-12-29 00:36:59 +00:00
// Encode a slice interface which contains the header and the list of transactions.
2013-12-29 22:53:12 +00:00
return Encode([]interface{}{header, encTx, uncles})
2013-12-27 20:22:57 +00:00
}
func (block *Block) UnmarshalRlp(data []byte) {
2013-12-28 01:24:16 +00:00
t, _ := Decode(data,0)
2013-12-29 00:36:59 +00:00
// interface slice assertion
2013-12-28 01:24:16 +00:00
if slice, ok := t.([]interface{}); ok {
2013-12-29 00:36:59 +00:00
// interface slice assertion
2013-12-28 14:18:08 +00:00
if header, ok := slice[0].([]interface{}); ok {
if number, ok := header[0].(uint8); ok {
block.number = uint32(number)
}
2014-01-02 22:02:24 +00:00
if prevHash, ok := header[1].([]uint8); ok {
2013-12-28 14:18:08 +00:00
block.prevHash = string(prevHash)
}
// sha of uncles is header[2]
if coinbase, ok := header[3].([]byte); ok {
block.coinbase = string(coinbase)
}
2014-01-02 22:02:24 +00:00
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))
}
2013-12-28 14:18:08 +00:00
// 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)
}
2013-12-29 22:53:12 +00:00
// 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)
2013-12-28 14:18:08 +00:00
}
if nonce, ok := header[8].(uint8); ok {
block.nonce = uint32(nonce)
}
2013-12-29 22:53:12 +00:00
if extra, ok := header[9].([]byte); ok {
block.extra = string(extra)
}
2013-12-28 14:18:08 +00:00
}
if txSlice, ok := slice[1].([]interface{}); ok {
2013-12-29 00:36:59 +00:00
// Create transaction slice equal to decoded tx interface slice
2013-12-28 14:18:08 +00:00
block.transactions = make([]*Transaction, len(txSlice))
2013-12-29 00:36:59 +00:00
// Unmarshal transactions
2013-12-28 14:18:08 +00:00
for i, tx := range txSlice {
if t, ok := tx.([]byte); ok {
tx := &Transaction{}
2013-12-29 00:36:59 +00:00
// Use the unmarshaled data to unmarshal the transaction
// t is still decoded.
2013-12-28 14:18:08 +00:00
tx.UnmarshalRlp(t)
block.transactions[i] = tx
}
}
2013-12-28 01:24:16 +00:00
}
}
2013-12-27 20:22:57 +00:00
}