Skip to content

Commit

Permalink
Merge branch 'master' into blob-transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
minh-bq committed Jul 30, 2024
2 parents f1f871d + 268d950 commit 714bc2b
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 91 deletions.
45 changes: 31 additions & 14 deletions core/vm/consortium_precompiled_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,16 @@ func init() {
}
}

func PrecompiledContractsConsortium(caller ContractRef, evm *EVM) map[common.Address]PrecompiledContract {
return map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{101}): &consortiumLog{},
common.BytesToAddress([]byte{102}): &consortiumValidatorSorting{caller: caller, evm: evm},
common.BytesToAddress([]byte{103}): &consortiumVerifyHeaders{caller: caller, evm: evm},
common.BytesToAddress([]byte{104}): &consortiumPickValidatorSet{caller: caller, evm: evm},
common.BytesToAddress([]byte{105}): &consortiumValidateFinalityProof{caller: caller, evm: evm},
}
}

func PrecompiledContractsConsortiumMiko(caller ContractRef, evm *EVM) map[common.Address]PrecompiledContract {
contracts := PrecompiledContractsConsortium(caller, evm)
contracts[common.BytesToAddress([]byte{106})] = &consortiumValidateProofOfPossession{caller: caller, evm: evm}
return contracts
type PrecompiledContractWithInit interface {
PrecompiledContract
Init(caller ContractRef, evm *EVM)
}

type consortiumLog struct{}

func (c *consortiumLog) Init(_ ContractRef, _ *EVM) {
}

func (c *consortiumLog) RequiredGas(_ []byte) uint64 {
return 0
}
Expand Down Expand Up @@ -145,6 +137,11 @@ type consortiumPickValidatorSet struct {
evm *EVM
}

func (c *consortiumPickValidatorSet) Init(caller ContractRef, evm *EVM) {
c.caller = caller
c.evm = evm
}

func (c *consortiumPickValidatorSet) RequiredGas(input []byte) uint64 {
// c.evm is nil in benchmark
if c.evm == nil || c.evm.chainRules.IsMiko {
Expand Down Expand Up @@ -278,6 +275,11 @@ type consortiumValidatorSorting struct {
evm *EVM
}

func (c *consortiumValidatorSorting) Init(caller ContractRef, evm *EVM) {
c.caller = caller
c.evm = evm
}

func (c *consortiumValidatorSorting) RequiredGas(input []byte) uint64 {
// c.evm is nil in benchmark
if c.evm == nil || c.evm.chainRules.IsMiko {
Expand Down Expand Up @@ -435,6 +437,11 @@ type consortiumVerifyHeaders struct {
test bool
}

func (c *consortiumVerifyHeaders) Init(caller ContractRef, evm *EVM) {
c.caller = caller
c.evm = evm
}

func (c *consortiumVerifyHeaders) RequiredGas(_ []byte) uint64 {
// c.evm is nil in benchmark
if c.evm == nil || c.evm.chainRules.IsMiko {
Expand Down Expand Up @@ -616,6 +623,11 @@ type consortiumValidateFinalityProof struct {
evm *EVM
}

func (c *consortiumValidateFinalityProof) Init(caller ContractRef, evm *EVM) {
c.caller = caller
c.evm = evm
}

func (contract *consortiumValidateFinalityProof) RequiredGas(input []byte) uint64 {
return params.ValidateFinalityProofGas
}
Expand Down Expand Up @@ -728,6 +740,11 @@ type consortiumValidateProofOfPossession struct {
evm *EVM
}

func (c *consortiumValidateProofOfPossession) Init(caller ContractRef, evm *EVM) {
c.caller = caller
c.evm = evm
}

func (contract *consortiumValidateProofOfPossession) RequiredGas(input []byte) uint64 {
return params.ValidateProofOfPossession
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/consortium_precompiled_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ func newEVM(caller common.Address, statedb StateDB) (*EVM, error) {
},
chainConfig: params.TestChainConfig,
StateDB: statedb,
chainRules: params.Rules{IsIstanbul: true, IsEIP150: true},
chainRules: params.Rules{IsIstanbul: true, IsEIP150: true, IsConsortiumV2: true},
}
evm.chainConfig.ConsortiumV2Block = common.Big1
evm.interpreter = NewEVMInterpreter(evm, Config{NoBaseFee: true})
Expand Down
136 changes: 81 additions & 55 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"crypto/sha256"
"encoding/binary"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/blake2b"
"github.com/ethereum/go-ethereum/crypto/bls12381"
"github.com/ethereum/go-ethereum/crypto/bn256"
"github.com/ethereum/go-ethereum/params"
"math/big"

//lint:ignore SA1019 Needed for precompile
"golang.org/x/crypto/ripemd160"
Expand All @@ -41,56 +42,6 @@ type PrecompiledContract interface {
Run(input []byte) ([]byte, error) // Run runs the precompiled contract
}

// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
// contracts used in the Frontier and Homestead releases.
var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
}

// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
// contracts used in the Byzantium release.
var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
}

// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
// contracts used in the Istanbul release.
var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
// contracts used in the Berlin release.
var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
// contracts specified in EIP-2537. These are exported for testing purposes.
var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
Expand All @@ -106,13 +57,78 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
}

var (
PrecompiledAddressesBerlin []common.Address
PrecompiledAddressesIstanbul []common.Address
PrecompiledAddressesByzantium []common.Address
PrecompiledAddressesHomestead []common.Address
PrecompiledAddressesBerlin []common.Address
PrecompiledAddressesMiko []common.Address
PrecompiledAddressesConsortium []common.Address
PrecompiledAddressesIstanbul []common.Address
PrecompiledAddressesByzantium []common.Address
PrecompiledAddressesHomestead []common.Address

// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
// contracts used in the Frontier and Homestead releases.
PrecompiledContractsHomestead map[common.Address]PrecompiledContract

// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
// contracts used in the Byzantium release.
PrecompiledContractsByzantium map[common.Address]PrecompiledContract

// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
// contracts used in the Istanbul release.
PrecompiledContractsIstanbul map[common.Address]PrecompiledContract

// PrecompiledContractsConsortium contains additional Consortium precompiled contract
// beside PrecompiledContractsIstanbul
PrecompiledContractsConsortium map[common.Address]PrecompiledContract

// PrecompiledContractsConsortium contains proof of possession precompiled contract
// beside PrecompiledContractsConsortium
PrecompiledContractsConsortiumMiko map[common.Address]PrecompiledContract

// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
// contracts used in the Berlin release.
PrecompiledContractsBerlin map[common.Address]PrecompiledContract
)

func copyPrecompiledContract(contracts map[common.Address]PrecompiledContract) map[common.Address]PrecompiledContract {
cpy := make(map[common.Address]PrecompiledContract)

for address, contract := range contracts {
cpy[address] = contract
}

return cpy
}

func init() {
PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
}

PrecompiledContractsByzantium = copyPrecompiledContract(PrecompiledContractsHomestead)
PrecompiledContractsByzantium[common.BytesToAddress([]byte{5})] = &bigModExp{eip2565: false}
PrecompiledContractsByzantium[common.BytesToAddress([]byte{6})] = &bn256AddByzantium{}
PrecompiledContractsByzantium[common.BytesToAddress([]byte{7})] = &bn256ScalarMulByzantium{}
PrecompiledContractsByzantium[common.BytesToAddress([]byte{8})] = &bn256PairingByzantium{}

PrecompiledContractsIstanbul = copyPrecompiledContract(PrecompiledContractsByzantium)
PrecompiledContractsIstanbul[common.BytesToAddress([]byte{9})] = &blake2F{}

PrecompiledContractsConsortium = copyPrecompiledContract(PrecompiledContractsIstanbul)
PrecompiledContractsConsortium[common.BytesToAddress([]byte{101})] = &consortiumLog{}
PrecompiledContractsConsortium[common.BytesToAddress([]byte{102})] = &consortiumValidatorSorting{}
PrecompiledContractsConsortium[common.BytesToAddress([]byte{103})] = &consortiumVerifyHeaders{}
PrecompiledContractsConsortium[common.BytesToAddress([]byte{104})] = &consortiumPickValidatorSet{}
PrecompiledContractsConsortium[common.BytesToAddress([]byte{105})] = &consortiumValidateFinalityProof{}

PrecompiledContractsConsortiumMiko = copyPrecompiledContract(PrecompiledContractsConsortium)
PrecompiledContractsConsortiumMiko[common.BytesToAddress([]byte{106})] = &consortiumValidateProofOfPossession{}

PrecompiledContractsBerlin = copyPrecompiledContract(PrecompiledContractsConsortiumMiko)
PrecompiledContractsBerlin[common.BytesToAddress([]byte{5})] = &bigModExp{eip2565: true}

for k := range PrecompiledContractsHomestead {
PrecompiledAddressesHomestead = append(PrecompiledAddressesHomestead, k)
}
Expand All @@ -122,6 +138,12 @@ func init() {
for k := range PrecompiledContractsIstanbul {
PrecompiledAddressesIstanbul = append(PrecompiledAddressesIstanbul, k)
}
for k := range PrecompiledContractsConsortium {
PrecompiledAddressesConsortium = append(PrecompiledAddressesConsortium, k)
}
for k := range PrecompiledContractsConsortiumMiko {
PrecompiledAddressesMiko = append(PrecompiledAddressesMiko, k)
}
for k := range PrecompiledContractsBerlin {
PrecompiledAddressesBerlin = append(PrecompiledAddressesBerlin, k)
}
Expand All @@ -132,6 +154,10 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsBerlin:
return PrecompiledAddressesBerlin
case rules.IsMiko:
return PrecompiledAddressesMiko
case rules.IsConsortiumV2:
return PrecompiledAddressesConsortium
case rules.IsIstanbul:
return PrecompiledAddressesIstanbul
case rules.IsByzantium:
Expand Down
35 changes: 14 additions & 21 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,29 @@ func (evm *EVM) precompile(caller ContractRef, addr common.Address) (Precompiled
return &blacklistedAddress{}, true
}

var tmpPrecompiles map[common.Address]PrecompiledContract
// add eth precompile contracts
var precompiles map[common.Address]PrecompiledContract
switch {
case evm.chainRules.IsBerlin:
tmpPrecompiles = PrecompiledContractsBerlin
precompiles = PrecompiledContractsBerlin
case evm.chainRules.IsMiko:
precompiles = PrecompiledContractsConsortiumMiko
case evm.chainRules.IsConsortiumV2:
precompiles = PrecompiledContractsConsortium
case evm.chainRules.IsIstanbul:
tmpPrecompiles = PrecompiledContractsIstanbul
precompiles = PrecompiledContractsIstanbul
case evm.chainRules.IsByzantium:
tmpPrecompiles = PrecompiledContractsByzantium
precompiles = PrecompiledContractsByzantium
default:
tmpPrecompiles = PrecompiledContractsHomestead
precompiles = PrecompiledContractsHomestead
}

precompiles := make(map[common.Address]PrecompiledContract)
for address, contract := range tmpPrecompiles {
precompiles[address] = contract
}

// add consortium precompiled contracts to list
var consortiumContracts map[common.Address]PrecompiledContract
if evm.chainRules.IsMiko {
consortiumContracts = PrecompiledContractsConsortiumMiko(caller, evm)
} else {
consortiumContracts = PrecompiledContractsConsortium(caller, evm)
}
for address, contract := range consortiumContracts {
precompiles[address] = contract
p, ok := precompiles[addr]
if ok {
if pWithInit, hasInit := p.(PrecompiledContractWithInit); hasInit {
pWithInit.Init(caller, evm)
}
}

p, ok := precompiles[addr]
return p, ok
}

Expand Down
Loading

0 comments on commit 714bc2b

Please sign in to comment.