diff options
Diffstat (limited to 'ethchain')
-rw-r--r-- | ethchain/block.go | 138 | ||||
-rw-r--r-- | ethchain/bloom9.go | 32 | ||||
-rw-r--r-- | ethchain/chain_manager.go | 2 | ||||
-rw-r--r-- | ethchain/derive_sha.go | 20 | ||||
-rw-r--r-- | ethchain/filter.go | 10 | ||||
-rw-r--r-- | ethchain/genesis.go | 11 | ||||
-rw-r--r-- | ethchain/state_manager.go | 35 | ||||
-rw-r--r-- | ethchain/state_transition.go | 12 | ||||
-rw-r--r-- | ethchain/transaction.go | 52 |
9 files changed, 168 insertions, 144 deletions
diff --git a/ethchain/block.go b/ethchain/block.go index b98d806d8..b31d68e4d 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -5,7 +5,6 @@ import ( "fmt" "math/big" "sort" - _ "strconv" "time" "github.com/ethereum/go-ethereum/ethcrypto" @@ -96,9 +95,10 @@ type Block struct { // Block Nonce for verification Nonce ethutil.Bytes // List of transactions and/or contracts - transactions []*Transaction - receipts []*Receipt - TxSha []byte + transactions Transactions + receipts Receipts + TxSha, ReceiptSha []byte + LogsBloom []byte } func NewBlockFromBytes(raw []byte) *Block { @@ -151,7 +151,7 @@ func (block *Block) Hash() ethutil.Bytes { func (block *Block) HashNoNonce() []byte { return ethcrypto.Sha3(ethutil.Encode([]interface{}{block.PrevHash, block.UncleSha, block.Coinbase, block.state.Trie.Root, - block.TxSha, block.Difficulty, block.Number, block.MinGasPrice, + block.ReceiptSha, block.Difficulty, block.Number, block.MinGasPrice, block.GasLimit, block.GasUsed, block.Time, block.Extra})) } @@ -191,9 +191,9 @@ func (block *Block) BlockInfo() BlockInfo { } func (self *Block) GetTransaction(hash []byte) *Transaction { - for _, receipt := range self.receipts { - if bytes.Compare(receipt.Tx.Hash(), hash) == 0 { - return receipt.Tx + for _, tx := range self.transactions { + if bytes.Compare(tx.Hash(), hash) == 0 { + return tx } } @@ -236,45 +236,34 @@ func (block *Block) rlpUncles() interface{} { func (block *Block) SetUncles(uncles []*Block) { block.Uncles = uncles - // Sha of the concatenated uncles - if len(uncles) > 0 { - block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles())) - } + block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles())) } -func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) { +func (self *Block) SetReceipts(receipts Receipts) { self.receipts = receipts + self.SetReceiptHash(receipts) +} + +func (self *Block) SetTransactions(txs Transactions) { self.setTransactions(txs) + self.SetTransactionHash(txs) } -func (block *Block) setTransactions(txs []*Transaction) { +func (block *Block) setTransactions(txs Transactions) { block.transactions = txs + block.LogsBloom = CreateBloom(block) } -func CreateTxSha(receipts Receipts) (sha []byte) { - trie := ethtrie.New(ethutil.Config.Db, "") - for i, receipt := range receipts { - trie.Update(string(ethutil.NewValue(i).Encode()), string(ethutil.NewValue(receipt.RlpData()).Encode())) - } - - switch trie.Root.(type) { - case string: - sha = []byte(trie.Root.(string)) - case []byte: - sha = trie.Root.([]byte) - default: - panic(fmt.Sprintf("invalid root type %T", trie.Root)) - } - - return sha +func (self *Block) SetTransactionHash(transactions Transactions) { + self.TxSha = DeriveSha(transactions) } -func (self *Block) SetTxHash(receipts Receipts) { - self.TxSha = CreateTxSha(receipts) +func (self *Block) SetReceiptHash(receipts Receipts) { + self.ReceiptSha = DeriveSha(receipts) } func (block *Block) Value() *ethutil.Value { - return ethutil.NewValue([]interface{}{block.header(), block.rlpReceipts(), block.rlpUncles()}) + return ethutil.NewValue([]interface{}{block.header(), block.transactions, block.rlpUncles()}) } func (block *Block) RlpEncode() []byte { @@ -289,33 +278,20 @@ func (block *Block) RlpDecode(data []byte) { } 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 = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val)) - block.TxSha = header.Get(4).Bytes() - block.Difficulty = header.Get(5).BigInt() - block.Number = header.Get(6).BigInt() - //fmt.Printf("#%v : %x\n", block.Number, block.Coinbase) - 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() + block.setHeader(decoder.Get(0)) // Tx list might be empty if this is an uncle. Uncles only have their // header set. if decoder.Get(1).IsNil() == false { // Yes explicitness - receipts := decoder.Get(1) - block.transactions = make([]*Transaction, receipts.Len()) - block.receipts = make([]*Receipt, receipts.Len()) - for i := 0; i < receipts.Len(); i++ { - receipt := NewRecieptFromValue(receipts.Get(i)) - block.transactions[i] = receipt.Tx - block.receipts[i] = receipt + //receipts := decoder.Get(1) + //block.receipts = make([]*Receipt, receipts.Len()) + it := decoder.Get(1).NewIterator() + block.transactions = make(Transactions, it.Len()) + for it.Next() { + block.transactions[it.Idx()] = NewTransactionFromValue(it.Value()) + //receipt := NewRecieptFromValue(receipts.Get(i)) + //block.transactions[i] = receipt.Tx + //block.receipts[i] = receipt } } @@ -330,22 +306,27 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) { } +func (self *Block) setHeader(header *ethutil.Value) { + self.PrevHash = header.Get(0).Bytes() + self.UncleSha = header.Get(1).Bytes() + self.Coinbase = header.Get(2).Bytes() + self.state = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val)) + self.TxSha = header.Get(4).Bytes() + self.ReceiptSha = header.Get(5).Bytes() + self.LogsBloom = header.Get(6).Bytes() + self.Difficulty = header.Get(7).BigInt() + self.Number = header.Get(8).BigInt() + self.MinGasPrice = header.Get(9).BigInt() + self.GasLimit = header.Get(10).BigInt() + self.GasUsed = header.Get(11).BigInt() + self.Time = int64(header.Get(12).BigInt().Uint64()) + self.Extra = header.Get(13).Str() + self.Nonce = header.Get(14).Bytes() +} + 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 = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val)) - block.TxSha = header.Get(4).Bytes() - block.Difficulty = header.Get(5).BigInt() - 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() + block.setHeader(header) return block } @@ -376,8 +357,12 @@ func (block *Block) header() []interface{} { block.Coinbase, // root state block.state.Trie.Root, - // Sha of tx + // tx root block.TxSha, + // Sha of tx + block.ReceiptSha, + // Bloom + block.LogsBloom, // Current block Difficulty block.Difficulty, // The block number @@ -404,7 +389,9 @@ func (block *Block) String() string { UncleSha: %x Coinbase: %x Root: %x - TxSha: %x + TxSha %x + ReceiptSha: %x + Bloom: %x Difficulty: %v Number: %v MinGas: %v @@ -422,6 +409,8 @@ func (block *Block) String() string { block.Coinbase, block.state.Trie.Root, block.TxSha, + block.ReceiptSha, + block.LogsBloom, block.Difficulty, block.Number, block.MinGasPrice, @@ -437,3 +426,8 @@ func (block *Block) String() string { func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(self.RlpEncode())) } + +// Implement RlpEncodable +func (self *Block) RlpData() interface{} { + return self.Value().Val +} diff --git a/ethchain/bloom9.go b/ethchain/bloom9.go index 65be6c7a2..508644b62 100644 --- a/ethchain/bloom9.go +++ b/ethchain/bloom9.go @@ -1,37 +1,43 @@ package ethchain -import "github.com/ethereum/go-ethereum/vm" +import ( + "math/big" -func CreateBloom(txs Transactions) uint64 { - var bin uint64 - for _, tx := range txs { - bin |= logsBloom(tx.logs) + "github.com/ethereum/go-ethereum/vm" +) + +func CreateBloom(block *Block) []byte { + bin := new(big.Int) + bin.Or(bin, bloom9(block.Coinbase)) + for _, tx := range block.Transactions() { + bin.Or(bin, LogsBloom(tx.logs)) } - return bin + return bin.Bytes() } -func logsBloom(logs []vm.Log) uint64 { - var bin uint64 +func LogsBloom(logs []vm.Log) *big.Int { + bin := new(big.Int) for _, log := range logs { data := [][]byte{log.Address} for _, topic := range log.Topics { - data = append(data, topic.Bytes()) + data = append(data, topic) } data = append(data, log.Data) for _, b := range data { - bin |= bloom9(b) + bin.Or(bin, bloom9(b)) } } return bin } -func bloom9(b []byte) uint64 { - var r uint64 +func bloom9(b []byte) *big.Int { + r := new(big.Int) for _, i := range []int{0, 2, 4} { - r |= 1 << (uint64(b[i+1]) + 256*(uint64(b[i])&1)) + t := big.NewInt(1) + r.Or(r, t.Lsh(t, uint(b[i+1])+256*(uint(b[i])&1))) } return r diff --git a/ethchain/chain_manager.go b/ethchain/chain_manager.go index d949f0ce7..46990bb22 100644 --- a/ethchain/chain_manager.go +++ b/ethchain/chain_manager.go @@ -87,8 +87,6 @@ func (bc *ChainManager) Reset() { bc.genesisBlock.state.Trie.Sync() // Prepare the genesis block bc.Add(bc.genesisBlock) - fk := append([]byte("bloom"), bc.genesisBlock.Hash()...) - bc.Ethereum.Db().Put(fk, make([]byte, 255)) bc.CurrentBlock = bc.genesisBlock bc.SetTotalDifficulty(ethutil.Big("0")) diff --git a/ethchain/derive_sha.go b/ethchain/derive_sha.go new file mode 100644 index 000000000..b41252e39 --- /dev/null +++ b/ethchain/derive_sha.go @@ -0,0 +1,20 @@ +package ethchain + +import ( + "github.com/ethereum/go-ethereum/ethtrie" + "github.com/ethereum/go-ethereum/ethutil" +) + +type DerivableList interface { + Len() int + GetRlp(i int) []byte +} + +func DeriveSha(list DerivableList) []byte { + trie := ethtrie.New(ethutil.Config.Db, "") + for i := 0; i < list.Len(); i++ { + trie.Update(string(ethutil.NewValue(i).Encode()), string(list.GetRlp(i))) + } + + return trie.GetRoot() +} diff --git a/ethchain/filter.go b/ethchain/filter.go index 4e7fe68a8..55d7072e2 100644 --- a/ethchain/filter.go +++ b/ethchain/filter.go @@ -2,7 +2,6 @@ package ethchain import ( "bytes" - "fmt" "math" "github.com/ethereum/go-ethereum/ethstate" @@ -171,13 +170,8 @@ func (self *Filter) FilterMessages(msgs []*ethstate.Message) []*ethstate.Message } func (self *Filter) bloomFilter(block *Block) bool { - fk := append([]byte("bloom"), block.Hash()...) - bin, err := self.eth.Db().Get(fk) - if err != nil { - fmt.Println(err) - } - - bloom := NewBloomFilter(bin) + // TODO update to the new bloom filter + bloom := NewBloomFilter(nil) var fromIncluded, toIncluded bool if len(self.from) > 0 { diff --git a/ethchain/genesis.go b/ethchain/genesis.go index 232986d53..d94e658b6 100644 --- a/ethchain/genesis.go +++ b/ethchain/genesis.go @@ -13,19 +13,24 @@ import ( var ZeroHash256 = make([]byte, 32) var ZeroHash160 = make([]byte, 20) +var ZeroHash512 = make([]byte, 64) var EmptyShaList = ethcrypto.Sha3(ethutil.Encode([]interface{}{})) var GenesisHeader = []interface{}{ // Previous hash (none) ZeroHash256, // Empty uncles - "", + EmptyShaList, // Coinbase ZeroHash160, // Root state - "", + EmptyShaList, // tx sha - "", + EmptyShaList, + // receipt list + EmptyShaList, + // bloom + ZeroHash512, // Difficulty //ethutil.BigPow(2, 22), big.NewInt(131072), diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index dba3ff202..e45d44752 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -101,7 +101,7 @@ func (self *StateManager) Stop() { func (self *StateManager) updateThread() { for ev := range self.events.Chan() { for _, block := range ev.(Blocks) { - err := self.Process(block, false) + err := self.Process(block) if err != nil { statelogger.Infoln(err) statelogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4]) @@ -173,8 +173,9 @@ done: state.Update() txGas.Sub(txGas, st.gas) - accumelative := new(big.Int).Set(totalUsedGas.Add(totalUsedGas, txGas)) - receipt := &Receipt{tx, ethutil.CopyBytes(state.Root().([]byte)), accumelative} + cumulative := new(big.Int).Set(totalUsedGas.Add(totalUsedGas, txGas)) + //receipt := &Receipt{tx, ethutil.CopyBytes(state.Root().([]byte)), accumelative} + receipt := &Receipt{ethutil.CopyBytes(state.Root().([]byte)), cumulative, LogsBloom(tx.logs).Bytes(), tx.logs} if i < len(block.Receipts()) { original := block.Receipts()[i] @@ -183,7 +184,7 @@ done: os.Exit(1) } - err := fmt.Errorf("#%d receipt failed (r) %v ~ %x <=> (c) %v ~ %x (%x...)", i+1, original.CumulativeGasUsed, original.PostState[0:4], receipt.CumulativeGasUsed, receipt.PostState[0:4], receipt.Tx.Hash()[0:4]) + err := fmt.Errorf("#%d receipt failed (r) %v ~ %x <=> (c) %v ~ %x (%x...)", i+1, original.CumulativeGasUsed, original.PostState[0:4], receipt.CumulativeGasUsed, receipt.PostState[0:4], tx.Hash()[0:4]) return nil, nil, nil, nil, err } @@ -205,7 +206,7 @@ done: return receipts, handled, unhandled, erroneous, err } -func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { +func (sm *StateManager) Process(block *Block) (err error) { // Processing a blocks may never happen simultaneously sm.mutex.Lock() defer sm.mutex.Unlock() @@ -235,14 +236,19 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { fmt.Printf("## %x %x ##\n", block.Hash(), block.Number) } + txSha := DeriveSha(block.transactions) + if bytes.Compare(txSha, block.TxSha) != 0 { + return fmt.Errorf("Error validating transaction sha. Received %x, got %x", block.ReceiptSha, txSha) + } + receipts, err := sm.ApplyDiff(state, parent, block) if err != nil { return err } - txSha := CreateTxSha(receipts) - if bytes.Compare(txSha, block.TxSha) != 0 { - return fmt.Errorf("Error validating tx sha. Received %x, got %x", block.TxSha, txSha) + receiptSha := DeriveSha(receipts) + if bytes.Compare(receiptSha, block.ReceiptSha) != 0 { + return fmt.Errorf("Error validating receipt sha. Received %x, got %x", block.ReceiptSha, receiptSha) } // Block validation @@ -271,20 +277,13 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { // Add the block to the chain sm.bc.Add(block) - sm.transState = state.Copy() + // TODO at this point we should also insert LOGS in to a database - // Create a bloom bin for this block - filter := sm.createBloomFilter(state) - // Persist the data - fk := append([]byte("bloom"), block.Hash()...) - sm.eth.Db().Put(fk, filter.Bin()) + sm.transState = state.Copy() statelogger.Infof("Imported block #%d (%x...)\n", block.Number, block.Hash()[0:4]) - if dontReact == false { - sm.eth.EventMux().Post(NewBlockEvent{block}) - state.Manifest().Reset() - } + state.Manifest().Reset() sm.eth.TxPool().RemoveSet(block.Transactions()) } else { diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index e6863b61f..809e5ad6a 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -5,7 +5,6 @@ import ( "math/big" "github.com/ethereum/go-ethereum/ethstate" - "github.com/ethereum/go-ethereum/ethtrie" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/vm" ) @@ -231,11 +230,9 @@ func (self *StateTransition) TransitionState() (err error) { msg.Output = ret } else { - // Add default LOG - // PUSH1 1 CALLER ADD LOG1 - addr := ethutil.BigD(sender.Address()) - addr.Add(addr, ethutil.Big1) - tx.addLog(vm.Log{sender.Address(), []*big.Int{addr}, nil}) + // Add default LOG. Default = big(sender.addr) + 1 + addr := ethutil.BigD(receiver.Address()) + tx.addLog(vm.Log{sender.Address(), [][]byte{addr.Add(addr, ethutil.Big1).Bytes()}, nil}) } } @@ -250,9 +247,7 @@ func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context callerClosure = vm.NewClosure(msg, transactor, context, script, self.gas, self.gasPrice) ) - //vm := vm.New(env, vm.Type(ethutil.Config.VmType)) evm := vm.New(env, vm.DebugVmTy) - ret, _, err = callerClosure.Call(evm, self.tx.Data) return @@ -264,7 +259,6 @@ func MakeContract(tx *Transaction, state *ethstate.State) *ethstate.StateObject contract := state.GetOrNewStateObject(addr) contract.InitCode = tx.Data - contract.State = ethstate.New(ethtrie.New(ethutil.Config.Db, "")) return contract } diff --git a/ethchain/transaction.go b/ethchain/transaction.go index abf2e37ac..10bf5bc8e 100644 --- a/ethchain/transaction.go +++ b/ethchain/transaction.go @@ -113,7 +113,8 @@ func (tx *Transaction) PublicKey() []byte { sig := append(r, s...) sig = append(sig, tx.v-27) - pubkey, _ := secp256k1.RecoverPubkey(hash, sig) + pubkey := ethcrypto.Ecrecover(append(hash, sig...)) + //pubkey, _ := secp256k1.RecoverPubkey(hash, sig) return pubkey } @@ -208,11 +209,11 @@ func (tx *Transaction) String() string { } type Receipt struct { - Tx *Transaction PostState []byte CumulativeGasUsed *big.Int + Bloom []byte + Logs vm.Logs } -type Receipts []*Receipt func NewRecieptFromValue(val *ethutil.Value) *Receipt { r := &Receipt{} @@ -222,25 +223,22 @@ func NewRecieptFromValue(val *ethutil.Value) *Receipt { } func (self *Receipt) RlpValueDecode(decoder *ethutil.Value) { - self.Tx = NewTransactionFromValue(decoder.Get(0)) - self.PostState = decoder.Get(1).Bytes() - self.CumulativeGasUsed = decoder.Get(2).BigInt() + self.PostState = decoder.Get(0).Bytes() + self.CumulativeGasUsed = decoder.Get(1).BigInt() + self.Bloom = decoder.Get(2).Bytes() + + it := decoder.Get(3).NewIterator() + for it.Next() { + self.Logs = append(self.Logs, vm.NewLogFromValue(it.Value())) + } } func (self *Receipt) RlpData() interface{} { - return []interface{}{self.Tx.RlpData(), self.PostState, self.CumulativeGasUsed} + return []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.Logs.RlpData()} } -func (self *Receipt) String() string { - return fmt.Sprintf(` - R - Tx:[ %v] - PostState: 0x%x - CumulativeGasUsed: %v - `, - self.Tx, - self.PostState, - self.CumulativeGasUsed) +func (self *Receipt) RlpEncode() []byte { + return ethutil.Encode(self.RlpData()) } func (self *Receipt) Cmp(other *Receipt) bool { @@ -251,11 +249,27 @@ func (self *Receipt) Cmp(other *Receipt) bool { return true } +type Receipts []*Receipt + +func (self Receipts) Len() int { return len(self) } +func (self Receipts) GetRlp(i int) []byte { return ethutil.Rlp(self[i]) } + // Transaction slice type for basic sorting type Transactions []*Transaction -func (s Transactions) Len() int { return len(s) } -func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (self Transactions) RlpData() interface{} { + // Marshal the transactions of this block + enc := make([]interface{}, len(self)) + for i, tx := range self { + // Cast it to a string (safe) + enc[i] = tx.RlpData() + } + + return enc +} +func (s Transactions) Len() int { return len(s) } +func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (s Transactions) GetRlp(i int) []byte { return ethutil.Rlp(s[i]) } type TxByNonce struct{ Transactions } |