Skip to content

Commit 79f62ea

Browse files
Merge pull request ethereum#23 from ethereum/master
Upstream changes from ethereum/go-ethereum
2 parents 5b27607 + 1a4e687 commit 79f62ea

File tree

261 files changed

+19258
-7074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

261 files changed

+19258
-7074
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Before you do a feature request please check and make sure that it isn't possible
44
through some other means. The JavaScript enabled console is a powerful feature
5-
in the right hands. Please check our [Bitchin' tricks](https://github.com/ethereum/go-ethereum/wiki/bitchin-tricks) wiki page for more info
5+
in the right hands. Please check our [Wiki page](https://github.com/ethereum/go-ethereum/wiki) for more info
66
and help.
77

88
## Contributing

accounts/abi/abi.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
9797
Type string
9898
Name string
9999
Constant bool
100-
Indexed bool
101100
Anonymous bool
102101
Inputs []Argument
103102
Outputs []Argument

accounts/abi/bind/backend.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ type ContractCaller interface {
5252
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
5353
}
5454

55-
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
56-
type DeployBackend interface {
57-
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
58-
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
59-
}
60-
6155
// PendingContractCaller defines methods to perform contract calls on the pending state.
6256
// Call will try to discover this interface when access to the pending state is requested.
6357
// If the backend does not support the pending state, Call returns ErrNoPendingState.
@@ -90,8 +84,29 @@ type ContractTransactor interface {
9084
SendTransaction(ctx context.Context, tx *types.Transaction) error
9185
}
9286

87+
// ContractFilterer defines the methods needed to access log events using one-off
88+
// queries or continuous event subscriptions.
89+
type ContractFilterer interface {
90+
// FilterLogs executes a log filter operation, blocking during execution and
91+
// returning all the results in one batch.
92+
//
93+
// TODO(karalabe): Deprecate when the subscription one can return past data too.
94+
FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
95+
96+
// SubscribeFilterLogs creates a background log filtering operation, returning
97+
// a subscription immediately, which can be used to stream the found events.
98+
SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
99+
}
100+
101+
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
102+
type DeployBackend interface {
103+
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
104+
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
105+
}
106+
93107
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
94108
type ContractBackend interface {
95109
ContractCaller
96110
ContractTransactor
111+
ContractFilterer
97112
}

accounts/abi/bind/backends/simulated.go

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ import (
3030
"github.com/ethereum/go-ethereum/common/math"
3131
"github.com/ethereum/go-ethereum/consensus/ethash"
3232
"github.com/ethereum/go-ethereum/core"
33+
"github.com/ethereum/go-ethereum/core/bloombits"
3334
"github.com/ethereum/go-ethereum/core/state"
3435
"github.com/ethereum/go-ethereum/core/types"
3536
"github.com/ethereum/go-ethereum/core/vm"
37+
"github.com/ethereum/go-ethereum/eth/filters"
3638
"github.com/ethereum/go-ethereum/ethdb"
39+
"github.com/ethereum/go-ethereum/event"
3740
"github.com/ethereum/go-ethereum/params"
41+
"github.com/ethereum/go-ethereum/rpc"
3842
)
3943

4044
// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
@@ -53,6 +57,8 @@ type SimulatedBackend struct {
5357
pendingBlock *types.Block // Currently pending block that will be imported on request
5458
pendingState *state.StateDB // Currently pending state that will be the active on on request
5559

60+
events *filters.EventSystem // Event system for filtering log events live
61+
5662
config *params.ChainConfig
5763
}
5864

@@ -62,8 +68,14 @@ func NewSimulatedBackend(alloc core.GenesisAlloc) *SimulatedBackend {
6268
database, _ := ethdb.NewMemDatabase()
6369
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, Alloc: alloc}
6470
genesis.MustCommit(database)
65-
blockchain, _ := core.NewBlockChain(database, genesis.Config, ethash.NewFaker(), vm.Config{})
66-
backend := &SimulatedBackend{database: database, blockchain: blockchain, config: genesis.Config}
71+
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{})
72+
73+
backend := &SimulatedBackend{
74+
database: database,
75+
blockchain: blockchain,
76+
config: genesis.Config,
77+
events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false),
78+
}
6779
backend.rollback()
6880
return backend
6981
}
@@ -90,8 +102,10 @@ func (b *SimulatedBackend) Rollback() {
90102

91103
func (b *SimulatedBackend) rollback() {
92104
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})
105+
statedb, _ := b.blockchain.State()
106+
93107
b.pendingBlock = blocks[0]
94-
b.pendingState, _ = state.New(b.pendingBlock.Root(), state.NewDatabase(b.database))
108+
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
95109
}
96110

97111
// CodeAt returns the code associated with a certain account in the blockchain.
@@ -248,7 +262,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
248262
return hi, nil
249263
}
250264

251-
// callContract implemens common code between normal and pending contract calls.
265+
// callContract implements common code between normal and pending contract calls.
252266
// state is modified during execution, make sure to copy it if necessary.
253267
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, uint64, bool, error) {
254268
// Ensure message is initialized properly.
@@ -297,12 +311,76 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
297311
}
298312
block.AddTx(tx)
299313
})
314+
statedb, _ := b.blockchain.State()
315+
300316
b.pendingBlock = blocks[0]
301-
b.pendingState, _ = state.New(b.pendingBlock.Root(), state.NewDatabase(b.database))
317+
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
302318
return nil
303319
}
304320

305-
// JumpTimeInSeconds adds skip seconds to the clock
321+
// FilterLogs executes a log filter operation, blocking during execution and
322+
// returning all the results in one batch.
323+
//
324+
// TODO(karalabe): Deprecate when the subscription one can return past data too.
325+
func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
326+
// Initialize unset filter boundaried to run from genesis to chain head
327+
from := int64(0)
328+
if query.FromBlock != nil {
329+
from = query.FromBlock.Int64()
330+
}
331+
to := int64(-1)
332+
if query.ToBlock != nil {
333+
to = query.ToBlock.Int64()
334+
}
335+
// Construct and execute the filter
336+
filter := filters.New(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
337+
338+
logs, err := filter.Logs(ctx)
339+
if err != nil {
340+
return nil, err
341+
}
342+
res := make([]types.Log, len(logs))
343+
for i, log := range logs {
344+
res[i] = *log
345+
}
346+
return res, nil
347+
}
348+
349+
// SubscribeFilterLogs creates a background log filtering operation, returning a
350+
// subscription immediately, which can be used to stream the found events.
351+
func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
352+
// Subscribe to contract events
353+
sink := make(chan []*types.Log)
354+
355+
sub, err := b.events.SubscribeLogs(query, sink)
356+
if err != nil {
357+
return nil, err
358+
}
359+
// Since we're getting logs in batches, we need to flatten them into a plain stream
360+
return event.NewSubscription(func(quit <-chan struct{}) error {
361+
defer sub.Unsubscribe()
362+
for {
363+
select {
364+
case logs := <-sink:
365+
for _, log := range logs {
366+
select {
367+
case ch <- *log:
368+
case err := <-sub.Err():
369+
return err
370+
case <-quit:
371+
return nil
372+
}
373+
}
374+
case err := <-sub.Err():
375+
return err
376+
case <-quit:
377+
return nil
378+
}
379+
}
380+
}), nil
381+
}
382+
383+
// AdjustTime adds a time shift to the simulated clock.
306384
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
307385
b.mu.Lock()
308386
defer b.mu.Unlock()
@@ -312,8 +390,10 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
312390
}
313391
block.OffsetTime(int64(adjustment.Seconds()))
314392
})
393+
statedb, _ := b.blockchain.State()
394+
315395
b.pendingBlock = blocks[0]
316-
b.pendingState, _ = state.New(b.pendingBlock.Root(), state.NewDatabase(b.database))
396+
b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
317397

318398
return nil
319399
}
@@ -331,3 +411,44 @@ func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
331411
func (m callmsg) Gas() uint64 { return m.CallMsg.Gas }
332412
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
333413
func (m callmsg) Data() []byte { return m.CallMsg.Data }
414+
415+
// filterBackend implements filters.Backend to support filtering for logs without
416+
// taking bloom-bits acceleration structures into account.
417+
type filterBackend struct {
418+
db ethdb.Database
419+
bc *core.BlockChain
420+
}
421+
422+
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
423+
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
424+
425+
func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
426+
if block == rpc.LatestBlockNumber {
427+
return fb.bc.CurrentHeader(), nil
428+
}
429+
return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
430+
}
431+
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
432+
return core.GetBlockReceipts(fb.db, hash, core.GetBlockNumber(fb.db, hash)), nil
433+
}
434+
435+
func (fb *filterBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
436+
return event.NewSubscription(func(quit <-chan struct{}) error {
437+
<-quit
438+
return nil
439+
})
440+
}
441+
func (fb *filterBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
442+
return fb.bc.SubscribeChainEvent(ch)
443+
}
444+
func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
445+
return fb.bc.SubscribeRemovedLogsEvent(ch)
446+
}
447+
func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
448+
return fb.bc.SubscribeLogsEvent(ch)
449+
}
450+
451+
func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
452+
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {
453+
panic("not supported")
454+
}

0 commit comments

Comments
 (0)