aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/bench_test.go2
-rw-r--r--core/block_validator.go3
-rw-r--r--core/blockchain.go18
-rw-r--r--core/blockchain_test.go54
-rw-r--r--core/chain_makers.go28
-rw-r--r--core/chain_makers_test.go2
-rw-r--r--core/chain_pow_test.go6
-rw-r--r--core/config.go7
-rw-r--r--core/dao.go74
-rw-r--r--core/dao_test.go132
-rw-r--r--core/database_util.go6
-rw-r--r--core/database_util_test.go2
-rw-r--r--core/headerchain.go18
-rw-r--r--core/state_processor.go6
-rw-r--r--core/tx_list.go342
-rw-r--r--core/tx_list_test.go52
-rw-r--r--core/tx_pool.go581
-rw-r--r--core/tx_pool_test.go369
-rw-r--r--core/types.go16
-rw-r--r--core/types/block.go152
-rw-r--r--core/types/bloom9.go24
-rw-r--r--core/types/json.go108
-rw-r--r--core/types/json_test.go213
-rw-r--r--core/types/receipt.go69
-rw-r--r--core/types/transaction.go173
-rw-r--r--core/types/transaction_test.go16
-rw-r--r--core/vm/contracts.go4
-rw-r--r--core/vm/environment.go2
-rw-r--r--core/vm/gas.go2
-rw-r--r--core/vm/instructions.go146
-rw-r--r--core/vm/jit.go8
-rw-r--r--core/vm/jit_test.go16
-rw-r--r--core/vm/log.go89
-rw-r--r--core/vm/log_test.go59
-rw-r--r--core/vm/logger.go64
-rw-r--r--core/vm/logger_test.go17
-rw-r--r--core/vm/runtime/env.go14
-rw-r--r--core/vm/segments.go4
-rw-r--r--core/vm/stack.go26
-rw-r--r--core/vm/vm.go18
-rw-r--r--core/vm_env.go14
41 files changed, 2230 insertions, 726 deletions
diff --git a/core/bench_test.go b/core/bench_test.go
index c6029499a..344e7e3c5 100644
--- a/core/bench_test.go
+++ b/core/bench_test.go
@@ -163,7 +163,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
// Generate a chain of b.N blocks using the supplied block
// generator function.
genesis := WriteGenesisBlockForTesting(db, GenesisAccount{benchRootAddr, benchRootFunds})
- chain, _ := GenerateChain(genesis, db, b.N, gen)
+ chain, _ := GenerateChain(nil, genesis, db, b.N, gen)
// Time the insertion of the new chain.
// State and blocks are stored in the same DB.
diff --git a/core/block_validator.go b/core/block_validator.go
index c3f959324..e5bc6178b 100644
--- a/core/block_validator.go
+++ b/core/block_validator.go
@@ -247,7 +247,8 @@ func ValidateHeader(config *ChainConfig, pow pow.PoW, header *types.Header, pare
return &BlockNonceErr{header.Number, header.Hash(), header.Nonce.Uint64()}
}
}
- return nil
+ // If all checks passed, validate the extra-data field for hard forks
+ return ValidateDAOHeaderExtraData(config, header)
}
// CalcDifficulty is the difficulty adjustment algorithm. It returns
diff --git a/core/blockchain.go b/core/blockchain.go
index 950804d40..888c98dce 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -778,6 +778,14 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
localTd := self.GetTd(self.currentBlock.Hash(), self.currentBlock.NumberU64())
externTd := new(big.Int).Add(block.Difficulty(), ptd)
+ // Irrelevant of the canonical status, write the block itself to the database
+ if err := self.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
+ glog.Fatalf("failed to write block total difficulty: %v", err)
+ }
+ if err := WriteBlock(self.chainDb, block); err != nil {
+ glog.Fatalf("failed to write block contents: %v", err)
+ }
+
// If the total difficulty is higher than our known, add it to the canonical chain
// Second clause in the if statement reduces the vulnerability to selfish mining.
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
@@ -788,19 +796,11 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
return NonStatTy, err
}
}
- // Insert the block as the new head of the chain
- self.insert(block)
+ self.insert(block) // Insert the block as the new head of the chain
status = CanonStatTy
} else {
status = SideStatTy
}
- // Irrelevant of the canonical status, write the block itself to the database
- if err := self.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
- glog.Fatalf("failed to write block total difficulty: %v", err)
- }
- if err := WriteBlock(self.chainDb, block); err != nil {
- glog.Fatalf("failed to write block contents: %v", err)
- }
self.futureBlocks.Remove(block.Hash())
diff --git a/core/blockchain_test.go b/core/blockchain_test.go
index a26fe4a1b..de3ef0a9c 100644
--- a/core/blockchain_test.go
+++ b/core/blockchain_test.go
@@ -712,7 +712,7 @@ func TestFastVsFullChains(t *testing.T) {
funds = big.NewInt(1000000000)
genesis = GenesisBlockForTesting(gendb, address, funds)
)
- blocks, receipts := GenerateChain(genesis, gendb, 1024, func(i int, block *BlockGen) {
+ blocks, receipts := GenerateChain(nil, genesis, gendb, 1024, func(i int, block *BlockGen) {
block.SetCoinbase(common.Address{0x00})
// If the block number is multiple of 3, send a few bonus transactions to the miner
@@ -795,7 +795,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
genesis = GenesisBlockForTesting(gendb, address, funds)
)
height := uint64(1024)
- blocks, receipts := GenerateChain(genesis, gendb, int(height), nil)
+ blocks, receipts := GenerateChain(nil, genesis, gendb, int(height), nil)
// Configure a subchain to roll back
remove := []common.Hash{}
@@ -895,7 +895,7 @@ func TestChainTxReorgs(t *testing.T) {
// - futureAdd: transaction added after the reorg has already finished
var pastAdd, freshAdd, futureAdd *types.Transaction
- chain, _ := GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {
+ chain, _ := GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {
switch i {
case 0:
pastDrop, _ = types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key2)
@@ -920,7 +920,7 @@ func TestChainTxReorgs(t *testing.T) {
}
// overwrite the old chain
- chain, _ = GenerateChain(genesis, db, 5, func(i int, gen *BlockGen) {
+ chain, _ = GenerateChain(nil, genesis, db, 5, func(i int, gen *BlockGen) {
switch i {
case 0:
pastAdd, _ = types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key3)
@@ -990,7 +990,7 @@ func TestLogReorgs(t *testing.T) {
blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
subs := evmux.Subscribe(RemovedLogsEvent{})
- chain, _ := GenerateChain(genesis, db, 2, func(i int, gen *BlockGen) {
+ chain, _ := GenerateChain(nil, genesis, db, 2, func(i int, gen *BlockGen) {
if i == 1 {
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), code).SignECDSA(key1)
if err != nil {
@@ -1003,7 +1003,7 @@ func TestLogReorgs(t *testing.T) {
t.Fatalf("failed to insert chain: %v", err)
}
- chain, _ = GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {})
+ chain, _ = GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {})
if _, err := blockchain.InsertChain(chain); err != nil {
t.Fatalf("failed to insert forked chain: %v", err)
}
@@ -1025,12 +1025,12 @@ func TestReorgSideEvent(t *testing.T) {
evmux := &event.TypeMux{}
blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
- chain, _ := GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {})
+ chain, _ := GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {})
if _, err := blockchain.InsertChain(chain); err != nil {
t.Fatalf("failed to insert chain: %v", err)
}
- replacementBlocks, _ := GenerateChain(genesis, db, 4, func(i int, gen *BlockGen) {
+ replacementBlocks, _ := GenerateChain(nil, genesis, db, 4, func(i int, gen *BlockGen) {
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), nil).SignECDSA(key1)
if i == 2 {
gen.OffsetTime(-1)
@@ -1090,3 +1090,41 @@ done:
}
}
+
+// Tests if the canonical block can be fetched from the database during chain insertion.
+func TestCanonicalBlockRetrieval(t *testing.T) {
+ var (
+ db, _ = ethdb.NewMemDatabase()
+ genesis = WriteGenesisBlockForTesting(db)
+ )
+
+ evmux := &event.TypeMux{}
+ blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
+
+ chain, _ := GenerateChain(nil, genesis, db, 10, func(i int, gen *BlockGen) {})
+
+ for i, _ := range chain {
+ go func(block *types.Block) {
+ // try to retrieve a block by its canonical hash and see if the block data can be retrieved.
+ for {
+ ch := GetCanonicalHash(db, block.NumberU64())
+ if ch == (common.Hash{}) {
+ continue // busy wait for canonical hash to be written
+ }
+ if ch != block.Hash() {
+ t.Fatalf("unknown canonical hash, want %s, got %s", block.Hash().Hex(), ch.Hex())
+ }
+ fb := GetBlock(db, ch, block.NumberU64())
+ if fb == nil {
+ t.Fatalf("unable to retrieve block %d for canonical hash: %s", block.NumberU64(), ch.Hex())
+ }
+ if fb.Hash() != block.Hash() {
+ t.Fatalf("invalid block hash for block %d, want %s, got %s", block.NumberU64(), block.Hash().Hex(), fb.Hash().Hex())
+ }
+ return
+ }
+ }(chain[i])
+
+ blockchain.InsertChain(types.Blocks{chain[i]})
+ }
+}
diff --git a/core/chain_makers.go b/core/chain_makers.go
index ef0ac66d1..0b9a5f75d 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
+ "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
)
@@ -35,7 +36,11 @@ import (
// MakeChainConfig returns a new ChainConfig with the ethereum default chain settings.
func MakeChainConfig() *ChainConfig {
- return &ChainConfig{HomesteadBlock: big.NewInt(0)}
+ return &ChainConfig{
+ HomesteadBlock: big.NewInt(0),
+ DAOForkBlock: nil,
+ DAOForkSupport: true,
+ }
}
// FakePow is a non-validating proof of work implementation.
@@ -173,10 +178,27 @@ func (b *BlockGen) OffsetTime(seconds int64) {
// Blocks created by GenerateChain do not contain valid proof of work
// values. Inserting them into BlockChain requires use of FakePow or
// a similar non-validating proof of work implementation.
-func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
+func GenerateChain(config *ChainConfig, parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
genblock := func(i int, h *types.Header, statedb *state.StateDB) (*types.Block, types.Receipts) {
b := &BlockGen{parent: parent, i: i, chain: blocks, header: h, statedb: statedb}
+
+ // Mutate the state and block according to any hard-fork specs
+ if config == nil {
+ config = MakeChainConfig()
+ }
+ if daoBlock := config.DAOForkBlock; daoBlock != nil {
+ limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
+ if h.Number.Cmp(daoBlock) >= 0 && h.Number.Cmp(limit) < 0 {
+ if config.DAOForkSupport {
+ h.Extra = common.CopyBytes(params.DAOForkBlockExtra)
+ }
+ }
+ }
+ if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(h.Number) == 0 {
+ ApplyDAOHardFork(statedb)
+ }
+ // Execute any user modifications to the block and finalize it
if gen != nil {
gen(i, b)
}
@@ -261,7 +283,7 @@ func makeHeaderChain(parent *types.Header, n int, db ethdb.Database, seed int) [
// makeBlockChain creates a deterministic chain of blocks rooted at parent.
func makeBlockChain(parent *types.Block, n int, db ethdb.Database, seed int) []*types.Block {
- blocks, _ := GenerateChain(parent, db, n, func(i int, b *BlockGen) {
+ blocks, _ := GenerateChain(nil, parent, db, n, func(i int, b *BlockGen) {
b.SetCoinbase(common.Address{0: byte(seed), 19: byte(i)})
})
return blocks
diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go
index 32c3efe8d..f52b09ad9 100644
--- a/core/chain_makers_test.go
+++ b/core/chain_makers_test.go
@@ -47,7 +47,7 @@ func ExampleGenerateChain() {
// This call generates a chain of 5 blocks. The function runs for
// each block and adds different features to gen based on the
// block index.
- chain, _ := GenerateChain(genesis, db, 5, func(i int, gen *BlockGen) {
+ chain, _ := GenerateChain(nil, genesis, db, 5, func(i int, gen *BlockGen) {
switch i {
case 0:
// In block 1, addr1 sends addr2 some ether.
diff --git a/core/chain_pow_test.go b/core/chain_pow_test.go
index d2b0bd144..2e26c8211 100644
--- a/core/chain_pow_test.go
+++ b/core/chain_pow_test.go
@@ -60,7 +60,7 @@ func TestPowVerification(t *testing.T) {
var (
testdb, _ = ethdb.NewMemDatabase()
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
- blocks, _ = GenerateChain(genesis, testdb, 8, nil)
+ blocks, _ = GenerateChain(nil, genesis, testdb, 8, nil)
)
headers := make([]*types.Header, len(blocks))
for i, block := range blocks {
@@ -115,7 +115,7 @@ func testPowConcurrentVerification(t *testing.T, threads int) {
var (
testdb, _ = ethdb.NewMemDatabase()
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
- blocks, _ = GenerateChain(genesis, testdb, 8, nil)
+ blocks, _ = GenerateChain(nil, genesis, testdb, 8, nil)
)
headers := make([]*types.Header, len(blocks))
for i, block := range blocks {
@@ -186,7 +186,7 @@ func testPowConcurrentAbortion(t *testing.T, threads int) {
var (
testdb, _ = ethdb.NewMemDatabase()
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
- blocks, _ = GenerateChain(genesis, testdb, 1024, nil)
+ blocks, _ = GenerateChain(nil, genesis, testdb, 1024, nil)
)
headers := make([]*types.Header, len(blocks))
for i, block := range blocks {
diff --git a/core/config.go b/core/config.go
index 81ca76aa3..c0d065a57 100644
--- a/core/config.go
+++ b/core/config.go
@@ -31,16 +31,17 @@ var ChainConfigNotFoundErr = errors.New("ChainConfig not found") // general conf
// that any network, identified by its genesis block, can have its own
// set of configuration options.
type ChainConfig struct {
- HomesteadBlock *big.Int // homestead switch block
+ HomesteadBlock *big.Int `json:"homesteadBlock"` // Homestead switch block (nil = no fork, 0 = already homestead)
+ DAOForkBlock *big.Int `json:"daoForkBlock"` // TheDAO hard-fork switch block (nil = no fork)
+ DAOForkSupport bool `json:"daoForkSupport"` // Whether the nodes supports or opposes the DAO hard-fork
VmConfig vm.Config `json:"-"`
}
// IsHomestead returns whether num is either equal to the homestead block or greater.
func (c *ChainConfig) IsHomestead(num *big.Int) bool {
- if num == nil {
+ if c.HomesteadBlock == nil || num == nil {
return false
}
-
return num.Cmp(c.HomesteadBlock) >= 0
}
diff --git a/core/dao.go b/core/dao.go
new file mode 100644
index 000000000..e315c9884
--- /dev/null
+++ b/core/dao.go
@@ -0,0 +1,74 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package core
+
+import (
+ "bytes"
+ "math/big"
+
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+// ValidateDAOHeaderExtraData validates the extra-data field of a block header to
+// ensure it conforms to DAO hard-fork rules.
+//
+// DAO hard-fork extension to the header validity:
+// a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range
+// with the fork specific extra-data set
+// b) if the node is pro-fork, require blocks in the specific range to have the
+// unique extra-data set.
+func ValidateDAOHeaderExtraData(config *ChainConfig, header *types.Header) error {
+ // Short circuit validation if the node doesn't care about the DAO fork
+ if config.DAOForkBlock == nil {
+ return nil
+ }
+ // Make sure the block is within the fork's modified extra-data range
+ limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
+ if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
+ return nil
+ }
+ // Depending whether we support or oppose the fork, validate the extra-data contents
+ if config.DAOForkSupport {
+ if bytes.Compare(header.Extra, params.DAOForkBlockExtra) != 0 {
+ return ValidationError("DAO pro-fork bad block extra-data: 0x%x", header.Extra)
+ }
+ } else {
+ if bytes.Compare(header.Extra, params.DAOForkBlockExtra) == 0 {
+ return ValidationError("DAO no-fork bad block extra-data: 0x%x", header.Extra)
+ }
+ }
+ // All ok, header has the same extra-data we expect
+ return nil
+}
+
+// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
+// rules, transferring all balances of a set of DAO accounts to a single refund
+// contract.
+func ApplyDAOHardFork(statedb *state.StateDB) {
+ // Retrieve the contract to refund balances into
+ refund := statedb.GetOrNewStateObject(params.DAORefundContract)
+
+ // Move every DAO account and extra-balance account funds into the refund contract
+ for _, addr := range params.DAODrainList {
+ if account := statedb.GetStateObject(addr); account != nil {
+ refund.AddBalance(account.Balance())
+ account.SetBalance(new(big.Int))
+ }
+ }
+}
diff --git a/core/dao_test.go b/core/dao_test.go
new file mode 100644
index 000000000..0830b1231
--- /dev/null
+++ b/core/dao_test.go
@@ -0,0 +1,132 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package core
+
+import (
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/event"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+// Tests that DAO-fork enabled clients can properly filter out fork-commencing
+// blocks based on their extradata fields.
+func TestDAOForkRangeExtradata(t *testing.T) {
+ forkBlock := big.NewInt(32)
+
+ // Generate a common prefix for both pro-forkers and non-forkers
+ db, _ := ethdb.NewMemDatabase()
+ genesis := WriteGenesisBlockForTesting(db)
+ prefix, _ := GenerateChain(nil, genesis, db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
+
+ // Create the concurrent, conflicting two nodes
+ proDb, _ := ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(proDb)
+ proConf := &ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: true}
+ proBc, _ := NewBlockChain(proDb, proConf, new(FakePow), new(event.TypeMux))
+
+ conDb, _ := ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(conDb)
+ conConf := &ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: false}
+ conBc, _ := NewBlockChain(conDb, conConf, new(FakePow), new(event.TypeMux))
+
+ if _, err := proBc.InsertChain(prefix); err != nil {
+ t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
+ }
+ if _, err := conBc.InsertChain(prefix); err != nil {
+ t.Fatalf("con-fork: failed to import chain prefix: %v", err)
+ }
+ // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
+ for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
+ // Create a pro-fork block, and try to feed into the no-fork chain
+ db, _ = ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(db)
+ bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux))
+
+ blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
+ for j := 0; j < len(blocks)/2; j++ {
+ blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
+ }
+ if _, err := bc.InsertChain(blocks); err != nil {
+ t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
+ }
+ blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := conBc.InsertChain(blocks); err == nil {
+ t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
+ }
+ // Create a proper no-fork block for the contra-forker
+ blocks, _ = GenerateChain(conConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := conBc.InsertChain(blocks); err != nil {
+ t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
+ }
+ // Create a no-fork block, and try to feed into the pro-fork chain
+ db, _ = ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(db)
+ bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux))
+
+ blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
+ for j := 0; j < len(blocks)/2; j++ {
+ blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
+ }
+ if _, err := bc.InsertChain(blocks); err != nil {
+ t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
+ }
+ blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := proBc.InsertChain(blocks); err == nil {
+ t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
+ }
+ // Create a proper pro-fork block for the pro-forker
+ blocks, _ = GenerateChain(proConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := proBc.InsertChain(blocks); err != nil {
+ t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
+ }
+ }
+ // Verify that contra-forkers accept pro-fork extra-datas after forking finishes
+ db, _ = ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(db)
+ bc, _ := NewBlockChain(db, conConf, new(FakePow), new(event.TypeMux))
+
+ blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
+ for j := 0; j < len(blocks)/2; j++ {
+ blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
+ }
+ if _, err := bc.InsertChain(blocks); err != nil {
+ t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
+ }
+ blocks, _ = GenerateChain(proConf, conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := conBc.InsertChain(blocks); err != nil {
+ t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
+ }
+ // Verify that pro-forkers accept contra-fork extra-datas after forking finishes
+ db, _ = ethdb.NewMemDatabase()
+ WriteGenesisBlockForTesting(db)
+ bc, _ = NewBlockChain(db, proConf, new(FakePow), new(event.TypeMux))
+
+ blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
+ for j := 0; j < len(blocks)/2; j++ {
+ blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
+ }
+ if _, err := bc.InsertChain(blocks); err != nil {
+ t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
+ }
+ blocks, _ = GenerateChain(conConf, proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
+ if _, err := proBc.InsertChain(blocks); err != nil {
+ t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
+ }
+}
diff --git a/core/database_util.go b/core/database_util.go
index 1529d7369..5f9afe6ba 100644
--- a/core/database_util.go
+++ b/core/database_util.go
@@ -204,7 +204,11 @@ func GetTd(db ethdb.Database, hash common.Hash, number uint64) *big.Int {
}
// GetBlock retrieves an entire block corresponding to the hash, assembling it
-// back from the stored header and body.
+// back from the stored header and body. If either the header or body could not
+// be retrieved nil is returned.
+//
+// Note, due to concurrent download of header and block body the header and thus
+// canonical hash can be stored in the database but the body data not (yet).
func GetBlock(db ethdb.Database, hash common.Hash, number uint64) *types.Block {
// Retrieve the block header and body contents
header := GetHeader(db, hash, number)
diff --git a/core/database_util_test.go b/core/database_util_test.go
index 6c19f78c8..280270ac8 100644
--- a/core/database_util_test.go
+++ b/core/database_util_test.go
@@ -561,7 +561,7 @@ func TestMipmapChain(t *testing.T) {
defer db.Close()
genesis := WriteGenesisBlockForTesting(db, GenesisAccount{addr, big.NewInt(1000000)})
- chain, receipts := GenerateChain(genesis, db, 1010, func(i int, gen *BlockGen) {
+ chain, receipts := GenerateChain(nil, genesis, db, 1010, func(i int, gen *BlockGen) {
var receipts types.Receipts
switch i {
case 1:
diff --git a/core/headerchain.go b/core/headerchain.go
index f856333a0..0f9dd7208 100644
--- a/core/headerchain.go
+++ b/core/headerchain.go
@@ -151,6 +151,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
localTd := hc.GetTd(hc.currentHeaderHash, hc.currentHeader.Number.Uint64())
externTd := new(big.Int).Add(header.Difficulty, ptd)
+ // Irrelevant of the canonical status, write the td and header to the database
+ if err := hc.WriteTd(hash, number, externTd); err != nil {
+ glog.Fatalf("failed to write header total difficulty: %v", err)
+ }
+ if err := WriteHeader(hc.chainDb, header); err != nil {
+ glog.Fatalf("failed to write header contents: %v", err)
+ }
+
// If the total difficulty is higher than our known, add it to the canonical chain
// Second clause in the if statement reduces the vulnerability to selfish mining.
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
@@ -176,6 +184,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
headNumber = headHeader.Number.Uint64() - 1
headHeader = hc.GetHeader(headHash, headNumber)
}
+
// Extend the canonical chain with the new header
if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil {
glog.Fatalf("failed to insert header number: %v", err)
@@ -183,19 +192,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil {
glog.Fatalf("failed to insert head header hash: %v", err)
}
+
hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header)
status = CanonStatTy
} else {
status = SideStatTy
}
- // Irrelevant of the canonical status, write the header itself to the database
- if err := hc.WriteTd(hash, number, externTd); err != nil {
- glog.Fatalf("failed to write header total difficulty: %v", err)
- }
- if err := WriteHeader(hc.chainDb, header); err != nil {
- glog.Fatalf("failed to write header contents: %v", err)
- }
+
hc.headerCache.Add(hash, header)
hc.numberCache.Add(hash, number)
diff --git a/core/state_processor.go b/core/state_processor.go
index 95b3057bb..fd8e9762e 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -65,7 +65,11 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
allLogs vm.Logs
gp = new(GasPool).AddGas(block.GasLimit())
)
-
+ // Mutate the the block and state according to any hard-fork specs
+ if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
+ ApplyDAOHardFork(statedb)
+ }
+ // Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
statedb.StartRecord(tx.Hash(), block.Hash(), i)
receipt, logs, _, err := ApplyTransaction(p.config, p.bc, gp, statedb, header, tx, totalUsedGas, cfg)
diff --git a/core/tx_list.go b/core/tx_list.go
new file mode 100644
index 000000000..c3ddf3148
--- /dev/null
+++ b/core/tx_list.go
@@ -0,0 +1,342 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package core
+
+import (
+ "container/heap"
+ "math"
+ "math/big"
+ "sort"
+
+ "github.com/ethereum/go-ethereum/core/types"
+)
+
+// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
+// retrieving sorted transactions from the possibly gapped future queue.
+type nonceHeap []uint64
+
+func (h nonceHeap) Len() int { return len(h) }
+func (h nonceHeap) Less(i, j int) bool { return h[i] < h[j] }
+func (h nonceHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
+
+func (h *nonceHeap) Push(x interface{}) {
+ *h = append(*h, x.(uint64))
+}
+
+func (h *nonceHeap) Pop() interface{} {
+ old := *h
+ n := len(old)
+ x := old[n-1]
+ *h = old[0 : n-1]
+ return x
+}
+
+// txSortedMap is a nonce->transaction hash map with a heap based index to allow
+// iterating over the contents in a nonce-incrementing way.
+type txSortedMap struct {
+ items map[uint64]*types.Transaction // Hash map storing the transaction data
+ index *nonceHeap // Heap of nonces of all the stored transactions (non-strict mode)
+ cache types.Transactions // Cache of the transactions already sorted
+}
+
+// newTxSortedMap creates a new sorted transaction map.
+func newTxSortedMap() *txSortedMap {
+ return &txSortedMap{
+ items: make(map[uint64]*types.Transaction),
+ index: &nonceHeap{},
+ }
+}
+
+// Get retrieves the current transactions associated with the given nonce.
+func (m *txSortedMap) Get(nonce uint64) *types.Transaction {
+ return m.items[nonce]
+}
+
+// Put inserts a new transaction into the map, also updating the map's nonce
+// index. If a transaction already exists with the same nonce, it's overwritten.
+func (m *txSortedMap) Put(tx *types.Transaction) {
+ nonce := tx.Nonce()
+ if m.items[nonce] == nil {
+ heap.Push(m.index, nonce)
+ }
+ m.items[nonce], m.cache = tx, nil
+}
+
+// Forward removes all transactions from the map with a nonce lower than the
+// provided threshold. Every removed transaction is returned for any post-removal
+// maintenance.
+func (m *txSortedMap) Forward(threshold uint64) types.Transactions {
+ var removed types.Transactions
+
+ // Pop off heap items until the threshold is reached
+ for m.index.Len() > 0 && (*m.index)[0] < threshold {
+ nonce := heap.Pop(m.index).(uint64)
+ removed = append(removed, m.items[nonce])
+ delete(m.items, nonce)
+ }
+ // If we had a cached order, shift the front
+ if m.cache != nil {
+ m.cache = m.cache[len(removed):]
+ }
+ return removed
+}
+
+// Filter iterates over the list of transactions and removes all of them for which
+// the specified function evaluates to true.
+func (m *txSortedMap) Filter(filter func(*types.Transaction) bool) types.Transactions {
+ var removed types.Transactions
+
+ // Collect all the transactions to filter out
+ for nonce, tx := range m.items {
+ if filter(tx) {
+ removed = append(removed, tx)
+ delete(m.items, nonce)
+ }
+ }
+ // If transactions were removed, the heap and cache are ruined
+ if len(removed) > 0 {
+ *m.index = make([]uint64, 0, len(m.items))
+ for nonce, _ := range m.items {
+ *m.index = append(*m.index, nonce)
+ }
+ heap.Init(m.index)
+
+ m.cache = nil
+ }
+ return removed
+}
+
+// Cap places a hard limit on the number of items, returning all transactions
+// exceeding that limit.
+func (m *txSortedMap) Cap(threshold int) types.Transactions {
+ // Short circuit if the number of items is under the limit
+ if len(m.items) <= threshold {
+ return nil
+ }
+ // Otherwise gather and drop the highest nonce'd transactions
+ var drops types.Transactions
+
+ sort.Sort(*m.index)
+ for size := len(m.items); size > threshold; size-- {
+ drops = append(drops, m.items[(*m.index)[size-1]])
+ delete(m.items, (*m.index)[size-1])
+ }
+ *m.index = (*m.index)[:threshold]
+ heap.Init(m.index)
+
+ // If we had a cache, shift the back
+ if m.cache != nil {
+ m.cache = m.cache[:len(m.cache)-len(drops)]
+ }
+ return drops
+}
+
+// Remove deletes a transaction from the maintained map, returning whether the
+// transaction was found.
+func (m *txSortedMap) Remove(nonce uint64) bool {
+ // Short circuit if no transaction is present
+ _, ok := m.items[nonce]
+ if !ok {
+ return false
+ }
+ // Otherwise delete the transaction and fix the heap index
+ for i := 0; i < m.index.Len(); i++ {
+ if (*m.index)[i] == nonce {
+ heap.Remove(m.index, i)
+ break
+ }
+ }
+ delete(m.items, nonce)
+ m.cache = nil
+
+ return true
+}
+
+// Ready retrieves a sequentially increasing list of transactions starting at the
+// provided nonce that is ready for processing. The returned transactions will be
+// removed from the list.
+//
+// Note, all transactions with nonces lower than start will also be returned to
+// prevent getting into and invalid state. This is not something that should ever
+// happen but better to be self correcting than failing!
+func (m *txSortedMap) Ready(start uint64) types.Transactions {
+ // Short circuit if no transactions are available
+ if m.index.Len() == 0 || (*m.index)[0] > start {
+ return nil
+ }
+ // Otherwise start accumulating incremental transactions
+ var ready types.Transactions
+ for next := (*m.index)[0]; m.index.Len() > 0 && (*m.index)[0] == next; next++ {
+ ready = append(ready, m.items[next])
+ delete(m.items, next)
+ heap.Pop(m.index)
+ }
+ m.cache = nil
+
+ return ready
+}
+
+// Len returns the length of the transaction map.
+func (m *txSortedMap) Len() int {
+ return len(m.items)
+}
+
+// Flatten creates a nonce-sorted slice of transactions based on the loosely
+// sorted internal representation. The result of the sorting is cached in case
+// it's requested again before any modifications are made to the contents.
+func (m *txSortedMap) Flatten() types.Transactions {
+ // If the sorting was not cached yet, create and cache it
+ if m.cache == nil {
+ m.cache = make(types.Transactions, 0, len(m.items))
+ for _, tx := range m.items {
+ m.cache = append(m.cache, tx)
+ }
+ sort.Sort(types.TxByNonce(m.cache))
+ }
+ // Copy the cache to prevent accidental modifications
+ txs := make(types.Transactions, len(m.cache))
+ copy(txs, m.cache)
+ return txs
+}
+
+// txList is a "list" of transactions belonging to an account, sorted by account
+// nonce. The same type can be used both for storing contiguous transactions for
+// the executable/pending queue; and for storing gapped transactions for the non-
+// executable/future queue, with minor behavoiral changes.
+type txList struct {
+ strict bool // Whether nonces are strictly continuous or not
+ txs *txSortedMap // Heap indexed sorted hash map of the transactions
+ costcap *big.Int // Price of the highest costing transaction (reset only if exceeds balance)
+}
+
+// newTxList create a new transaction list for maintaining nonce-indexable fast,
+// gapped, sortable transaction lists.
+func newTxList(strict bool) *txList {
+ return &txList{
+ strict: strict,
+ txs: newTxSortedMap(),
+ costcap: new(big.Int),
+ }
+}
+
+// Add tries to insert a new transaction into the list, returning whether the
+// transaction was accepted, and if yes, any previous transaction it replaced.
+//
+// If the new transaction is accepted into the list, the lists' cost threshold
+// is also potentially updated.
+func (l *txList) Add(tx *types.Transaction) (bool, *types.Transaction) {
+ // If there's an older better transaction, abort
+ old := l.txs.Get(tx.Nonce())
+ if old != nil && old.GasPrice().Cmp(tx.GasPrice()) >= 0 {
+ return false, nil
+ }
+ // Otherwise overwrite the old transaction with the current one
+ l.txs.Put(tx)
+ if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 {
+ l.costcap = cost
+ }
+ return true, old
+}
+
+// Forward removes all transactions from the list with a nonce lower than the
+// provided threshold. Every removed transaction is returned for any post-removal
+// maintenance.
+func (l *txList) Forward(threshold uint64) types.Transactions {
+ return l.txs.Forward(threshold)
+}
+
+// Filter removes all transactions from the list with a cost higher than the
+// provided threshold. Every removed transaction is returned for any post-removal
+// maintenance. Strict-mode invalidated transactions are also returned.
+//
+// This method uses the cached costcap to quickly decide if there's even a point
+// in calculating all the costs or if the balance covers all. If the threshold is
+// lower than the costcap, the costcap will be reset to a new high after removing
+// expensive the too transactions.
+func (l *txList) Filter(threshold *big.Int) (types.Transactions, types.Transactions) {
+ // If all transactions are below the threshold, short circuit
+ if l.costcap.Cmp(threshold) <= 0 {
+ return nil, nil
+ }
+ l.costcap = new(big.Int).Set(threshold) // Lower the cap to the threshold
+
+ // Filter out all the transactions above the account's funds
+ removed := l.txs.Filter(func(tx *types.Transaction) bool { return tx.Cost().Cmp(threshold) > 0 })
+
+ // If the list was strict, filter anything above the lowest nonce
+ var invalids types.Transactions
+ if l.strict && len(removed) > 0 {
+ lowest := uint64(math.MaxUint64)
+ for _, tx := range removed {
+ if nonce := tx.Nonce(); lowest > nonce {
+ lowest = nonce
+ }
+ }
+ invalids = l.txs.Filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest })
+ }
+ return removed, invalids
+}
+
+// Cap places a hard limit on the number of items, returning all transactions
+// exceeding that limit.
+func (l *txList) Cap(threshold int) types.Transactions {
+ return l.txs.Cap(threshold)
+}
+
+// Remove deletes a transaction from the maintained list, returning whether the
+// transaction was found, and also returning any transaction invalidated due to
+// the deletion (strict mode only).
+func (l *txList) Remove(tx *types.Transaction) (bool, types.Transactions) {
+ // Remove the transaction from the set
+ nonce := tx.Nonce()
+ if removed := l.txs.Remove(nonce); !removed {
+ return false, nil
+ }
+ // In strict mode, filter out non-executable transactions
+ if l.strict {
+ return true, l.txs.Filter(func(tx *types.Transaction) bool { return tx.Nonce() > nonce })
+ }
+ return true, nil
+}
+
+// Ready retrieves a sequentially increasing list of transactions starting at the
+// provided nonce that is ready for processing. The returned transactions will be
+// removed from the list.
+//
+// Note, all transactions with nonces lower than start will also be returned to
+// prevent getting into and invalid state. This is not something that should ever
+// happen but better to be self correcting than failing!
+func (l *txList) Ready(start uint64) types.Transactions {
+ return l.txs.Ready(start)
+}
+
+// Len returns the length of the transaction list.
+func (l *txList) Len() int {
+ return l.txs.Len()
+}
+
+// Empty returns whether the list of transactions is empty or not.
+func (l *txList) Empty() bool {
+ return l.Len() == 0
+}
+
+// Flatten creates a nonce-sorted slice of transactions based on the loosely
+// sorted internal representation. The result of the sorting is cached in case
+// it's requested again before any modifications are made to the contents.
+func (l *txList) Flatten() types.Transactions {
+ return l.txs.Flatten()
+}
diff --git a/core/tx_list_test.go b/core/tx_list_test.go
new file mode 100644
index 000000000..92b211937
--- /dev/null
+++ b/core/tx_list_test.go
@@ -0,0 +1,52 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package core
+
+import (
+ "math/big"
+ "math/rand"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/crypto"
+)
+
+// Tests that transactions can be added to strict lists and list contents and
+// nonce boundaries are correctly maintained.
+func TestStrictTxListAdd(t *testing.T) {
+ // Generate a list of transactions to insert
+ key, _ := crypto.GenerateKey()
+
+ txs := make(types.Transactions, 1024)
+ for i := 0; i < len(txs); i++ {
+ txs[i] = transaction(uint64(i), new(big.Int), key)
+ }
+ // Insert the transactions in a random order
+ list := newTxList(true)
+ for _, v := range rand.Perm(len(txs)) {
+ list.Add(txs[v])
+ }
+ // Verify internal state
+ if len(list.txs.items) != len(txs) {
+ t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs))
+ }
+ for i, tx := range txs {
+ if list.txs.items[tx.Nonce()] != tx {
+ t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx)
+ }
+ }
+}
diff --git a/core/tx_pool.go b/core/tx_pool.go
index 596356377..f8b11a7ce 100644
--- a/core/tx_pool.go
+++ b/core/tx_pool.go
@@ -45,8 +45,11 @@ var (
ErrNegativeValue = errors.New("Negative value")
)
-const (
- maxQueued = 64 // max limit of queued txs per address
+var (
+ maxQueuedPerAccount = uint64(64) // Max limit of queued transactions per address
+ maxQueuedInTotal = uint64(65536) // Max limit of queued transactions from all accounts
+ maxQueuedLifetime = 3 * time.Hour // Max amount of time transactions from idle accounts are queued
+ evictionInterval = time.Minute // Time interval to check for evictable transactions
)
type stateFn func() (*state.StateDB, error)
@@ -68,10 +71,14 @@ type TxPool struct {
events event.Subscription
localTx *txSet
mu sync.RWMutex
- pending map[common.Hash]*types.Transaction // processable transactions
- queue map[common.Address]map[common.Hash]*types.Transaction
- wg sync.WaitGroup // for shutdown sync
+ pending map[common.Address]*txList // All currently processable transactions
+ queue map[common.Address]*txList // Queued but non-processable transactions
+ all map[common.Hash]*types.Transaction // All transactions to allow lookups
+ beats map[common.Address]time.Time // Last heartbeat from each known account
+
+ wg sync.WaitGroup // for shutdown sync
+ quit chan struct{}
homestead bool
}
@@ -79,8 +86,10 @@ type TxPool struct {
func NewTxPool(config *ChainConfig, eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool {
pool := &TxPool{
config: config,
- pending: make(map[common.Hash]*types.Transaction),
- queue: make(map[common.Address]map[common.Hash]*types.Transaction),
+ pending: make(map[common.Address]*txList),
+ queue: make(map[common.Address]*txList),
+ all: make(map[common.Hash]*types.Transaction),
+ beats: make(map[common.Address]time.Time),
eventMux: eventMux,
currentState: currentStateFn,
gasLimit: gasLimitFn,
@@ -88,10 +97,12 @@ func NewTxPool(config *ChainConfig, eventMux *event.TypeMux, currentStateFn stat
pendingState: nil,
localTx: newTxSet(),
events: eventMux.Subscribe(ChainHeadEvent{}, GasPriceChanged{}, RemovedTransactionEvent{}),
+ quit: make(chan struct{}),
}
- pool.wg.Add(1)
+ pool.wg.Add(2)
go pool.eventLoop()
+ go pool.expirationLoop()
return pool
}
@@ -117,7 +128,7 @@ func (pool *TxPool) eventLoop() {
pool.minGasPrice = ev.Price
pool.mu.Unlock()
case RemovedTransactionEvent:
- pool.AddTransactions(ev.Txs)
+ pool.AddBatch(ev.Txs)
}
}
}
@@ -125,12 +136,12 @@ func (pool *TxPool) eventLoop() {
func (pool *TxPool) resetState() {
currentState, err := pool.currentState()
if err != nil {
- glog.V(logger.Info).Infoln("failed to get current state: %v", err)
+ glog.V(logger.Error).Infof("Failed to get current state: %v", err)
return
}
managedState := state.ManageState(currentState)
if err != nil {
- glog.V(logger.Info).Infoln("failed to get managed state: %v", err)
+ glog.V(logger.Error).Infof("Failed to get managed state: %v", err)
return
}
pool.pendingState = managedState
@@ -139,26 +150,21 @@ func (pool *TxPool) resetState() {
// any transactions that have been included in the block or
// have been invalidated because of another transaction (e.g.
// higher gas price)
- pool.validatePool()
-
- // Loop over the pending transactions and base the nonce of the new
- // pending transaction set.
- for _, tx := range pool.pending {
- if addr, err := tx.From(); err == nil {
- // Set the nonce. Transaction nonce can never be lower
- // than the state nonce; validatePool took care of that.
- if pool.pendingState.GetNonce(addr) <= tx.Nonce() {
- pool.pendingState.SetNonce(addr, tx.Nonce()+1)
- }
- }
+ pool.demoteUnexecutables()
+
+ // Update all accounts to the latest known pending nonce
+ for addr, list := range pool.pending {
+ txs := list.Flatten() // Heavy but will be cached and is needed by the miner anyway
+ pool.pendingState.SetNonce(addr, txs[len(txs)-1].Nonce()+1)
}
// Check the queue and move transactions over to the pending if possible
// or remove those that have become invalid
- pool.checkQueue()
+ pool.promoteExecutables()
}
func (pool *TxPool) Stop() {
pool.events.Unsubscribe()
+ close(pool.quit)
pool.wg.Wait()
glog.V(logger.Info).Infoln("Transaction pool stopped")
}
@@ -170,47 +176,58 @@ func (pool *TxPool) State() *state.ManagedState {
return pool.pendingState
}
+// Stats retrieves the current pool stats, namely the number of pending and the
+// number of queued (non-executable) transactions.
func (pool *TxPool) Stats() (pending int, queued int) {
pool.mu.RLock()
defer pool.mu.RUnlock()
- pending = len(pool.pending)
- for _, txs := range pool.queue {
- queued += len(txs)
+ for _, list := range pool.pending {
+ pending += list.Len()
+ }
+ for _, list := range pool.queue {
+ queued += list.Len()
}
return
}
// Content retrieves the data content of the transaction pool, returning all the
-// pending as well as queued transactions, grouped by account and nonce.
-func (pool *TxPool) Content() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) {
+// pending as well as queued transactions, grouped by account and sorted by nonce.
+func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
pool.mu.RLock()
defer pool.mu.RUnlock()
- // Retrieve all the pending transactions and sort by account and by nonce
- pending := make(map[common.Address]map[uint64][]*types.Transaction)
- for _, tx := range pool.pending {
- account, _ := tx.From()
-
- owned, ok := pending[account]
- if !ok {
- owned = make(map[uint64][]*types.Transaction)
- pending[account] = owned
- }
- owned[tx.Nonce()] = append(owned[tx.Nonce()], tx)
- }
- // Retrieve all the queued transactions and sort by account and by nonce
- queued := make(map[common.Address]map[uint64][]*types.Transaction)
- for account, txs := range pool.queue {
- owned := make(map[uint64][]*types.Transaction)
- for _, tx := range txs {
- owned[tx.Nonce()] = append(owned[tx.Nonce()], tx)
- }
- queued[account] = owned
+ pending := make(map[common.Address]types.Transactions)
+ for addr, list := range pool.pending {
+ pending[addr] = list.Flatten()
+ }
+ queued := make(map[common.Address]types.Transactions)
+ for addr, list := range pool.queue {
+ queued[addr] = list.Flatten()
}
return pending, queued
}
+// Pending retrieves all currently processable transactions, groupped by origin
+// account and sorted by nonce. The returned transaction set is a copy and can be
+// freely modified by calling code.
+func (pool *TxPool) Pending() map[common.Address]types.Transactions {
+ pool.mu.Lock()
+ defer pool.mu.Unlock()
+
+ // check queue first
+ pool.promoteExecutables()
+
+ // invalidate any txs
+ pool.demoteUnexecutables()
+
+ pending := make(map[common.Address]types.Transactions)
+ for addr, list := range pool.pending {
+ pending[addr] = list.Flatten()
+ }
+ return pending
+}
+
// SetLocal marks a transaction as local, skipping gas price
// check against local miner minimum in the future
func (pool *TxPool) SetLocal(tx *types.Transaction) {
@@ -276,312 +293,348 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
return nil
}
-// validate and queue transactions.
-func (self *TxPool) add(tx *types.Transaction) error {
+// add validates a transaction and inserts it into the non-executable queue for
+// later pending promotion and execution.
+func (pool *TxPool) add(tx *types.Transaction) error {
+ // If the transaction is alreayd known, discard it
hash := tx.Hash()
-
- if self.pending[hash] != nil {
- return fmt.Errorf("Known transaction (%x)", hash[:4])
+ if pool.all[hash] != nil {
+ return fmt.Errorf("Known transaction: %x", hash[:4])
}
- err := self.validateTx(tx)
- if err != nil {
+ // Otherwise ensure basic validation passes and queue it up
+ if err := pool.validateTx(tx); err != nil {
return err
}
- self.queueTx(hash, tx)
+ pool.enqueueTx(hash, tx)
+ // Print a log message if low enough level is set
if glog.V(logger.Debug) {
- var toname string
+ rcpt := "[NEW_CONTRACT]"
if to := tx.To(); to != nil {
- toname = common.Bytes2Hex(to[:4])
- } else {
- toname = "[NEW_CONTRACT]"
+ rcpt = common.Bytes2Hex(to[:4])
}
- // we can ignore the error here because From is
- // verified in ValidateTransaction.
- f, _ := tx.From()
- from := common.Bytes2Hex(f[:4])
- glog.Infof("(t) %x => %s (%v) %x\n", from, toname, tx.Value, hash)
+ from, _ := tx.From() // from already verified during tx validation
+ glog.Infof("(t) 0x%x => %s (%v) %x\n", from[:4], rcpt, tx.Value, hash)
}
-
return nil
}
-// queueTx will queue an unknown transaction
-func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) {
+// enqueueTx inserts a new transaction into the non-executable transaction queue.
+//
+// Note, this method assumes the pool lock is held!
+func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction) {
+ // Try to insert the transaction into the future queue
from, _ := tx.From() // already validated
- if self.queue[from] == nil {
- self.queue[from] = make(map[common.Hash]*types.Transaction)
+ if pool.queue[from] == nil {
+ pool.queue[from] = newTxList(false)
+ }
+ inserted, old := pool.queue[from].Add(tx)
+ if !inserted {
+ return // An older transaction was better, discard this
+ }
+ // Discard any previous transaction and mark this
+ if old != nil {
+ delete(pool.all, old.Hash())
}
- self.queue[from][hash] = tx
+ pool.all[hash] = tx
}
-// addTx will add a transaction to the pending (processable queue) list of transactions
-func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Transaction) {
- // init delayed since tx pool could have been started before any state sync
+// promoteTx adds a transaction to the pending (processable) list of transactions.
+//
+// Note, this method assumes the pool lock is held!
+func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) {
+ // Init delayed since tx pool could have been started before any state sync
if pool.pendingState == nil {
pool.resetState()
}
+ // Try to insert the transaction into the pending queue
+ if pool.pending[addr] == nil {
+ pool.pending[addr] = newTxList(true)
+ }
+ list := pool.pending[addr]
- if _, ok := pool.pending[hash]; !ok {
- pool.pending[hash] = tx
-
- // Increment the nonce on the pending state. This can only happen if
- // the nonce is +1 to the previous one.
- pool.pendingState.SetNonce(addr, tx.Nonce()+1)
- // Notify the subscribers. This event is posted in a goroutine
- // because it's possible that somewhere during the post "Remove transaction"
- // gets called which will then wait for the global tx pool lock and deadlock.
- go pool.eventMux.Post(TxPreEvent{tx})
+ inserted, old := list.Add(tx)
+ if !inserted {
+ // An older transaction was better, discard this
+ delete(pool.all, hash)
+ return
+ }
+ // Otherwise discard any previous transaction and mark this
+ if old != nil {
+ delete(pool.all, old.Hash())
}
+ pool.all[hash] = tx // Failsafe to work around direct pending inserts (tests)
+
+ // Set the potentially new pending nonce and notify any subsystems of the new tx
+ pool.beats[addr] = time.Now()
+ pool.pendingState.SetNonce(addr, tx.Nonce()+1)
+ go pool.eventMux.Post(TxPreEvent{tx})
}
// Add queues a single transaction in the pool if it is valid.
-func (self *TxPool) Add(tx *types.Transaction) error {
- self.mu.Lock()
- defer self.mu.Unlock()
+func (pool *TxPool) Add(tx *types.Transaction) error {
+ pool.mu.Lock()
+ defer pool.mu.Unlock()
- if err := self.add(tx); err != nil {
+ if err := pool.add(tx); err != nil {
return err
}
- self.checkQueue()
+ pool.promoteExecutables()
+
return nil
}
-// AddTransactions attempts to queue all valid transactions in txs.
-func (self *TxPool) AddTransactions(txs []*types.Transaction) {
- self.mu.Lock()
- defer self.mu.Unlock()
+// AddBatch attempts to queue a batch of transactions.
+func (pool *TxPool) AddBatch(txs []*types.Transaction) {
+ pool.mu.Lock()
+ defer pool.mu.Unlock()
for _, tx := range txs {
- if err := self.add(tx); err != nil {
+ if err := pool.add(tx); err != nil {
glog.V(logger.Debug).Infoln("tx error:", err)
- } else {
- h := tx.Hash()
- glog.V(logger.Debug).Infof("tx %x\n", h[:4])
}
}
-
- // check and validate the queue
- self.checkQueue()
+ pool.promoteExecutables()
}
-// GetTransaction returns a transaction if it is contained in the pool
+// Get returns a transaction if it is contained in the pool
// and nil otherwise.
-func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
- tp.mu.RLock()
- defer tp.mu.RUnlock()
-
- // check the txs first
- if tx, ok := tp.pending[hash]; ok {
- return tx
- }
- // check queue
- for _, txs := range tp.queue {
- if tx, ok := txs[hash]; ok {
- return tx
- }
- }
- return nil
-}
+func (pool *TxPool) Get(hash common.Hash) *types.Transaction {
+ pool.mu.RLock()
+ defer pool.mu.RUnlock()
-// GetTransactions returns all currently processable transactions.
-// The returned slice may be modified by the caller.
-func (self *TxPool) GetTransactions() (txs types.Transactions) {
- self.mu.Lock()
- defer self.mu.Unlock()
+ return pool.all[hash]
+}
- // check queue first
- self.checkQueue()
- // invalidate any txs
- self.validatePool()
+// Remove removes the transaction with the given hash from the pool.
+func (pool *TxPool) Remove(hash common.Hash) {
+ pool.mu.Lock()
+ defer pool.mu.Unlock()
- txs = make(types.Transactions, len(self.pending))
- i := 0
- for _, tx := range self.pending {
- txs[i] = tx
- i++
- }
- return txs
+ pool.removeTx(hash)
}
-// GetQueuedTransactions returns all non-processable transactions.
-func (self *TxPool) GetQueuedTransactions() types.Transactions {
- self.mu.RLock()
- defer self.mu.RUnlock()
+// RemoveBatch removes all given transactions from the pool.
+func (pool *TxPool) RemoveBatch(txs types.Transactions) {
+ pool.mu.Lock()
+ defer pool.mu.Unlock()
- var ret types.Transactions
- for _, txs := range self.queue {
- for _, tx := range txs {
- ret = append(ret, tx)
- }
+ for _, tx := range txs {
+ pool.removeTx(tx.Hash())
}
- sort.Sort(types.TxByNonce(ret))
- return ret
}
-// RemoveTransactions removes all given transactions from the pool.
-func (self *TxPool) RemoveTransactions(txs types.Transactions) {
- self.mu.Lock()
- defer self.mu.Unlock()
- for _, tx := range txs {
- self.removeTx(tx.Hash())
+// removeTx removes a single transaction from the queue, moving all subsequent
+// transactions back to the future queue.
+func (pool *TxPool) removeTx(hash common.Hash) {
+ // Fetch the transaction we wish to delete
+ tx, ok := pool.all[hash]
+ if !ok {
+ return
}
-}
+ addr, _ := tx.From() // already validated during insertion
-// RemoveTx removes the transaction with the given hash from the pool.
-func (pool *TxPool) RemoveTx(hash common.Hash) {
- pool.mu.Lock()
- defer pool.mu.Unlock()
- pool.removeTx(hash)
-}
+ // Remove it from the list of known transactions
+ delete(pool.all, hash)
-func (pool *TxPool) removeTx(hash common.Hash) {
- // delete from pending pool
- delete(pool.pending, hash)
- // delete from queue
- for address, txs := range pool.queue {
- if _, ok := txs[hash]; ok {
- if len(txs) == 1 {
- // if only one tx, remove entire address entry.
- delete(pool.queue, address)
+ // Remove the transaction from the pending lists and reset the account nonce
+ if pending := pool.pending[addr]; pending != nil {
+ if removed, invalids := pending.Remove(tx); removed {
+ // If no more transactions are left, remove the list
+ if pending.Empty() {
+ delete(pool.pending, addr)
+ delete(pool.beats, addr)
} else {
- delete(txs, hash)
+ // Otherwise postpone any invalidated transactions
+ for _, tx := range invalids {
+ pool.enqueueTx(tx.Hash(), tx)
+ }
+ }
+ // Update the account nonce if needed
+ if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce {
+ pool.pendingState.SetNonce(addr, tx.Nonce())
}
- break
+ }
+ }
+ // Transaction is in the future queue
+ if future := pool.queue[addr]; future != nil {
+ future.Remove(tx)
+ if future.Empty() {
+ delete(pool.queue, addr)
}
}
}
-// checkQueue moves transactions that have become processable to main pool.
-func (pool *TxPool) checkQueue() {
- // init delayed since tx pool could have been started before any state sync
+// promoteExecutables moves transactions that have become processable from the
+// future queue to the set of pending transactions. During this process, all
+// invalidated transactions (low nonce, low balance) are deleted.
+func (pool *TxPool) promoteExecutables() {
+ // Init delayed since tx pool could have been started before any state sync
if pool.pendingState == nil {
pool.resetState()
}
+ // Retrieve the current state to allow nonce and balance checking
+ state, err := pool.currentState()
+ if err != nil {
+ glog.Errorf("Could not get current state: %v", err)
+ return
+ }
+ // Iterate over all accounts and promote any executable transactions
+ queued := uint64(0)
- var promote txQueue
- for address, txs := range pool.queue {
- currentState, err := pool.currentState()
- if err != nil {
- glog.Errorf("could not get current state: %v", err)
- return
+ for addr, list := range pool.queue {
+ // Drop all transactions that are deemed too old (low nonce)
+ for _, tx := range list.Forward(state.GetNonce(addr)) {
+ if glog.V(logger.Core) {
+ glog.Infof("Removed old queued transaction: %v", tx)
+ }
+ delete(pool.all, tx.Hash())
}
- balance := currentState.GetBalance(address)
-
- var (
- guessedNonce = pool.pendingState.GetNonce(address) // nonce currently kept by the tx pool (pending state)
- trueNonce = currentState.GetNonce(address) // nonce known by the last state
- )
- promote = promote[:0]
- for hash, tx := range txs {
- // Drop processed or out of fund transactions
- if tx.Nonce() < trueNonce || balance.Cmp(tx.Cost()) < 0 {
- if glog.V(logger.Core) {
- glog.Infof("removed tx (%v) from pool queue: low tx nonce or out of funds\n", tx)
- }
- delete(txs, hash)
- continue
+ // Drop all transactions that are too costly (low balance)
+ drops, _ := list.Filter(state.GetBalance(addr))
+ for _, tx := range drops {
+ if glog.V(logger.Core) {
+ glog.Infof("Removed unpayable queued transaction: %v", tx)
}
- // Collect the remaining transactions for the next pass.
- promote = append(promote, txQueueEntry{hash, address, tx})
+ delete(pool.all, tx.Hash())
}
- // Find the next consecutive nonce range starting at the current account nonce,
- // pushing the guessed nonce forward if we add consecutive transactions.
- sort.Sort(promote)
- for i, entry := range promote {
- // If we reached a gap in the nonces, enforce transaction limit and stop
- if entry.Nonce() > guessedNonce {
- if len(promote)-i > maxQueued {
- if glog.V(logger.Debug) {
- glog.Infof("Queued tx limit exceeded for %s. Tx %s removed\n", common.PP(address[:]), common.PP(entry.hash[:]))
- }
- for _, drop := range promote[i+maxQueued:] {
- delete(txs, drop.hash)
- }
- }
- break
+ // Gather all executable transactions and promote them
+ for _, tx := range list.Ready(pool.pendingState.GetNonce(addr)) {
+ if glog.V(logger.Core) {
+ glog.Infof("Promoting queued transaction: %v", tx)
}
- // Otherwise promote the transaction and move the guess nonce if needed
- pool.addTx(entry.hash, address, entry.Transaction)
- delete(txs, entry.hash)
-
- if entry.Nonce() == guessedNonce {
- guessedNonce++
+ pool.promoteTx(addr, tx.Hash(), tx)
+ }
+ // Drop all transactions over the allowed limit
+ for _, tx := range list.Cap(int(maxQueuedPerAccount)) {
+ if glog.V(logger.Core) {
+ glog.Infof("Removed cap-exceeding queued transaction: %v", tx)
}
+ delete(pool.all, tx.Hash())
}
+ queued += uint64(list.Len())
+
// Delete the entire queue entry if it became empty.
- if len(txs) == 0 {
- delete(pool.queue, address)
+ if list.Empty() {
+ delete(pool.queue, addr)
+ }
+ }
+ // If we've queued more transactions than the hard limit, drop oldest ones
+ if queued > maxQueuedInTotal {
+ // Sort all accounts with queued transactions by heartbeat
+ addresses := make(addresssByHeartbeat, 0, len(pool.queue))
+ for addr, _ := range pool.queue {
+ addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
+ }
+ sort.Sort(addresses)
+
+ // Drop transactions until the total is below the limit
+ for drop := queued - maxQueuedInTotal; drop > 0; {
+ addr := addresses[len(addresses)-1]
+ list := pool.queue[addr.address]
+
+ addresses = addresses[:len(addresses)-1]
+
+ // Drop all transactions if they are less than the overflow
+ if size := uint64(list.Len()); size <= drop {
+ for _, tx := range list.Flatten() {
+ pool.removeTx(tx.Hash())
+ }
+ drop -= size
+ continue
+ }
+ // Otherwise drop only last few transactions
+ txs := list.Flatten()
+ for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
+ pool.removeTx(txs[i].Hash())
+ drop--
+ }
}
}
}
-// validatePool removes invalid and processed transactions from the main pool.
-// If a transaction is removed for being invalid (e.g. out of funds), all sub-
-// sequent (Still valid) transactions are moved back into the future queue. This
-// is important to prevent a drained account from DOSing the network with non
-// executable transactions.
-func (pool *TxPool) validatePool() {
+// demoteUnexecutables removes invalid and processed transactions from the pools
+// executable/pending queue and any subsequent transactions that become unexecutable
+// are moved back into the future queue.
+func (pool *TxPool) demoteUnexecutables() {
+ // Retrieve the current state to allow nonce and balance checking
state, err := pool.currentState()
if err != nil {
glog.V(logger.Info).Infoln("failed to get current state: %v", err)
return
}
- balanceCache := make(map[common.Address]*big.Int)
-
- // Clean up the pending pool, accumulating invalid nonces
- gaps := make(map[common.Address]uint64)
+ // Iterate over all accounts and demote any non-executable transactions
+ for addr, list := range pool.pending {
+ nonce := state.GetNonce(addr)
- for hash, tx := range pool.pending {
- sender, _ := tx.From() // err already checked
-
- // Perform light nonce and balance validation
- balance := balanceCache[sender]
- if balance == nil {
- balance = state.GetBalance(sender)
- balanceCache[sender] = balance
+ // Drop all transactions that are deemed too old (low nonce)
+ for _, tx := range list.Forward(nonce) {
+ if glog.V(logger.Core) {
+ glog.Infof("Removed old pending transaction: %v", tx)
+ }
+ delete(pool.all, tx.Hash())
}
- if past := state.GetNonce(sender) > tx.Nonce(); past || balance.Cmp(tx.Cost()) < 0 {
- // Remove an already past it invalidated transaction
+ // Drop all transactions that are too costly (low balance), and queue any invalids back for later
+ drops, invalids := list.Filter(state.GetBalance(addr))
+ for _, tx := range drops {
if glog.V(logger.Core) {
- glog.Infof("removed tx (%v) from pool: low tx nonce or out of funds\n", tx)
+ glog.Infof("Removed unpayable pending transaction: %v", tx)
}
- delete(pool.pending, hash)
-
- // Track the smallest invalid nonce to postpone subsequent transactions
- if !past {
- if prev, ok := gaps[sender]; !ok || tx.Nonce() < prev {
- gaps[sender] = tx.Nonce()
- }
+ delete(pool.all, tx.Hash())
+ }
+ for _, tx := range invalids {
+ if glog.V(logger.Core) {
+ glog.Infof("Demoting pending transaction: %v", tx)
}
+ pool.enqueueTx(tx.Hash(), tx)
+ }
+ // Delete the entire queue entry if it became empty.
+ if list.Empty() {
+ delete(pool.pending, addr)
+ delete(pool.beats, addr)
}
}
- // Move all transactions after a gap back to the future queue
- if len(gaps) > 0 {
- for hash, tx := range pool.pending {
- sender, _ := tx.From()
- if gap, ok := gaps[sender]; ok && tx.Nonce() >= gap {
- if glog.V(logger.Core) {
- glog.Infof("postponed tx (%v) due to introduced gap\n", tx)
+}
+
+// expirationLoop is a loop that periodically iterates over all accounts with
+// queued transactions and drop all that have been inactive for a prolonged amount
+// of time.
+func (pool *TxPool) expirationLoop() {
+ defer pool.wg.Done()
+
+ evict := time.NewTicker(evictionInterval)
+ defer evict.Stop()
+
+ for {
+ select {
+ case <-evict.C:
+ pool.mu.Lock()
+ for addr := range pool.queue {
+ if time.Since(pool.beats[addr]) > maxQueuedLifetime {
+ for _, tx := range pool.queue[addr].Flatten() {
+ pool.removeTx(tx.Hash())
+ }
}
- pool.queueTx(hash, tx)
- delete(pool.pending, hash)
}
+ pool.mu.Unlock()
+
+ case <-pool.quit:
+ return
}
}
}
-type txQueue []txQueueEntry
-
-type txQueueEntry struct {
- hash common.Hash
- addr common.Address
- *types.Transaction
+// addressByHeartbeat is an account address tagged with its last activity timestamp.
+type addressByHeartbeat struct {
+ address common.Address
+ heartbeat time.Time
}
-func (q txQueue) Len() int { return len(q) }
-func (q txQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
-func (q txQueue) Less(i, j int) bool { return q[i].Nonce() < q[j].Nonce() }
+type addresssByHeartbeat []addressByHeartbeat
+
+func (a addresssByHeartbeat) Len() int { return len(a) }
+func (a addresssByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) }
+func (a addresssByHeartbeat) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// txSet represents a set of transaction hashes in which entries
// are automatically dropped after txSetDuration time
diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go
index ed9bea75f..4bc5aed38 100644
--- a/core/tx_pool_test.go
+++ b/core/tx_pool_test.go
@@ -19,7 +19,9 @@ package core
import (
"crypto/ecdsa"
"math/big"
+ "math/rand"
"testing"
+ "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
@@ -38,10 +40,10 @@ func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
db, _ := ethdb.NewMemDatabase()
statedb, _ := state.New(common.Hash{}, db)
- var m event.TypeMux
key, _ := crypto.GenerateKey()
- newPool := NewTxPool(testChainConfig(), &m, func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
+ newPool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
newPool.resetState()
+
return newPool, key
}
@@ -91,9 +93,9 @@ func TestTransactionQueue(t *testing.T) {
from, _ := tx.From()
currentState, _ := pool.currentState()
currentState.AddBalance(from, big.NewInt(1000))
- pool.queueTx(tx.Hash(), tx)
+ pool.enqueueTx(tx.Hash(), tx)
- pool.checkQueue()
+ pool.promoteExecutables()
if len(pool.pending) != 1 {
t.Error("expected valid txs to be 1 is", len(pool.pending))
}
@@ -101,14 +103,14 @@ func TestTransactionQueue(t *testing.T) {
tx = transaction(1, big.NewInt(100), key)
from, _ = tx.From()
currentState.SetNonce(from, 2)
- pool.queueTx(tx.Hash(), tx)
- pool.checkQueue()
- if _, ok := pool.pending[tx.Hash()]; ok {
+ pool.enqueueTx(tx.Hash(), tx)
+ pool.promoteExecutables()
+ if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok {
t.Error("expected transaction to be in tx pool")
}
- if len(pool.queue[from]) > 0 {
- t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
+ if len(pool.queue) > 0 {
+ t.Error("expected transaction queue to be empty. is", len(pool.queue))
}
pool, key = setupTxPool()
@@ -118,17 +120,17 @@ func TestTransactionQueue(t *testing.T) {
from, _ = tx1.From()
currentState, _ = pool.currentState()
currentState.AddBalance(from, big.NewInt(1000))
- pool.queueTx(tx1.Hash(), tx1)
- pool.queueTx(tx2.Hash(), tx2)
- pool.queueTx(tx3.Hash(), tx3)
+ pool.enqueueTx(tx1.Hash(), tx1)
+ pool.enqueueTx(tx2.Hash(), tx2)
+ pool.enqueueTx(tx3.Hash(), tx3)
- pool.checkQueue()
+ pool.promoteExecutables()
if len(pool.pending) != 1 {
t.Error("expected tx pool to be 1, got", len(pool.pending))
}
- if len(pool.queue[from]) != 2 {
- t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
+ if pool.queue[from].Len() != 2 {
+ t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
}
}
@@ -138,24 +140,21 @@ func TestRemoveTx(t *testing.T) {
from, _ := tx.From()
currentState, _ := pool.currentState()
currentState.AddBalance(from, big.NewInt(1))
- pool.queueTx(tx.Hash(), tx)
- pool.addTx(tx.Hash(), from, tx)
+
+ pool.enqueueTx(tx.Hash(), tx)
+ pool.promoteTx(from, tx.Hash(), tx)
if len(pool.queue) != 1 {
t.Error("expected queue to be 1, got", len(pool.queue))
}
-
if len(pool.pending) != 1 {
- t.Error("expected txs to be 1, got", len(pool.pending))
+ t.Error("expected pending to be 1, got", len(pool.pending))
}
-
- pool.RemoveTx(tx.Hash())
-
+ pool.Remove(tx.Hash())
if len(pool.queue) > 0 {
t.Error("expected queue to be 0, got", len(pool.queue))
}
-
if len(pool.pending) > 0 {
- t.Error("expected txs to be 0, got", len(pool.pending))
+ t.Error("expected pending to be 0, got", len(pool.pending))
}
}
@@ -188,7 +187,7 @@ func TestTransactionChainFork(t *testing.T) {
if err := pool.add(tx); err != nil {
t.Error("didn't expect error", err)
}
- pool.RemoveTransactions([]*types.Transaction{tx})
+ pool.RemoveBatch([]*types.Transaction{tx})
// reset the pool's internal state
resetState()
@@ -210,18 +209,38 @@ func TestTransactionDoubleNonce(t *testing.T) {
}
resetState()
- tx := transaction(0, big.NewInt(100000), key)
- tx2 := transaction(0, big.NewInt(1000000), key)
- if err := pool.add(tx); err != nil {
+ tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(key)
+ tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(key)
+ tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(key)
+
+ // Add the first two transaction, ensure higher priced stays only
+ if err := pool.add(tx1); err != nil {
t.Error("didn't expect error", err)
}
if err := pool.add(tx2); err != nil {
t.Error("didn't expect error", err)
}
-
- pool.checkQueue()
- if len(pool.pending) != 2 {
- t.Error("expected 2 pending txs. Got", len(pool.pending))
+ pool.promoteExecutables()
+ if pool.pending[addr].Len() != 1 {
+ t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
+ }
+ if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
+ t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
+ }
+ // Add the thid transaction and ensure it's not saved (smaller price)
+ if err := pool.add(tx3); err != nil {
+ t.Error("didn't expect error", err)
+ }
+ pool.promoteExecutables()
+ if pool.pending[addr].Len() != 1 {
+ t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
+ }
+ if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
+ t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
+ }
+ // Ensure the total transaction count is correct
+ if len(pool.all) != 1 {
+ t.Error("expected 1 total transactions, got", len(pool.all))
}
}
@@ -237,8 +256,11 @@ func TestMissingNonce(t *testing.T) {
if len(pool.pending) != 0 {
t.Error("expected 0 pending transactions, got", len(pool.pending))
}
- if len(pool.queue[addr]) != 1 {
- t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
+ if pool.queue[addr].Len() != 1 {
+ t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
+ }
+ if len(pool.all) != 1 {
+ t.Error("expected 1 total transactions, got", len(pool.all))
}
}
@@ -270,8 +292,11 @@ func TestRemovedTxEvent(t *testing.T) {
currentState.AddBalance(from, big.NewInt(1000000000000))
pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
pool.eventMux.Post(ChainHeadEvent{nil})
- if len(pool.pending) != 1 {
- t.Error("expected 1 pending tx, got", len(pool.pending))
+ if pool.pending[from].Len() != 1 {
+ t.Error("expected 1 pending tx, got", pool.pending[from].Len())
+ }
+ if len(pool.all) != 1 {
+ t.Error("expected 1 total transactions, got", len(pool.all))
}
}
@@ -292,41 +317,50 @@ func TestTransactionDropping(t *testing.T) {
tx10 = transaction(10, big.NewInt(100), key)
tx11 = transaction(11, big.NewInt(200), key)
)
- pool.addTx(tx0.Hash(), account, tx0)
- pool.addTx(tx1.Hash(), account, tx1)
- pool.queueTx(tx10.Hash(), tx10)
- pool.queueTx(tx11.Hash(), tx11)
+ pool.promoteTx(account, tx0.Hash(), tx0)
+ pool.promoteTx(account, tx1.Hash(), tx1)
+ pool.enqueueTx(tx10.Hash(), tx10)
+ pool.enqueueTx(tx11.Hash(), tx11)
// Check that pre and post validations leave the pool as is
- if len(pool.pending) != 2 {
- t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2)
+ if pool.pending[account].Len() != 2 {
+ t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
}
- if len(pool.queue[account]) != 2 {
- t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2)
+ if pool.queue[account].Len() != 2 {
+ t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
+ }
+ if len(pool.all) != 4 {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
}
pool.resetState()
- if len(pool.pending) != 2 {
- t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2)
+ if pool.pending[account].Len() != 2 {
+ t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
+ }
+ if pool.queue[account].Len() != 2 {
+ t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
}
- if len(pool.queue[account]) != 2 {
- t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2)
+ if len(pool.all) != 4 {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
}
// Reduce the balance of the account, and check that invalidated transactions are dropped
state.AddBalance(account, big.NewInt(-750))
pool.resetState()
- if _, ok := pool.pending[tx0.Hash()]; !ok {
+ if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
t.Errorf("funded pending transaction missing: %v", tx0)
}
- if _, ok := pool.pending[tx1.Hash()]; ok {
+ if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; ok {
t.Errorf("out-of-fund pending transaction present: %v", tx1)
}
- if _, ok := pool.queue[account][tx10.Hash()]; !ok {
+ if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
t.Errorf("funded queued transaction missing: %v", tx10)
}
- if _, ok := pool.queue[account][tx11.Hash()]; ok {
+ if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; ok {
t.Errorf("out-of-fund queued transaction present: %v", tx11)
}
+ if len(pool.all) != 2 {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
+ }
}
// Tests that if a transaction is dropped from the current pending pool (e.g. out
@@ -349,55 +383,64 @@ func TestTransactionPostponing(t *testing.T) {
} else {
tx = transaction(uint64(i), big.NewInt(500), key)
}
- pool.addTx(tx.Hash(), account, tx)
+ pool.promoteTx(account, tx.Hash(), tx)
txns = append(txns, tx)
}
// Check that pre and post validations leave the pool as is
- if len(pool.pending) != len(txns) {
- t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns))
+ if pool.pending[account].Len() != len(txns) {
+ t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
+ }
+ if len(pool.queue) != 0 {
+ t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
}
- if len(pool.queue[account]) != 0 {
- t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0)
+ if len(pool.all) != len(txns) {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
}
pool.resetState()
- if len(pool.pending) != len(txns) {
- t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns))
+ if pool.pending[account].Len() != len(txns) {
+ t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
+ }
+ if len(pool.queue) != 0 {
+ t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
}
- if len(pool.queue[account]) != 0 {
- t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0)
+ if len(pool.all) != len(txns) {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
}
// Reduce the balance of the account, and check that transactions are reorganised
state.AddBalance(account, big.NewInt(-750))
pool.resetState()
- if _, ok := pool.pending[txns[0].Hash()]; !ok {
+ if _, ok := pool.pending[account].txs.items[txns[0].Nonce()]; !ok {
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
}
- if _, ok := pool.queue[account][txns[0].Hash()]; ok {
+ if _, ok := pool.queue[account].txs.items[txns[0].Nonce()]; ok {
t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
}
for i, tx := range txns[1:] {
if i%2 == 1 {
- if _, ok := pool.pending[tx.Hash()]; ok {
+ if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
}
- if _, ok := pool.queue[account][tx.Hash()]; !ok {
+ if _, ok := pool.queue[account].txs.items[tx.Nonce()]; !ok {
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
}
} else {
- if _, ok := pool.pending[tx.Hash()]; ok {
+ if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
}
- if _, ok := pool.queue[account][tx.Hash()]; ok {
+ if _, ok := pool.queue[account].txs.items[tx.Nonce()]; ok {
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
}
}
}
+ if len(pool.all) != len(txns)/2 {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
+ }
}
// Tests that if the transaction count belonging to a single account goes above
// some threshold, the higher transactions are dropped to prevent DOS attacks.
-func TestTransactionQueueLimiting(t *testing.T) {
+func TestTransactionQueueAccountLimiting(t *testing.T) {
// Create a test account and fund it
pool, key := setupTxPool()
account, _ := transaction(0, big.NewInt(0), key).From()
@@ -406,23 +449,104 @@ func TestTransactionQueueLimiting(t *testing.T) {
state.AddBalance(account, big.NewInt(1000000))
// Keep queuing up transactions and make sure all above a limit are dropped
- for i := uint64(1); i <= maxQueued+5; i++ {
+ for i := uint64(1); i <= maxQueuedPerAccount+5; i++ {
if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
}
if len(pool.pending) != 0 {
t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0)
}
- if i <= maxQueued {
- if len(pool.queue[account]) != int(i) {
- t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), i)
+ if i <= maxQueuedPerAccount {
+ if pool.queue[account].Len() != int(i) {
+ t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i)
}
} else {
- if len(pool.queue[account]) != maxQueued {
- t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, len(pool.queue[account]), maxQueued)
+ if pool.queue[account].Len() != int(maxQueuedPerAccount) {
+ t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), maxQueuedPerAccount)
}
}
}
+ if len(pool.all) != int(maxQueuedPerAccount) {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueuedPerAccount)
+ }
+}
+
+// Tests that if the transaction count belonging to multiple accounts go above
+// some threshold, the higher transactions are dropped to prevent DOS attacks.
+func TestTransactionQueueGlobalLimiting(t *testing.T) {
+ // Reduce the queue limits to shorten test time
+ defer func(old uint64) { maxQueuedInTotal = old }(maxQueuedInTotal)
+ maxQueuedInTotal = maxQueuedPerAccount * 3
+
+ // Create the pool to test the limit enforcement with
+ db, _ := ethdb.NewMemDatabase()
+ statedb, _ := state.New(common.Hash{}, db)
+
+ pool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
+ pool.resetState()
+
+ // Create a number of test accounts and fund them
+ state, _ := pool.currentState()
+
+ keys := make([]*ecdsa.PrivateKey, 5)
+ for i := 0; i < len(keys); i++ {
+ keys[i], _ = crypto.GenerateKey()
+ state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
+ }
+ // Generate and queue a batch of transactions
+ nonces := make(map[common.Address]uint64)
+
+ txs := make(types.Transactions, 0, 3*maxQueuedInTotal)
+ for len(txs) < cap(txs) {
+ key := keys[rand.Intn(len(keys))]
+ addr := crypto.PubkeyToAddress(key.PublicKey)
+
+ txs = append(txs, transaction(nonces[addr]+1, big.NewInt(100000), key))
+ nonces[addr]++
+ }
+ // Import the batch and verify that limits have been enforced
+ pool.AddBatch(txs)
+
+ queued := 0
+ for addr, list := range pool.queue {
+ if list.Len() > int(maxQueuedPerAccount) {
+ t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), maxQueuedPerAccount)
+ }
+ queued += list.Len()
+ }
+ if queued > int(maxQueuedInTotal) {
+ t.Fatalf("total transactions overflow allowance: %d > %d", queued, maxQueuedInTotal)
+ }
+}
+
+// Tests that if an account remains idle for a prolonged amount of time, any
+// non-executable transactions queued up are dropped to prevent wasting resources
+// on shuffling them around.
+func TestTransactionQueueTimeLimiting(t *testing.T) {
+ // Reduce the queue limits to shorten test time
+ defer func(old time.Duration) { maxQueuedLifetime = old }(maxQueuedLifetime)
+ defer func(old time.Duration) { evictionInterval = old }(evictionInterval)
+ maxQueuedLifetime = time.Second
+ evictionInterval = time.Second
+
+ // Create a test account and fund it
+ pool, key := setupTxPool()
+ account, _ := transaction(0, big.NewInt(0), key).From()
+
+ state, _ := pool.currentState()
+ state.AddBalance(account, big.NewInt(1000000))
+
+ // Queue up a batch of transactions
+ for i := uint64(1); i <= maxQueuedPerAccount; i++ {
+ if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
+ t.Fatalf("tx %d: failed to add transaction: %v", i, err)
+ }
+ }
+ // Wait until at least two expiration cycles hit and make sure the transactions are gone
+ time.Sleep(2 * evictionInterval)
+ if len(pool.queue) > 0 {
+ t.Fatalf("old transactions remained after eviction")
+ }
}
// Tests that even if the transaction count belonging to a single account goes
@@ -437,17 +561,20 @@ func TestTransactionPendingLimiting(t *testing.T) {
state.AddBalance(account, big.NewInt(1000000))
// Keep queuing up transactions and make sure all above a limit are dropped
- for i := uint64(0); i < maxQueued+5; i++ {
+ for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
}
- if len(pool.pending) != int(i)+1 {
- t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), i+1)
+ if pool.pending[account].Len() != int(i)+1 {
+ t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1)
}
- if len(pool.queue[account]) != 0 {
- t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), 0)
+ if len(pool.queue) != 0 {
+ t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0)
}
}
+ if len(pool.all) != int(maxQueuedPerAccount+5) {
+ t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueuedPerAccount+5)
+ }
}
// Tests that the transaction limits are enforced the same way irrelevant whether
@@ -462,39 +589,42 @@ func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
state1, _ := pool1.currentState()
state1.AddBalance(account1, big.NewInt(1000000))
- for i := uint64(0); i < maxQueued+5; i++ {
+ for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
if err := pool1.Add(transaction(origin+i, big.NewInt(100000), key1)); err != nil {
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
}
}
- // Add a batch of transactions to a pool in one bit batch
+ // Add a batch of transactions to a pool in one big batch
pool2, key2 := setupTxPool()
account2, _ := transaction(0, big.NewInt(0), key2).From()
state2, _ := pool2.currentState()
state2.AddBalance(account2, big.NewInt(1000000))
txns := []*types.Transaction{}
- for i := uint64(0); i < maxQueued+5; i++ {
+ for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
}
- pool2.AddTransactions(txns)
+ pool2.AddBatch(txns)
// Ensure the batch optimization honors the same pool mechanics
if len(pool1.pending) != len(pool2.pending) {
t.Errorf("pending transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.pending), len(pool2.pending))
}
- if len(pool1.queue[account1]) != len(pool2.queue[account2]) {
- t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue[account1]), len(pool2.queue[account2]))
+ if len(pool1.queue) != len(pool2.queue) {
+ t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue), len(pool2.queue))
+ }
+ if len(pool1.all) != len(pool2.all) {
+ t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
}
}
// Benchmarks the speed of validating the contents of the pending queue of the
// transaction pool.
-func BenchmarkValidatePool100(b *testing.B) { benchmarkValidatePool(b, 100) }
-func BenchmarkValidatePool1000(b *testing.B) { benchmarkValidatePool(b, 1000) }
-func BenchmarkValidatePool10000(b *testing.B) { benchmarkValidatePool(b, 10000) }
+func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }
+func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) }
+func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) }
-func benchmarkValidatePool(b *testing.B, size int) {
+func benchmarkPendingDemotion(b *testing.B, size int) {
// Add a batch of transactions to a pool one by one
pool, key := setupTxPool()
account, _ := transaction(0, big.NewInt(0), key).From()
@@ -503,22 +633,22 @@ func benchmarkValidatePool(b *testing.B, size int) {
for i := 0; i < size; i++ {
tx := transaction(uint64(i), big.NewInt(100000), key)
- pool.addTx(tx.Hash(), account, tx)
+ pool.promoteTx(account, tx.Hash(), tx)
}
// Benchmark the speed of pool validation
b.ResetTimer()
for i := 0; i < b.N; i++ {
- pool.validatePool()
+ pool.demoteUnexecutables()
}
}
// Benchmarks the speed of scheduling the contents of the future queue of the
// transaction pool.
-func BenchmarkCheckQueue100(b *testing.B) { benchmarkCheckQueue(b, 100) }
-func BenchmarkCheckQueue1000(b *testing.B) { benchmarkCheckQueue(b, 1000) }
-func BenchmarkCheckQueue10000(b *testing.B) { benchmarkCheckQueue(b, 10000) }
+func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) }
+func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) }
+func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) }
-func benchmarkCheckQueue(b *testing.B, size int) {
+func benchmarkFuturePromotion(b *testing.B, size int) {
// Add a batch of transactions to a pool one by one
pool, key := setupTxPool()
account, _ := transaction(0, big.NewInt(0), key).From()
@@ -527,11 +657,56 @@ func benchmarkCheckQueue(b *testing.B, size int) {
for i := 0; i < size; i++ {
tx := transaction(uint64(1+i), big.NewInt(100000), key)
- pool.queueTx(tx.Hash(), tx)
+ pool.enqueueTx(tx.Hash(), tx)
}
// Benchmark the speed of pool validation
b.ResetTimer()
for i := 0; i < b.N; i++ {
- pool.checkQueue()
+ pool.promoteExecutables()
+ }
+}
+
+// Benchmarks the speed of iterative transaction insertion.
+func BenchmarkPoolInsert(b *testing.B) {
+ // Generate a batch of transactions to enqueue into the pool
+ pool, key := setupTxPool()
+ account, _ := transaction(0, big.NewInt(0), key).From()
+ state, _ := pool.currentState()
+ state.AddBalance(account, big.NewInt(1000000))
+
+ txs := make(types.Transactions, b.N)
+ for i := 0; i < b.N; i++ {
+ txs[i] = transaction(uint64(i), big.NewInt(100000), key)
+ }
+ // Benchmark importing the transactions into the queue
+ b.ResetTimer()
+ for _, tx := range txs {
+ pool.Add(tx)
+ }
+}
+
+// Benchmarks the speed of batched transaction insertion.
+func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100) }
+func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000) }
+func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000) }
+
+func benchmarkPoolBatchInsert(b *testing.B, size int) {
+ // Generate a batch of transactions to enqueue into the pool
+ pool, key := setupTxPool()
+ account, _ := transaction(0, big.NewInt(0), key).From()
+ state, _ := pool.currentState()
+ state.AddBalance(account, big.NewInt(1000000))
+
+ batches := make([]types.Transactions, b.N)
+ for i := 0; i < b.N; i++ {
+ batches[i] = make(types.Transactions, size)
+ for j := 0; j < size; j++ {
+ batches[i][j] = transaction(uint64(size*i+j), big.NewInt(100000), key)
+ }
+ }
+ // Benchmark importing the transactions into the queue
+ b.ResetTimer()
+ for _, batch := range batches {
+ pool.AddBatch(batch)
}
}
diff --git a/core/types.go b/core/types.go
index 20f33a153..d84d0987f 100644
--- a/core/types.go
+++ b/core/types.go
@@ -19,12 +19,9 @@ package core
import (
"math/big"
- "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/event"
)
// Validator is an interface which defines the standard for block validation.
@@ -63,16 +60,3 @@ type HeaderValidator interface {
type Processor interface {
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, vm.Logs, *big.Int, error)
}
-
-// Backend is an interface defining the basic functionality for an operable node
-// with all the functionality to be a functional, valid Ethereum operator.
-//
-// TODO Remove this
-type Backend interface {
- AccountManager() *accounts.Manager
- BlockChain() *BlockChain
- TxPool() *TxPool
- ChainDb() ethdb.Database
- DappDb() ethdb.Database
- EventMux() *event.TypeMux
-}
diff --git a/core/types/block.go b/core/types/block.go
index 37b6f3ec1..559fbdd20 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -18,9 +18,9 @@
package types
import (
- "bytes"
"encoding/binary"
"encoding/json"
+ "errors"
"fmt"
"io"
"math/big"
@@ -33,25 +33,53 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
+var (
+ EmptyRootHash = DeriveSha(Transactions{})
+ EmptyUncleHash = CalcUncleHash(nil)
+)
+
+var (
+ errMissingHeaderMixDigest = errors.New("missing mixHash in JSON block header")
+ errMissingHeaderFields = errors.New("missing required JSON block header fields")
+ errBadNonceSize = errors.New("invalid block nonce size, want 8 bytes")
+)
+
// A BlockNonce is a 64-bit hash which proves (combined with the
// mix-hash) that a sufficient amount of computation has been carried
// out on a block.
type BlockNonce [8]byte
+// EncodeNonce converts the given integer to a block nonce.
func EncodeNonce(i uint64) BlockNonce {
var n BlockNonce
binary.BigEndian.PutUint64(n[:], i)
return n
}
+// Uint64 returns the integer value of a block nonce.
func (n BlockNonce) Uint64() uint64 {
return binary.BigEndian.Uint64(n[:])
}
+// MarshalJSON implements json.Marshaler
func (n BlockNonce) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"0x%x"`, n)), nil
}
+// UnmarshalJSON implements json.Unmarshaler
+func (n *BlockNonce) UnmarshalJSON(input []byte) error {
+ var b hexBytes
+ if err := b.UnmarshalJSON(input); err != nil {
+ return err
+ }
+ if len(b) != 8 {
+ return errBadNonceSize
+ }
+ copy((*n)[:], b)
+ return nil
+}
+
+// Header represents Ethereum block headers.
type Header struct {
ParentHash common.Hash // Hash to the previous block
UncleHash common.Hash // Uncles of this block
@@ -70,10 +98,31 @@ type Header struct {
Nonce BlockNonce
}
+type jsonHeader struct {
+ ParentHash *common.Hash `json:"parentHash"`
+ UncleHash *common.Hash `json:"sha3Uncles"`
+ Coinbase *common.Address `json:"miner"`
+ Root *common.Hash `json:"stateRoot"`
+ TxHash *common.Hash `json:"transactionsRoot"`
+ ReceiptHash *common.Hash `json:"receiptRoot"`
+ Bloom *Bloom `json:"logsBloom"`
+ Difficulty *hexBig `json:"difficulty"`
+ Number *hexBig `json:"number"`
+ GasLimit *hexBig `json:"gasLimit"`
+ GasUsed *hexBig `json:"gasUsed"`
+ Time *hexBig `json:"timestamp"`
+ Extra *hexBytes `json:"extraData"`
+ MixDigest *common.Hash `json:"mixHash"`
+ Nonce *BlockNonce `json:"nonce"`
+}
+
+// Hash returns the block hash of the header, which is simply the keccak256 hash of its
+// RLP encoding.
func (h *Header) Hash() common.Hash {
return rlpHash(h)
}
+// HashNoNonce returns the hash which is used as input for the proof-of-work search.
func (h *Header) HashNoNonce() common.Hash {
return rlpHash([]interface{}{
h.ParentHash,
@@ -92,25 +141,62 @@ func (h *Header) HashNoNonce() common.Hash {
})
}
-func (h *Header) UnmarshalJSON(data []byte) error {
- var ext struct {
- ParentHash string
- Coinbase string
- Difficulty string
- GasLimit string
- Time *big.Int
- Extra string
- }
- dec := json.NewDecoder(bytes.NewReader(data))
- if err := dec.Decode(&ext); err != nil {
+// MarshalJSON encodes headers into the web3 RPC response block format.
+func (h *Header) MarshalJSON() ([]byte, error) {
+ return json.Marshal(&jsonHeader{
+ ParentHash: &h.ParentHash,
+ UncleHash: &h.UncleHash,
+ Coinbase: &h.Coinbase,
+ Root: &h.Root,
+ TxHash: &h.TxHash,
+ ReceiptHash: &h.ReceiptHash,
+ Bloom: &h.Bloom,
+ Difficulty: (*hexBig)(h.Difficulty),
+ Number: (*hexBig)(h.Number),
+ GasLimit: (*hexBig)(h.GasLimit),
+ GasUsed: (*hexBig)(h.GasUsed),
+ Time: (*hexBig)(h.Time),
+ Extra: (*hexBytes)(&h.Extra),
+ MixDigest: &h.MixDigest,
+ Nonce: &h.Nonce,
+ })
+}
+
+// UnmarshalJSON decodes headers from the web3 RPC response block format.
+func (h *Header) UnmarshalJSON(input []byte) error {
+ var dec jsonHeader
+ if err := json.Unmarshal(input, &dec); err != nil {
return err
}
-
- h.ParentHash = common.HexToHash(ext.ParentHash)
- h.Coinbase = common.HexToAddress(ext.Coinbase)
- h.Difficulty = common.String2Big(ext.Difficulty)
- h.Time = ext.Time
- h.Extra = []byte(ext.Extra)
+ // Ensure that all fields are set. MixDigest is checked separately because
+ // it is a recent addition to the spec (as of August 2016) and older RPC server
+ // implementations might not provide it.
+ if dec.MixDigest == nil {
+ return errMissingHeaderMixDigest
+ }
+ if dec.ParentHash == nil || dec.UncleHash == nil || dec.Coinbase == nil ||
+ dec.Root == nil || dec.TxHash == nil || dec.ReceiptHash == nil ||
+ dec.Bloom == nil || dec.Difficulty == nil || dec.Number == nil ||
+ dec.GasLimit == nil || dec.GasUsed == nil || dec.Time == nil ||
+ dec.Extra == nil || dec.Nonce == nil {
+ return errMissingHeaderFields
+ }
+ // Assign all values.
+ h.ParentHash = *dec.ParentHash
+ h.UncleHash = *dec.UncleHash
+ h.Coinbase = *dec.Coinbase
+ h.Root = *dec.Root
+ h.TxHash = *dec.TxHash
+ h.ReceiptHash = *dec.ReceiptHash
+ h.Bloom = *dec.Bloom
+ h.Difficulty = (*big.Int)(dec.Difficulty)
+ h.Number = (*big.Int)(dec.Number)
+ h.GasLimit = (*big.Int)(dec.GasLimit)
+ h.GasUsed = (*big.Int)(dec.GasUsed)
+ h.Time = (*big.Int)(dec.Time)
+ h.Extra = *dec.Extra
+ h.MixDigest = *dec.MixDigest
+ h.Nonce = *dec.Nonce
return nil
}
@@ -128,6 +214,7 @@ type Body struct {
Uncles []*Header
}
+// Block represents a block in the Ethereum blockchain.
type Block struct {
header *Header
uncles []*Header
@@ -176,11 +263,6 @@ type storageblock struct {
TD *big.Int
}
-var (
- EmptyRootHash = DeriveSha(Transactions{})
- EmptyUncleHash = CalcUncleHash(nil)
-)
-
// NewBlock creates a new block. The input data is copied,
// changes to header and to the field values will not affect the
// block.
@@ -253,23 +335,7 @@ func CopyHeader(h *Header) *Header {
return &cpy
}
-func (b *Block) ValidateFields() error {
- if b.header == nil {
- return fmt.Errorf("header is nil")
- }
- for i, transaction := range b.transactions {
- if transaction == nil {
- return fmt.Errorf("transaction %d is nil", i)
- }
- }
- for i, uncle := range b.uncles {
- if uncle == nil {
- return fmt.Errorf("uncle %d is nil", i)
- }
- }
- return nil
-}
-
+// DecodeRLP decodes the Ethereum
func (b *Block) DecodeRLP(s *rlp.Stream) error {
var eb extblock
_, size, _ := s.Kind()
@@ -281,6 +347,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
return nil
}
+// EncodeRLP serializes b into the Ethereum RLP block format.
func (b *Block) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, extblock{
Header: b.header,
@@ -300,6 +367,7 @@ func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
}
// TODO: copies
+
func (b *Block) Uncles() []*Header { return b.uncles }
func (b *Block) Transactions() Transactions { return b.transactions }
@@ -387,8 +455,8 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
return block
}
-// Implement pow.Block
-
+// Hash returns the keccak256 hash of b's header.
+// The hash is computed on the first call and cached thereafter.
func (b *Block) Hash() common.Hash {
if hash := b.hash.Load(); hash != nil {
return hash.(common.Hash)
diff --git a/core/types/bloom9.go b/core/types/bloom9.go
index ecf2bffc2..d3945a734 100644
--- a/core/types/bloom9.go
+++ b/core/types/bloom9.go
@@ -31,28 +31,34 @@ type bytesBacked interface {
const bloomLength = 256
+// Bloom represents a 256 bit bloom filter.
type Bloom [bloomLength]byte
+// BytesToBloom converts a byte slice to a bloom filter.
+// It panics if b is not of suitable size.
func BytesToBloom(b []byte) Bloom {
var bloom Bloom
bloom.SetBytes(b)
return bloom
}
+// SetBytes sets the content of b to the given bytes.
+// It panics if d is not of suitable size.
func (b *Bloom) SetBytes(d []byte) {
if len(b) < len(d) {
panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
}
-
copy(b[bloomLength-len(d):], d)
}
+// Add adds d to the filter. Future calls of Test(d) will return true.
func (b *Bloom) Add(d *big.Int) {
bin := new(big.Int).SetBytes(b[:])
bin.Or(bin, bloom9(d.Bytes()))
b.SetBytes(bin.Bytes())
}
+// Big converts b to a big integer.
func (b Bloom) Big() *big.Int {
return common.Bytes2Big(b[:])
}
@@ -69,8 +75,22 @@ func (b Bloom) TestBytes(test []byte) bool {
return b.Test(common.BytesToBig(test))
}
+// MarshalJSON encodes b as a hex string with 0x prefix.
func (b Bloom) MarshalJSON() ([]byte, error) {
- return []byte(fmt.Sprintf(`"%#x"`, b.Bytes())), nil
+ return []byte(fmt.Sprintf(`"%#x"`, b[:])), nil
+}
+
+// UnmarshalJSON b as a hex string with 0x prefix.
+func (b *Bloom) UnmarshalJSON(input []byte) error {
+ var dec hexBytes
+ if err := dec.UnmarshalJSON(input); err != nil {
+ return err
+ }
+ if len(dec) != bloomLength {
+ return fmt.Errorf("invalid bloom size, want %d bytes", bloomLength)
+ }
+ copy((*b)[:], dec)
+ return nil
}
func CreateBloom(receipts Receipts) Bloom {
diff --git a/core/types/json.go b/core/types/json.go
new file mode 100644
index 000000000..d2718a96d
--- /dev/null
+++ b/core/types/json.go
@@ -0,0 +1,108 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "encoding/hex"
+ "fmt"
+ "math/big"
+)
+
+// JSON unmarshaling utilities.
+
+type hexBytes []byte
+
+func (b *hexBytes) MarshalJSON() ([]byte, error) {
+ if b != nil {
+ return []byte(fmt.Sprintf(`"0x%x"`, []byte(*b))), nil
+ }
+ return nil, nil
+}
+
+func (b *hexBytes) UnmarshalJSON(input []byte) error {
+ if len(input) < 2 || input[0] != '"' || input[len(input)-1] != '"' {
+ return fmt.Errorf("cannot unmarshal non-string into hexBytes")
+ }
+ input = input[1 : len(input)-1]
+ if len(input) < 2 || input[0] != '0' || input[1] != 'x' {
+ return fmt.Errorf("missing 0x prefix in hexBytes input %q", input)
+ }
+ dec := make(hexBytes, (len(input)-2)/2)
+ if _, err := hex.Decode(dec, input[2:]); err != nil {
+ return err
+ }
+ *b = dec
+ return nil
+}
+
+type hexBig big.Int
+
+func (b *hexBig) MarshalJSON() ([]byte, error) {
+ if b != nil {
+ return []byte(fmt.Sprintf(`"0x%x"`, (*big.Int)(b))), nil
+ }
+ return nil, nil
+}
+
+func (b *hexBig) UnmarshalJSON(input []byte) error {
+ raw, err := checkHexNumber(input)
+ if err != nil {
+ return err
+ }
+ dec, ok := new(big.Int).SetString(string(raw), 16)
+ if !ok {
+ return fmt.Errorf("invalid hex number")
+ }
+ *b = (hexBig)(*dec)
+ return nil
+}
+
+type hexUint64 uint64
+
+func (b *hexUint64) MarshalJSON() ([]byte, error) {
+ if b != nil {
+ return []byte(fmt.Sprintf(`"0x%x"`, *(*uint64)(b))), nil
+ }
+ return nil, nil
+}
+
+func (b *hexUint64) UnmarshalJSON(input []byte) error {
+ raw, err := checkHexNumber(input)
+ if err != nil {
+ return err
+ }
+ _, err = fmt.Sscanf(string(raw), "%x", b)
+ return err
+}
+
+func checkHexNumber(input []byte) (raw []byte, err error) {
+ if len(input) < 2 || input[0] != '"' || input[len(input)-1] != '"' {
+ return nil, fmt.Errorf("cannot unmarshal non-string into hex number")
+ }
+ input = input[1 : len(input)-1]
+ if len(input) < 2 || input[0] != '0' || input[1] != 'x' {
+ return nil, fmt.Errorf("missing 0x prefix in hex number input %q", input)
+ }
+ if len(input) == 2 {
+ return nil, fmt.Errorf("empty hex number")
+ }
+ raw = input[2:]
+ if len(raw)%2 != 0 {
+ raw = append([]byte{'0'}, raw...)
+ }
+ return raw, nil
+}
diff --git a/core/types/json_test.go b/core/types/json_test.go
new file mode 100644
index 000000000..605c2b564
--- /dev/null
+++ b/core/types/json_test.go
@@ -0,0 +1,213 @@
+package types
+
+import (
+ "encoding/json"
+ "reflect"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+var unmarshalHeaderTests = map[string]struct {
+ input string
+ wantHash common.Hash
+ wantError error
+}{
+ "block 0x1e2200": {
+ input: `{"difficulty":"0x311ca98cebfe","extraData":"0x7777772e62772e636f6d","gasLimit":"0x47db3d","gasUsed":"0x43760c","hash":"0x3724bc6b9dcd4a2b3a26e0ed9b821e7380b5b3d7dec7166c7983cead62a37e48","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","mixHash":"0x1ccfddb506dac5afc09b6f92eb09a043ffc8e08f7592250af57b9c64c20f9b25","nonce":"0x670bd98c79585197","number":"0x1e2200","parentHash":"0xd3e13296d064e7344f20c57c57b67a022f6bf7741fa42428c2db77e91abdf1f8","receiptRoot":"0xeeab1776c1fafbe853a8ee0c1bafe2e775a1b6fdb6ff3e9f9410ddd4514889ff","sha3Uncles":"0x5fbfa4ec8b089678c53b6798cc0d9260ea40a529e06d5300aae35596262e0eb3","size":"0x57f","stateRoot":"0x62ad2007e4a3f31ea98e5d2fd150d894887bafde36eeac7331a60ae12053ec76","timestamp":"0x579b82f2","totalDifficulty":"0x24fe813c101d00f97","transactions":["0xb293408e85735bfc78b35aa89de8b48e49641e3d82e3d52ea2d44ec42a4e88cf","0x124acc383ff2da6faa0357829084dae64945221af6f6f09da1d11688b779f939","0xee090208b6051c442ccdf9ec19f66389e604d342a6d71144c7227ce995bef46f"],"transactionsRoot":"0xce0042dd9af0c1923dd7f58ca6faa156d39d4ef39fdb65c5bcd1d4b4720096db","uncles":["0x6818a31d1f204cf640c952082940b68b8db6d1b39ee71f7efe0e3629ed5d7eb3"]}`,
+ wantHash: common.HexToHash("0x3724bc6b9dcd4a2b3a26e0ed9b821e7380b5b3d7dec7166c7983cead62a37e48"),
+ },
+ "bad nonce": {
+ input: `{"difficulty":"0x311ca98cebfe","extraData":"0x7777772e62772e636f6d","gasLimit":"0x47db3d","gasUsed":"0x43760c","hash":"0x3724bc6b9dcd4a2b3a26e0ed9b821e7380b5b3d7dec7166c7983cead62a37e48","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","mixHash":"0x1ccfddb506dac5afc09b6f92eb09a043ffc8e08f7592250af57b9c64c20f9b25","nonce":"0x670bd98c7958","number":"0x1e2200","parentHash":"0xd3e13296d064e7344f20c57c57b67a022f6bf7741fa42428c2db77e91abdf1f8","receiptRoot":"0xeeab1776c1fafbe853a8ee0c1bafe2e775a1b6fdb6ff3e9f9410ddd4514889ff","sha3Uncles":"0x5fbfa4ec8b089678c53b6798cc0d9260ea40a529e06d5300aae35596262e0eb3","size":"0x57f","stateRoot":"0x62ad2007e4a3f31ea98e5d2fd150d894887bafde36eeac7331a60ae12053ec76","timestamp":"0x579b82f2","totalDifficulty":"0x24fe813c101d00f97","transactions":["0xb293408e85735bfc78b35aa89de8b48e49641e3d82e3d52ea2d44ec42a4e88cf","0x124acc383ff2da6faa0357829084dae64945221af6f6f09da1d11688b779f939","0xee090208b6051c442ccdf9ec19f66389e604d342a6d71144c7227ce995bef46f"],"transactionsRoot":"0xce0042dd9af0c1923dd7f58ca6faa156d39d4ef39fdb65c5bcd1d4b4720096db","uncles":["0x6818a31d1f204cf640c952082940b68b8db6d1b39ee71f7efe0e3629ed5d7eb3"]}`,
+ wantError: errBadNonceSize,
+ },
+ "missing mixHash": {
+ input: `{"difficulty":"0x311ca98cebfe","extraData":"0x7777772e62772e636f6d","gasLimit":"0x47db3d","gasUsed":"0x43760c","hash":"0x3724bc6b9dcd4a2b3a26e0ed9b821e7380b5b3d7dec7166c7983cead62a37e48","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","nonce":"0x670bd98c79585197","number":"0x1e2200","parentHash":"0xd3e13296d064e7344f20c57c57b67a022f6bf7741fa42428c2db77e91abdf1f8","receiptRoot":"0xeeab1776c1fafbe853a8ee0c1bafe2e775a1b6fdb6ff3e9f9410ddd4514889ff","sha3Uncles":"0x5fbfa4ec8b089678c53b6798cc0d9260ea40a529e06d5300aae35596262e0eb3","size":"0x57f","stateRoot":"0x62ad2007e4a3f31ea98e5d2fd150d894887bafde36eeac7331a60ae12053ec76","timestamp":"0x579b82f2","totalDifficulty":"0x24fe813c101d00f97","transactions":["0xb293408e85735bfc78b35aa89de8b48e49641e3d82e3d52ea2d44ec42a4e88cf","0x124acc383ff2da6faa0357829084dae64945221af6f6f09da1d11688b779f939","0xee090208b6051c442ccdf9ec19f66389e604d342a6d71144c7227ce995bef46f"],"transactionsRoot":"0xce0042dd9af0c1923dd7f58ca6faa156d39d4ef39fdb65c5bcd1d4b4720096db","uncles":["0x6818a31d1f204cf640c952082940b68b8db6d1b39ee71f7efe0e3629ed5d7eb3"]}`,
+ wantError: errMissingHeaderMixDigest,
+ },
+ "missing fields": {
+ input: `{"gasLimit":"0x47db3d","gasUsed":"0x43760c","hash":"0x3724bc6b9dcd4a2b3a26e0ed9b821e7380b5b3d7dec7166c7983cead62a37e48","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","mixHash":"0x1ccfddb506dac5afc09b6f92eb09a043ffc8e08f7592250af57b9c64c20f9b25","nonce":"0x670bd98c79585197","number":"0x1e2200","parentHash":"0xd3e13296d064e7344f20c57c57b67a022f6bf7741fa42428c2db77e91abdf1f8","receiptRoot":"0xeeab1776c1fafbe853a8ee0c1bafe2e775a1b6fdb6ff3e9f9410ddd4514889ff","sha3Uncles":"0x5fbfa4ec8b089678c53b6798cc0d9260ea40a529e06d5300aae35596262e0eb3","size":"0x57f","stateRoot":"0x62ad2007e4a3f31ea98e5d2fd150d894887bafde36eeac7331a60ae12053ec76","timestamp":"0x579b82f2","totalDifficulty":"0x24fe813c101d00f97","transactions":["0xb293408e85735bfc78b35aa89de8b48e49641e3d82e3d52ea2d44ec42a4e88cf","0x124acc383ff2da6faa0357829084dae64945221af6f6f09da1d11688b779f939","0xee090208b6051c442ccdf9ec19f66389e604d342a6d71144c7227ce995bef46f"],"transactionsRoot":"0xce0042dd9af0c1923dd7f58ca6faa156d39d4ef39fdb65c5bcd1d4b4720096db","uncles":["0x6818a31d1f204cf640c952082940b68b8db6d1b39ee71f7efe0e3629ed5d7eb3"]}`,
+ wantError: errMissingHeaderFields,
+ },
+}
+
+func TestUnmarshalHeader(t *testing.T) {
+ for name, test := range unmarshalHeaderTests {
+ var head *Header
+ err := json.Unmarshal([]byte(test.input), &head)
+ if !checkError(t, name, err, test.wantError) {
+ continue
+ }
+ if head.Hash() != test.wantHash {
+ t.Errorf("test %q: got hash %x, want %x", name, head.Hash(), test.wantHash)
+ continue
+ }
+ }
+}
+
+func TestMarshalHeader(t *testing.T) {
+ for name, test := range unmarshalHeaderTests {
+ if test.wantError != nil {
+ continue
+ }
+ var original *Header
+ json.Unmarshal([]byte(test.input), &original)
+
+ blob, err := json.Marshal(original)
+ if err != nil {
+ t.Errorf("test %q: failed to marshal header: %v", name, err)
+ continue
+ }
+ var proced *Header
+ if err := json.Unmarshal(blob, &proced); err != nil {
+ t.Errorf("Test %q: failed to unmarshal marhsalled header: %v", name, err)
+ continue
+ }
+ if !reflect.DeepEqual(original, proced) {
+ t.Errorf("test %q: header mismatch: have %+v, want %+v", name, proced, original)
+ continue
+ }
+ }
+}
+
+var unmarshalTransactionTests = map[string]struct {
+ input string
+ wantHash common.Hash
+ wantFrom common.Address
+ wantError error
+}{
+ "value transfer": {
+ input: `{"blockHash":"0x0188a05dcc825bd1a05dab91bea0c03622542683446e56302eabb46097d4ae11","blockNumber":"0x1e478d","from":"0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9","input":"0x","nonce":"0x58d","to":"0x88f252f674ac755feff877abf957d4aa05adce86","transactionIndex":"0x1","value":"0x19f0ec3ed71ec00","v":"0x1c","r":"0x53829f206c99b866672f987909d556cd1c2eb60e990a3425f65083977c14187b","s":"0x5cc52383e41c923ec7d63749c1f13a7236b540527ee5b9a78b3fb869a66f60e"}`,
+ wantHash: common.HexToHash("0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9"),
+ wantFrom: common.HexToAddress("0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689"),
+ },
+ "bad signature fields": {
+ input: `{"blockHash":"0x0188a05dcc825bd1a05dab91bea0c03622542683446e56302eabb46097d4ae11","blockNumber":"0x1e478d","from":"0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9","input":"0x","nonce":"0x58d","to":"0x88f252f674ac755feff877abf957d4aa05adce86","transactionIndex":"0x1","value":"0x19f0ec3ed71ec00","v":"0x58","r":"0x53829f206c99b866672f987909d556cd1c2eb60e990a3425f65083977c14187b","s":"0x5cc52383e41c923ec7d63749c1f13a7236b540527ee5b9a78b3fb869a66f60e"}`,
+ wantError: ErrInvalidSig,
+ },
+ "missing signature v": {
+ input: `{"blockHash":"0x0188a05dcc825bd1a05dab91bea0c03622542683446e56302eabb46097d4ae11","blockNumber":"0x1e478d","from":"0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9","input":"0x","nonce":"0x58d","to":"0x88f252f674ac755feff877abf957d4aa05adce86","transactionIndex":"0x1","value":"0x19f0ec3ed71ec00","r":"0x53829f206c99b866672f987909d556cd1c2eb60e990a3425f65083977c14187b","s":"0x5cc52383e41c923ec7d63749c1f13a7236b540527ee5b9a78b3fb869a66f60e"}`,
+ wantError: errMissingTxSignatureFields,
+ },
+ "missing signature fields": {
+ input: `{"blockHash":"0x0188a05dcc825bd1a05dab91bea0c03622542683446e56302eabb46097d4ae11","blockNumber":"0x1e478d","from":"0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9","input":"0x","nonce":"0x58d","to":"0x88f252f674ac755feff877abf957d4aa05adce86","transactionIndex":"0x1","value":"0x19f0ec3ed71ec00"}`,
+ wantError: errMissingTxSignatureFields,
+ },
+ "missing fields": {
+ input: `{"blockHash":"0x0188a05dcc825bd1a05dab91bea0c03622542683446e56302eabb46097d4ae11","blockNumber":"0x1e478d","from":"0xf36c3f6c4a2ce8d353fb92d5cd10d19ce69ae689","hash":"0xd91c08f1e27c5ce7e1f57d78d7c56a9ee446be07b9635d84d0475660ea8905e9","input":"0x","nonce":"0x58d","to":"0x88f252f674ac755feff877abf957d4aa05adce86","transactionIndex":"0x1","value":"0x19f0ec3ed71ec00","v":"0x1c","r":"0x53829f206c99b866672f987909d556cd1c2eb60e990a3425f65083977c14187b","s":"0x5cc52383e41c923ec7d63749c1f13a7236b540527ee5b9a78b3fb869a66f60e"}`,
+ wantError: errMissingTxFields,
+ },
+}
+
+func TestUnmarshalTransaction(t *testing.T) {
+ for name, test := range unmarshalTransactionTests {
+ var tx *Transaction
+ err := json.Unmarshal([]byte(test.input), &tx)
+ if !checkError(t, name, err, test.wantError) {
+ continue
+ }
+ if tx.Hash() != test.wantHash {
+ t.Errorf("test %q: got hash %x, want %x", name, tx.Hash(), test.wantHash)
+ continue
+ }
+ from, err := tx.From()
+ if err != nil {
+ t.Errorf("test %q: From error %v", name, err)
+ }
+ if from != test.wantFrom {
+ t.Errorf("test %q: sender mismatch: got %x, want %x", name, from, test.wantFrom)
+ }
+ }
+}
+
+func TestMarshalTransaction(t *testing.T) {
+ for name, test := range unmarshalTransactionTests {
+ if test.wantError != nil {
+ continue
+ }
+ var original *Transaction
+ json.Unmarshal([]byte(test.input), &original)
+
+ blob, err := json.Marshal(original)
+ if err != nil {
+ t.Errorf("test %q: failed to marshal transaction: %v", name, err)
+ continue
+ }
+ var proced *Transaction
+ if err := json.Unmarshal(blob, &proced); err != nil {
+ t.Errorf("Test %q: failed to unmarshal marhsalled transaction: %v", name, err)
+ continue
+ }
+ proced.Hash() // hack private fields to pass deep equal
+ if !reflect.DeepEqual(original, proced) {
+ t.Errorf("test %q: transaction mismatch: have %+v, want %+v", name, proced, original)
+ continue
+ }
+ }
+}
+
+var unmarshalReceiptTests = map[string]struct {
+ input string
+ wantError error
+}{
+ "ok": {
+ input: `{"blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","blockNumber":"0x1e773b","contractAddress":null,"cumulativeGasUsed":"0x10cea","from":"0xdf21fa922215b1a56f5a6d6294e6e36c85a0acfb","gasUsed":"0xbae2","logs":[{"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000df21fa922215b1a56f5a6d6294e6e36c85a0acfb","0x00000000000000000000000032be343b94f860124dc4fee278fdcbd38c102d88"],"data":"0x0000000000000000000000000000000000000000000000027cfefc4f3f392700","blockNumber":"0x1e773b","transactionIndex":"0x1","transactionHash":"0x0b4cc7844537023b709953390e3881ec5b233703a8e8824dc03e13729a1bd95a","blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","logIndex":"0x0"}],"logsBloom":"0x00000000000000020000000000020000000000000000000000000000000000000000000000000000000000000000000000040000000000000100000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000010000000000000000000000000000000000000000000000010000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000002002000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000","root":"0x6e8a06b2dac39ac5c9d4db5fb2a2a94ef7a6e5ec1c554079112112caf162998a","to":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","transactionHash":"0x0b4cc7844537023b709953390e3881ec5b233703a8e8824dc03e13729a1bd95a","transactionIndex":"0x1"}`,
+ },
+ "missing post state": {
+ input: `{"blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","blockNumber":"0x1e773b","contractAddress":null,"cumulativeGasUsed":"0x10cea","from":"0xdf21fa922215b1a56f5a6d6294e6e36c85a0acfb","gasUsed":"0xbae2","logs":[{"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000df21fa922215b1a56f5a6d6294e6e36c85a0acfb","0x00000000000000000000000032be343b94f860124dc4fee278fdcbd38c102d88"],"data":"0x0000000000000000000000000000000000000000000000027cfefc4f3f392700","blockNumber":"0x1e773b","transactionIndex":"0x1","transactionHash":"0x0b4cc7844537023b709953390e3881ec5b233703a8e8824dc03e13729a1bd95a","blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","logIndex":"0x0"}],"logsBloom":"0x00000000000000020000000000020000000000000000000000000000000000000000000000000000000000000000000000040000000000000100000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000010000000000000000000000000000000000000000000000010000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000002002000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000","to":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","transactionHash":"0x0b4cc7844537023b709953390e3881ec5b233703a8e8824dc03e13729a1bd95a","transactionIndex":"0x1"}`,
+ wantError: errMissingReceiptPostState,
+ },
+ "missing fields": {
+ input: `{"blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","blockNumber":"0x1e773b","contractAddress":null,"cumulativeGasUsed":"0x10cea","from":"0xdf21fa922215b1a56f5a6d6294e6e36c85a0acfb","gasUsed":"0xbae2","logs":[{"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000df21fa922215b1a56f5a6d6294e6e36c85a0acfb","0x00000000000000000000000032be343b94f860124dc4fee278fdcbd38c102d88"],"data":"0x0000000000000000000000000000000000000000000000027cfefc4f3f392700","blockNumber":"0x1e773b","transactionIndex":"0x1","transactionHash":"0x0b4cc7844537023b709953390e3881ec5b233703a8e8824dc03e13729a1bd95a","blockHash":"0xad20a0f78d19d7857067a9c06e6411efeab7673e183e4a545f53b724bb7fabf0","logIndex":"0x0"}],"logsBloom":"0x00000000000000020000000000020000000000000000000000000000000000000000000000000000000000000000000000040000000000000100000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000010000000000000000000000000000000000000000000000010000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000002002000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000","root":"0x6e8a06b2dac39ac5c9d4db5fb2a2a94ef7a6e5ec1c554079112112caf162998a","to":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413"}`,
+ wantError: errMissingReceiptFields,
+ },
+}
+
+func TestUnmarshalReceipt(t *testing.T) {
+ for name, test := range unmarshalReceiptTests {
+ var r *Receipt
+ err := json.Unmarshal([]byte(test.input), &r)
+ checkError(t, name, err, test.wantError)
+ }
+}
+
+func TestMarshalReceipt(t *testing.T) {
+ for name, test := range unmarshalReceiptTests {
+ if test.wantError != nil {
+ continue
+ }
+ var original *Receipt
+ json.Unmarshal([]byte(test.input), &original)
+
+ blob, err := json.Marshal(original)
+ if err != nil {
+ t.Errorf("test %q: failed to marshal receipt: %v", name, err)
+ continue
+ }
+ var proced *Receipt
+ if err := json.Unmarshal(blob, &proced); err != nil {
+ t.Errorf("Test %q: failed to unmarshal marhsalled receipt: %v", name, err)
+ continue
+ }
+ if !reflect.DeepEqual(original, proced) {
+ t.Errorf("test %q: receipt mismatch: have %+v, want %+v", name, proced, original)
+ continue
+ }
+ }
+}
+
+func checkError(t *testing.T, testname string, got, want error) bool {
+ if got == nil {
+ if want != nil {
+ t.Errorf("test %q: got no error, want %q", testname, want)
+ return false
+ }
+ return true
+ }
+ if want == nil {
+ t.Errorf("test %q: unexpected error %q", testname, got)
+ } else if got.Error() != want.Error() {
+ t.Errorf("test %q: got error %q, want %q", testname, got, want)
+ }
+ return false
+}
diff --git a/core/types/receipt.go b/core/types/receipt.go
index 5f847fc5c..b00fdabff 100644
--- a/core/types/receipt.go
+++ b/core/types/receipt.go
@@ -17,6 +17,8 @@
package types
import (
+ "encoding/json"
+ "errors"
"fmt"
"io"
"math/big"
@@ -26,6 +28,11 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
+var (
+ errMissingReceiptPostState = errors.New("missing post state root in JSON receipt")
+ errMissingReceiptFields = errors.New("missing required JSON receipt fields")
+)
+
// Receipt represents the results of a transaction.
type Receipt struct {
// Consensus fields
@@ -34,12 +41,22 @@ type Receipt struct {
Bloom Bloom
Logs vm.Logs
- // Implementation fields
+ // Implementation fields (don't reorder!)
TxHash common.Hash
ContractAddress common.Address
GasUsed *big.Int
}
+type jsonReceipt struct {
+ PostState *common.Hash `json:"root"`
+ CumulativeGasUsed *hexBig `json:"cumulativeGasUsed"`
+ Bloom *Bloom `json:"logsBloom"`
+ Logs *vm.Logs `json:"logs"`
+ TxHash *common.Hash `json:"transactionHash"`
+ ContractAddress *common.Address `json:"contractAddress"`
+ GasUsed *hexBig `json:"gasUsed"`
+}
+
// NewReceipt creates a barebone transaction receipt, copying the init fields.
func NewReceipt(root []byte, cumulativeGasUsed *big.Int) *Receipt {
return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
@@ -67,13 +84,49 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
return nil
}
-// RlpEncode implements common.RlpEncode required for SHA3 derivation.
-func (r *Receipt) RlpEncode() []byte {
- bytes, err := rlp.EncodeToBytes(r)
- if err != nil {
- panic(err)
+// MarshalJSON encodes receipts into the web3 RPC response block format.
+func (r *Receipt) MarshalJSON() ([]byte, error) {
+ root := common.BytesToHash(r.PostState)
+
+ return json.Marshal(&jsonReceipt{
+ PostState: &root,
+ CumulativeGasUsed: (*hexBig)(r.CumulativeGasUsed),
+ Bloom: &r.Bloom,
+ Logs: &r.Logs,
+ TxHash: &r.TxHash,
+ ContractAddress: &r.ContractAddress,
+ GasUsed: (*hexBig)(r.GasUsed),
+ })
+}
+
+// UnmarshalJSON decodes the web3 RPC receipt format.
+func (r *Receipt) UnmarshalJSON(input []byte) error {
+ var dec jsonReceipt
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
}
- return bytes
+ // Ensure that all fields are set. PostState is checked separately because it is a
+ // recent addition to the RPC spec (as of August 2016) and older implementations might
+ // not provide it. Note that ContractAddress is not checked because it can be null.
+ if dec.PostState == nil {
+ return errMissingReceiptPostState
+ }
+ if dec.CumulativeGasUsed == nil || dec.Bloom == nil ||
+ dec.Logs == nil || dec.TxHash == nil || dec.GasUsed == nil {
+ return errMissingReceiptFields
+ }
+ *r = Receipt{
+ PostState: (*dec.PostState)[:],
+ CumulativeGasUsed: (*big.Int)(dec.CumulativeGasUsed),
+ Bloom: *dec.Bloom,
+ Logs: *dec.Logs,
+ TxHash: *dec.TxHash,
+ GasUsed: (*big.Int)(dec.GasUsed),
+ }
+ if dec.ContractAddress != nil {
+ r.ContractAddress = *dec.ContractAddress
+ }
+ return nil
}
// String implements the Stringer interface.
@@ -122,7 +175,7 @@ func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
return nil
}
-// Receipts is a wrapper around a Receipt array to implement types.DerivableList.
+// Receipts is a wrapper around a Receipt array to implement DerivableList.
type Receipts []*Receipt
// Len returns the number of receipts in this list.
diff --git a/core/types/transaction.go b/core/types/transaction.go
index c71c98aa7..f0512ae7e 100644
--- a/core/types/transaction.go
+++ b/core/types/transaction.go
@@ -19,21 +19,24 @@ package types
import (
"container/heap"
"crypto/ecdsa"
+ "encoding/json"
"errors"
"fmt"
"io"
"math/big"
- "sort"
"sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rlp"
)
-var ErrInvalidSig = errors.New("invalid v, r, s values")
+var ErrInvalidSig = errors.New("invalid transaction v, r, s values")
+
+var (
+ errMissingTxSignatureFields = errors.New("missing required JSON transaction signature fields")
+ errMissingTxFields = errors.New("missing required JSON transaction fields")
+)
type Transaction struct {
data txdata
@@ -53,6 +56,20 @@ type txdata struct {
R, S *big.Int // signature
}
+type jsonTransaction struct {
+ Hash *common.Hash `json:"hash"`
+ AccountNonce *hexUint64 `json:"nonce"`
+ Price *hexBig `json:"gasPrice"`
+ GasLimit *hexBig `json:"gas"`
+ Recipient *common.Address `json:"to"`
+ Amount *hexBig `json:"value"`
+ Payload *hexBytes `json:"input"`
+ V *hexUint64 `json:"v"`
+ R *hexBig `json:"r"`
+ S *hexBig `json:"s"`
+}
+
+// NewContractCreation creates a new transaction with no recipient.
func NewContractCreation(nonce uint64, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction {
if len(data) > 0 {
data = common.CopyBytes(data)
@@ -69,6 +86,7 @@ func NewContractCreation(nonce uint64, amount, gasLimit, gasPrice *big.Int, data
}}
}
+// NewTransaction creates a new transaction with the given fields.
func NewTransaction(nonce uint64, to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction {
if len(data) > 0 {
data = common.CopyBytes(data)
@@ -95,10 +113,12 @@ func NewTransaction(nonce uint64, to common.Address, amount, gasLimit, gasPrice
return &Transaction{data: d}
}
+// DecodeRLP implements rlp.Encoder
func (tx *Transaction) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &tx.data)
}
+// DecodeRLP implements rlp.Decoder
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
err := s.Decode(&tx.data)
@@ -108,6 +128,60 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
return err
}
+// MarshalJSON encodes transactions into the web3 RPC response block format.
+func (tx *Transaction) MarshalJSON() ([]byte, error) {
+ hash, v := tx.Hash(), uint64(tx.data.V)
+
+ return json.Marshal(&jsonTransaction{
+ Hash: &hash,
+ AccountNonce: (*hexUint64)(&tx.data.AccountNonce),
+ Price: (*hexBig)(tx.data.Price),
+ GasLimit: (*hexBig)(tx.data.GasLimit),
+ Recipient: tx.data.Recipient,
+ Amount: (*hexBig)(tx.data.Amount),
+ Payload: (*hexBytes)(&tx.data.Payload),
+ V: (*hexUint64)(&v),
+ R: (*hexBig)(tx.data.R),
+ S: (*hexBig)(tx.data.S),
+ })
+}
+
+// UnmarshalJSON decodes the web3 RPC transaction format.
+func (tx *Transaction) UnmarshalJSON(input []byte) error {
+ var dec jsonTransaction
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ // Ensure that all fields are set. V, R, S are checked separately because they're a
+ // recent addition to the RPC spec (as of August 2016) and older implementations might
+ // not provide them. Note that Recipient is not checked because it can be missing for
+ // contract creations.
+ if dec.V == nil || dec.R == nil || dec.S == nil {
+ return errMissingTxSignatureFields
+ }
+ if !crypto.ValidateSignatureValues(byte(*dec.V), (*big.Int)(dec.R), (*big.Int)(dec.S), false) {
+ return ErrInvalidSig
+ }
+ if dec.AccountNonce == nil || dec.Price == nil || dec.GasLimit == nil || dec.Amount == nil || dec.Payload == nil {
+ return errMissingTxFields
+ }
+ // Assign the fields. This is not atomic but reusing transactions
+ // for decoding isn't thread safe anyway.
+ *tx = Transaction{}
+ tx.data = txdata{
+ AccountNonce: uint64(*dec.AccountNonce),
+ Recipient: dec.Recipient,
+ Amount: (*big.Int)(dec.Amount),
+ GasLimit: (*big.Int)(dec.GasLimit),
+ Price: (*big.Int)(dec.Price),
+ Payload: *dec.Payload,
+ V: byte(*dec.V),
+ R: (*big.Int)(dec.R),
+ S: (*big.Int)(dec.S),
+ }
+ return nil
+}
+
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
func (tx *Transaction) Gas() *big.Int { return new(big.Int).Set(tx.data.GasLimit) }
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
@@ -215,6 +289,7 @@ func (tx *Transaction) Cost() *big.Int {
return total
}
+// SignatureValues returns the ECDSA signature values contained in the transaction.
func (tx *Transaction) SignatureValues() (v byte, r *big.Int, s *big.Int) {
return tx.data.V, new(big.Int).Set(tx.data.R), new(big.Int).Set(tx.data.S)
}
@@ -235,7 +310,6 @@ func (tx *Transaction) publicKey(homestead bool) ([]byte, error) {
hash := tx.SigHash()
pub, err := crypto.Ecrecover(hash[:], sig)
if err != nil {
- glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
return nil, err
}
if len(pub) == 0 || pub[0] != 4 {
@@ -370,49 +444,58 @@ func (s *TxByPrice) Pop() interface{} {
return x
}
-// SortByPriceAndNonce sorts the transactions by price in such a way that the
-// nonce orderings within a single account are maintained.
-//
-// Note, this is not as trivial as it seems from the first look as there are three
-// different criteria that need to be taken into account (price, nonce, account
-// match), which cannot be done with any plain sorting method, as certain items
-// cannot be compared without context.
+// TransactionsByPriceAndNonce represents a set of transactions that can return
+// transactions in a profit-maximising sorted order, while supporting removing
+// entire batches of transactions for non-executable accounts.
+type TransactionsByPriceAndNonce struct {
+ txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
+ heads TxByPrice // Next transaction for each unique account (price heap)
+}
+
+// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
+// price sorted transactions in a nonce-honouring way.
//
-// This method first sorts the separates the list of transactions into individual
-// sender accounts and sorts them by nonce. After the account nonce ordering is
-// satisfied, the results are merged back together by price, always comparing only
-// the head transaction from each account. This is done via a heap to keep it fast.
-func SortByPriceAndNonce(txs []*Transaction) {
- // Separate the transactions by account and sort by nonce
- byNonce := make(map[common.Address][]*Transaction)
- for _, tx := range txs {
- acc, _ := tx.From() // we only sort valid txs so this cannot fail
- byNonce[acc] = append(byNonce[acc], tx)
+// Note, the input map is reowned so the caller should not interact any more with
+// if after providng it to the constructor.
+func NewTransactionsByPriceAndNonce(txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
+ // Initialize a price based heap with the head transactions
+ heads := make(TxByPrice, 0, len(txs))
+ for acc, accTxs := range txs {
+ heads = append(heads, accTxs[0])
+ txs[acc] = accTxs[1:]
}
- for _, accTxs := range byNonce {
- sort.Sort(TxByNonce(accTxs))
+ heap.Init(&heads)
+
+ // Assemble and return the transaction set
+ return &TransactionsByPriceAndNonce{
+ txs: txs,
+ heads: heads,
}
- // Initialize a price based heap with the head transactions
- byPrice := make(TxByPrice, 0, len(byNonce))
- for acc, accTxs := range byNonce {
- byPrice = append(byPrice, accTxs[0])
- byNonce[acc] = accTxs[1:]
+}
+
+// Peek returns the next transaction by price.
+func (t *TransactionsByPriceAndNonce) Peek() *Transaction {
+ if len(t.heads) == 0 {
+ return nil
}
- heap.Init(&byPrice)
-
- // Merge by replacing the best with the next from the same account
- txs = txs[:0]
- for len(byPrice) > 0 {
- // Retrieve the next best transaction by price
- best := heap.Pop(&byPrice).(*Transaction)
-
- // Push in its place the next transaction from the same account
- acc, _ := best.From() // we only sort valid txs so this cannot fail
- if accTxs, ok := byNonce[acc]; ok && len(accTxs) > 0 {
- heap.Push(&byPrice, accTxs[0])
- byNonce[acc] = accTxs[1:]
- }
- // Accumulate the best priced transaction
- txs = append(txs, best)
+ return t.heads[0]
+}
+
+// Shift replaces the current best head with the next one from the same account.
+func (t *TransactionsByPriceAndNonce) Shift() {
+ acc, _ := t.heads[0].From() // we only sort valid txs so this cannot fail
+
+ if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
+ t.heads[0], t.txs[acc] = txs[0], txs[1:]
+ heap.Fix(&t.heads, 0)
+ } else {
+ heap.Pop(&t.heads)
}
}
+
+// Pop removes the best transaction, *not* replacing it with the next one from
+// the same account. This should be used when a transaction cannot be executed
+// and hence all subsequent ones should be discarded from the same account.
+func (t *TransactionsByPriceAndNonce) Pop() {
+ heap.Pop(&t.heads)
+}
diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go
index 62420e71f..8b0b02c3e 100644
--- a/core/types/transaction_test.go
+++ b/core/types/transaction_test.go
@@ -128,15 +128,25 @@ func TestTransactionPriceNonceSort(t *testing.T) {
keys[i], _ = crypto.GenerateKey()
}
// Generate a batch of transactions with overlapping values, but shifted nonces
- txs := []*Transaction{}
+ groups := map[common.Address]Transactions{}
for start, key := range keys {
+ addr := crypto.PubkeyToAddress(key.PublicKey)
for i := 0; i < 25; i++ {
tx, _ := NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(int64(start+i)), nil).SignECDSA(key)
- txs = append(txs, tx)
+ groups[addr] = append(groups[addr], tx)
}
}
// Sort the transactions and cross check the nonce ordering
- SortByPriceAndNonce(txs)
+ txset := NewTransactionsByPriceAndNonce(groups)
+
+ txs := Transactions{}
+ for {
+ if tx := txset.Peek(); tx != nil {
+ txs = append(txs, tx)
+ txset.Shift()
+ }
+ break
+ }
for i, txi := range txs {
fromi, _ := txi.From()
diff --git a/core/vm/contracts.go b/core/vm/contracts.go
index 5cc9f903b..b45f14724 100644
--- a/core/vm/contracts.go
+++ b/core/vm/contracts.go
@@ -95,7 +95,7 @@ func ecrecoverFunc(in []byte) []byte {
// tighter sig s values in homestead only apply to tx sigs
if !crypto.ValidateSignatureValues(v, r, s, false) {
- glog.V(logger.Debug).Infof("EC RECOVER FAIL: v, r or s value invalid")
+ glog.V(logger.Detail).Infof("ECRECOVER error: v, r or s value invalid")
return nil
}
@@ -106,7 +106,7 @@ func ecrecoverFunc(in []byte) []byte {
pubKey, err := crypto.Ecrecover(in[:32], rsv)
// make sure the public key is a valid one
if err != nil {
- glog.V(logger.Error).Infof("EC RECOVER FAIL: ", err)
+ glog.V(logger.Detail).Infoln("ECRECOVER error: ", err)
return nil
}
diff --git a/core/vm/environment.go b/core/vm/environment.go
index 664887454..747627565 100644
--- a/core/vm/environment.go
+++ b/core/vm/environment.go
@@ -73,8 +73,6 @@ type Environment interface {
DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error)
// Create a new contract
Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
-
- StructLogs() []StructLog
}
// Vm is the basic interface for an implementation of the EVM.
diff --git a/core/vm/gas.go b/core/vm/gas.go
index 09feddd7d..eb2c16346 100644
--- a/core/vm/gas.go
+++ b/core/vm/gas.go
@@ -38,7 +38,7 @@ var (
)
// baseCheck checks for any stack error underflows
-func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
+func baseCheck(op OpCode, stack *Stack, gas *big.Int) error {
// PUSH and DUP are a bit special. They all cost the same but we do want to have checking on stack push limit
// PUSH is also allowed to calculate the same price for all PUSHes
// DUP requirements are handled elsewhere (except for the stack limit check)
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
index e2fc5ee0f..a95ba26c5 100644
--- a/core/vm/instructions.go
+++ b/core/vm/instructions.go
@@ -27,14 +27,14 @@ import (
type programInstruction interface {
// executes the program instruction and allows the instruction to modify the state of the program
- do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error)
+ do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)
// returns whether the program instruction halts the execution of the JIT
halts() bool
// Returns the current op code (debugging purposes)
Op() OpCode
}
-type instrFn func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack)
+type instrFn func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack)
type instruction struct {
op OpCode
@@ -58,7 +58,7 @@ func jump(mapping map[uint64]uint64, destinations map[uint64]struct{}, contract
return mapping[to.Uint64()], nil
}
-func (instr instruction) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) {
+func (instr instruction) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// calculate the new memory size and gas price for the current executing opcode
newMemSize, cost, err := jitCalculateGasAndSize(env, contract, instr, env.Db(), memory, stack)
if err != nil {
@@ -114,26 +114,26 @@ func (instr instruction) Op() OpCode {
return instr.op
}
-func opStaticJump(instr instruction, pc *uint64, ret *big.Int, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opStaticJump(instr instruction, pc *uint64, ret *big.Int, env Environment, contract *Contract, memory *Memory, stack *Stack) {
ret.Set(instr.data)
}
-func opAdd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opAdd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(U256(x.Add(x, y)))
}
-func opSub(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSub(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(U256(x.Sub(x, y)))
}
-func opMul(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMul(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(U256(x.Mul(x, y)))
}
-func opDiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opDiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
if y.Cmp(common.Big0) != 0 {
stack.push(U256(x.Div(x, y)))
@@ -142,7 +142,7 @@ func opDiv(instr instruction, pc *uint64, env Environment, contract *Contract, m
}
}
-func opSdiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSdiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := S256(stack.pop()), S256(stack.pop())
if y.Cmp(common.Big0) == 0 {
stack.push(new(big.Int))
@@ -162,7 +162,7 @@ func opSdiv(instr instruction, pc *uint64, env Environment, contract *Contract,
}
}
-func opMod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
if y.Cmp(common.Big0) == 0 {
stack.push(new(big.Int))
@@ -171,7 +171,7 @@ func opMod(instr instruction, pc *uint64, env Environment, contract *Contract, m
}
}
-func opSmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := S256(stack.pop()), S256(stack.pop())
if y.Cmp(common.Big0) == 0 {
@@ -191,12 +191,12 @@ func opSmod(instr instruction, pc *uint64, env Environment, contract *Contract,
}
}
-func opExp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opExp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(U256(x.Exp(x, y, Pow256)))
}
-func opSignExtend(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSignExtend(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
back := stack.pop()
if back.Cmp(big.NewInt(31)) < 0 {
bit := uint(back.Uint64()*8 + 7)
@@ -213,12 +213,12 @@ func opSignExtend(instr instruction, pc *uint64, env Environment, contract *Cont
}
}
-func opNot(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opNot(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x := stack.pop()
stack.push(U256(x.Not(x)))
}
-func opLt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opLt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
if x.Cmp(y) < 0 {
stack.push(big.NewInt(1))
@@ -227,7 +227,7 @@ func opLt(instr instruction, pc *uint64, env Environment, contract *Contract, me
}
}
-func opGt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opGt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
if x.Cmp(y) > 0 {
stack.push(big.NewInt(1))
@@ -236,7 +236,7 @@ func opGt(instr instruction, pc *uint64, env Environment, contract *Contract, me
}
}
-func opSlt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSlt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := S256(stack.pop()), S256(stack.pop())
if x.Cmp(S256(y)) < 0 {
stack.push(big.NewInt(1))
@@ -245,7 +245,7 @@ func opSlt(instr instruction, pc *uint64, env Environment, contract *Contract, m
}
}
-func opSgt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSgt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := S256(stack.pop()), S256(stack.pop())
if x.Cmp(y) > 0 {
stack.push(big.NewInt(1))
@@ -254,7 +254,7 @@ func opSgt(instr instruction, pc *uint64, env Environment, contract *Contract, m
}
}
-func opEq(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opEq(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
if x.Cmp(y) == 0 {
stack.push(big.NewInt(1))
@@ -263,7 +263,7 @@ func opEq(instr instruction, pc *uint64, env Environment, contract *Contract, me
}
}
-func opIszero(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opIszero(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x := stack.pop()
if x.Cmp(common.Big0) > 0 {
stack.push(new(big.Int))
@@ -272,19 +272,19 @@ func opIszero(instr instruction, pc *uint64, env Environment, contract *Contract
}
}
-func opAnd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opAnd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(x.And(x, y))
}
-func opOr(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opOr(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(x.Or(x, y))
}
-func opXor(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opXor(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y := stack.pop(), stack.pop()
stack.push(x.Xor(x, y))
}
-func opByte(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opByte(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
th, val := stack.pop(), stack.pop()
if th.Cmp(big.NewInt(32)) < 0 {
byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
@@ -293,7 +293,7 @@ func opByte(instr instruction, pc *uint64, env Environment, contract *Contract,
stack.push(new(big.Int))
}
}
-func opAddmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opAddmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y, z := stack.pop(), stack.pop(), stack.pop()
if z.Cmp(Zero) > 0 {
add := x.Add(x, y)
@@ -303,7 +303,7 @@ func opAddmod(instr instruction, pc *uint64, env Environment, contract *Contract
stack.push(new(big.Int))
}
}
-func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
x, y, z := stack.pop(), stack.pop(), stack.pop()
if z.Cmp(Zero) > 0 {
mul := x.Mul(x, y)
@@ -314,45 +314,45 @@ func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract
}
}
-func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
offset, size := stack.pop(), stack.pop()
hash := crypto.Keccak256(memory.Get(offset.Int64(), size.Int64()))
stack.push(common.BytesToBig(hash))
}
-func opAddress(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opAddress(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(common.Bytes2Big(contract.Address().Bytes()))
}
-func opBalance(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opBalance(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
addr := common.BigToAddress(stack.pop())
balance := env.Db().GetBalance(addr)
stack.push(new(big.Int).Set(balance))
}
-func opOrigin(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opOrigin(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(env.Origin().Big())
}
-func opCaller(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCaller(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(contract.Caller().Big())
}
-func opCallValue(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCallValue(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(new(big.Int).Set(contract.value))
}
-func opCalldataLoad(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCalldataLoad(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(common.Bytes2Big(getData(contract.Input, stack.pop(), common.Big32)))
}
-func opCalldataSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCalldataSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(big.NewInt(int64(len(contract.Input))))
}
-func opCalldataCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCalldataCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
var (
mOff = stack.pop()
cOff = stack.pop()
@@ -361,18 +361,18 @@ func opCalldataCopy(instr instruction, pc *uint64, env Environment, contract *Co
memory.Set(mOff.Uint64(), l.Uint64(), getData(contract.Input, cOff, l))
}
-func opExtCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opExtCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
addr := common.BigToAddress(stack.pop())
l := big.NewInt(int64(len(env.Db().GetCode(addr))))
stack.push(l)
}
-func opCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
l := big.NewInt(int64(len(contract.Code)))
stack.push(l)
}
-func opCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
var (
mOff = stack.pop()
cOff = stack.pop()
@@ -383,7 +383,7 @@ func opCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contra
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
}
-func opExtCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opExtCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
var (
addr = common.BigToAddress(stack.pop())
mOff = stack.pop()
@@ -395,11 +395,11 @@ func opExtCodeCopy(instr instruction, pc *uint64, env Environment, contract *Con
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
}
-func opGasprice(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opGasprice(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(new(big.Int).Set(contract.Price))
}
-func opBlockhash(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opBlockhash(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
num := stack.pop()
n := new(big.Int).Sub(env.BlockNumber(), common.Big257)
@@ -410,43 +410,43 @@ func opBlockhash(instr instruction, pc *uint64, env Environment, contract *Contr
}
}
-func opCoinbase(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCoinbase(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(env.Coinbase().Big())
}
-func opTimestamp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opTimestamp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(U256(new(big.Int).Set(env.Time())))
}
-func opNumber(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opNumber(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(U256(new(big.Int).Set(env.BlockNumber())))
}
-func opDifficulty(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opDifficulty(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(U256(new(big.Int).Set(env.Difficulty())))
}
-func opGasLimit(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opGasLimit(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(U256(new(big.Int).Set(env.GasLimit())))
}
-func opPop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opPop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.pop()
}
-func opPush(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opPush(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(new(big.Int).Set(instr.data))
}
-func opDup(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opDup(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.dup(int(instr.data.Int64()))
}
-func opSwap(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSwap(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.swap(int(instr.data.Int64()))
}
-func opLog(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opLog(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
n := int(instr.data.Int64())
topics := make([]common.Hash, n)
mStart, mSize := stack.pop(), stack.pop()
@@ -459,55 +459,55 @@ func opLog(instr instruction, pc *uint64, env Environment, contract *Contract, m
env.AddLog(log)
}
-func opMload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
offset := stack.pop()
val := common.BigD(memory.Get(offset.Int64(), 32))
stack.push(val)
}
-func opMstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
// pop value of the stack
mStart, val := stack.pop(), stack.pop()
memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256))
}
-func opMstore8(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMstore8(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
off, val := stack.pop().Int64(), stack.pop().Int64()
memory.store[off] = byte(val & 0xff)
}
-func opSload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
loc := common.BigToHash(stack.pop())
val := env.Db().GetState(contract.Address(), loc).Big()
stack.push(val)
}
-func opSstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
loc := common.BigToHash(stack.pop())
val := stack.pop()
env.Db().SetState(contract.Address(), loc, common.BigToHash(val))
}
-func opJump(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opJump(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
}
-func opJumpi(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opJumpi(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
}
-func opJumpdest(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opJumpdest(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
}
-func opPc(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opPc(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(new(big.Int).Set(instr.data))
}
-func opMsize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opMsize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(big.NewInt(int64(memory.Len())))
}
-func opGas(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opGas(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.push(new(big.Int).Set(contract.Gas))
}
-func opCreate(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCreate(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
var (
value = stack.pop()
offset, size = stack.pop(), stack.pop()
@@ -529,7 +529,7 @@ func opCreate(instr instruction, pc *uint64, env Environment, contract *Contract
}
}
-func opCall(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCall(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
gas := stack.pop()
// pop gas and value of the stack.
addr, value := stack.pop(), stack.pop()
@@ -560,7 +560,7 @@ func opCall(instr instruction, pc *uint64, env Environment, contract *Contract,
}
}
-func opCallCode(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opCallCode(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
gas := stack.pop()
// pop gas and value of the stack.
addr, value := stack.pop(), stack.pop()
@@ -591,7 +591,7 @@ func opCallCode(instr instruction, pc *uint64, env Environment, contract *Contra
}
}
-func opDelegateCall(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opDelegateCall(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
gas, to, inOffset, inSize, outOffset, outSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
toAddr := common.BigToAddress(to)
@@ -605,12 +605,12 @@ func opDelegateCall(instr instruction, pc *uint64, env Environment, contract *Co
}
}
-func opReturn(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opReturn(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
}
-func opStop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opStop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
}
-func opSuicide(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+func opSuicide(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
balance := env.Db().GetBalance(contract.Address())
env.Db().AddBalance(common.BigToAddress(stack.pop()), balance)
@@ -621,7 +621,7 @@ func opSuicide(instr instruction, pc *uint64, env Environment, contract *Contrac
// make log instruction function
func makeLog(size int) instrFn {
- return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+ return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
topics := make([]common.Hash, size)
mStart, mSize := stack.pop(), stack.pop()
for i := 0; i < size; i++ {
@@ -636,7 +636,7 @@ func makeLog(size int) instrFn {
// make push instruction function
func makePush(size uint64, bsize *big.Int) instrFn {
- return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+ return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
byts := getData(contract.Code, new(big.Int).SetUint64(*pc+1), bsize)
stack.push(common.Bytes2Big(byts))
*pc += size
@@ -645,7 +645,7 @@ func makePush(size uint64, bsize *big.Int) instrFn {
// make push instruction function
func makeDup(size int64) instrFn {
- return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+ return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.dup(int(size))
}
}
@@ -654,7 +654,7 @@ func makeDup(size int64) instrFn {
func makeSwap(size int64) instrFn {
// switch n + 1 otherwise n would be swapped with n
size += 1
- return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
+ return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
stack.swap(int(size))
}
}
diff --git a/core/vm/jit.go b/core/vm/jit.go
index f56d7c1af..460a68ddd 100644
--- a/core/vm/jit.go
+++ b/core/vm/jit.go
@@ -303,7 +303,7 @@ func RunProgram(program *Program, env Environment, contract *Contract, input []b
return runProgram(program, 0, NewMemory(), newstack(), env, contract, input)
}
-func runProgram(program *Program, pcstart uint64, mem *Memory, stack *stack, env Environment, contract *Contract, input []byte) ([]byte, error) {
+func runProgram(program *Program, pcstart uint64, mem *Memory, stack *Stack, env Environment, contract *Contract, input []byte) ([]byte, error) {
contract.Input = input
var (
@@ -357,7 +357,7 @@ func validDest(dests map[uint64]struct{}, dest *big.Int) bool {
// jitCalculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
// the operation. This does not reduce gas or resizes the memory.
-func jitCalculateGasAndSize(env Environment, contract *Contract, instr instruction, statedb Database, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
+func jitCalculateGasAndSize(env Environment, contract *Contract, instr instruction, statedb Database, mem *Memory, stack *Stack) (*big.Int, *big.Int, error) {
var (
gas = new(big.Int)
newMemSize *big.Int = new(big.Int)
@@ -421,7 +421,7 @@ func jitCalculateGasAndSize(env Environment, contract *Contract, instr instructi
g = params.SstoreClearGas
} else {
- g = params.SstoreClearGas
+ g = params.SstoreResetGas
}
gas.Set(g)
case SUICIDE:
@@ -491,7 +491,7 @@ func jitCalculateGasAndSize(env Environment, contract *Contract, instr instructi
// jitBaseCheck is the same as baseCheck except it doesn't do the look up in the
// gas table. This is done during compilation instead.
-func jitBaseCheck(instr instruction, stack *stack, gas *big.Int) error {
+func jitBaseCheck(instr instruction, stack *Stack, gas *big.Int) error {
err := stack.require(instr.spop)
if err != nil {
return err
diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go
index 403c15a8d..809abfea9 100644
--- a/core/vm/jit_test.go
+++ b/core/vm/jit_test.go
@@ -85,7 +85,7 @@ func TestCompiling(t *testing.T) {
func TestResetInput(t *testing.T) {
var sender account
- env := NewEnv(false, true)
+ env := NewEnv(&Config{EnableJit: true, ForceJit: true})
contract := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
contract.CodeAddr = &common.Address{}
@@ -144,7 +144,7 @@ func runVmBench(test vmBench, b *testing.B) {
if test.precompile && !test.forcejit {
NewProgram(test.code)
}
- env := NewEnv(test.nojit, test.forcejit)
+ env := NewEnv(&Config{EnableJit: !test.nojit, ForceJit: test.forcejit})
b.ResetTimer()
@@ -166,12 +166,9 @@ type Env struct {
evm *EVM
}
-func NewEnv(noJit, forceJit bool) *Env {
+func NewEnv(config *Config) *Env {
env := &Env{gasLimit: big.NewInt(10000), depth: 0}
- env.evm = New(env, Config{
- EnableJit: !noJit,
- ForceJit: forceJit,
- })
+ env.evm = New(env, *config)
return env
}
@@ -179,11 +176,6 @@ func (self *Env) RuleSet() RuleSet { return ruleSet{new(big.Int)} }
func (self *Env) Vm() Vm { return self.evm }
func (self *Env) Origin() common.Address { return common.Address{} }
func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
-func (self *Env) AddStructLog(log StructLog) {
-}
-func (self *Env) StructLogs() []StructLog {
- return nil
-}
//func (self *Env) PrevHash() []byte { return self.parent }
func (self *Env) Coinbase() common.Address { return common.Address{} }
diff --git a/core/vm/log.go b/core/vm/log.go
index e4cc6021b..b292f5f43 100644
--- a/core/vm/log.go
+++ b/core/vm/log.go
@@ -18,6 +18,7 @@ package vm
import (
"encoding/json"
+ "errors"
"fmt"
"io"
@@ -25,18 +26,33 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
+var errMissingLogFields = errors.New("missing required JSON log fields")
+
+// Log represents a contract log event. These events are generated by the LOG
+// opcode and stored/indexed by the node.
type Log struct {
- // Consensus fields
- Address common.Address
- Topics []common.Hash
- Data []byte
+ // Consensus fields.
+ Address common.Address // address of the contract that generated the event
+ Topics []common.Hash // list of topics provided by the contract.
+ Data []byte // supplied by the contract, usually ABI-encoded
+
+ // Derived fields (don't reorder!).
+ BlockNumber uint64 // block in which the transaction was included
+ TxHash common.Hash // hash of the transaction
+ TxIndex uint // index of the transaction in the block
+ BlockHash common.Hash // hash of the block in which the transaction was included
+ Index uint // index of the log in the receipt
+}
- // Derived fields (don't reorder!)
- BlockNumber uint64
- TxHash common.Hash
- TxIndex uint
- BlockHash common.Hash
- Index uint
+type jsonLog struct {
+ Address *common.Address `json:"address"`
+ Topics *[]common.Hash `json:"topics"`
+ Data string `json:"data"`
+ BlockNumber string `json:"blockNumber"`
+ TxIndex string `json:"transactionIndex"`
+ TxHash *common.Hash `json:"transactionHash"`
+ BlockHash *common.Hash `json:"blockHash"`
+ Index string `json:"logIndex"`
}
func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
@@ -64,19 +80,50 @@ func (l *Log) String() string {
return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, l.Address, l.Topics, l.Data, l.TxHash, l.TxIndex, l.BlockHash, l.Index)
}
+// MarshalJSON implements json.Marshaler.
func (r *Log) MarshalJSON() ([]byte, error) {
- fields := map[string]interface{}{
- "address": r.Address,
- "data": fmt.Sprintf("%#x", r.Data),
- "blockNumber": fmt.Sprintf("%#x", r.BlockNumber),
- "logIndex": fmt.Sprintf("%#x", r.Index),
- "blockHash": r.BlockHash,
- "transactionHash": r.TxHash,
- "transactionIndex": fmt.Sprintf("%#x", r.TxIndex),
- "topics": r.Topics,
- }
+ return json.Marshal(&jsonLog{
+ Address: &r.Address,
+ Topics: &r.Topics,
+ Data: fmt.Sprintf("0x%x", r.Data),
+ BlockNumber: fmt.Sprintf("0x%x", r.BlockNumber),
+ TxIndex: fmt.Sprintf("0x%x", r.TxIndex),
+ TxHash: &r.TxHash,
+ BlockHash: &r.BlockHash,
+ Index: fmt.Sprintf("0x%x", r.Index),
+ })
+}
- return json.Marshal(fields)
+// UnmarshalJSON implements json.Umarshaler.
+func (r *Log) UnmarshalJSON(input []byte) error {
+ var dec jsonLog
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.Address == nil || dec.Topics == nil || dec.Data == "" || dec.BlockNumber == "" ||
+ dec.TxIndex == "" || dec.TxHash == nil || dec.BlockHash == nil || dec.Index == "" {
+ return errMissingLogFields
+ }
+ declog := Log{
+ Address: *dec.Address,
+ Topics: *dec.Topics,
+ TxHash: *dec.TxHash,
+ BlockHash: *dec.BlockHash,
+ }
+ if _, err := fmt.Sscanf(dec.Data, "0x%x", &declog.Data); err != nil {
+ return fmt.Errorf("invalid hex log data")
+ }
+ if _, err := fmt.Sscanf(dec.BlockNumber, "0x%x", &declog.BlockNumber); err != nil {
+ return fmt.Errorf("invalid hex log block number")
+ }
+ if _, err := fmt.Sscanf(dec.TxIndex, "0x%x", &declog.TxIndex); err != nil {
+ return fmt.Errorf("invalid hex log tx index")
+ }
+ if _, err := fmt.Sscanf(dec.Index, "0x%x", &declog.Index); err != nil {
+ return fmt.Errorf("invalid hex log index")
+ }
+ *r = declog
+ return nil
}
type Logs []*Log
diff --git a/core/vm/log_test.go b/core/vm/log_test.go
new file mode 100644
index 000000000..775016f9c
--- /dev/null
+++ b/core/vm/log_test.go
@@ -0,0 +1,59 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+var unmarshalLogTests = map[string]struct {
+ input string
+ wantError error
+}{
+ "ok": {
+ input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
+ },
+ "missing data": {
+ input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
+ wantError: errMissingLogFields,
+ },
+}
+
+func TestUnmarshalLog(t *testing.T) {
+ for name, test := range unmarshalLogTests {
+ var log *Log
+ err := json.Unmarshal([]byte(test.input), &log)
+ checkError(t, name, err, test.wantError)
+ }
+}
+
+func checkError(t *testing.T, testname string, got, want error) bool {
+ if got == nil {
+ if want != nil {
+ t.Errorf("test %q: got no error, want %q", testname, want)
+ return false
+ }
+ return true
+ }
+ if want == nil {
+ t.Errorf("test %q: unexpected error %q", testname, got)
+ } else if got.Error() != want.Error() {
+ t.Errorf("test %q: got error %q, want %q", testname, got, want)
+ }
+ return false
+}
diff --git a/core/vm/logger.go b/core/vm/logger.go
index cbdc8a744..ae62b6b57 100644
--- a/core/vm/logger.go
+++ b/core/vm/logger.go
@@ -36,19 +36,12 @@ func (self Storage) Copy() Storage {
return cpy
}
-// StructLogCollector is the basic interface to capture emited logs by the EVM logger.
-type StructLogCollector interface {
- // Adds the structured log to the collector.
- AddStructLog(StructLog)
-}
-
// LogConfig are the configuration options for structured logger the EVM
type LogConfig struct {
- DisableMemory bool // disable memory capture
- DisableStack bool // disable stack capture
- DisableStorage bool // disable storage capture
- FullStorage bool // show full storage (slow)
- Collector StructLogCollector // the log collector
+ DisableMemory bool // disable memory capture
+ DisableStack bool // disable stack capture
+ DisableStorage bool // disable storage capture
+ FullStorage bool // show full storage (slow)
}
// StructLog is emitted to the Environment each cycle and lists information about the current internal state
@@ -65,36 +58,42 @@ type StructLog struct {
Err error
}
-// Logger is an EVM state logger and implements VmLogger.
+// Tracer is used to collect execution traces from an EVM transaction
+// execution. CaptureState is called for each step of the VM with the
+// current VM state.
+// Note that reference types are actual VM data structures; make copies
+// if you need to retain them beyond the current call.
+type Tracer interface {
+ CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error)
+}
+
+// StructLogger is an EVM state logger and implements Tracer.
//
-// Logger can capture state based on the given Log configuration and also keeps
+// StructLogger can capture state based on the given Log configuration and also keeps
// a track record of modified storage which is used in reporting snapshots of the
// contract their storage.
-type Logger struct {
+type StructLogger struct {
cfg LogConfig
- env Environment
+ logs []StructLog
changedValues map[common.Address]Storage
}
-// newLogger returns a new logger
-func newLogger(cfg LogConfig, env Environment) *Logger {
- return &Logger{
- cfg: cfg,
- env: env,
+// NewLogger returns a new logger
+func NewStructLogger(cfg *LogConfig) *StructLogger {
+ logger := &StructLogger{
changedValues: make(map[common.Address]Storage),
}
+ if cfg != nil {
+ logger.cfg = *cfg
+ }
+ return logger
}
// captureState logs a new structured log message and pushes it out to the environment
//
// captureState also tracks SSTORE ops to track dirty values.
-func (l *Logger) captureState(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, contract *Contract, depth int, err error) {
- // short circuit if no log collector is present
- if l.cfg.Collector == nil {
- return
- }
-
+func (l *StructLogger) CaptureState(env Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) {
// initialise new changed values storage container for this contract
// if not present.
if l.changedValues[contract.Address()] == nil {
@@ -139,7 +138,7 @@ func (l *Logger) captureState(pc uint64, op OpCode, gas, cost *big.Int, memory *
storage = make(Storage)
// Get the contract account and loop over each storage entry. This may involve looping over
// the trie and is a very expensive process.
- l.env.Db().GetAccount(contract.Address()).ForEachStorage(func(key, value common.Hash) bool {
+ env.Db().GetAccount(contract.Address()).ForEachStorage(func(key, value common.Hash) bool {
storage[key] = value
// Return true, indicating we'd like to continue.
return true
@@ -150,9 +149,14 @@ func (l *Logger) captureState(pc uint64, op OpCode, gas, cost *big.Int, memory *
}
}
// create a new snaptshot of the EVM.
- log := StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, l.env.Depth(), err}
- // Add the log to the collector
- l.cfg.Collector.AddStructLog(log)
+ log := StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, env.Depth(), err}
+
+ l.logs = append(l.logs, log)
+}
+
+// StructLogs returns a list of captured log entries
+func (l *StructLogger) StructLogs() []StructLog {
+ return l.logs
}
// StdErrFormat formats a slice of StructLogs to human readable format
diff --git a/core/vm/logger_test.go b/core/vm/logger_test.go
index 144569865..e85bca227 100644
--- a/core/vm/logger_test.go
+++ b/core/vm/logger_test.go
@@ -47,19 +47,18 @@ type dummyEnv struct {
func newDummyEnv(ref *dummyContractRef) *dummyEnv {
return &dummyEnv{
- Env: NewEnv(true, false),
+ Env: NewEnv(&Config{EnableJit: false, ForceJit: false}),
ref: ref,
}
}
func (d dummyEnv) GetAccount(common.Address) Account {
return d.ref
}
-func (d dummyEnv) AddStructLog(StructLog) {}
func TestStoreCapture(t *testing.T) {
var (
- env = NewEnv(true, false)
- logger = newLogger(LogConfig{Collector: env}, env)
+ env = NewEnv(&Config{EnableJit: false, ForceJit: false})
+ logger = NewStructLogger(nil)
mem = NewMemory()
stack = newstack()
contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), new(big.Int), new(big.Int))
@@ -69,7 +68,7 @@ func TestStoreCapture(t *testing.T) {
var index common.Hash
- logger.captureState(0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
+ logger.CaptureState(env, 0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
if len(logger.changedValues[contract.Address()]) == 0 {
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
}
@@ -86,18 +85,18 @@ func TestStorageCapture(t *testing.T) {
ref = &dummyContractRef{}
contract = NewContract(ref, ref, new(big.Int), new(big.Int), new(big.Int))
env = newDummyEnv(ref)
- logger = newLogger(LogConfig{Collector: env}, env)
+ logger = NewStructLogger(nil)
mem = NewMemory()
stack = newstack()
)
- logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
+ logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
if ref.calledForEach {
t.Error("didn't expect for each to be called")
}
- logger = newLogger(LogConfig{Collector: env, FullStorage: true}, env)
- logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
+ logger = NewStructLogger(&LogConfig{FullStorage: true})
+ logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
if !ref.calledForEach {
t.Error("expected for each to be called")
}
diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go
index d8c98e545..a4793c98f 100644
--- a/core/vm/runtime/env.go
+++ b/core/vm/runtime/env.go
@@ -39,8 +39,6 @@ type Env struct {
difficulty *big.Int
gasLimit *big.Int
- logs []vm.StructLog
-
getHashFn func(uint64) common.Hash
evm *vm.EVM
@@ -62,23 +60,11 @@ func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
Debug: cfg.Debug,
EnableJit: !cfg.DisableJit,
ForceJit: !cfg.DisableJit,
-
- Logger: vm.LogConfig{
- Collector: env,
- },
})
return env
}
-func (self *Env) StructLogs() []vm.StructLog {
- return self.logs
-}
-
-func (self *Env) AddStructLog(log vm.StructLog) {
- self.logs = append(self.logs, log)
-}
-
func (self *Env) RuleSet() vm.RuleSet { return self.ruleSet }
func (self *Env) Vm() vm.Vm { return self.evm }
func (self *Env) Origin() common.Address { return self.origin }
diff --git a/core/vm/segments.go b/core/vm/segments.go
index c69ceddf4..648d8a04a 100644
--- a/core/vm/segments.go
+++ b/core/vm/segments.go
@@ -24,7 +24,7 @@ type jumpSeg struct {
gas *big.Int
}
-func (j jumpSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) {
+func (j jumpSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
if !contract.UseGas(j.gas) {
return nil, OutOfGasError
}
@@ -42,7 +42,7 @@ type pushSeg struct {
gas *big.Int
}
-func (s pushSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) {
+func (s pushSeg) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Use the calculated gas. When insufficient gas is present, use all gas and return an
// Out Of Gas error
if !contract.UseGas(s.gas) {
diff --git a/core/vm/stack.go b/core/vm/stack.go
index 0046edec2..f6c1f76e4 100644
--- a/core/vm/stack.go
+++ b/core/vm/stack.go
@@ -24,58 +24,58 @@ import (
// stack is an object for basic stack operations. Items popped to the stack are
// expected to be changed and modified. stack does not take care of adding newly
// initialised objects.
-type stack struct {
+type Stack struct {
data []*big.Int
}
-func newstack() *stack {
- return &stack{}
+func newstack() *Stack {
+ return &Stack{}
}
-func (st *stack) Data() []*big.Int {
+func (st *Stack) Data() []*big.Int {
return st.data
}
-func (st *stack) push(d *big.Int) {
+func (st *Stack) push(d *big.Int) {
// NOTE push limit (1024) is checked in baseCheck
//stackItem := new(big.Int).Set(d)
//st.data = append(st.data, stackItem)
st.data = append(st.data, d)
}
-func (st *stack) pushN(ds ...*big.Int) {
+func (st *Stack) pushN(ds ...*big.Int) {
st.data = append(st.data, ds...)
}
-func (st *stack) pop() (ret *big.Int) {
+func (st *Stack) pop() (ret *big.Int) {
ret = st.data[len(st.data)-1]
st.data = st.data[:len(st.data)-1]
return
}
-func (st *stack) len() int {
+func (st *Stack) len() int {
return len(st.data)
}
-func (st *stack) swap(n int) {
+func (st *Stack) swap(n int) {
st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
}
-func (st *stack) dup(n int) {
+func (st *Stack) dup(n int) {
st.push(new(big.Int).Set(st.data[st.len()-n]))
}
-func (st *stack) peek() *big.Int {
+func (st *Stack) peek() *big.Int {
return st.data[st.len()-1]
}
-func (st *stack) require(n int) error {
+func (st *Stack) require(n int) error {
if st.len() < n {
return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n)
}
return nil
}
-func (st *stack) Print() {
+func (st *Stack) Print() {
fmt.Println("### stack ###")
if len(st.data) > 0 {
for i, val := range st.data {
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 0f93715d6..9d7b55058 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -33,7 +33,7 @@ type Config struct {
Debug bool
EnableJit bool
ForceJit bool
- Logger LogConfig
+ Tracer Tracer
}
// EVM is used to run Ethereum based contracts and will utilise the
@@ -44,22 +44,14 @@ type EVM struct {
env Environment
jumpTable vmJumpTable
cfg Config
-
- logger *Logger
}
// New returns a new instance of the EVM.
func New(env Environment, cfg Config) *EVM {
- var logger *Logger
- if cfg.Debug {
- logger = newLogger(cfg.Logger, env)
- }
-
return &EVM{
env: env,
jumpTable: newJumpTable(env.RuleSet(), env.BlockNumber()),
cfg: cfg,
- logger: logger,
}
}
@@ -149,7 +141,7 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
defer func() {
if err != nil && evm.cfg.Debug {
- evm.logger.captureState(pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), err)
+ evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), err)
}
}()
@@ -191,7 +183,7 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
mem.Resize(newMemSize.Uint64())
// Add a log message
if evm.cfg.Debug {
- evm.logger.captureState(pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
+ evm.cfg.Tracer.CaptureState(evm.env, pc, op, contract.Gas, cost, mem, stack, contract, evm.env.Depth(), nil)
}
if opPtr := evm.jumpTable[op]; opPtr.valid {
@@ -241,7 +233,7 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
// calculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
// the operation. This does not reduce gas or resizes the memory.
-func calculateGasAndSize(env Environment, contract *Contract, caller ContractRef, op OpCode, statedb Database, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
+func calculateGasAndSize(env Environment, contract *Contract, caller ContractRef, op OpCode, statedb Database, mem *Memory, stack *Stack) (*big.Int, *big.Int, error) {
var (
gas = new(big.Int)
newMemSize *big.Int = new(big.Int)
@@ -306,7 +298,7 @@ func calculateGasAndSize(env Environment, contract *Contract, caller ContractRef
g = params.SstoreClearGas
} else {
// non 0 => non 0 (or 0 => 0)
- g = params.SstoreClearGas
+ g = params.SstoreResetGas
}
gas.Set(g)
case SUICIDE:
diff --git a/core/vm_env.go b/core/vm_env.go
index 599672382..e541eaef4 100644
--- a/core/vm_env.go
+++ b/core/vm_env.go
@@ -49,7 +49,6 @@ type VMEnv struct {
header *types.Header // Header information
chain *BlockChain // Blockchain handle
- logs []vm.StructLog // Logs for the custom structured logger
getHashFn func(uint64) common.Hash // getHashFn callback is used to retrieve block hashes
}
@@ -63,11 +62,6 @@ func NewEnv(state *state.StateDB, chainConfig *ChainConfig, chain *BlockChain, m
getHashFn: GetHashFn(header.ParentHash, chain),
}
- // if no log collector is present set self as the collector
- if cfg.Logger.Collector == nil {
- cfg.Logger.Collector = env
- }
-
env.evm = vm.New(env, cfg)
return env
}
@@ -121,11 +115,3 @@ func (self *VMEnv) DelegateCall(me vm.ContractRef, addr common.Address, data []b
func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
return Create(self, me, data, gas, price, value)
}
-
-func (self *VMEnv) StructLogs() []vm.StructLog {
- return self.logs
-}
-
-func (self *VMEnv) AddStructLog(log vm.StructLog) {
- self.logs = append(self.logs, log)
-}