github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/core/chain.go (about) 1 package core 2 3 // TODO: This functionality and implementation may be deprecated 4 5 import ( 6 "math/big" 7 8 ethcmn "github.com/ethereum/go-ethereum/common" 9 ethcons "github.com/ethereum/go-ethereum/consensus" 10 ethstate "github.com/ethereum/go-ethereum/core/state" 11 ethtypes "github.com/ethereum/go-ethereum/core/types" 12 ethrpc "github.com/ethereum/go-ethereum/rpc" 13 ) 14 15 // ChainContext implements Ethereum's core.ChainContext and consensus.Engine 16 // interfaces. It is needed in order to apply and process Ethereum 17 // transactions. There should only be a single implementation in FBChain. For 18 // the purposes of FBchain, it should be support retrieving headers and 19 // consensus parameters from the current blockchain to be used during 20 // transaction processing. 21 // 22 // NOTE: FBchain will distribute the fees out to validators, so the structure 23 // and functionality of this is a WIP and subject to change. 24 type ChainContext struct { 25 Coinbase ethcmn.Address 26 headersByNumber map[uint64]*ethtypes.Header 27 } 28 29 // NewChainContext generates new ChainContext based on Ethereum's core.ChainContext and 30 // consensus.Engine interfaces in order to process Ethereum transactions. 31 func NewChainContext() *ChainContext { 32 return &ChainContext{ 33 headersByNumber: make(map[uint64]*ethtypes.Header), 34 } 35 } 36 37 // Engine implements Ethereum's core.ChainContext interface. As a ChainContext 38 // implements the consensus.Engine interface, it is simply returned. 39 func (cc *ChainContext) Engine() ethcons.Engine { 40 return cc 41 } 42 43 // SetHeader implements Ethereum's core.ChainContext interface. It sets the 44 // header for the given block number. 45 func (cc *ChainContext) SetHeader(number uint64, header *ethtypes.Header) { 46 cc.headersByNumber[number] = header 47 } 48 49 // GetHeader implements Ethereum's core.ChainContext interface. 50 // 51 // TODO: The Cosmos SDK supports retreiving such information in contexts and 52 // multi-store, so this will be need to be integrated. 53 func (cc *ChainContext) GetHeader(_ ethcmn.Hash, number uint64) *ethtypes.Header { 54 if header, ok := cc.headersByNumber[number]; ok { 55 return header 56 } 57 58 return nil 59 } 60 61 // Author implements Ethereum's consensus.Engine interface. It is responsible 62 // for returned the address of the validtor to receive any fees. This function 63 // is only invoked if the given author in the ApplyTransaction call is nil. 64 // 65 // NOTE: FBchain will distribute the fees out to validators, so the structure 66 // and functionality of this is a WIP and subject to change. 67 func (cc *ChainContext) Author(_ *ethtypes.Header) (ethcmn.Address, error) { 68 return cc.Coinbase, nil 69 } 70 71 // APIs implements Ethereum's consensus.Engine interface. It currently performs 72 // a no-op. 73 // 74 // TODO: Do we need to support such RPC APIs? This will tie into a bigger 75 // discussion on if we want to support web3. 76 func (cc *ChainContext) APIs(_ ethcons.ChainHeaderReader) []ethrpc.API { 77 return nil 78 } 79 80 // CalcDifficulty implements Ethereum's consensus.Engine interface. It currently 81 // performs a no-op. 82 func (cc *ChainContext) CalcDifficulty(_ ethcons.ChainHeaderReader, _ uint64, _ *ethtypes.Header) *big.Int { 83 return nil 84 } 85 86 // Finalize implements Ethereum's consensus.Engine interface. It currently 87 // performs a no-op. 88 // 89 // TODO: Figure out if this needs to be hooked up to any part of the ABCI? 90 func (cc *ChainContext) Finalize( 91 _ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ *ethstate.StateDB, 92 _ []*ethtypes.Transaction, _ []*ethtypes.Header) { 93 } 94 95 // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block 96 // rewards) and assembles the final block. 97 // 98 // Note: The block header and state database might be updated to reflect any 99 // consensus rules that happen at finalization (e.g. block rewards). 100 // TODO: Figure out if this needs to be hooked up to any part of the ABCI? 101 func (cc *ChainContext) FinalizeAndAssemble(_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ *ethstate.StateDB, _ []*ethtypes.Transaction, 102 _ []*ethtypes.Header, _ []*ethtypes.Receipt) (*ethtypes.Block, error) { 103 return nil, nil 104 } 105 106 // Prepare implements Ethereum's consensus.Engine interface. It currently 107 // performs a no-op. 108 // 109 // TODO: Figure out if this needs to be hooked up to any part of the ABCI? 110 func (cc *ChainContext) Prepare(_ ethcons.ChainHeaderReader, _ *ethtypes.Header) error { 111 return nil 112 } 113 114 // Seal implements Ethereum's consensus.Engine interface. It currently 115 // performs a no-op. 116 // 117 // TODO: Figure out if this needs to be hooked up to any part of the ABCI? 118 func (cc *ChainContext) Seal(_ ethcons.ChainHeaderReader, _ *ethtypes.Block, _ chan<- *ethtypes.Block, _ <-chan struct{}) error { 119 return nil 120 } 121 122 // SealHash implements Ethereum's consensus.Engine interface. It returns the 123 // hash of a block prior to it being sealed. 124 func (cc *ChainContext) SealHash(header *ethtypes.Header) ethcmn.Hash { 125 return ethcmn.Hash{} 126 } 127 128 // VerifyHeader implements Ethereum's consensus.Engine interface. It currently 129 // performs a no-op. 130 // 131 // TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK 132 // handlers? 133 func (cc *ChainContext) VerifyHeader(_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ bool) error { 134 return nil 135 } 136 137 // VerifyHeaders implements Ethereum's consensus.Engine interface. It 138 // currently performs a no-op. 139 // 140 // TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK 141 // handlers? 142 func (cc *ChainContext) VerifyHeaders(_ ethcons.ChainHeaderReader, _ []*ethtypes.Header, _ []bool) (chan<- struct{}, <-chan error) { 143 return nil, nil 144 } 145 146 // VerifySeal implements Ethereum's consensus.Engine interface. It currently 147 // performs a no-op. 148 // 149 // TODO: Figure out if this needs to be hooked up to any part of the Cosmos SDK 150 // handlers? 151 func (cc *ChainContext) VerifySeal(_ ethcons.ChainHeaderReader, _ *ethtypes.Header) error { 152 return nil 153 } 154 155 // VerifyUncles implements Ethereum's consensus.Engine interface. It currently 156 // performs a no-op. 157 func (cc *ChainContext) VerifyUncles(_ ethcons.ChainReader, _ *ethtypes.Block) error { 158 return nil 159 } 160 161 // Close implements Ethereum's consensus.Engine interface. It terminates any 162 // background threads maintained by the consensus engine. It currently performs 163 // a no-op. 164 func (cc *ChainContext) Close() error { 165 return nil 166 }