forked from cerc-io/plugeth
Updated block to use state instead of trie directly
This commit is contained in:
parent
f1b354e6aa
commit
d65b4cd0dd
@ -34,7 +34,8 @@ type Block struct {
|
|||||||
// The coin base address
|
// The coin base address
|
||||||
Coinbase []byte
|
Coinbase []byte
|
||||||
// Block Trie state
|
// Block Trie state
|
||||||
state *ethutil.Trie
|
//state *ethutil.Trie
|
||||||
|
state *State
|
||||||
// Difficulty for the current block
|
// Difficulty for the current block
|
||||||
Difficulty *big.Int
|
Difficulty *big.Int
|
||||||
// Creation time
|
// Creation time
|
||||||
@ -94,7 +95,7 @@ func CreateBlock(root interface{},
|
|||||||
block.SetTransactions(txes)
|
block.SetTransactions(txes)
|
||||||
block.SetUncles([]*Block{})
|
block.SetUncles([]*Block{})
|
||||||
|
|
||||||
block.state = ethutil.NewTrie(ethutil.Config.Db, root)
|
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, root))
|
||||||
|
|
||||||
for _, tx := range txes {
|
for _, tx := range txes {
|
||||||
block.MakeContract(tx)
|
block.MakeContract(tx)
|
||||||
@ -109,15 +110,15 @@ func (block *Block) Hash() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) HashNoNonce() []byte {
|
func (block *Block) HashNoNonce() []byte {
|
||||||
return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.Root, block.TxSha, block.Difficulty, block.Time, block.Extra}))
|
return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Extra}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) PrintHash() {
|
func (block *Block) PrintHash() {
|
||||||
fmt.Println(block)
|
fmt.Println(block)
|
||||||
fmt.Println(ethutil.NewValue(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.Root, block.TxSha, block.Difficulty, block.Time, block.Extra, block.Nonce})))
|
fmt.Println(ethutil.NewValue(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Extra, block.Nonce})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) State() *ethutil.Trie {
|
func (block *Block) State() *State {
|
||||||
return block.state
|
return block.state
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +126,7 @@ func (block *Block) Transactions() []*Transaction {
|
|||||||
return block.transactions
|
return block.transactions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (block *Block) GetContract(addr []byte) *Contract {
|
func (block *Block) GetContract(addr []byte) *Contract {
|
||||||
data := block.state.Get(string(addr))
|
data := block.state.Get(string(addr))
|
||||||
if data == "" {
|
if data == "" {
|
||||||
@ -152,13 +154,13 @@ func (block *Block) UpdateContract(addr []byte, contract *Contract) {
|
|||||||
// Make sure the state is synced
|
// Make sure the state is synced
|
||||||
//contract.State().Sync()
|
//contract.State().Sync()
|
||||||
|
|
||||||
block.state.Update(string(addr), string(contract.RlpEncode()))
|
block.state.trie.Update(string(addr), string(contract.RlpEncode()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) GetAddr(addr []byte) *Address {
|
func (block *Block) GetAddr(addr []byte) *Address {
|
||||||
var address *Address
|
var address *Address
|
||||||
|
|
||||||
data := block.State().Get(string(addr))
|
data := block.state.trie.Get(string(addr))
|
||||||
if data == "" {
|
if data == "" {
|
||||||
address = NewAddress(big.NewInt(0))
|
address = NewAddress(big.NewInt(0))
|
||||||
} else {
|
} else {
|
||||||
@ -168,11 +170,12 @@ func (block *Block) GetAddr(addr []byte) *Address {
|
|||||||
return address
|
return address
|
||||||
}
|
}
|
||||||
func (block *Block) UpdateAddr(addr []byte, address *Address) {
|
func (block *Block) UpdateAddr(addr []byte, address *Address) {
|
||||||
block.state.Update(string(addr), string(address.RlpEncode()))
|
block.state.trie.Update(string(addr), string(address.RlpEncode()))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
|
func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
|
||||||
contract := block.GetContract(addr)
|
contract := block.state.GetContract(addr)
|
||||||
// If we can't pay the fee return
|
// If we can't pay the fee return
|
||||||
if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
|
if contract == nil || contract.Amount.Cmp(fee) < 0 /* amount < fee */ {
|
||||||
fmt.Println("Contract has insufficient funds", contract.Amount, fee)
|
fmt.Println("Contract has insufficient funds", contract.Amount, fee)
|
||||||
@ -182,9 +185,9 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
|
|||||||
|
|
||||||
base := new(big.Int)
|
base := new(big.Int)
|
||||||
contract.Amount = base.Sub(contract.Amount, fee)
|
contract.Amount = base.Sub(contract.Amount, fee)
|
||||||
block.state.Update(string(addr), string(contract.RlpEncode()))
|
block.state.trie.Update(string(addr), string(contract.RlpEncode()))
|
||||||
|
|
||||||
data := block.state.Get(string(block.Coinbase))
|
data := block.state.trie.Get(string(block.Coinbase))
|
||||||
|
|
||||||
// Get the ether (Coinbase) and add the fee (gief fee to miner)
|
// Get the ether (Coinbase) and add the fee (gief fee to miner)
|
||||||
ether := NewAddressFromData([]byte(data))
|
ether := NewAddressFromData([]byte(data))
|
||||||
@ -192,7 +195,7 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
|
|||||||
base = new(big.Int)
|
base = new(big.Int)
|
||||||
ether.Amount = base.Add(ether.Amount, fee)
|
ether.Amount = base.Add(ether.Amount, fee)
|
||||||
|
|
||||||
block.state.Update(string(block.Coinbase), string(ether.RlpEncode()))
|
block.state.trie.Update(string(block.Coinbase), string(ether.RlpEncode()))
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -207,25 +210,29 @@ func (block *Block) BlockInfo() BlockInfo {
|
|||||||
|
|
||||||
// Sync the block's state and contract respectively
|
// Sync the block's state and contract respectively
|
||||||
func (block *Block) Sync() {
|
func (block *Block) Sync() {
|
||||||
// Sync all contracts currently in cache
|
/*
|
||||||
for _, val := range block.contractStates {
|
// Sync all contracts currently in cache
|
||||||
val.Sync()
|
for _, val := range block.contractStates {
|
||||||
}
|
val.Sync()
|
||||||
|
}
|
||||||
|
*/
|
||||||
// Sync the block state itself
|
// Sync the block state itself
|
||||||
block.state.Sync()
|
block.state.trie.Sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) Undo() {
|
func (block *Block) Undo() {
|
||||||
// Sync all contracts currently in cache
|
/*
|
||||||
for _, val := range block.contractStates {
|
// Sync all contracts currently in cache
|
||||||
val.Undo()
|
for _, val := range block.contractStates {
|
||||||
}
|
val.Undo()
|
||||||
|
}
|
||||||
|
*/
|
||||||
// Sync the block state itself
|
// Sync the block state itself
|
||||||
block.state.Undo()
|
block.state.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) MakeContract(tx *Transaction) {
|
func (block *Block) MakeContract(tx *Transaction) {
|
||||||
contract := MakeContract(tx, NewState(block.state))
|
contract := MakeContract(tx, block.state)
|
||||||
if contract != nil {
|
if contract != nil {
|
||||||
block.contractStates[string(tx.Hash()[12:])] = contract.state
|
block.contractStates[string(tx.Hash()[12:])] = contract.state
|
||||||
}
|
}
|
||||||
@ -308,7 +315,7 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
|
|||||||
block.PrevHash = header.Get(0).Bytes()
|
block.PrevHash = header.Get(0).Bytes()
|
||||||
block.UncleSha = header.Get(1).Bytes()
|
block.UncleSha = header.Get(1).Bytes()
|
||||||
block.Coinbase = header.Get(2).Bytes()
|
block.Coinbase = header.Get(2).Bytes()
|
||||||
block.state = ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val)
|
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
|
||||||
block.TxSha = header.Get(4).Bytes()
|
block.TxSha = header.Get(4).Bytes()
|
||||||
block.Difficulty = header.Get(5).BigInt()
|
block.Difficulty = header.Get(5).BigInt()
|
||||||
block.Time = int64(header.Get(6).BigInt().Uint64())
|
block.Time = int64(header.Get(6).BigInt().Uint64())
|
||||||
@ -345,7 +352,7 @@ func NewUncleBlockFromValue(header *ethutil.Value) *Block {
|
|||||||
block.PrevHash = header.Get(0).Bytes()
|
block.PrevHash = header.Get(0).Bytes()
|
||||||
block.UncleSha = header.Get(1).Bytes()
|
block.UncleSha = header.Get(1).Bytes()
|
||||||
block.Coinbase = header.Get(2).Bytes()
|
block.Coinbase = header.Get(2).Bytes()
|
||||||
block.state = ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val)
|
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
|
||||||
block.TxSha = header.Get(4).Bytes()
|
block.TxSha = header.Get(4).Bytes()
|
||||||
block.Difficulty = header.Get(5).BigInt()
|
block.Difficulty = header.Get(5).BigInt()
|
||||||
block.Time = int64(header.Get(6).BigInt().Uint64())
|
block.Time = int64(header.Get(6).BigInt().Uint64())
|
||||||
@ -356,7 +363,7 @@ func NewUncleBlockFromValue(header *ethutil.Value) *Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (block *Block) String() string {
|
func (block *Block) String() string {
|
||||||
return fmt.Sprintf("Block(%x):\nPrevHash:%x\nUncleSha:%x\nCoinbase:%x\nRoot:%x\nTxSha:%x\nDiff:%v\nTime:%d\nNonce:%x\nTxs:%d\n", block.Hash(), block.PrevHash, block.UncleSha, block.Coinbase, block.state.Root, block.TxSha, block.Difficulty, block.Time, block.Nonce, len(block.transactions))
|
return fmt.Sprintf("Block(%x):\nPrevHash:%x\nUncleSha:%x\nCoinbase:%x\nRoot:%x\nTxSha:%x\nDiff:%v\nTime:%d\nNonce:%x\nTxs:%d\n", block.Hash(), block.PrevHash, block.UncleSha, block.Coinbase, block.state.trie.Root, block.TxSha, block.Difficulty, block.Time, block.Nonce, len(block.transactions))
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////// UNEXPORTED /////////////////
|
//////////// UNEXPORTED /////////////////
|
||||||
@ -369,7 +376,7 @@ func (block *Block) header() []interface{} {
|
|||||||
// Coinbase address
|
// Coinbase address
|
||||||
block.Coinbase,
|
block.Coinbase,
|
||||||
// root state
|
// root state
|
||||||
block.state.Root,
|
block.state.trie.Root,
|
||||||
// Sha of tx
|
// Sha of tx
|
||||||
block.TxSha,
|
block.TxSha,
|
||||||
// Current block Difficulty
|
// Current block Difficulty
|
||||||
|
@ -39,7 +39,7 @@ func (bc *BlockChain) NewBlock(coinbase []byte, txs []*Transaction) *Block {
|
|||||||
hash := ZeroHash256
|
hash := ZeroHash256
|
||||||
|
|
||||||
if bc.CurrentBlock != nil {
|
if bc.CurrentBlock != nil {
|
||||||
root = bc.CurrentBlock.State().Root
|
root = bc.CurrentBlock.state.trie.Root
|
||||||
hash = bc.LastBlockHash
|
hash = bc.LastBlockHash
|
||||||
lastBlockTime = bc.CurrentBlock.Time
|
lastBlockTime = bc.CurrentBlock.Time
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,9 @@ func AddTestNetFunds(block *Block) {
|
|||||||
} {
|
} {
|
||||||
//log.Println("2^200 Wei to", addr)
|
//log.Println("2^200 Wei to", addr)
|
||||||
codedAddr, _ := hex.DecodeString(addr)
|
codedAddr, _ := hex.DecodeString(addr)
|
||||||
addr := block.GetAddr(codedAddr)
|
addr := block.state.GetAccount(codedAddr)
|
||||||
addr.Amount = ethutil.BigPow(2, 200)
|
addr.Amount = ethutil.BigPow(2, 200)
|
||||||
block.UpdateAddr(codedAddr, addr)
|
block.state.UpdateAccount(codedAddr, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ func NewBlockManager(speaker PublicSpeaker) *BlockManager {
|
|||||||
if bm.bc.CurrentBlock == nil {
|
if bm.bc.CurrentBlock == nil {
|
||||||
AddTestNetFunds(bm.bc.genesisBlock)
|
AddTestNetFunds(bm.bc.genesisBlock)
|
||||||
|
|
||||||
bm.bc.genesisBlock.State().Sync()
|
bm.bc.genesisBlock.state.trie.Sync()
|
||||||
// Prepare the genesis block
|
// Prepare the genesis block
|
||||||
bm.bc.Add(bm.bc.genesisBlock)
|
bm.bc.Add(bm.bc.genesisBlock)
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ func NewBlockManager(speaker PublicSpeaker) *BlockManager {
|
|||||||
|
|
||||||
// Watches any given address and puts it in the address state store
|
// Watches any given address and puts it in the address state store
|
||||||
func (bm *BlockManager) WatchAddr(addr []byte) *AddressState {
|
func (bm *BlockManager) WatchAddr(addr []byte) *AddressState {
|
||||||
account := bm.bc.CurrentBlock.GetAddr(addr)
|
account := bm.bc.CurrentBlock.state.GetAccount(addr)
|
||||||
|
|
||||||
return bm.addrStateStore.Add(addr, account)
|
return bm.addrStateStore.Add(addr, account)
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ func (bm *BlockManager) WatchAddr(addr []byte) *AddressState {
|
|||||||
func (bm *BlockManager) GetAddrState(addr []byte) *AddressState {
|
func (bm *BlockManager) GetAddrState(addr []byte) *AddressState {
|
||||||
account := bm.addrStateStore.Get(addr)
|
account := bm.addrStateStore.Get(addr)
|
||||||
if account == nil {
|
if account == nil {
|
||||||
a := bm.bc.CurrentBlock.GetAddr(addr)
|
a := bm.bc.CurrentBlock.state.GetAccount(addr)
|
||||||
account = &AddressState{Nonce: a.Nonce, Account: a}
|
account = &AddressState{Nonce: a.Nonce, Account: a}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ func (bm *BlockManager) ApplyTransactions(block *Block, txs []*Transaction) {
|
|||||||
if tx.IsContract() {
|
if tx.IsContract() {
|
||||||
block.MakeContract(tx)
|
block.MakeContract(tx)
|
||||||
} else {
|
} else {
|
||||||
if contract := block.GetContract(tx.Recipient); contract != nil {
|
if contract := block.state.GetContract(tx.Recipient); contract != nil {
|
||||||
bm.ProcessContract(contract, tx, block)
|
bm.ProcessContract(contract, tx, block)
|
||||||
} else {
|
} else {
|
||||||
err := bm.TransactionPool.ProcessTransaction(tx, block)
|
err := bm.TransactionPool.ProcessTransaction(tx, block)
|
||||||
@ -161,8 +161,8 @@ func (bm *BlockManager) ProcessBlock(block *Block) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !block.State().Cmp(bm.bc.CurrentBlock.State()) {
|
if !block.state.Cmp(bm.bc.CurrentBlock.state) {
|
||||||
return fmt.Errorf("Invalid merkle root. Expected %x, got %x", block.State().Root, bm.bc.CurrentBlock.State().Root)
|
return fmt.Errorf("Invalid merkle root. Expected %x, got %x", block.State().trie.Root, bm.bc.CurrentBlock.State().trie.Root)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the new total difficulty and sync back to the db
|
// Calculate the new total difficulty and sync back to the db
|
||||||
@ -267,17 +267,17 @@ func CalculateUncleReward(block *Block) *big.Int {
|
|||||||
|
|
||||||
func (bm *BlockManager) AccumelateRewards(processor *Block, block *Block) error {
|
func (bm *BlockManager) AccumelateRewards(processor *Block, block *Block) error {
|
||||||
// Get the coinbase rlp data
|
// Get the coinbase rlp data
|
||||||
addr := processor.GetAddr(block.Coinbase)
|
addr := processor.state.GetAccount(block.Coinbase)
|
||||||
// Reward amount of ether to the coinbase address
|
// Reward amount of ether to the coinbase address
|
||||||
addr.AddFee(CalculateBlockReward(block, len(block.Uncles)))
|
addr.AddFee(CalculateBlockReward(block, len(block.Uncles)))
|
||||||
|
|
||||||
processor.UpdateAddr(block.Coinbase, addr)
|
processor.state.UpdateAccount(block.Coinbase, addr)
|
||||||
|
|
||||||
for _, uncle := range block.Uncles {
|
for _, uncle := range block.Uncles {
|
||||||
uncleAddr := processor.GetAddr(uncle.Coinbase)
|
uncleAddr := processor.state.GetAccount(uncle.Coinbase)
|
||||||
uncleAddr.AddFee(CalculateUncleReward(uncle))
|
uncleAddr.AddFee(CalculateUncleReward(uncle))
|
||||||
|
|
||||||
processor.UpdateAddr(uncle.Coinbase, uncleAddr)
|
processor.state.UpdateAccount(uncle.Coinbase, uncleAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -298,7 +298,7 @@ func (bm *BlockManager) ProcessContract(contract *Contract, tx *Transaction, blo
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
vm := &Vm{}
|
vm := &Vm{}
|
||||||
vm.Process(contract, NewState(block.state), RuntimeVars{
|
vm.Process(contract, block.state, RuntimeVars{
|
||||||
address: tx.Hash()[12:],
|
address: tx.Hash()[12:],
|
||||||
blockNumber: block.BlockInfo().Number,
|
blockNumber: block.BlockInfo().Number,
|
||||||
sender: tx.Sender(),
|
sender: tx.Sender(),
|
||||||
|
@ -13,6 +13,10 @@ func NewState(trie *ethutil.Trie) *State {
|
|||||||
return &State{trie: trie}
|
return &State{trie: trie}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *State) Reset() {
|
||||||
|
s.trie.Undo()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *State) GetContract(addr []byte) *Contract {
|
func (s *State) GetContract(addr []byte) *Contract {
|
||||||
data := s.trie.Get(string(addr))
|
data := s.trie.Get(string(addr))
|
||||||
if data == "" {
|
if data == "" {
|
||||||
@ -54,3 +58,7 @@ func (s *State) GetAccount(addr []byte) (account *Address) {
|
|||||||
func (s *State) UpdateAccount(addr []byte, account *Address) {
|
func (s *State) UpdateAccount(addr []byte, account *Address) {
|
||||||
s.trie.Update(string(addr), string(account.RlpEncode()))
|
s.trie.Update(string(addr), string(account.RlpEncode()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *State) Cmp(other *State) bool {
|
||||||
|
return s.trie.Cmp(other.trie)
|
||||||
|
}
|
||||||
|
@ -104,7 +104,7 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
// Get the sender
|
// Get the sender
|
||||||
sender := block.GetAddr(tx.Sender())
|
sender := block.state.GetAccount(tx.Sender())
|
||||||
|
|
||||||
// Make sure there's enough in the sender's account. Having insufficient
|
// Make sure there's enough in the sender's account. Having insufficient
|
||||||
// funds won't invalidate this transaction but simple ignores it.
|
// funds won't invalidate this transaction but simple ignores it.
|
||||||
@ -122,7 +122,7 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the receiver
|
// Get the receiver
|
||||||
receiver := block.GetAddr(tx.Recipient)
|
receiver := block.state.GetAccount(tx.Recipient)
|
||||||
sender.Nonce += 1
|
sender.Nonce += 1
|
||||||
|
|
||||||
// Send Tx to self
|
// Send Tx to self
|
||||||
@ -136,10 +136,10 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, block *Block) (err error
|
|||||||
// Add the amount to receivers account which should conclude this transaction
|
// Add the amount to receivers account which should conclude this transaction
|
||||||
receiver.Amount.Add(receiver.Amount, tx.Value)
|
receiver.Amount.Add(receiver.Amount, tx.Value)
|
||||||
|
|
||||||
block.UpdateAddr(tx.Recipient, receiver)
|
block.state.UpdateAccount(tx.Recipient, receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
block.UpdateAddr(tx.Sender(), sender)
|
block.state.UpdateAccount(tx.Sender(), sender)
|
||||||
|
|
||||||
log.Printf("[TXPL] Processed Tx %x\n", tx.Hash())
|
log.Printf("[TXPL] Processed Tx %x\n", tx.Hash())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user