mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
contracts/release,eth,accounts/abi/bind/backends updated contract code + support subscribe logs
- update release contract code for solidity 0.4 - add support for subscribing to filter events in simulated backend and contract backend
This commit is contained in:
parent
08dde72722
commit
c064b1d94d
5 changed files with 75 additions and 12 deletions
|
|
@ -303,6 +303,13 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SubscribeFilterLogs emits logs that match the given criteria over the given channel.
|
||||||
|
//
|
||||||
|
// Note: it is currently not implemented.
|
||||||
|
func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
|
||||||
|
return nil, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
||||||
type callmsg struct {
|
type callmsg struct {
|
||||||
ethereum.CallMsg
|
ethereum.CallMsg
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -58,7 +58,7 @@ contract ReleaseOracle {
|
||||||
// isSigner is a modifier to authorize contract transactions.
|
// isSigner is a modifier to authorize contract transactions.
|
||||||
modifier isSigner() {
|
modifier isSigner() {
|
||||||
if (authorised[msg.sender]) {
|
if (authorised[msg.sender]) {
|
||||||
_
|
_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/eth"
|
"github.com/ethereum/go-ethereum/eth"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/filters"
|
||||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||||
"github.com/ethereum/go-ethereum/les"
|
"github.com/ethereum/go-ethereum/les"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
|
@ -62,19 +63,26 @@ type ReleaseService struct {
|
||||||
func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) {
|
func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) {
|
||||||
// Retrieve the Ethereum service dependency to access the blockchain
|
// Retrieve the Ethereum service dependency to access the blockchain
|
||||||
var apiBackend ethapi.Backend
|
var apiBackend ethapi.Backend
|
||||||
|
var filterBackend filters.Backend
|
||||||
var ethereum *eth.Ethereum
|
var ethereum *eth.Ethereum
|
||||||
|
lightMode := false
|
||||||
|
|
||||||
if err := ctx.Service(ðereum); err == nil {
|
if err := ctx.Service(ðereum); err == nil {
|
||||||
apiBackend = ethereum.ApiBackend
|
apiBackend = ethereum.ApiBackend
|
||||||
|
filterBackend = ethereum.ApiBackend
|
||||||
} else {
|
} else {
|
||||||
var ethereum *les.LightEthereum
|
var ethereum *les.LightEthereum
|
||||||
if err := ctx.Service(ðereum); err == nil {
|
if err := ctx.Service(ðereum); err == nil {
|
||||||
apiBackend = ethereum.ApiBackend
|
apiBackend = ethereum.ApiBackend
|
||||||
|
filterBackend = ethereum.ApiBackend
|
||||||
|
lightMode = true
|
||||||
} else {
|
} else {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the release service
|
// Construct the release service
|
||||||
contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(apiBackend))
|
contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(apiBackend, filterBackend, lightMode))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
eth/bind.go
34
eth/bind.go
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/filters"
|
||||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
|
@ -40,15 +41,17 @@ type ContractBackend struct {
|
||||||
eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
|
eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
|
||||||
bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data
|
bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data
|
||||||
txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
|
txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
|
||||||
|
es *filters.EventSystem // Enable log subscriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContractBackend creates a new native contract backend using an existing
|
// NewContractBackend creates a new native contract backend using an existing
|
||||||
// Ethereum object.
|
// Ethereum object.
|
||||||
func NewContractBackend(apiBackend ethapi.Backend) *ContractBackend {
|
func NewContractBackend(apiBackend ethapi.Backend, filterBackend filters.Backend, lightMode bool) *ContractBackend {
|
||||||
return &ContractBackend{
|
return &ContractBackend{
|
||||||
eapi: ethapi.NewPublicEthereumAPI(apiBackend),
|
eapi: ethapi.NewPublicEthereumAPI(apiBackend),
|
||||||
bcapi: ethapi.NewPublicBlockChainAPI(apiBackend),
|
bcapi: ethapi.NewPublicBlockChainAPI(apiBackend),
|
||||||
txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend, new(ethapi.AddrLocker)),
|
txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend, new(ethapi.AddrLocker)),
|
||||||
|
es: filters.NewEventSystem(apiBackend.EventMux(), filterBackend, lightMode),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,3 +139,32 @@ func (b *ContractBackend) SendTransaction(ctx context.Context, tx *types.Transac
|
||||||
_, err := b.txapi.SendRawTransaction(ctx, raw)
|
_, err := b.txapi.SendRawTransaction(ctx, raw)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SubscribeFilterLogs emits logs that match the given criteria over the given channel.
|
||||||
|
func (b *ContractBackend) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, logs chan<- types.Log) (ethereum.Subscription, error) {
|
||||||
|
f := filters.FilterCriteria{q.FromBlock, q.ToBlock, q.Addresses, q.Topics}
|
||||||
|
l := make(chan []*types.Log)
|
||||||
|
|
||||||
|
sub, err := b.es.SubscribeLogs(f, l)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: rewrite filters.EventSystem so SubscribeLogs
|
||||||
|
// accepts chan types.Log instead of chan []*type.Log
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sub.Err():
|
||||||
|
close(l)
|
||||||
|
return
|
||||||
|
case ls := <-l:
|
||||||
|
for _, l := range ls {
|
||||||
|
logs <- *l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return sub, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue