Skip to content

Commit

Permalink
Merged in coinplugin/go-metadium (pull request #18)
Browse files Browse the repository at this point in the history
Master

Approved-by: Uh Sado <sadoc@metadium.com>
  • Loading branch information
sadoci committed Mar 20, 2019
2 parents b9a17c4 + 7f4a4ab commit 9589638
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 129 deletions.
4 changes: 2 additions & 2 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
} else {
blockReward = MetadiumBlockReward
coinbase, rewards, err := metaminer.CalculateRewards(
header.Number, blockReward, big.NewInt(int64(header.Fees)),
header.Number, blockReward, header.Fees,
func(addr common.Address, amt *big.Int) {
state.AddBalance(addr, amt)
})
Expand All @@ -663,7 +663,7 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
} else {
// upon error, rewards go to the coinbase
reward := new(big.Int)
reward.Add(blockReward, big.NewInt(int64(header.Fees)))
reward.Add(blockReward, header.Fees)
state.AddBalance(header.Coinbase, reward)
}
}
Expand Down
28 changes: 28 additions & 0 deletions consensus/ethash/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
)

const (
Expand All @@ -49,6 +50,33 @@ var (
// Seal implements consensus.Engine, attempting to find a nonce that satisfies
// the block's difficulty requirements.
func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
if !metaminer.IsPoW() {
// In Metadium, return immediately
ethash.lock.Lock()
if ethash.rand == nil {
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
ethash.lock.Unlock()
return err
}
ethash.rand = rand.New(rand.NewSource(seed.Int64()))
}
ethash.lock.Unlock()
abort := make(chan struct{})
found := make(chan *types.Block)
go ethash.mine(block, 0, uint64(ethash.rand.Int63()), abort, found)
var result *types.Block
select {
case result = <-found:
select {
case results <- result:
default:
log.Warn("Sealing result is not read by miner", "sealhash", ethash.SealHash(block.Header()))
}
}
return nil
}

// If we're running a fake PoW, simply return a 0 nonce immediately
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
header := block.Header()
Expand Down
7 changes: 4 additions & 3 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/state"
Expand Down Expand Up @@ -77,13 +78,13 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
// transition, such as amount of used gas, the receipt roots and the state root
// itself. ValidateState returns a database batch if the validation was a success
// otherwise nil and an error is returned.
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas, fees uint64) error {
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64, fees *big.Int) error {
header := block.Header()
if block.GasUsed() != usedGas {
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
}
if block.Fees() != fees {
return fmt.Errorf("invalid fees collected (remote: %d local: %d)", block.Fees(), fees)
if block.Fees().Cmp(fees) != 0 {
return fmt.Errorf("invalid fees collected (remote: %v local: %v)", block.Fees(), fees)
}

// Validate the received block's bloom with the one derived from the generated receipts.
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
b.SetCoinbase(common.Address{})
}
b.statedb.Prepare(tx.Hash(), common.Hash{}, len(b.txs))
fees := new(uint64)
fees := new(big.Int)
receipt, _, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, fees, vm.Config{})
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions core/metadium_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12286,7 +12286,7 @@ var (
"balance": "0x3f514193abb840000"
}
},
"coinbase": "0x7eA34544DFA38F6614281adA30af1259c9BCA9Ec",
"coinbase": "0x880a74d68b09418136c4442D1eA0f5cC72E5325A",
"config": {
"byzantiumBlock": 0,
"chainId": 11,
Expand All @@ -12295,7 +12295,7 @@ var (
"homesteadBlock": 0
},
"difficulty": "0x1",
"extraData": "0x4f757220766973696f6e20697320746f206372656174652061206672656520776f726c64207468726f7567682073656c662d736f7665726569676e206964656e746974792e202f205768656e204920646973636f7665722077686f204920616d2c2049276c6c20626520667265652e202d2d2052616c706820456c6c69736f6e2c20496e76697369626c65204d616e0a30786661353163663461303830376432333330323561346564646636623436393634376162376232353630613331393136626535393030616461356331343539343962363166623535306636316132346231663432373434386336623962663861656236383239363965313136343335343666646233623538383434313234316163",
"extraData": "0x4f757220766973696f6e20697320746f206372656174652061206672656520776f726c64207468726f7567682073656c662d736f7665726569676e206964656e746974792e202f205768656e204920646973636f7665722077686f204920616d2c2049276c6c20626520667265652e202d2d2052616c706820456c6c69736f6e2c20496e76697369626c65204d616e0a30783165633236316462636533636530303232366237393765333030626135363836336666363563313338323633623938313435646364393762346561336134633761386432303431346537613031383566333263633730386430633163393935613837376536633738373666353061626331336637373430623961633330376266",
"gasLimit": "0x10000000",
"minerNodeId": "0x0",
"minerNodeSig": "0x0",
Expand Down
14 changes: 8 additions & 6 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package core

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
Expand Down Expand Up @@ -53,11 +55,11 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
// Process returns the receipts and logs accumulated during the process and
// returns the amount of gas that was used in the process. If any of the
// transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, uint64, error) {
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, *big.Int, error) {
var (
receipts types.Receipts
usedGas = new(uint64)
fees = new(uint64)
fees = new(big.Int)
header = block.Header()
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())
Expand All @@ -71,22 +73,22 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
statedb.Prepare(tx.Hash(), block.Hash(), i)
receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, fees, cfg)
if err != nil {
return nil, nil, 0, 0, err
return nil, nil, 0, big.NewInt(0), err
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
}
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts)

return receipts, allLogs, *usedGas, *fees, nil
return receipts, allLogs, *usedGas, fees, nil
}

// ApplyTransaction attempts to apply a transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, fees *uint64, cfg vm.Config) (*types.Receipt, uint64, error) {
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, fees *big.Int, cfg vm.Config) (*types.Receipt, uint64, error) {
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
if err != nil {
return nil, 0, err
Expand All @@ -109,7 +111,7 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
}
*usedGas += gas
*fees += fee
fees.Add(fees, big.NewInt(int64(fee)))

// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
// based on the eip phase, we're passing whether the root touch-delete accounts.
Expand Down
6 changes: 4 additions & 2 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package core

import (
"math/big"

"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand All @@ -32,7 +34,7 @@ type Validator interface {

// ValidateState validates the given statedb and optionally the receipts and
// gas used.
ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas, fees uint64) error
ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64, fees *big.Int) error
}

// Processor is an interface for processing blocks using a given initial state.
Expand All @@ -42,5 +44,5 @@ type Validator interface {
// of gas used in the process and return an error if any of the internal rules
// failed.
type Processor interface {
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, uint64, error)
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, *big.Int, error)
}
6 changes: 3 additions & 3 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type Header struct {
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
Fees uint64 `json:"fees" gencodec:"required"`
Fees *big.Int `json:"fees" gencodec:"required"`
Time *big.Int `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData" gencodec:"required"`
Rewards []byte `json:"rewards" gencodec:"required"`
Expand All @@ -95,7 +95,7 @@ type headerMarshaling struct {
Number *hexutil.Big
GasLimit hexutil.Uint64
GasUsed hexutil.Uint64
Fees hexutil.Uint64
Fees *hexutil.Big
Time *hexutil.Big
Extra hexutil.Bytes
Rewards hexutil.Bytes
Expand Down Expand Up @@ -297,7 +297,7 @@ func (b *Block) Transaction(hash common.Hash) *Transaction {
func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
func (b *Block) Fees() uint64 { return b.header.Fees }
func (b *Block) Fees() *big.Int { return b.header.Fees }
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) }

Expand Down
8 changes: 4 additions & 4 deletions core/types/gen_header_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
"size": hexutil.Uint64(b.Size()),
"gasLimit": hexutil.Uint64(head.GasLimit),
"gasUsed": hexutil.Uint64(head.GasUsed),
"fees": hexutil.Uint64(head.Fees),
"fees": (*hexutil.Big)(head.Fees),
"timestamp": (*hexutil.Big)(head.Time),
"transactionsRoot": head.TxHash,
"receiptsRoot": head.ReceiptHash,
Expand Down
18 changes: 2 additions & 16 deletions metadium/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,7 @@ func (ma *metaAdmin) amPartner() bool {
if admin == nil {
return false
}
return (admin.nodeInfo != nil && admin.nodeInfo.ID == admin.bootNodeId) ||
admin.self != nil
return admin.self != nil || (admin.nodeInfo != nil && admin.nodeInfo.ID == admin.bootNodeId)
}

func AmPartner() bool {
Expand Down Expand Up @@ -1353,20 +1352,7 @@ func (ma *metaAdmin) getTxPoolStatus() (pending, queued uint, err error) {
}

func requirePendingTxs() bool {
if admin == nil {
return false
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

block, err := admin.cli.HeaderByNumber(ctx, nil)
if err != nil {
return false
}
height := int(block.Number.Int64())

if !IsMiner(height + 1) {
if !IsMiner() {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions metadium/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
IsMinerFunc func(int) bool
IsMinerFunc func() bool
AmPartnerFunc func() bool
IsPartnerFunc func(string) bool
LogBlockFunc func(int64)
Expand All @@ -24,11 +24,11 @@ var (
SuggestGasPriceFunc func() *big.Int
)

func IsMiner(height int) bool {
func IsMiner() bool {
if IsMinerFunc == nil {
return false
} else {
return IsMinerFunc(height)
return IsMinerFunc()
}
}

Expand Down
Loading

0 comments on commit 9589638

Please sign in to comment.