Skip to content

Commit

Permalink
modify consensus of parlia
Browse files Browse the repository at this point in the history
  • Loading branch information
somewheel committed Aug 25, 2021
1 parent 3270b8e commit 3a0cbc2
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 37 deletions.
48 changes: 48 additions & 0 deletions common/gopool/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gopool

import (
"time"

"github.com/panjf2000/ants/v2"
)

var (
// Init a instance pool when importing ants.
defaultPool, _ = ants.NewPool(ants.DefaultAntsPoolSize, ants.WithExpiryDuration(10*time.Second))
)

// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
}

// Submit submits a task to pool.
func Submit(task func()) error {
return defaultPool.Submit(task)
}

// Running returns the number of the currently running goroutines.
func Running() int {
return defaultPool.Running()
}

// Cap returns the capacity of this default pool.
func Cap() int {
return defaultPool.Cap()
}

// Free returns the available goroutines to work.
func Free() int {
return defaultPool.Free()
}

// Release Closes the default pool.
func Release() {
defaultPool.Release()
}

// Reboot reboots the default pool.
func Reboot() {
defaultPool.Reboot()
}
4 changes: 4 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"github.com/ledgerwatch/erigon/rpc"
)

var (
SystemAddress = common.HexToAddress("0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE")
)

// ChainHeaderReader defines a small collection of methods needed to access the local
// blockchain during header verification.
type ChainHeaderReader interface {
Expand Down
85 changes: 48 additions & 37 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"io"
"math"

// "math/big"
"math/big"
"math/rand"
"sort"
Expand All @@ -16,22 +18,25 @@ import (
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"

// "github.com/ethereum/go-ethereum"
// "github.com/ethereum/go-ethereum/accounts"
// "github.com/ethereum/go-ethereum/accounts/abi"
// "github.com/ethereum/go-ethereum/common"
// "github.com/ethereum/go-ethereum/common/gopool"
// "github.com/ethereum/go-ethereum/common/hexutil"
// "github.com/ethereum/go-ethereum/consensus"
// "github.com/ethereum/go-ethereum/consensus/misc"
// "github.com/ethereum/go-ethereum/core"
// "github.com/ethereum/go-ethereum/core/forkid"
// "github.com/ethereum/go-ethereum/core/state"
// "github.com/ethereum/go-ethereum/core/systemcontracts"
ethereum "github.com/ledgerwatch/erigon"
// "github.com/ledgerwatch/erigon/accounts"
// "github.com/ledgerwatch/erigon/accounts/abi"
// "github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/gopool"
// "github.com/ledgerwatch/erigon/common/hexutil"
// "github.com/ledgerwatch/erigon/consensus"
// "github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/core/state"

"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/internal/ethapi"

// "github.com/ethereum/go-ethereum/core/vm"
// "github.com/ethereum/go-ethereum/crypto"
// "github.com/ethereum/go-ethereum/ethdb"
Expand All @@ -45,6 +50,7 @@ import (
"github.com/ledgerwatch/erigon/accounts/abi"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/crypto"
Expand Down Expand Up @@ -156,8 +162,8 @@ var (

// SignerFn is a signer callback function to request a header to be signed by a
// backing account.
type SignerFn func(accounts.Account, string, []byte) ([]byte, error)
type SignerTxFn func(accounts.Account, *types.Transaction, *big.Int) (*types.Transaction, error)
type SignerFn func(common.Address, string, []byte) ([]byte, error)
type SignerTxFn func(common.Address, *types.Transaction, *big.Int) (*types.Transaction, error)

func isToSystemContract(to common.Address) bool {
return systemContracts[to]
Expand Down Expand Up @@ -219,7 +225,7 @@ type Parlia struct {

lock sync.RWMutex // Protects the signer fields

// ethAPI *ethapi.PublicBlockChainAPI
// ethAPI *ethapi.PublicBlockChainAPI //todo migrate
validatorSetABI abi.ABI
slashABI abi.ABI

Expand All @@ -231,7 +237,7 @@ type Parlia struct {
func New(
chainConfig *params.ChainConfig,
snapshotConfig *params.SnapshotConfig,
cliqueDB kv.RwDB,
db kv.RwDB,
) *Parlia {
// get parlia config
parliaConfig := chainConfig.Parlia
Expand All @@ -258,32 +264,37 @@ func New(
if err != nil {
panic(err)
}
var signer types.Signer
signer = *types.MakeSigner(chainConfig, 1)
c := &Parlia{
chainConfig: chainConfig,
config: parliaConfig,
genesisHash: nil,
db: db,
ethAPI: nil,
chainConfig: chainConfig,
config: parliaConfig,
genesisHash: common.Hash{}, //todo migrate
db: db,
// ethAPI: nil, //todo migrate
recentSnaps: recentSnaps,
signatures: signatures,
validatorSetABI: vABI,
slashABI: sABI,
signer: types.NewEIP155Signer(chainConfig.ChainID),
signer: signer,
}

return c
}

func (p *Parlia) IsSystemTransaction(tx *types.Transaction, header *types.Header) (bool, error) {
// deploy a contract
if tx.To() == nil {
if (*tx).GetTo() == nil {
return false, nil
}
sender, err := types.Sender(p.signer, tx)
// sender, err := types.Sender(p.signer, tx) //todo
sender, err := p.signer.Sender(*tx)

if err != nil {
return false, errors.New("UnAuthorized transaction")
}
if sender == header.Coinbase && isToSystemContract(*tx.To()) && tx.GasPrice().Cmp(big.NewInt(0)) == 0 {
// (*tx).GetPrice().Cmp(uint256.NewInt(0))
if sender == header.Coinbase && isToSystemContract(*(*tx).GetTo()) && (*tx).GetPrice().Cmp(uint256.NewInt(0)) == 0 {
return true, nil
}
return false, nil
Expand Down Expand Up @@ -657,7 +668,7 @@ func (p *Parlia) Prepare(chain consensus.ChainHeaderReader, header *types.Header

// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState, txs *[]*types.Transaction,
uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64) error {
// warn if not in majority fork
number := header.Number.Uint64()
Expand Down Expand Up @@ -727,7 +738,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState,
txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
cx := chainContext{Chain: chain, parlia: p}
Expand Down Expand Up @@ -1010,7 +1021,7 @@ func (p *Parlia) getCurrentValidators(blockHash common.Hash) ([]common.Address,
}

// slash spoiled validators
func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
func (p *Parlia) distributeIncoming(val common.Address, state *state.IntraBlockState, header *types.Header, chain chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
coinbase := header.Coinbase
balance := state.GetBalance(consensus.SystemAddress)
Expand Down Expand Up @@ -1038,7 +1049,7 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
}

// slash spoiled validators
func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
func (p *Parlia) slash(spoiledVal common.Address, state *state.IntraBlockState, header *types.Header, chain chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
// method
method := "slash"
Expand All @@ -1058,7 +1069,7 @@ func (p *Parlia) slash(spoiledVal common.Address, state *state.StateDB, header *
}

// init contract
func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain core.ChainContext,
func (p *Parlia) initContract(state *state.IntraBlockState, header *types.Header, chain chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
// method
method := "init"
Expand Down Expand Up @@ -1090,7 +1101,7 @@ func (p *Parlia) initContract(state *state.StateDB, header *types.Header, chain
return nil
}

func (p *Parlia) distributeToSystem(amount *big.Int, state *state.StateDB, header *types.Header, chain core.ChainContext,
func (p *Parlia) distributeToSystem(amount *big.Int, state *state.IntraBlockState, header *types.Header, chain chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
// get system message
msg := p.getSystemMessage(header.Coinbase, common.HexToAddress(systemcontracts.SystemRewardContract), nil, amount)
Expand All @@ -1100,7 +1111,7 @@ func (p *Parlia) distributeToSystem(amount *big.Int, state *state.StateDB, heade

// slash spoiled validators
func (p *Parlia) distributeToValidator(amount *big.Int, validator common.Address,
state *state.StateDB, header *types.Header, chain core.ChainContext,
state *state.IntraBlockState, header *types.Header, chain chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
// method
method := "deposit"
Expand Down Expand Up @@ -1135,9 +1146,9 @@ func (p *Parlia) getSystemMessage(from, toAddress common.Address, data []byte, v

func (p *Parlia) applyTransaction(
msg callmsg,
state *state.StateDB,
state *state.IntraBlockState,
header *types.Header,
chainContext core.ChainContext,
chainContext chainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt,
receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool,
) (err error) {
Expand Down Expand Up @@ -1285,13 +1296,13 @@ func (m callmsg) Data() []byte { return m.CallMsg.Data }
// apply message
func applyMessage(
msg callmsg,
state *state.StateDB,
state *state.IntraBlockState,
header *types.Header,
chainConfig *params.ChainConfig,
chainContext core.ChainContext,
chainContext chainContext,
) (uint64, error) {
// Create a new context to be used in the EVM environment
context := core.NewEVMBlockContext(header, chainContext, nil)
context := core.NewEVMBlockContext(header, c.Chain.GetHeader, nil)
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(context, vm.TxContext{Origin: msg.From(), GasPrice: big.NewInt(0)}, state, chainConfig, vm.Config{})
Expand Down
15 changes: 15 additions & 0 deletions core/systemcontracts/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package systemcontracts

const (
// genesis contracts
ValidatorContract = "0x0000000000000000000000000000000000001000"
SlashContract = "0x0000000000000000000000000000000000001001"
SystemRewardContract = "0x0000000000000000000000000000000000001002"
LightClientContract = "0x0000000000000000000000000000000000001003"
TokenHubContract = "0x0000000000000000000000000000000000001004"
RelayerIncentivizeContract = "0x0000000000000000000000000000000000001005"
RelayerHubContract = "0x0000000000000000000000000000000000001006"
GovHubContract = "0x0000000000000000000000000000000000001007"
TokenManagerContract = "0x0000000000000000000000000000000000001008"
CrossChainContract = "0x0000000000000000000000000000000000002000"
)
347 changes: 347 additions & 0 deletions core/systemcontracts/upgrade.go

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ var (
ErigonGenesisHash = common.HexToHash("0xfecd5c85712e36f30f09ba3a42386b42c46b5ba5395a4246b952e655f9aa0f58")
CalaverasGenesisHash = common.HexToHash("0xeb9233d066c275efcdfed8037f4fc082770176aefdbcb7691c71da412a5670f2")
SokolGenesisHash = common.HexToHash("0x5b28c1bfd3a15230c9a46b399cd0f9a6920d432e85381cc6a140b06e8410112f")

BSCGenesisHash = common.HexToHash("0x0d21840abff46b96c84b2ac9e10e4f5cdaeb5693cb665db62a2f3b02d2d57b5b")
ChapelGenesisHash = common.HexToHash("0x6d3c66c5357ec91d5c43af47e234a939b22557cbb552dc45bebbceeed90fbe34")
RialtoGenesisHash = common.HexToHash("0x005dc005bddd1967de6187c1c23be801eb7abdd80cebcc24f341b727b70311d6")
YoloV3GenesisHash = common.HexToHash("0xf1f2876e8500c77afcc03228757b39477eceffccf645b734967fe3c7e16967b7")
)

var (
Expand Down Expand Up @@ -373,6 +378,10 @@ type ChainConfig struct {
BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin)
LondonBlock *big.Int `json:"londonBlock,omitempty"` // London switch block (nil = no fork, 0 = already on london)

RamanujanBlock *big.Int `json:"ramanujanBlock,omitempty" toml:",omitempty"` // ramanujanBlock switch block (nil = no fork, 0 = already activated)
NielsBlock *big.Int `json:"nielsBlock,omitempty" toml:",omitempty"` // nielsBlock switch block (nil = no fork, 0 = already activated)
MirrorSyncBlock *big.Int `json:"mirrorSyncBlock,omitempty" toml:",omitempty"` // mirrorSyncBlock switch block (nil = no fork, 0 = already activated)

// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
Expand Down Expand Up @@ -527,6 +536,31 @@ func (c *ChainConfig) IsLondon(num uint64) bool {
return isForked(c.LondonBlock, num)
}

// IsRamanujan returns whether num is either equal to the IsRamanujan fork block or greater.
func (c *ChainConfig) IsRamanujan(num *big.Int) bool {
return isForked(c.RamanujanBlock, num.Uint64())
}

// IsOnRamanujan returns whether num is equal to the Ramanujan fork block
func (c *ChainConfig) IsOnRamanujan(num *big.Int) bool {
return configNumEqual(c.RamanujanBlock, num)
}

// IsNiels returns whether num is either equal to the Niels fork block or greater.
func (c *ChainConfig) IsNiels(num *big.Int) bool {
return isForked(c.NielsBlock, num.Uint64())
}

// IsOnNiels returns whether num is equal to the IsNiels fork block
func (c *ChainConfig) IsOnNiels(num *big.Int) bool {
return configNumEqual(c.NielsBlock, num)
}

// IsOnMirrorSync returns whether num is equal to the MirrorSync fork block
func (c *ChainConfig) IsOnMirrorSync(num *big.Int) bool {
return configNumEqual(c.MirrorSyncBlock, num)
}

// CheckCompatible checks whether scheduled fork transitions have been imported
// with a mismatching chain configuration.
func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
Expand Down

0 comments on commit 3a0cbc2

Please sign in to comment.