|
|
@ -15,6 +15,7 @@ type Block struct { |
|
|
|
uncles []*Block |
|
|
|
uncles []*Block |
|
|
|
coinbase string |
|
|
|
coinbase string |
|
|
|
// state xxx
|
|
|
|
// state xxx
|
|
|
|
|
|
|
|
state *Trie |
|
|
|
difficulty uint32 |
|
|
|
difficulty uint32 |
|
|
|
// Creation time
|
|
|
|
// Creation time
|
|
|
|
time int64 |
|
|
|
time int64 |
|
|
@ -34,7 +35,7 @@ func NewBlock(raw []byte) *Block { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Creates a new block. This is currently for testing
|
|
|
|
// 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{ |
|
|
|
block := &Block{ |
|
|
|
// Slice of transactions to include in this block
|
|
|
|
// Slice of transactions to include in this block
|
|
|
|
transactions: transactions, |
|
|
|
transactions: transactions, |
|
|
@ -49,12 +50,32 @@ func CreateBlock(/* TODO use raw data */transactions []*Transaction) *Block { |
|
|
|
return 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() { |
|
|
|
func (block *Block) Update() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Returns a hash of the block
|
|
|
|
// Returns a hash of the block
|
|
|
|
func (block *Block) Hash() string { |
|
|
|
func (block *Block) Hash() []byte { |
|
|
|
return Sha256Hex(block.MarshalRlp()) |
|
|
|
return Sha256Bin(block.MarshalRlp()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (block *Block) MarshalRlp() []byte { |
|
|
|
func (block *Block) MarshalRlp() []byte { |
|
|
@ -73,7 +94,7 @@ func (block *Block) MarshalRlp() []byte { |
|
|
|
"", |
|
|
|
"", |
|
|
|
block.coinbase, |
|
|
|
block.coinbase, |
|
|
|
// root state
|
|
|
|
// root state
|
|
|
|
"", |
|
|
|
block.state.root, |
|
|
|
// Sha of tx
|
|
|
|
// Sha of tx
|
|
|
|
string(Sha256Bin([]byte(Encode(encTx)))), |
|
|
|
string(Sha256Bin([]byte(Encode(encTx)))), |
|
|
|
block.difficulty, |
|
|
|
block.difficulty, |
|
|
@ -99,7 +120,7 @@ func (block *Block) UnmarshalRlp(data []byte) { |
|
|
|
block.number = uint32(number) |
|
|
|
block.number = uint32(number) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if prevHash, ok := header[1].([]byte); ok { |
|
|
|
if prevHash, ok := header[1].([]uint8); ok { |
|
|
|
block.prevHash = string(prevHash) |
|
|
|
block.prevHash = string(prevHash) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -109,7 +130,12 @@ func (block *Block) UnmarshalRlp(data []byte) { |
|
|
|
block.coinbase = string(coinbase) |
|
|
|
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]
|
|
|
|
// sha is header[5]
|
|
|
|
|
|
|
|
|
|
|
|