diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-03 20:50:51 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-03 20:50:51 +0800 |
commit | 6095edac5843aa18e389ef2deaa49fe05b7fcfb9 (patch) | |
tree | 851b5616b2024248b6b9790199ef289e04725cbb /chain | |
parent | 64f35ba8d1f31d6821a0a1bf946c71396a996f30 (diff) | |
parent | 3d9a4e7084c33cb28a2265c0dd232a0ea3871c92 (diff) | |
download | go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar.gz go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar.bz2 go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar.lz go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar.xz go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.tar.zst go-tangerine-6095edac5843aa18e389ef2deaa49fe05b7fcfb9.zip |
merge
Diffstat (limited to 'chain')
-rw-r--r-- | chain/block_manager.go | 55 | ||||
-rw-r--r-- | chain/chain_manager.go | 3 | ||||
-rw-r--r-- | chain/dagger.go | 14 | ||||
-rw-r--r-- | chain/error.go | 4 | ||||
-rw-r--r-- | chain/filter.go | 4 | ||||
-rw-r--r-- | chain/state_transition.go | 6 | ||||
-rw-r--r-- | chain/types/block.go | 2 | ||||
-rw-r--r-- | chain/types/bloom9.go | 4 |
8 files changed, 49 insertions, 43 deletions
diff --git a/chain/block_manager.go b/chain/block_manager.go index e40ff7955..56511113d 100644 --- a/chain/block_manager.go +++ b/chain/block_manager.go @@ -118,6 +118,19 @@ func (sm *BlockManager) ChainManager() *ChainManager { return sm.bc } +func (sm *BlockManager) TransitionState(statedb *state.State, parent, block *types.Block) (receipts types.Receipts, err error) { + coinbase := statedb.GetOrNewStateObject(block.Coinbase) + coinbase.SetGasPool(block.CalcGasLimit(parent)) + + // Process the transactions on to current block + receipts, _, _, _, err = sm.ProcessTransactions(coinbase, statedb, block, parent, block.Transactions()) + if err != nil { + return nil, err + } + + return receipts, nil +} + func (self *BlockManager) ProcessTransactions(coinbase *state.StateObject, state *state.State, block, parent *types.Block, txs types.Transactions) (types.Receipts, types.Transactions, types.Transactions, types.Transactions, error) { var ( receipts types.Receipts @@ -125,6 +138,7 @@ func (self *BlockManager) ProcessTransactions(coinbase *state.StateObject, state erroneous types.Transactions totalUsedGas = big.NewInt(0) err error + cumulativeSum = new(big.Int) ) done: @@ -156,6 +170,7 @@ done: } txGas.Sub(txGas, st.gas) + cumulativeSum.Add(cumulativeSum, new(big.Int).Mul(txGas, tx.GasPrice)) // Update the state with pending changes state.Update(txGas) @@ -176,6 +191,7 @@ done: } } + block.Reward = cumulativeSum block.GasUsed = totalUsedGas return receipts, handled, unhandled, erroneous, err @@ -187,8 +203,7 @@ func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Mes defer sm.mutex.Unlock() if sm.bc.HasBlock(block.Hash()) { - fmt.Println("already having this block") - return nil, nil, nil + return nil, nil, &KnownBlockError{block.Number, block.Hash()} } if !sm.bc.HasBlock(block.PrevHash) { @@ -214,7 +229,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I fmt.Printf("## %x %x ##\n", block.Hash(), block.Number) } - _, err = sm.ApplyDiff(state, parent, block) + _, err = sm.TransitionState(state, parent, block) if err != nil { return } @@ -235,12 +250,10 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I // Block validation if err = sm.ValidateBlock(block, parent); err != nil { - statelogger.Errorln("validating block:", err) return } if err = sm.AccumelateRewards(state, block, parent); err != nil { - statelogger.Errorln("accumulating reward", err) return } @@ -273,7 +286,6 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I sm.transState = state.Copy() sm.eth.TxPool().RemoveSet(block.Transactions()) - fmt.Println("TD", td) return td, messages, nil } else { @@ -281,19 +293,6 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I } } -func (sm *BlockManager) ApplyDiff(state *state.State, parent, block *types.Block) (receipts types.Receipts, err error) { - coinbase := state.GetOrNewStateObject(block.Coinbase) - coinbase.SetGasPool(block.CalcGasLimit(parent)) - - // Process the transactions on to current block - receipts, _, _, _, err = sm.ProcessTransactions(coinbase, state, block, parent, block.Transactions()) - if err != nil { - return nil, err - } - - return receipts, nil -} - func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { uncleDiff := new(big.Int) for _, uncle := range block.Uncles { @@ -309,9 +308,6 @@ func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { // is greater than the previous. if td.Cmp(sm.bc.TD) > 0 { return td, true - - // Set the new total difficulty back to the block chain - //sm.bc.SetTotalDifficulty(td) } return nil, false @@ -375,17 +371,25 @@ func (sm *BlockManager) AccumelateRewards(state *state.State, block, parent *typ r := new(big.Int) r.Mul(BlockReward, big.NewInt(15)).Div(r, big.NewInt(16)) - uncleAccount := state.GetAccount(uncle.Coinbase) + uncleAccount := statedb.GetAccount(uncle.Coinbase) uncleAccount.AddAmount(r) reward.Add(reward, new(big.Int).Div(BlockReward, big.NewInt(32))) } // Get the account associated with the coinbase - account := state.GetAccount(block.Coinbase) + account := statedb.GetAccount(block.Coinbase) // Reward amount of ether to the coinbase address account.AddAmount(reward) + statedb.Manifest().AddMessage(&state.Message{ + To: block.Coinbase, From: block.Coinbase, + Input: nil, + Origin: nil, + Block: block.Hash(), Timestamp: block.Time, Coinbase: block.Coinbase, Number: block.Number, + Value: new(big.Int).Add(reward, block.Reward), + }) + return nil } @@ -403,8 +407,7 @@ func (sm *BlockManager) GetMessages(block *types.Block) (messages []*state.Messa defer state.Reset() - sm.ApplyDiff(state, parent, block) - + sm.TransitionState(state, parent, block) sm.AccumelateRewards(state, block, parent) return state.Manifest().Messages, nil diff --git a/chain/chain_manager.go b/chain/chain_manager.go index 8e0529533..8525ffdfd 100644 --- a/chain/chain_manager.go +++ b/chain/chain_manager.go @@ -258,6 +258,9 @@ func (self *ChainManager) InsertChain(chain Blocks) error { continue } + chainlogger.Infof("block #%v process failed (%x)\n", block.Number, block.Hash()[:4]) + chainlogger.Infoln(block) + chainlogger.Infoln(err) return err } diff --git a/chain/dagger.go b/chain/dagger.go index f7e2229e9..507850389 100644 --- a/chain/dagger.go +++ b/chain/dagger.go @@ -82,13 +82,17 @@ func (pow *EasyPow) Verify(hash []byte, diff *big.Int, nonce []byte) bool { d := append(hash, nonce...) sha.Write(d) - v := ethutil.BigPow(2, 256) - ret := new(big.Int).Div(v, diff) + verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) + res := ethutil.U256(ethutil.BigD(sha.Sum(nil))) - res := new(big.Int) - res.SetBytes(sha.Sum(nil)) + /* + fmt.Printf("hash w/o nonce %x\n", hash) + fmt.Printf("2**256 / %v = %v\n", diff, verification) + fmt.Printf("%v <= %v\n", res, verification) + fmt.Printf("vlen: %d rlen: %d\n", len(verification.Bytes()), len(res.Bytes())) + */ - return res.Cmp(ret) == -1 + return res.Cmp(verification) <= 0 } func (pow *EasyPow) SetHash(hash *big.Int) { diff --git a/chain/error.go b/chain/error.go index 7dce2b608..0c4d6e59e 100644 --- a/chain/error.go +++ b/chain/error.go @@ -128,12 +128,12 @@ func IsTDError(e error) bool { } type KnownBlockError struct { - number uint64 + number *big.Int hash []byte } func (self *KnownBlockError) Error() string { - return fmt.Sprintf("block %d already known (%x)", self.number, self.hash[0:4]) + return fmt.Sprintf("block %v already known (%x)", self.number, self.hash[0:4]) } func IsKnownBlockErr(e error) bool { _, ok := e.(*KnownBlockError) diff --git a/chain/filter.go b/chain/filter.go index fd8adaa8f..bb8db38a5 100644 --- a/chain/filter.go +++ b/chain/filter.go @@ -176,7 +176,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { var fromIncluded, toIncluded bool if len(self.from) > 0 { for _, from := range self.from { - if types.BloomLookup(block.LogsBloom, from) { + if types.BloomLookup(block.LogsBloom, from) || bytes.Equal(block.Coinbase, from) { fromIncluded = true break } @@ -187,7 +187,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { if len(self.to) > 0 { for _, to := range self.to { - if types.BloomLookup(block.LogsBloom, ethutil.U256(new(big.Int).Add(ethutil.Big1, ethutil.BigD(to))).Bytes()) { + if types.BloomLookup(block.LogsBloom, ethutil.U256(new(big.Int).Add(ethutil.Big1, ethutil.BigD(to))).Bytes()) || bytes.Equal(block.Coinbase, to) { toIncluded = true break } diff --git a/chain/state_transition.go b/chain/state_transition.go index 53b1f6e2d..8f7b55cd4 100644 --- a/chain/state_transition.go +++ b/chain/state_transition.go @@ -169,12 +169,6 @@ func (self *StateTransition) TransitionState() (err error) { return } - //dataPrice := big.NewInt(int64(len(self.data))) - //dataPrice.Mul(dataPrice, vm.GasData) - //if err = self.UseGas(dataPrice); err != nil { - // return - //} - if sender.Balance().Cmp(self.value) < 0 { return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance) } diff --git a/chain/types/block.go b/chain/types/block.go index 5c8bb5f6c..feab7072f 100644 --- a/chain/types/block.go +++ b/chain/types/block.go @@ -97,6 +97,8 @@ type Block struct { receipts Receipts TxSha, ReceiptSha []byte LogsBloom []byte + + Reward *big.Int } func NewBlockFromBytes(raw []byte) *Block { diff --git a/chain/types/bloom9.go b/chain/types/bloom9.go index 626711cca..77aa6eef5 100644 --- a/chain/types/bloom9.go +++ b/chain/types/bloom9.go @@ -11,13 +11,13 @@ import ( func CreateBloom(receipts Receipts) []byte { bin := new(big.Int) for _, receipt := range receipts { - bin.Or(bin, logsBloom(receipt.logs)) + bin.Or(bin, LogsBloom(receipt.logs)) } return ethutil.LeftPadBytes(bin.Bytes(), 64) } -func logsBloom(logs state.Logs) *big.Int { +func LogsBloom(logs state.Logs) *big.Int { bin := new(big.Int) for _, log := range logs { data := [][]byte{log.Address} |