2018-07-04 23:38:20 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
2018-07-26 13:36:01 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
ethcons "github.com/ethereum/go-ethereum/consensus"
|
2018-07-04 23:38:20 +00:00
|
|
|
ethstate "github.com/ethereum/go-ethereum/core/state"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
ethrpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainContext implements Ethereum's core.ChainContext and consensus.Engine
|
|
|
|
// interfaces. It is needed in order to apply and process Ethereum
|
|
|
|
// transactions. There should only be a single implementation in Ethermint. For
|
|
|
|
// the purposes of Ethermint, it should be support retrieving headers and
|
|
|
|
// consensus parameters from the current blockchain to be used during
|
|
|
|
// transaction processing.
|
|
|
|
//
|
|
|
|
// NOTE: Ethermint will distribute the fees out to validators, so the structure
|
|
|
|
// and functionality of this is a WIP and subject to change.
|
|
|
|
type ChainContext struct {
|
2018-07-26 13:36:01 +00:00
|
|
|
Coinbase ethcmn.Address
|
2018-07-05 21:01:52 +00:00
|
|
|
headersByNumber map[uint64]*ethtypes.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChainContext() *ChainContext {
|
|
|
|
return &ChainContext{
|
|
|
|
headersByNumber: make(map[uint64]*ethtypes.Header),
|
|
|
|
}
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Engine implements Ethereum's core.ChainContext interface. As a ChainContext
|
|
|
|
// implements the consensus.Engine interface, it is simply returned.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) Engine() ethcons.Engine {
|
2018-07-04 23:38:20 +00:00
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:36:01 +00:00
|
|
|
// SetHeader implements Ethereum's core.ChainContext interface. It sets the
|
|
|
|
// header for the given block number.
|
2018-07-05 21:01:52 +00:00
|
|
|
func (cc *ChainContext) SetHeader(number uint64, header *ethtypes.Header) {
|
|
|
|
cc.headersByNumber[number] = header
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:36:01 +00:00
|
|
|
// GetHeader implements Ethereum's core.ChainContext interface.
|
2018-07-04 23:38:20 +00:00
|
|
|
//
|
|
|
|
// TODO: The Cosmos SDK supports retreiving such information in contexts and
|
|
|
|
// multi-store, so this will be need to be integrated.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) GetHeader(_ ethcmn.Hash, number uint64) *ethtypes.Header {
|
2018-07-05 21:01:52 +00:00
|
|
|
if header, ok := cc.headersByNumber[number]; ok {
|
|
|
|
return header
|
|
|
|
}
|
2018-07-26 13:36:01 +00:00
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Author implements Ethereum's consensus.Engine interface. It is responsible
|
|
|
|
// for returned the address of the validtor to receive any fees. This function
|
|
|
|
// is only invoked if the given author in the ApplyTransaction call is nil.
|
|
|
|
//
|
|
|
|
// NOTE: Ethermint will distribute the fees out to validators, so the structure
|
|
|
|
// and functionality of this is a WIP and subject to change.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) Author(_ *ethtypes.Header) (ethcmn.Address, error) {
|
2018-07-05 15:06:15 +00:00
|
|
|
return cc.Coinbase, nil
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// APIs implements Ethereum's consensus.Engine interface. It currently performs
|
|
|
|
// a no-op.
|
2018-07-04 23:38:20 +00:00
|
|
|
//
|
|
|
|
// TODO: Do we need to support such RPC APIs? This will tie into a bigger
|
|
|
|
// discussion on if we want to support web3.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) APIs(_ ethcons.ChainReader) []ethrpc.API {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// CalcDifficulty implements Ethereum's consensus.Engine interface. It currently
|
|
|
|
// performs a no-op.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) CalcDifficulty(_ ethcons.ChainReader, _ uint64, _ *ethtypes.Header) *big.Int {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// Finalize implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
|
|
|
|
func (cc *ChainContext) Finalize(
|
2018-07-26 13:36:01 +00:00
|
|
|
_ ethcons.ChainReader, _ *ethtypes.Header, _ *ethstate.StateDB,
|
2018-07-04 23:38:20 +00:00
|
|
|
_ []*ethtypes.Transaction, _ []*ethtypes.Header, _ []*ethtypes.Receipt,
|
|
|
|
) (*ethtypes.Block, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// Prepare implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) Prepare(_ ethcons.ChainReader, _ *ethtypes.Header) error {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// Seal implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
|
2018-09-28 21:40:58 +00:00
|
|
|
func (cc *ChainContext) Seal(_ ethcons.ChainReader, _ *ethtypes.Block, _ chan<- *ethtypes.Block, _ <-chan struct{}) error {
|
|
|
|
return nil
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// SealHash implements Ethereum's consensus.Engine interface. It returns the
|
|
|
|
// hash of a block prior to it being sealed.
|
|
|
|
func (cc *ChainContext) SealHash(header *ethtypes.Header) ethcmn.Hash {
|
|
|
|
return ethcmn.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyHeader implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
|
|
|
|
// handlers?
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) VerifyHeader(_ ethcons.ChainReader, _ *ethtypes.Header, _ bool) error {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// VerifyHeaders implements Ethereum's consensus.Engine interface. It
|
2018-07-04 23:38:20 +00:00
|
|
|
// currently performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
|
|
|
|
// handlers?
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) VerifyHeaders(_ ethcons.ChainReader, _ []*ethtypes.Header, _ []bool) (chan<- struct{}, <-chan error) {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// VerifySeal implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
|
|
|
//
|
|
|
|
// TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK
|
|
|
|
// handlers?
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) VerifySeal(_ ethcons.ChainReader, _ *ethtypes.Header) error {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:40:58 +00:00
|
|
|
// VerifyUncles implements Ethereum's consensus.Engine interface. It currently
|
2018-07-04 23:38:20 +00:00
|
|
|
// performs a no-op.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (cc *ChainContext) VerifyUncles(_ ethcons.ChainReader, _ *ethtypes.Block) error {
|
2018-07-04 23:38:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 21:40:58 +00:00
|
|
|
|
|
|
|
// Close implements Ethereum's consensus.Engine interface. It terminates any
|
|
|
|
// background threads maintained by the consensus engine. It currently performs
|
|
|
|
// a no-op.
|
|
|
|
func (cc *ChainContext) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|