go-ethereum/ethchain/block.go

380 lines
8.8 KiB
Go
Raw Normal View History

2014-02-15 00:56:09 +02:00
package ethchain
import (
"fmt"
"github.com/ethereum/eth-go/ethutil"
"math/big"
2014-05-20 16:02:46 +03:00
"strconv"
2014-02-15 00:56:09 +02:00
"time"
)
type BlockInfo struct {
Number uint64
Hash []byte
Parent []byte
}
func (bi *BlockInfo) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data)
bi.Number = decoder.Get(0).Uint()
bi.Hash = decoder.Get(1).Bytes()
bi.Parent = decoder.Get(2).Bytes()
}
func (bi *BlockInfo) RlpEncode() []byte {
return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent})
}
type Block struct {
// Hash to the previous block
PrevHash []byte
// Uncles of this block
Uncles []*Block
UncleSha []byte
// The coin base address
Coinbase []byte
// Block Trie state
//state *ethutil.Trie
state *State
2014-02-15 00:56:09 +02:00
// Difficulty for the current block
Difficulty *big.Int
// Creation time
Time int64
2014-05-20 15:29:52 +03:00
// The block number
Number *big.Int
// Minimum Gas Price
MinGasPrice *big.Int
// Gas limit
GasLimit *big.Int
// Gas used
GasUsed *big.Int
2014-02-15 00:56:09 +02:00
// Extra data
Extra string
// Block Nonce for verification
Nonce []byte
// List of transactions and/or contracts
transactions []*Transaction
TxSha []byte
2014-02-19 12:35:17 +02:00
contractStates map[string]*ethutil.Trie
2014-02-15 00:56:09 +02:00
}
// New block takes a raw encoded string
// XXX DEPRICATED
func NewBlockFromData(raw []byte) *Block {
return NewBlockFromBytes(raw)
}
func NewBlockFromBytes(raw []byte) *Block {
block := &Block{}
block.RlpDecode(raw)
return block
}
// New block takes a raw encoded string
func NewBlockFromRlpValue(rlpValue *ethutil.Value) *Block {
block := &Block{}
block.RlpValueDecode(rlpValue)
return block
}
func CreateBlock(root interface{},
prevHash []byte,
base []byte,
Difficulty *big.Int,
Nonce []byte,
extra string,
txes []*Transaction) *Block {
block := &Block{
// Slice of transactions to include in this block
2014-02-19 12:35:17 +02:00
transactions: txes,
PrevHash: prevHash,
Coinbase: base,
Difficulty: Difficulty,
Nonce: Nonce,
Time: time.Now().Unix(),
Extra: extra,
UncleSha: EmptyShaList,
2014-05-20 20:19:53 +03:00
GasUsed: new(big.Int),
MinGasPrice: new(big.Int),
GasLimit: new(big.Int),
2014-02-19 12:35:17 +02:00
contractStates: make(map[string]*ethutil.Trie),
2014-02-15 00:56:09 +02:00
}
block.SetTransactions(txes)
block.SetUncles([]*Block{})
2014-04-29 13:36:27 +03:00
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, root))
2014-02-15 00:56:09 +02:00
for _, tx := range txes {
block.MakeContract(tx)
}
return block
}
// Returns a hash of the block
func (block *Block) Hash() []byte {
2014-02-15 02:34:18 +02:00
return ethutil.Sha3Bin(block.Value().Encode())
2014-02-15 00:56:09 +02:00
}
func (block *Block) HashNoNonce() []byte {
return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Extra}))
2014-02-15 00:56:09 +02:00
}
func (block *Block) State() *State {
2014-02-15 00:56:09 +02:00
return block.state
}
func (block *Block) Transactions() []*Transaction {
return block.transactions
}
func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
contract := block.state.GetStateObject(addr)
2014-02-15 00:56:09 +02:00
// If we can't pay the fee return
if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
fmt.Println("Contract has insufficient funds", contract.Amount, fee)
return false
}
base := new(big.Int)
contract.Amount = base.Sub(contract.Amount, fee)
block.state.trie.Update(string(addr), string(contract.RlpEncode()))
2014-02-15 00:56:09 +02:00
data := block.state.trie.Get(string(block.Coinbase))
2014-02-15 00:56:09 +02:00
// Get the ether (Coinbase) and add the fee (gief fee to miner)
account := NewStateObjectFromBytes(block.Coinbase, []byte(data))
2014-02-15 00:56:09 +02:00
base = new(big.Int)
account.Amount = base.Add(account.Amount, fee)
2014-02-15 00:56:09 +02:00
//block.state.trie.Update(string(block.Coinbase), string(ether.RlpEncode()))
block.state.UpdateStateObject(account)
2014-02-15 00:56:09 +02:00
return true
}
func (block *Block) BlockInfo() BlockInfo {
bi := BlockInfo{}
data, _ := ethutil.Config.Db.Get(append(block.Hash(), []byte("Info")...))
bi.RlpDecode(data)
return bi
}
2014-02-19 12:35:17 +02:00
// Sync the block's state and contract respectively
func (block *Block) Sync() {
block.state.Sync()
2014-02-19 12:35:17 +02:00
}
func (block *Block) Undo() {
// Sync the block state itself
block.state.Reset()
2014-02-19 12:35:17 +02:00
}
2014-02-15 00:56:09 +02:00
func (block *Block) MakeContract(tx *Transaction) {
contract := MakeContract(tx, block.state)
if contract != nil {
block.state.states[string(tx.Hash()[12:])] = contract.state
}
2014-02-15 00:56:09 +02:00
}
/////// Block Encoding
func (block *Block) rlpTxs() interface{} {
// Marshal the transactions of this block
encTx := make([]interface{}, len(block.transactions))
for i, tx := range block.transactions {
// Cast it to a string (safe)
encTx[i] = tx.RlpData()
}
return encTx
}
func (block *Block) rlpUncles() interface{} {
// Marshal the transactions of this block
uncles := make([]interface{}, len(block.Uncles))
for i, uncle := range block.Uncles {
// Cast it to a string (safe)
uncles[i] = uncle.header()
}
return uncles
}
func (block *Block) SetUncles(uncles []*Block) {
block.Uncles = uncles
// Sha of the concatenated uncles
block.UncleSha = ethutil.Sha3Bin(ethutil.Encode(block.rlpUncles()))
}
func (block *Block) SetTransactions(txs []*Transaction) {
block.transactions = txs
2014-05-20 16:02:46 +03:00
trie := ethutil.NewTrie(ethutil.Config.Db, "")
for i, tx := range txs {
trie.Update(strconv.Itoa(i), string(tx.RlpEncode()))
}
2014-05-20 23:04:47 +03:00
switch trie.Root.(type) {
case string:
block.TxSha = []byte(trie.Root.(string))
case []byte:
block.TxSha = trie.Root.([]byte)
default:
panic(fmt.Sprintf("invalid root type %T", trie.Root))
}
2014-02-15 00:56:09 +02:00
}
2014-02-15 02:34:18 +02:00
func (block *Block) Value() *ethutil.Value {
return ethutil.NewValue([]interface{}{block.header(), block.rlpTxs(), block.rlpUncles()})
2014-02-15 00:56:09 +02:00
}
func (block *Block) RlpEncode() []byte {
// Encode a slice interface which contains the header and the list of
// transactions.
2014-02-15 02:34:18 +02:00
return block.Value().Encode()
2014-02-15 00:56:09 +02:00
}
func (block *Block) RlpDecode(data []byte) {
rlpValue := ethutil.NewValueFromBytes(data)
block.RlpValueDecode(rlpValue)
}
func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
header := decoder.Get(0)
block.PrevHash = header.Get(0).Bytes()
block.UncleSha = header.Get(1).Bytes()
block.Coinbase = header.Get(2).Bytes()
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
2014-02-15 00:56:09 +02:00
block.TxSha = header.Get(4).Bytes()
block.Difficulty = header.Get(5).BigInt()
2014-05-20 15:29:52 +03:00
block.Number = header.Get(6).BigInt()
block.MinGasPrice = header.Get(7).BigInt()
block.GasLimit = header.Get(8).BigInt()
block.GasUsed = header.Get(9).BigInt()
block.Time = int64(header.Get(10).BigInt().Uint64())
block.Extra = header.Get(11).Str()
block.Nonce = header.Get(12).Bytes()
2014-02-19 12:35:17 +02:00
block.contractStates = make(map[string]*ethutil.Trie)
2014-02-15 00:56:09 +02:00
// Tx list might be empty if this is an uncle. Uncles only have their
// header set.
if decoder.Get(1).IsNil() == false { // Yes explicitness
txes := decoder.Get(1)
block.transactions = make([]*Transaction, txes.Len())
for i := 0; i < txes.Len(); i++ {
tx := NewTransactionFromValue(txes.Get(i))
block.transactions[i] = tx
}
}
if decoder.Get(2).IsNil() == false { // Yes explicitness
uncles := decoder.Get(2)
block.Uncles = make([]*Block, uncles.Len())
for i := 0; i < uncles.Len(); i++ {
block.Uncles[i] = NewUncleBlockFromValue(uncles.Get(i))
}
}
}
func NewUncleBlockFromValue(header *ethutil.Value) *Block {
block := &Block{}
block.PrevHash = header.Get(0).Bytes()
block.UncleSha = header.Get(1).Bytes()
block.Coinbase = header.Get(2).Bytes()
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
2014-02-15 00:56:09 +02:00
block.TxSha = header.Get(4).Bytes()
block.Difficulty = header.Get(5).BigInt()
2014-05-20 15:29:52 +03:00
block.Number = header.Get(6).BigInt()
block.MinGasPrice = header.Get(7).BigInt()
block.GasLimit = header.Get(8).BigInt()
block.GasUsed = header.Get(9).BigInt()
block.Time = int64(header.Get(10).BigInt().Uint64())
block.Extra = header.Get(11).Str()
block.Nonce = header.Get(12).Bytes()
2014-02-15 00:56:09 +02:00
return block
}
2014-03-20 12:20:29 +02:00
func (block *Block) GetRoot() interface{} {
return block.state.trie.Root
}
2014-02-15 00:56:09 +02:00
func (block *Block) header() []interface{} {
return []interface{}{
// Sha of the previous block
block.PrevHash,
// Sha of uncles
block.UncleSha,
// Coinbase address
block.Coinbase,
// root state
block.state.trie.Root,
2014-02-15 00:56:09 +02:00
// Sha of tx
block.TxSha,
// Current block Difficulty
block.Difficulty,
2014-05-20 15:29:52 +03:00
// The block number
block.Number,
// Block minimum gas price
block.MinGasPrice,
// Block upper gas bound
block.GasLimit,
// Block gas used
block.GasUsed,
2014-02-15 00:56:09 +02:00
// Time the block was found?
block.Time,
// Extra data
block.Extra,
// Block's Nonce for validation
block.Nonce,
}
}
2014-05-20 16:02:46 +03:00
func (block *Block) String() string {
return fmt.Sprintf(`
BLOCK(%x):
PrevHash: %x
UncleSha: %x
Coinbase: %x
Root: %x
TxSha: %x
Difficulty: %v
Number: %v
MinGas: %v
MaxLimit: %v
GasUsed: %v
Time: %v
Extra: %v
Nonce: %x
`,
block.Hash(),
block.PrevHash,
block.UncleSha,
block.Coinbase,
block.state.trie.Root,
block.TxSha,
block.Difficulty,
block.Number,
block.MinGasPrice,
block.GasLimit,
block.GasUsed,
block.Time,
block.Extra,
block.Nonce)
}