diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/block_processor.go | 41 | ||||
-rw-r--r-- | core/chain_makers.go | 10 | ||||
-rw-r--r-- | core/chain_manager.go | 48 | ||||
-rw-r--r-- | core/error.go | 6 | ||||
-rw-r--r-- | core/execution.go | 27 | ||||
-rw-r--r-- | core/filter.go | 24 | ||||
-rw-r--r-- | core/genesis.go | 7 | ||||
-rw-r--r-- | core/state_transition.go | 37 | ||||
-rw-r--r-- | core/transaction_pool.go | 65 | ||||
-rw-r--r-- | core/types/block.go | 96 | ||||
-rw-r--r-- | core/types/block_test.go | 18 | ||||
-rw-r--r-- | core/types/bloom9.go | 28 | ||||
-rw-r--r-- | core/types/common.go | 32 | ||||
-rw-r--r-- | core/types/derive_sha.go | 6 | ||||
-rw-r--r-- | core/types/receipt.go | 38 | ||||
-rw-r--r-- | core/types/transaction.go | 141 | ||||
-rw-r--r-- | core/types/transaction_test.go | 57 | ||||
-rw-r--r-- | core/vm_env.go | 42 |
18 files changed, 407 insertions, 316 deletions
diff --git a/core/block_processor.go b/core/block_processor.go index f67d6d006..b12f88c47 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -1,14 +1,13 @@ package core import ( - "bytes" "fmt" "math/big" "sync" "time" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" @@ -82,7 +81,8 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated _, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, block), tx, cb) if err != nil && (IsNonceErr(err) || state.IsGasLimitErr(err) || IsInvalidTxErr(err)) { // If the account is managed, remove the invalid nonce. - self.bc.TxState().RemoveNonce(tx.From(), tx.Nonce()) + from, _ := tx.From() + self.bc.TxState().RemoveNonce(from, tx.Nonce()) return nil, nil, err } @@ -90,7 +90,7 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated statedb.Update(nil) cumulative := new(big.Int).Set(usedGas.Add(usedGas, gas)) - receipt := types.NewReceipt(statedb.Root(), cumulative) + receipt := types.NewReceipt(statedb.Root().Bytes(), cumulative) receipt.SetLogs(statedb.Logs()) receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) chainlogger.Debugln(receipt) @@ -190,7 +190,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big // Validate the received block's bloom with the one derived from the generated receipts. // For valid blocks this should always validate to true. rbloom := types.CreateBloom(receipts) - if bytes.Compare(rbloom, header.Bloom) != 0 { + if rbloom != header.Bloom { err = fmt.Errorf("unable to replicate block's bloom=%x", rbloom) return } @@ -198,14 +198,14 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big // The transactions Trie's root (R = (Tr [[H1, T1], [H2, T2], ... [Hn, Tn]])) // can be used by light clients to make sure they've received the correct Txs txSha := types.DeriveSha(block.Transactions()) - if bytes.Compare(txSha, header.TxHash) != 0 { + if txSha != header.TxHash { err = fmt.Errorf("validating transaction root. received=%x got=%x", header.TxHash, txSha) return } // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]])) receiptSha := types.DeriveSha(receipts) - if bytes.Compare(receiptSha, header.ReceiptHash) != 0 { + if receiptSha != header.ReceiptHash { err = fmt.Errorf("validating receipt root. received=%x got=%x", header.ReceiptHash, receiptSha) return } @@ -218,7 +218,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big // Commit state objects/accounts to a temporary trie (does not save) // used to calculate the state root. state.Update(common.Big0) - if !bytes.Equal(header.Root, state.Root()) { + if header.Root != state.Root() { err = fmt.Errorf("invalid merkle root. received=%x got=%x", header.Root, state.Root()) return } @@ -234,7 +234,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big putTx(sm.extraDb, tx) } - chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) + chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4]) return td, nil } @@ -284,35 +284,34 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren ancestors := set.New() uncles := set.New() - ancestorHeaders := make(map[string]*types.Header) + ancestorHeaders := make(map[common.Hash]*types.Header) for _, ancestor := range sm.bc.GetAncestors(block, 7) { - hash := string(ancestor.Hash()) - ancestorHeaders[hash] = ancestor.Header() - ancestors.Add(hash) + ancestorHeaders[ancestor.Hash()] = ancestor.Header() + ancestors.Add(ancestor.Hash()) // Include ancestors uncles in the uncle set. Uncles must be unique. for _, uncle := range ancestor.Uncles() { - uncles.Add(string(uncle.Hash())) + uncles.Add(uncle.Hash()) } } - uncles.Add(string(block.Hash())) + uncles.Add(block.Hash()) for _, uncle := range block.Uncles() { - if uncles.Has(string(uncle.Hash())) { + if uncles.Has(uncle.Hash()) { // Error not unique return UncleError("Uncle not unique") } - uncles.Add(string(uncle.Hash())) + uncles.Add(uncle.Hash()) - if ancestors.Has(string(uncle.Hash())) { + if ancestors.Has(uncle.Hash()) { return UncleError("Uncle is ancestor") } - if !ancestors.Has(string(uncle.ParentHash)) { + if !ancestors.Has(uncle.ParentHash) { return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) } - if err := sm.ValidateHeader(uncle, ancestorHeaders[string(uncle.ParentHash)]); err != nil { + if err := sm.ValidateHeader(uncle, ancestorHeaders[uncle.ParentHash]); err != nil { return ValidationError(fmt.Sprintf("%v", err)) } @@ -358,5 +357,5 @@ func putTx(db common.Database, tx *types.Transaction) { statelogger.Infoln("Failed encoding tx", err) return } - db.Put(tx.Hash(), rlpEnc) + db.Put(tx.Hash().Bytes(), rlpEnc) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 59c297dbe..857af960c 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -4,8 +4,8 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/state" @@ -29,7 +29,7 @@ var ( // Utility functions for making chains on the fly // Exposed for sake of testing from other packages (eg. go-ethash) -func NewBlockFromParent(addr []byte, parent *types.Block) *types.Block { +func NewBlockFromParent(addr common.Address, parent *types.Block) *types.Block { return newBlockFromParent(addr, parent) } @@ -54,7 +54,7 @@ func NewCanonical(n int, db common.Database) (*BlockProcessor, error) { } // block time is fixed at 10 seconds -func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { +func newBlockFromParent(addr common.Address, parent *types.Block) *types.Block { block := types.NewBlock(parent.Hash(), addr, parent.Root(), common.BigPow(2, 32), 0, "") block.SetUncles(nil) block.SetTransactions(nil) @@ -74,8 +74,8 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { // Actually make a block by simulating what miner would do // we seed chains by the first byte of the coinbase func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block { - addr := common.LeftPadBytes([]byte{byte(i)}, 20) - addr[0] = byte(seed) + var addr common.Address + addr[0], addr[19] = byte(seed), byte(i) block := newBlockFromParent(addr, parent) state := state.New(block.Root(), db) cbase := state.GetOrNewStateObject(addr) diff --git a/core/chain_manager.go b/core/chain_manager.go index ff91b0427..5316a3423 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -6,8 +6,8 @@ import ( "math/big" "sync" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/rlp" @@ -86,7 +86,7 @@ type ChainManager struct { tsmu sync.RWMutex td *big.Int currentBlock *types.Block - lastBlockHash []byte + lastBlockHash common.Hash transState *state.StateDB txState *state.ManagedState @@ -112,7 +112,7 @@ func (self *ChainManager) Td() *big.Int { return self.td } -func (self *ChainManager) LastBlockHash() []byte { +func (self *ChainManager) LastBlockHash() common.Hash { self.mu.RLock() defer self.mu.RUnlock() @@ -126,7 +126,7 @@ func (self *ChainManager) CurrentBlock() *types.Block { return self.currentBlock } -func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) { +func (self *ChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) { self.mu.RLock() defer self.mu.RUnlock() @@ -168,7 +168,7 @@ func (self *ChainManager) setTransState(statedb *state.StateDB) { func (bc *ChainManager) setLastBlock() { data, _ := bc.blockDb.Get([]byte("LastBlock")) if len(data) != 0 { - block := bc.GetBlock(data) + block := bc.GetBlock(common.BytesToHash(data)) bc.currentBlock = block bc.lastBlockHash = block.Hash() @@ -182,12 +182,14 @@ func (bc *ChainManager) setLastBlock() { } // Block creation & chain handling -func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { +func (bc *ChainManager) NewBlock(coinbase common.Address) *types.Block { bc.mu.RLock() defer bc.mu.RUnlock() - var root []byte - parentHash := ZeroHash256 + var ( + root common.Hash + parentHash common.Hash + ) if bc.currentBlock != nil { root = bc.currentBlock.Header().Root @@ -234,7 +236,7 @@ func (bc *ChainManager) Reset() { } func (bc *ChainManager) removeBlock(block *types.Block) { - bc.blockDb.Delete(append(blockHashPre, block.Hash()...)) + bc.blockDb.Delete(append(blockHashPre, block.Hash().Bytes()...)) } func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { @@ -268,18 +270,18 @@ func (self *ChainManager) Export() []byte { func (bc *ChainManager) insert(block *types.Block) { //encodedBlock := common.Encode(block) - bc.blockDb.Put([]byte("LastBlock"), block.Hash()) + bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes()) bc.currentBlock = block bc.lastBlockHash = block.Hash() key := append(blockNumPre, block.Number().Bytes()...) - bc.blockDb.Put(key, bc.lastBlockHash) + bc.blockDb.Put(key, bc.lastBlockHash.Bytes()) } func (bc *ChainManager) write(block *types.Block) { encodedBlock := common.Encode(block.RlpDataForStorage()) - key := append(blockHashPre, block.Hash()...) + key := append(blockHashPre, block.Hash().Bytes()...) bc.blockDb.Put(key, encodedBlock) } @@ -289,12 +291,12 @@ func (bc *ChainManager) Genesis() *types.Block { } // Block fetching methods -func (bc *ChainManager) HasBlock(hash []byte) bool { - data, _ := bc.blockDb.Get(append(blockHashPre, hash...)) +func (bc *ChainManager) HasBlock(hash common.Hash) bool { + data, _ := bc.blockDb.Get(append(blockHashPre, hash[:]...)) return len(data) != 0 } -func (self *ChainManager) GetBlockHashesFromHash(hash []byte, max uint64) (chain [][]byte) { +func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) (chain []common.Hash) { block := self.GetBlock(hash) if block == nil { return @@ -317,8 +319,8 @@ func (self *ChainManager) GetBlockHashesFromHash(hash []byte, max uint64) (chain return } -func (self *ChainManager) GetBlock(hash []byte) *types.Block { - data, _ := self.blockDb.Get(append(blockHashPre, hash...)) +func (self *ChainManager) GetBlock(hash common.Hash) *types.Block { + data, _ := self.blockDb.Get(append(blockHashPre, hash[:]...)) if len(data) == 0 { return nil } @@ -340,7 +342,7 @@ func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block { return nil } - return self.GetBlock(key) + return self.GetBlock(common.BytesToHash(key)) } func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) { @@ -418,7 +420,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { } h := block.Header() - chainlogger.Infof("INVALID block #%v (%x)\n", h.Number, h.Hash()[:4]) + chainlogger.Infof("INVALID block #%v (%x)\n", h.Number, h.Hash().Bytes()[:4]) chainlogger.Infoln(err) chainlogger.Debugln(block) return err @@ -435,7 +437,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { // At this point it's possible that a different chain (fork) becomes the new canonical chain. if td.Cmp(self.td) > 0 { if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, common.Big1)) < 0 { - chainlogger.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, block.Hash()[:4], td, cblock.Header().Number, cblock.Hash()[:4], self.td) + chash := cblock.Hash() + hash := block.Hash() + chainlogger.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], td, cblock.Header().Number, chash[:4], self.td) queue[i] = ChainSplitEvent{block} queueEvent.splitCount++ @@ -507,7 +511,9 @@ out: } } +/* // Satisfy state query interface -func (self *ChainManager) GetAccount(addr []byte) *state.StateObject { +func (self *ChainManager) GetAccount(addr common.Hash) *state.StateObject { return self.State().GetAccount(addr) } +*/ diff --git a/core/error.go b/core/error.go index 69e320eb0..f6ac26cff 100644 --- a/core/error.go +++ b/core/error.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "math/big" + + "github.com/ethereum/go-ethereum/common" ) var ( @@ -21,7 +23,7 @@ func (err *ParentErr) Error() string { return err.Message } -func ParentError(hash []byte) error { +func ParentError(hash common.Hash) error { return &ParentErr{Message: fmt.Sprintf("Block's parent unknown %x", hash)} } @@ -136,7 +138,7 @@ func IsTDError(e error) bool { type KnownBlockError struct { number *big.Int - hash []byte + hash common.Hash } func (self *KnownBlockError) Error() string { diff --git a/core/execution.go b/core/execution.go index be45eeeb4..4f15fb42a 100644 --- a/core/execution.go +++ b/core/execution.go @@ -4,6 +4,7 @@ import ( "math/big" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" @@ -11,26 +12,23 @@ import ( type Execution struct { env vm.Environment - address, input []byte + address *common.Address + input []byte Gas, price, value *big.Int } -func NewExecution(env vm.Environment, address, input []byte, gas, gasPrice, value *big.Int) *Execution { +func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution { return &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value} } -func (self *Execution) Addr() []byte { - return self.address -} - -func (self *Execution) Call(codeAddr []byte, caller vm.ContextRef) ([]byte, error) { +func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) { // Retrieve the executing code code := self.env.State().GetCode(codeAddr) - return self.exec(code, codeAddr, caller) + return self.exec(&codeAddr, code, caller) } -func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret []byte, err error) { +func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) { env := self.env evm := vm.NewVm(env) if env.Depth() == vm.MaxCallDepth { @@ -40,14 +38,15 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret } vsnapshot := env.State().Copy() - if len(self.address) == 0 { + if self.address == nil { // Generate a new address nonce := env.State().GetNonce(caller.Address()) - self.address = crypto.CreateAddress(caller.Address(), nonce) + addr := crypto.CreateAddress(caller.Address(), nonce) + self.address = &addr env.State().SetNonce(caller.Address(), nonce+1) } - from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address) + from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(*self.address) err = env.Transfer(from, to, self.value) if err != nil { env.State().Set(vsnapshot) @@ -73,8 +72,8 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret } func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) { - ret, err = self.exec(self.input, nil, caller) - account = self.env.State().GetStateObject(self.address) + ret, err = self.exec(nil, self.input, caller) + account = self.env.State().GetStateObject(*self.address) return } diff --git a/core/filter.go b/core/filter.go index d58aa8d7c..e08aac120 100644 --- a/core/filter.go +++ b/core/filter.go @@ -1,9 +1,9 @@ package core import ( - "bytes" "math" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/state" ) @@ -16,8 +16,8 @@ type FilterOptions struct { Earliest int64 Latest int64 - Address [][]byte - Topics [][][]byte + Address []common.Address + Topics [][]common.Hash Skip int Max int @@ -29,9 +29,9 @@ type Filter struct { earliest int64 latest int64 skip int - address [][]byte + address []common.Address max int - topics [][][]byte + topics [][]common.Hash BlockCallback func(*types.Block) PendingCallback func(*types.Block) @@ -67,11 +67,11 @@ func (self *Filter) SetLatestBlock(latest int64) { self.latest = latest } -func (self *Filter) SetAddress(addr [][]byte) { +func (self *Filter) SetAddress(addr []common.Address) { self.address = addr } -func (self *Filter) SetTopics(topics [][][]byte) { +func (self *Filter) SetTopics(topics [][]common.Hash) { self.topics = topics } @@ -131,9 +131,9 @@ func (self *Filter) Find() state.Logs { return logs[skip:] } -func includes(addresses [][]byte, a []byte) bool { +func includes(addresses []common.Address, a common.Address) bool { for _, addr := range addresses { - if !bytes.Equal(addr, a) { + if addr != a { return false } } @@ -151,13 +151,13 @@ Logs: continue } - logTopics := make([][]byte, len(self.topics)) + logTopics := make([]common.Hash, len(self.topics)) copy(logTopics, log.Topics()) for i, topics := range self.topics { for _, topic := range topics { var match bool - if bytes.Equal(log.Topics()[i], topic) { + if log.Topics()[i] == topic { match = true } if !match { @@ -176,7 +176,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { if len(self.address) > 0 { var included bool for _, addr := range self.address { - if types.BloomLookup(block.Bloom(), addr) { + if types.BloomLookup(block.Bloom(), addr.Hash()) { included = true break } diff --git a/core/genesis.go b/core/genesis.go index bfd51f196..70845b502 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -6,9 +6,9 @@ import ( "math/big" "os" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/state" ) @@ -26,12 +26,11 @@ var GenesisDiff = big.NewInt(131072) var GenesisGasLimit = big.NewInt(3141592) func GenesisBlock(db common.Database) *types.Block { - genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, GenesisDiff, 42, "") + genesis := types.NewBlock(common.Hash{}, common.Address{}, common.Hash{}, GenesisDiff, 42, "") genesis.Header().Number = common.Big0 genesis.Header().GasLimit = GenesisGasLimit genesis.Header().GasUsed = common.Big0 genesis.Header().Time = 0 - genesis.Header().MixDigest = make([]byte, 32) genesis.Td = common.Big0 @@ -49,7 +48,7 @@ func GenesisBlock(db common.Database) *types.Block { statedb := state.New(genesis.Root(), db) for addr, account := range accounts { codedAddr := common.Hex2Bytes(addr) - accountState := statedb.GetAccount(codedAddr) + accountState := statedb.GetAccount(common.BytesToAddress(codedAddr)) accountState.SetBalance(common.Big(account.Balance)) statedb.UpdateStateObject(accountState) } diff --git a/core/state_transition.go b/core/state_transition.go index 279abee62..1509a5258 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -4,8 +4,8 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -31,7 +31,7 @@ var () * 6) Derive new state root */ type StateTransition struct { - coinbase []byte + coinbase common.Address msg Message gas, gasPrice *big.Int initialGas *big.Int @@ -44,9 +44,10 @@ type StateTransition struct { env vm.Environment } +// Message represents a message sent to a contract. type Message interface { - From() []byte - To() []byte + From() (common.Address, error) + To() *common.Address GasPrice() *big.Int Gas() *big.Int @@ -56,13 +57,8 @@ type Message interface { Data() []byte } -func AddressFromMessage(msg Message) []byte { - // Generate a new address - return crypto.Sha3(common.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:] -} - func MessageCreatesContract(msg Message) bool { - return len(msg.To()) == 0 + return msg.To() == nil } func MessageGasValue(msg Message) *big.Int { @@ -92,13 +88,18 @@ func (self *StateTransition) Coinbase() *state.StateObject { return self.state.GetOrNewStateObject(self.coinbase) } func (self *StateTransition) From() *state.StateObject { - return self.state.GetOrNewStateObject(self.msg.From()) + f, _ := self.msg.From() + return self.state.GetOrNewStateObject(f) } func (self *StateTransition) To() *state.StateObject { - if self.msg != nil && MessageCreatesContract(self.msg) { + if self.msg == nil { return nil } - return self.state.GetOrNewStateObject(self.msg.To()) + to := self.msg.To() + if to == nil { + return nil // contract creation + } + return self.state.GetOrNewStateObject(*to) } func (self *StateTransition) UseGas(amount *big.Int) error { @@ -119,7 +120,7 @@ func (self *StateTransition) BuyGas() error { sender := self.From() if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 { - return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address()[:4], MessageGasValue(self.msg), sender.Balance()) + return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], MessageGasValue(self.msg), sender.Balance()) } coinbase := self.Coinbase() @@ -196,7 +197,8 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er var ref vm.ContextRef if MessageCreatesContract(msg) { contract := makeContract(msg, self.state) - ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) + addr := contract.Address() + ret, err, ref = vmenv.Create(sender, &addr, self.msg.Data(), self.gas, self.gasPrice, self.value) if err == nil { dataGas := big.NewInt(int64(len(ret))) dataGas.Mul(dataGas, vm.GasCreateByte) @@ -230,7 +232,7 @@ func (self *StateTransition) refundGas() { for addr, ref := range self.state.Refunds() { refund := common.BigMin(uhalf, ref) self.gas.Add(self.gas, refund) - self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice())) + self.state.AddBalance(common.StringToAddress(addr), refund.Mul(refund, self.msg.GasPrice())) } coinbase.RefundGas(self.gas, self.msg.GasPrice()) @@ -242,7 +244,8 @@ func (self *StateTransition) gasUsed() *big.Int { // Converts an message in to a state object func makeContract(msg Message, state *state.StateDB) *state.StateObject { - addr := AddressFromMessage(msg) + faddr, _ := msg.From() + addr := crypto.CreateAddress(faddr, msg.Nonce()) contract := state.GetOrNewStateObject(addr) contract.SetInitCode(msg.Data()) diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 515cc2040..7c50e5e53 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -5,8 +5,8 @@ import ( "fmt" "sync" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" ) @@ -20,9 +20,7 @@ var ( const txPoolQueueSize = 50 type TxPoolHook chan *types.Transaction -type TxMsg struct { - Tx *types.Transaction -} +type TxMsg struct{ Tx *types.Transaction } const ( minGasPrice = 1000000 @@ -44,7 +42,7 @@ type TxPool struct { quit chan bool // The actual pool //pool *list.List - txs map[string]*types.Transaction + txs map[common.Hash]*types.Transaction SecondaryProcessor TxProcessor @@ -55,7 +53,7 @@ type TxPool struct { func NewTxPool(eventMux *event.TypeMux) *TxPool { return &TxPool{ - txs: make(map[string]*types.Transaction), + txs: make(map[common.Hash]*types.Transaction), queueChan: make(chan *types.Transaction, txPoolQueueSize), quit: make(chan bool), eventMux: eventMux, @@ -63,21 +61,16 @@ func NewTxPool(eventMux *event.TypeMux) *TxPool { } func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { - if len(tx.To()) != 0 && len(tx.To()) != 20 { - return fmt.Errorf("Invalid recipient. len = %d", len(tx.To())) + // Validate sender + if _, err := tx.From(); err != nil { + return err } - // Validate curve param v, _, _ := tx.Curve() if v > 28 || v < 27 { return fmt.Errorf("tx.v != (28 || 27) => %v", v) } - - // Validate sender address - senderAddr := tx.From() - if senderAddr == nil || len(senderAddr) != 20 { - return ErrInvalidSender - } + return nil /* XXX this kind of validation needs to happen elsewhere in the gui when sending txs. Other clients should do their own validation. Value transfer could throw error @@ -91,19 +84,17 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From()) } */ - - return nil } func (self *TxPool) addTx(tx *types.Transaction) { - self.txs[string(tx.Hash())] = tx + self.txs[tx.Hash()] = tx } func (self *TxPool) add(tx *types.Transaction) error { - if self.txs[string(tx.Hash())] != nil { - return fmt.Errorf("Known transaction (%x)", tx.Hash()[0:4]) + hash := tx.Hash() + if self.txs[hash] != nil { + return fmt.Errorf("Known transaction (%x)", hash[0:4]) } - err := self.ValidateTransaction(tx) if err != nil { return err @@ -111,19 +102,17 @@ func (self *TxPool) add(tx *types.Transaction) error { self.addTx(tx) - var to string - if len(tx.To()) > 0 { - to = common.Bytes2Hex(tx.To()[:4]) + var toname string + if to := tx.To(); to != nil { + toname = common.Bytes2Hex(to[:4]) } else { - to = "[NEW_CONTRACT]" + toname = "[NEW_CONTRACT]" } - var from string - if len(tx.From()) > 0 { - from = common.Bytes2Hex(tx.From()[:4]) - } else { - return errors.New(fmt.Sprintf("FROM ADDRESS MUST BE POSITIVE (was %v)", tx.From())) - } - txplogger.Debugf("(t) %x => %s (%v) %x\n", from, to, tx.Value, tx.Hash()) + // we can ignore the error here because From is + // verified in ValidateTransaction. + f, _ := tx.From() + from := common.Bytes2Hex(f[:4]) + txplogger.Debugf("(t) %x => %s (%v) %x\n", from, toname, tx.Value, tx.Hash()) // Notify the subscribers go self.eventMux.Post(TxPreEvent{tx}) @@ -140,6 +129,7 @@ func (self *TxPool) Add(tx *types.Transaction) error { defer self.mu.Unlock() return self.add(tx) } + func (self *TxPool) AddTransactions(txs []*types.Transaction) { self.mu.Lock() defer self.mu.Unlock() @@ -148,7 +138,8 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) { if err := self.add(tx); err != nil { txplogger.Debugln(err) } else { - txplogger.Debugf("tx %x\n", tx.Hash()[0:4]) + h := tx.Hash() + txplogger.Debugf("tx %x\n", h[:4]) } } } @@ -172,7 +163,8 @@ func (pool *TxPool) RemoveInvalid(query StateQuery) { var removedTxs types.Transactions for _, tx := range pool.txs { - sender := query.GetAccount(tx.From()) + from, _ := tx.From() + sender := query.GetAccount(from[:]) err := pool.ValidateTransaction(tx) if err != nil || sender.Nonce() >= tx.Nonce() { removedTxs = append(removedTxs, tx) @@ -186,14 +178,13 @@ func (pool *TxPool) RemoveInvalid(query StateQuery) { func (self *TxPool) RemoveSet(txs types.Transactions) { self.mu.Lock() defer self.mu.Unlock() - for _, tx := range txs { - delete(self.txs, string(tx.Hash())) + delete(self.txs, tx.Hash()) } } func (pool *TxPool) Flush() { - pool.txs = make(map[string]*types.Transaction) + pool.txs = make(map[common.Hash]*types.Transaction) } func (pool *TxPool) Start() { diff --git a/core/types/block.go b/core/types/block.go index 2d65cdca6..aca23aa04 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -1,33 +1,32 @@ package types import ( - "bytes" "encoding/binary" "fmt" "math/big" "sort" "time" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) type Header struct { // Hash to the previous block - ParentHash []byte + ParentHash common.Hash // Uncles of this block - UncleHash []byte + UncleHash common.Hash // The coin base address - Coinbase []byte + Coinbase common.Address // Block Trie state - Root []byte + Root common.Hash // Tx sha - TxHash []byte + TxHash common.Hash // Receipt sha - ReceiptHash []byte + ReceiptHash common.Hash // Bloom - Bloom []byte + Bloom Bloom // Difficulty for the current block Difficulty *big.Int // The block number @@ -41,9 +40,9 @@ type Header struct { // Extra data Extra string // Mix digest for quick checking to prevent DOS - MixDigest []byte + MixDigest common.Hash // Nonce - Nonce []byte + Nonce [8]byte } func (self *Header) rlpData(withNonce bool) []interface{} { @@ -73,28 +72,30 @@ func (self *Header) RlpData() interface{} { return self.rlpData(true) } -func (self *Header) Hash() []byte { - return crypto.Sha3(common.Encode(self.rlpData(true))) +func (self *Header) Hash() common.Hash { + return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(true)))) } -func (self *Header) HashNoNonce() []byte { - return crypto.Sha3(common.Encode(self.rlpData(false))) +func (self *Header) HashNoNonce() common.Hash { + return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(false)))) } type Block struct { - // Preset Hash for mock - HeaderHash []byte - ParentHeaderHash []byte - header *Header - uncles []*Header - transactions Transactions - Td *big.Int + // Preset Hash for mock (Tests) + HeaderHash common.Hash + ParentHeaderHash common.Hash + // ^^^^ ignore ^^^^ + + header *Header + uncles []*Header + transactions Transactions + Td *big.Int receipts Receipts Reward *big.Int } -func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce uint64, extra string) *Block { +func NewBlock(parentHash common.Hash, coinbase common.Address, root common.Hash, difficulty *big.Int, nonce uint64, extra string) *Block { header := &Header{ Root: root, ParentHash: parentHash, @@ -105,16 +106,15 @@ func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.I GasUsed: new(big.Int), GasLimit: new(big.Int), } - header.setNonce(nonce) + header.SetNonce(nonce) block := &Block{header: header, Reward: new(big.Int)} return block } -func (self *Header) setNonce(nonce uint64) { - self.Nonce = make([]byte, 8) - binary.BigEndian.PutUint64(self.Nonce, nonce) +func (self *Header) SetNonce(nonce uint64) { + binary.BigEndian.PutUint64(self.Nonce[:], nonce) } func NewBlockWithHeader(header *Header) *Block { @@ -148,16 +148,16 @@ func (self *Block) Uncles() []*Header { func (self *Block) SetUncles(uncleHeaders []*Header) { self.uncles = uncleHeaders - self.header.UncleHash = crypto.Sha3(common.Encode(uncleHeaders)) + self.header.UncleHash = common.BytesToHash(crypto.Sha3(common.Encode(uncleHeaders))) } func (self *Block) Transactions() Transactions { return self.transactions } -func (self *Block) Transaction(hash []byte) *Transaction { +func (self *Block) Transaction(hash common.Hash) *Transaction { for _, transaction := range self.transactions { - if bytes.Equal(hash, transaction.Hash()) { + if transaction.Hash() == hash { return transaction } } @@ -196,23 +196,23 @@ func (self *Block) RlpDataForStorage() interface{} { } // Header accessors (add as you need them) -func (self *Block) Number() *big.Int { return self.header.Number } -func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() } -func (self *Block) MixDigest() []byte { return self.header.MixDigest } +func (self *Block) Number() *big.Int { return self.header.Number } +func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() } +func (self *Block) MixDigest() common.Hash { return self.header.MixDigest } func (self *Block) Nonce() uint64 { - return binary.BigEndian.Uint64(self.header.Nonce) + return binary.BigEndian.Uint64(self.header.Nonce[:]) } func (self *Block) SetNonce(nonce uint64) { - self.header.setNonce(nonce) + self.header.SetNonce(nonce) } -func (self *Block) Bloom() []byte { return self.header.Bloom } -func (self *Block) Coinbase() []byte { return self.header.Coinbase } -func (self *Block) Time() int64 { return int64(self.header.Time) } -func (self *Block) GasLimit() *big.Int { return self.header.GasLimit } -func (self *Block) GasUsed() *big.Int { return self.header.GasUsed } -func (self *Block) Root() []byte { return self.header.Root } -func (self *Block) SetRoot(root []byte) { self.header.Root = root } +func (self *Block) Bloom() Bloom { return self.header.Bloom } +func (self *Block) Coinbase() common.Address { return self.header.Coinbase } +func (self *Block) Time() int64 { return int64(self.header.Time) } +func (self *Block) GasLimit() *big.Int { return self.header.GasLimit } +func (self *Block) GasUsed() *big.Int { return self.header.GasUsed } +func (self *Block) Root() common.Hash { return self.header.Root } +func (self *Block) SetRoot(root common.Hash) { self.header.Root = root } func (self *Block) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) } func (self *Block) GetTransaction(i int) *Transaction { if len(self.transactions) > i { @@ -228,19 +228,19 @@ func (self *Block) GetUncle(i int) *Header { } // Implement pow.Block -func (self *Block) Difficulty() *big.Int { return self.header.Difficulty } -func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() } +func (self *Block) Difficulty() *big.Int { return self.header.Difficulty } +func (self *Block) HashNoNonce() common.Hash { return self.header.HashNoNonce() } -func (self *Block) Hash() []byte { - if self.HeaderHash != nil { +func (self *Block) Hash() common.Hash { + if (self.HeaderHash != common.Hash{}) { return self.HeaderHash } else { return self.header.Hash() } } -func (self *Block) ParentHash() []byte { - if self.ParentHeaderHash != nil { +func (self *Block) ParentHash() common.Hash { + if (self.ParentHeaderHash != common.Hash{}) { return self.ParentHeaderHash } else { return self.header.ParentHash diff --git a/core/types/block_test.go b/core/types/block_test.go index ab1254f4c..16ba3043f 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -1 +1,19 @@ package types + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" +) + +// XXX Tests doesn't really do anything. This tests exists while working on the fixed size conversions +func TestConversion(t *testing.T) { + var ( + parent common.Hash + coinbase common.Address + hash common.Hash + ) + + NewBlock(parent, coinbase, hash, big.NewInt(0), 0, "") +} diff --git a/core/types/bloom9.go b/core/types/bloom9.go index af76f226f..64a8ff49a 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -3,32 +3,32 @@ package types import ( "math/big" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/state" ) -func CreateBloom(receipts Receipts) []byte { +func CreateBloom(receipts Receipts) Bloom { bin := new(big.Int) for _, receipt := range receipts { bin.Or(bin, LogsBloom(receipt.logs)) } - return common.LeftPadBytes(bin.Bytes(), 256) + return BytesToBloom(bin.Bytes()) } func LogsBloom(logs state.Logs) *big.Int { bin := new(big.Int) for _, log := range logs { - data := make([][]byte, len(log.Topics())+1) - data[0] = log.Address() + data := make([]common.Hash, len(log.Topics())) + bin.Or(bin, bloom9(log.Address().Bytes())) for i, topic := range log.Topics() { - data[i+1] = topic + data[i] = topic } for _, b := range data { - bin.Or(bin, common.BigD(bloom9(crypto.Sha3(b)).Bytes())) + bin.Or(bin, bloom9(b[:])) } } @@ -36,20 +36,24 @@ func LogsBloom(logs state.Logs) *big.Int { } func bloom9(b []byte) *big.Int { + b = crypto.Sha3(b[:]) + r := new(big.Int) - for i := 0; i < 16; i += 2 { + for i := 0; i < 6; i += 2 { t := big.NewInt(1) - b := uint(b[i+1]) + 1024*(uint(b[i])&1) + b := (uint(b[i+1]) + (uint(b[i]) << 8)) & 2047 r.Or(r, t.Lsh(t, b)) } return r } -func BloomLookup(bin, topic []byte) bool { - bloom := common.BigD(bin) - cmp := bloom9(crypto.Sha3(topic)) +var Bloom9 = bloom9 + +func BloomLookup(bin Bloom, topic common.Hash) bool { + bloom := bin.Big() + cmp := bloom9(topic[:]) return bloom.And(bloom, cmp).Cmp(cmp) == 0 } diff --git a/core/types/common.go b/core/types/common.go index 795374959..cb57ef053 100644 --- a/core/types/common.go +++ b/core/types/common.go @@ -1,7 +1,37 @@ package types -import "math/big" +import ( + "fmt" + "math/big" + + "github.com/ethereum/go-ethereum/common" +) type BlockProcessor interface { Process(*Block) (*big.Int, error) } + +const bloomLength = 256 + +type Bloom [bloomLength]byte + +func BytesToBloom(b []byte) Bloom { + var bloom Bloom + bloom.SetBytes(b) + return bloom +} + +func (b *Bloom) SetBytes(d []byte) { + if len(b) < len(d) { + panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d))) + } + + // reverse loop + for i := len(d) - 1; i >= 0; i-- { + b[bloomLength-len(d)+i] = b[i] + } +} + +func (b Bloom) Big() *big.Int { + return common.Bytes2Big(b[:]) +} diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go index 593a31f1c..0fc45a508 100644 --- a/core/types/derive_sha.go +++ b/core/types/derive_sha.go @@ -1,8 +1,8 @@ package types import ( - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" ) @@ -11,12 +11,12 @@ type DerivableList interface { GetRlp(i int) []byte } -func DeriveSha(list DerivableList) []byte { +func DeriveSha(list DerivableList) common.Hash { db, _ := ethdb.NewMemDatabase() trie := trie.New(nil, db) for i := 0; i < list.Len(); i++ { trie.Update(common.Encode(i), list.GetRlp(i)) } - return trie.Root() + return common.BytesToHash(trie.Root()) } diff --git a/core/types/receipt.go b/core/types/receipt.go index be14d0e0e..f88d42b29 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -3,16 +3,18 @@ package types import ( "bytes" "fmt" + "io" "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) type Receipt struct { PostState []byte CumulativeGasUsed *big.Int - Bloom []byte + Bloom Bloom logs state.Logs } @@ -20,34 +22,26 @@ func NewReceipt(root []byte, cumalativeGasUsed *big.Int) *Receipt { return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumalativeGasUsed)} } -func NewRecieptFromValue(val *common.Value) *Receipt { - r := &Receipt{} - r.RlpValueDecode(val) - - return r -} - func (self *Receipt) SetLogs(logs state.Logs) { self.logs = logs } -func (self *Receipt) RlpValueDecode(decoder *common.Value) { - 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, state.NewLogFromValue(it.Value())) - } +func (self *Receipt) EncodeRLP(w io.Writer) error { + return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs}) } +/* func (self *Receipt) RlpData() interface{} { return []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs.RlpData()} } +*/ func (self *Receipt) RlpEncode() []byte { - return common.Encode(self.RlpData()) + bytes, err := rlp.EncodeToBytes(self) + if err != nil { + fmt.Println("TMP -- RECEIPT ENCODE ERROR", err) + } + return bytes } func (self *Receipt) Cmp(other *Receipt) bool { @@ -64,6 +58,7 @@ func (self *Receipt) String() string { type Receipts []*Receipt +/* func (self Receipts) RlpData() interface{} { data := make([]interface{}, len(self)) for i, receipt := range self { @@ -72,9 +67,14 @@ func (self Receipts) RlpData() interface{} { return data } +*/ func (self Receipts) RlpEncode() []byte { - return common.Encode(self.RlpData()) + bytes, err := rlp.EncodeToBytes(self) + if err != nil { + fmt.Println("TMP -- RECEIPTS ENCODE ERROR", err) + } + return bytes } func (self Receipts) Len() int { return len(self) } diff --git a/core/types/transaction.go b/core/types/transaction.go index dcd48af11..24035a3ae 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -2,13 +2,15 @@ package types import ( "bytes" - "crypto/ecdsa" + "errors" "fmt" + "io" "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/secp256k1" - "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/rlp" ) @@ -20,39 +22,32 @@ type Transaction struct { AccountNonce uint64 Price *big.Int GasLimit *big.Int - Recipient []byte + Recipient *common.Address // nil means contract creation Amount *big.Int Payload []byte V byte R, S []byte } -func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction { - return NewTransactionMessage(nil, Amount, gasAmount, price, data) +func NewContractCreationTx(amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction { + return &Transaction{Recipient: nil, Amount: amount, GasLimit: gasLimit, Price: gasPrice, Payload: data} } -func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction { - return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data} +func NewTransactionMessage(to common.Address, amount, gasAmount, gasPrice *big.Int, data []byte) *Transaction { + return &Transaction{Recipient: &to, Amount: amount, GasLimit: gasAmount, Price: gasPrice, Payload: data} } func NewTransactionFromBytes(data []byte) *Transaction { - tx := &Transaction{} - tx.RlpDecode(data) - - return tx -} - -func NewTransactionFromAmount(val *common.Value) *Transaction { - tx := &Transaction{} - tx.RlpValueDecode(val) - + tx := new(Transaction) + rlp.Decode(bytes.NewReader(data), tx) return tx } -func (tx *Transaction) Hash() []byte { - data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload} - - return crypto.Sha3(common.Encode(data)) +func (tx *Transaction) Hash() (a common.Hash) { + h := sha3.NewKeccak256() + rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}) + h.Sum(a[:0]) + return a } func (self *Transaction) Data() []byte { @@ -79,68 +74,47 @@ func (self *Transaction) SetNonce(AccountNonce uint64) { self.AccountNonce = AccountNonce } -func (self *Transaction) From() []byte { - return self.sender() +func (self *Transaction) From() (common.Address, error) { + pubkey := self.PublicKey() + if len(pubkey) == 0 || pubkey[0] != 4 { + return common.Address{}, errors.New("invalid public key") + } + var addr common.Address + copy(addr[:], crypto.Sha3(pubkey[1:])) + return addr, nil } -func (self *Transaction) To() []byte { - return self.Recipient +// To returns the recipient of the transaction. +// If transaction is a contract creation (with no recipient address) +// To returns nil. +func (tx *Transaction) To() *common.Address { + return tx.Recipient } func (tx *Transaction) Curve() (v byte, r []byte, s []byte) { v = byte(tx.V) r = common.LeftPadBytes(tx.R, 32) s = common.LeftPadBytes(tx.S, 32) - return } func (tx *Transaction) Signature(key []byte) []byte { hash := tx.Hash() - - sig, _ := secp256k1.Sign(hash, key) - + sig, _ := secp256k1.Sign(hash[:], key) return sig } func (tx *Transaction) PublicKey() []byte { hash := tx.Hash() - v, r, s := tx.Curve() - sig := append(r, s...) sig = append(sig, v-27) //pubkey := crypto.Ecrecover(append(hash, sig...)) - pubkey, _ := secp256k1.RecoverPubkey(hash, sig) - + pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig) return pubkey } -func (tx *Transaction) sender() []byte { - pubkey := tx.PublicKey() - - // Validate the returned key. - // Return nil if public key isn't in full format - if len(pubkey) == 0 || pubkey[0] != 4 { - return nil - } - - return crypto.Sha3(pubkey[1:])[12:] -} - -// TODO: deprecate after new accounts & key stores are integrated -func (tx *Transaction) Sign(privk []byte) error { - - sig := tx.Signature(privk) - - tx.R = sig[:32] - tx.S = sig[32:64] - tx.V = sig[64] + 27 - - return nil -} - func (tx *Transaction) SetSignatureValues(sig []byte) error { tx.R = sig[:32] tx.S = sig[32:64] @@ -148,42 +122,47 @@ func (tx *Transaction) SetSignatureValues(sig []byte) error { return nil } -func (tx *Transaction) SignECDSA(key *ecdsa.PrivateKey) error { - return tx.Sign(crypto.FromECDSA(key)) +func (tx Transaction) EncodeRLP(w io.Writer) error { + return rlp.Encode(w, []interface{}{ + tx.AccountNonce, + tx.Price, tx.GasLimit, + tx.Recipient, + tx.Amount, + tx.Payload, + tx.V, + tx.R, + tx.S, + }) } +// TODO: remove func (tx *Transaction) RlpData() interface{} { data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload} - return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes()) } +// TODO: remove func (tx *Transaction) RlpEncode() []byte { return common.Encode(tx) } -func (tx *Transaction) RlpDecode(data []byte) { - rlp.Decode(bytes.NewReader(data), tx) -} - -func (tx *Transaction) RlpValueDecode(decoder *common.Value) { - tx.AccountNonce = decoder.Get(0).Uint() - tx.Price = decoder.Get(1).BigInt() - tx.GasLimit = decoder.Get(2).BigInt() - tx.Recipient = decoder.Get(3).Bytes() - tx.Amount = decoder.Get(4).BigInt() - tx.Payload = decoder.Get(5).Bytes() - tx.V = decoder.Get(6).Byte() - tx.R = decoder.Get(7).Bytes() - tx.S = decoder.Get(8).Bytes() -} - func (tx *Transaction) String() string { + var from, to string + if f, err := tx.From(); err != nil { + from = "[invalid sender]" + } else { + from = fmt.Sprintf("%x", f[:]) + } + if t := tx.To(); t == nil { + to = "[contract creation]" + } else { + to = fmt.Sprintf("%x", t[:]) + } return fmt.Sprintf(` TX(%x) Contract: %v - From: %x - To: %x + From: %s + To: %s Nonce: %v GasPrice: %v GasLimit %v @@ -196,8 +175,8 @@ func (tx *Transaction) String() string { `, tx.Hash(), len(tx.Recipient) == 0, - tx.From(), - tx.To(), + from, + to, tx.AccountNonce, tx.Price, tx.GasLimit, @@ -213,6 +192,7 @@ func (tx *Transaction) String() string { // Transaction slice type for basic sorting type Transactions []*Transaction +// TODO: remove func (self Transactions) RlpData() interface{} { // Marshal the transactions of this block enc := make([]interface{}, len(self)) @@ -223,6 +203,7 @@ func (self Transactions) RlpData() interface{} { 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 common.Rlp(s[i]) } diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index ab1254f4c..0b0dfe3ff 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -1 +1,58 @@ package types + +import ( + "bytes" + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" +) + +// The values in those tests are from the Transaction Tests +// at github.com/ethereum/tests. + +var ( + emptyTx = NewTransactionMessage( + common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), + big.NewInt(0), big.NewInt(0), big.NewInt(0), + nil, + ) + + rightvrsRecipient = common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b") + rightvrsTx = &Transaction{ + Recipient: &rightvrsRecipient, + AccountNonce: 3, + Price: big.NewInt(1), + GasLimit: big.NewInt(2000), + Amount: big.NewInt(10), + Payload: common.FromHex("5544"), + V: 28, + R: common.FromHex("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a"), + S: common.FromHex("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3"), + } +) + +func TestTransactionHash(t *testing.T) { + // "EmptyTransaction" + if emptyTx.Hash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") { + t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash()) + } + + // "RightVRSTest" + if rightvrsTx.Hash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") { + t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash()) + } +} + +func TestTransactionEncode(t *testing.T) { + // "RightVRSTest" + txb, err := rlp.EncodeToBytes(rightvrsTx) + if err != nil { + t.Fatalf("encode error: %v", err) + } + should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3") + if !bytes.Equal(txb, should) { + t.Errorf("encoded RLP mismatch, got %x", txb) + } +} diff --git a/core/vm_env.go b/core/vm_env.go index c7491bcdc..7845d1cd9 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -3,6 +3,7 @@ package core import ( "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" @@ -27,24 +28,24 @@ func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types } } -func (self *VMEnv) Origin() []byte { return self.msg.From() } -func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } -func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() } -func (self *VMEnv) Time() int64 { return self.block.Time() } -func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() } -func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() } -func (self *VMEnv) Value() *big.Int { return self.msg.Value() } -func (self *VMEnv) State() *state.StateDB { return self.state } -func (self *VMEnv) Depth() int { return self.depth } -func (self *VMEnv) SetDepth(i int) { self.depth = i } -func (self *VMEnv) VmType() vm.Type { return self.typ } -func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t } -func (self *VMEnv) GetHash(n uint64) []byte { +func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f } +func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } +func (self *VMEnv) Coinbase() common.Address { return self.block.Coinbase() } +func (self *VMEnv) Time() int64 { return self.block.Time() } +func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() } +func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() } +func (self *VMEnv) Value() *big.Int { return self.msg.Value() } +func (self *VMEnv) State() *state.StateDB { return self.state } +func (self *VMEnv) Depth() int { return self.depth } +func (self *VMEnv) SetDepth(i int) { self.depth = i } +func (self *VMEnv) VmType() vm.Type { return self.typ } +func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t } +func (self *VMEnv) GetHash(n uint64) common.Hash { if block := self.chain.GetBlockByNumber(n); block != nil { return block.Hash() } - return nil + return common.Hash{} } func (self *VMEnv) AddLog(log state.Log) { self.state.AddLog(log) @@ -53,20 +54,21 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } -func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution { +func (self *VMEnv) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *Execution { return NewExecution(self, addr, data, gas, price, value) } -func (self *VMEnv) Call(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { - exe := self.vm(addr, data, gas, price, value) +func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + exe := self.vm(&addr, data, gas, price, value) return exe.Call(addr, me) } -func (self *VMEnv) CallCode(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { - exe := self.vm(me.Address(), data, gas, price, value) +func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + maddr := me.Address() + exe := self.vm(&maddr, data, gas, price, value) return exe.Call(addr, me) } -func (self *VMEnv) Create(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { +func (self *VMEnv) Create(me vm.ContextRef, addr *common.Address, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { exe := self.vm(addr, data, gas, price, value) return exe.Create(me) } |