Skip to content

Commit

Permalink
Ava-labs/core-eth v1.12.0 (avalanchego 1.10.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht authored Feb 14, 2024
2 parents 07d97eb + d71f828 commit 481b3f9
Show file tree
Hide file tree
Showing 88 changed files with 1,547 additions and 1,695 deletions.
12 changes: 6 additions & 6 deletions accounts/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,12 @@ func TestBareEvents(t *testing.T) {
// TestUnpackEvent is based on this contract:
//
// contract T {
// event received(address sender, uint amount, bytes memo);
// event receivedAddr(address sender);
// function receive(bytes memo) external payable {
// received(msg.sender, msg.value, memo);
// receivedAddr(msg.sender);
// }
// event received(address sender, uint amount, bytes memo);
// event receivedAddr(address sender);
// function receive(bytes memo) external payable {
// received(msg.sender, msg.value, memo);
// receivedAddr(msg.sender);
// }
// }
//
// When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
Expand Down
76 changes: 60 additions & 16 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import (
"github.com/ava-labs/coreth/accounts/abi/bind"
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/admin"
"github.com/ava-labs/coreth/core/bloombits"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/eth/ethadmin"
"github.com/ava-labs/coreth/eth/filters"
"github.com/ava-labs/coreth/ethdb"
"github.com/ava-labs/coreth/interfaces"
Expand Down Expand Up @@ -104,26 +104,69 @@ type SimulatedBackend struct {
config *params.ChainConfig
}

// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
// NewSimulatedCaminoBackendWithDatabase creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, ctrl admin.AdminController) *SimulatedBackend {
cpcfg := params.TestChainConfig
func NewSimulatedBackendWithDatabaseAndChainConfig(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, cpcfg *params.ChainConfig) *SimulatedBackend {
cpcfg.ChainID = big.NewInt(1337)
genesis := core.Genesis{Config: cpcfg, GasLimit: gasLimit, Alloc: alloc, InitialAdmin: addr}
genesis := core.Genesis{
Config: cpcfg,
GasLimit: gasLimit,
Alloc: alloc,
InitialAdmin: addr,
}
if addr.String() != "0x0000000000000000000000000000000000000000" {
genesis.PreDeploy()
}
genesis.MustCommit(database)
cacheConfig := &core.CacheConfig{}
blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, dummy.NewFaker(), vm.Config{}, common.Hash{}, false)
backend := &SimulatedBackend{
database: database,
blockchain: blockchain,
config: genesis.Config,
}
adminCtrl := ethadmin.NewController(backend, blockchain.Config())
blockchain.SetAdminController(adminCtrl)

blockchain, _ := core.NewBlockChain(database, cacheConfig, genesis.Config, dummy.NewFaker(), vm.Config{AdminContoller: ctrl}, common.Hash{})
filterBackend := &filterBackend{database, blockchain, backend}
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{})
backend.events = filters.NewEventSystem(backend.filterSystem, false)

backend.rollback(blockchain.CurrentBlock())
return backend
}

// NewSimulatedCaminoBackendWithDatabase creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithChainConfig(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, cpcfg *params.ChainConfig) *SimulatedBackend {
return NewSimulatedBackendWithDatabaseAndChainConfig(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, cpcfg)
}

// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address) *SimulatedBackend {
cpcfg := params.TestChainConfig
cpcfg.ChainID = big.NewInt(1337)
genesis := core.Genesis{
Config: cpcfg,
GasLimit: gasLimit,
Alloc: alloc,
InitialAdmin: addr,
}
if addr.String() != "0x0000000000000000000000000000000000000000" {
genesis.PreDeploy()
}
cacheConfig := &core.CacheConfig{}
blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, dummy.NewFaker(), vm.Config{}, common.Hash{}, false)
backend := &SimulatedBackend{
database: database,
blockchain: blockchain,
config: genesis.Config,
}
adminCtrl := ethadmin.NewController(backend, blockchain.Config())
blockchain.SetAdminController(adminCtrl)

filterBackend := &filterBackend{database, blockchain, backend}
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{})
Expand All @@ -137,21 +180,14 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis
// for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, common.Address{}, nil)
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, common.Address{})
}

// NewSimulatedBackendWithInitialAdmin creates a new binding backend using a simulated blockchain
// for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithInitialAdmin(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address) *SimulatedBackend {
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, nil)
}

// NewSimulatedBackendWithInitialAdmin creates a new binding backend using a simulated blockchain
// for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithInitialAdminAndAdminController(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, ctrl admin.AdminController) *SimulatedBackend {
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, ctrl)
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr)
}

// Close terminates the underlying blockchain's update loop.
Expand Down Expand Up @@ -865,6 +901,14 @@ func (b *SimulatedBackend) Blockchain() *core.BlockChain {
return b.blockchain
}

func (b *SimulatedBackend) StateByHeader(ctx context.Context, header *types.Header) (*state.StateDB, error) {
return b.blockchain.StateAt(header.Root)
}

func (b *SimulatedBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
return b.blockchain.SubscribeChainHeadEvent(ch)
}

// callMsg implements core.Message to allow passing it as a transaction simulator.
type callMsg struct {
interfaces.CallMsg
Expand Down
72 changes: 37 additions & 35 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ func TestSimulatedBackend(t *testing.T) {

var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")

// the following is based on this contract:
// the following is based on this contract:
//
// contract T {
// event received(address sender, uint amount, bytes memo);
// event receivedAddr(address sender);
//
// function receive(bytes calldata memo) external payable returns (string memory res) {
// emit received(msg.sender, msg.value, memo);
// emit receivedAddr(msg.sender);
// return "hello world";
// return "hello world";
// }
// }
const abiJSON = `[ { "constant": false, "inputs": [ { "name": "memo", "type": "bytes" } ], "name": "receive", "outputs": [ { "name": "res", "type": "string" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "sender", "type": "address" }, { "indexed": false, "name": "amount", "type": "uint256" }, { "indexed": false, "name": "memo", "type": "bytes" } ], "name": "received", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "sender", "type": "address" } ], "name": "receivedAddr", "type": "event" } ]`
Expand Down Expand Up @@ -433,12 +434,13 @@ func TestEstimateGas(t *testing.T) {
/*
pragma solidity ^0.6.4;
contract GasEstimation {
function PureRevert() public { revert(); }
function Revert() public { revert("revert reason");}
function OOG() public { for (uint i = 0; ; i++) {}}
function Assert() public { assert(false);}
function Valid() public {}
}*/
function PureRevert() public { revert(); }
function Revert() public { revert("revert reason");}
function OOG() public { for (uint i = 0; ; i++) {}}
function Assert() public { assert(false);}
function Valid() public {}
}
*/
const contractAbi = "[{\"inputs\":[],\"name\":\"Assert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"OOG\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"PureRevert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"Revert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"Valid\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
const contractBin = "0x60806040523480156100115760006000fd5b50610017565b61016e806100266000396000f3fe60806040523480156100115760006000fd5b506004361061005c5760003560e01c806350f6fe3414610062578063aa8b1d301461006c578063b9b046f914610076578063d8b9839114610080578063e09fface1461008a5761005c565b60006000fd5b61006a610094565b005b6100746100ad565b005b61007e6100b5565b005b6100886100c2565b005b610092610135565b005b6000600090505b5b808060010191505061009b565b505b565b60006000fd5b565b600015156100bf57fe5b5b565b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600d8152602001807f72657665727420726561736f6e0000000000000000000000000000000000000081526020015060200191505060405180910390fd5b565b5b56fea2646970667358221220345bbcbb1a5ecf22b53a78eaebf95f8ee0eceff6d10d4b9643495084d2ec934a64736f6c63430006040033"

Expand Down Expand Up @@ -1079,27 +1081,27 @@ func TestPendingAndCallContract(t *testing.T) {
// This test is based on the following contract:
/*
contract Reverter {
function revertString() public pure{
require(false, "some error");
}
function revertNoString() public pure {
require(false, "");
}
function revertASM() public pure {
assembly {
revert(0x0, 0x0)
}
}
function noRevert() public pure {
assembly {
// Assembles something that looks like require(false, "some error") but is not reverted
mstore(0x0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
mstore(0x4, 0x0000000000000000000000000000000000000000000000000000000000000020)
mstore(0x24, 0x000000000000000000000000000000000000000000000000000000000000000a)
mstore(0x44, 0x736f6d65206572726f7200000000000000000000000000000000000000000000)
return(0x0, 0x64)
}
}
function revertString() public pure{
require(false, "some error");
}
function revertNoString() public pure {
require(false, "");
}
function revertASM() public pure {
assembly {
revert(0x0, 0x0)
}
}
function noRevert() public pure {
assembly {
// Assembles something that looks like require(false, "some error") but is not reverted
mstore(0x0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
mstore(0x4, 0x0000000000000000000000000000000000000000000000000000000000000020)
mstore(0x24, 0x000000000000000000000000000000000000000000000000000000000000000a)
mstore(0x44, 0x736f6d65206572726f7200000000000000000000000000000000000000000000)
return(0x0, 0x64)
}
}
}*/
func TestCallContractRevert(t *testing.T) {
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
Expand Down Expand Up @@ -1226,11 +1228,11 @@ func TestFork(t *testing.T) {
/*
Example contract to test event emission:
pragma solidity >=0.7.0 <0.9.0;
contract Callable {
event Called();
function Call() public { emit Called(); }
}
pragma solidity >=0.7.0 <0.9.0;
contract Callable {
event Called();
function Call() public { emit Called(); }
}
*/
// The fork tests are commented out because transactions are not indexed in coreth until they are marked
// as accepted, which breaks the logic of these tests.
Expand All @@ -1250,7 +1252,7 @@ contract Callable {
// // 7. Mine two blocks to trigger a reorg.
// // 8. Check that the event was removed.
// // 9. Re-send the transaction and mine a block.
// // 10. Check that the event was reborn.
// // 10. Check that the event was reborn.
// func TestForkLogsReborn(t *testing.T) {
// testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
// sim := simTestBackend(testAddr)
Expand Down
4 changes: 3 additions & 1 deletion accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import (
"github.com/ethereum/go-ethereum/event"
)

const basefeeWiggleMultiplier = 2

var (
ErrNilAssetAmount = errors.New("cannot specify nil asset amount for native asset call")
errNativeAssetDeployContract = errors.New("cannot specify native asset params while deploying a contract")
Expand Down Expand Up @@ -316,7 +318,7 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
if gasFeeCap == nil {
gasFeeCap = new(big.Int).Add(
gasTipCap,
new(big.Int).Mul(head.BaseFee, big.NewInt(2)),
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)),
)
}
if gasFeeCap.Cmp(gasTipCap) < 0 {
Expand Down
22 changes: 12 additions & 10 deletions accounts/abi/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ import (
)

// ConvertType converts an interface of a runtime type into a interface of the
// given type
// e.g. turn
// var fields []reflect.StructField
// given type, e.g. turn this code:
//
// var fields []reflect.StructField
//
// fields = append(fields, reflect.StructField{
// Name: "X",
// Type: reflect.TypeOf(new(big.Int)),
// Tag: reflect.StructTag("json:\"" + "x" + "\""),
// }
//
// into
// type TupleT struct { X *big.Int }
// into:
//
// type TupleT struct { X *big.Int }
func ConvertType(in interface{}, proto interface{}) interface{} {
protoType := reflect.TypeOf(proto)
if reflect.TypeOf(in).ConvertibleTo(protoType) {
Expand Down Expand Up @@ -182,11 +183,12 @@ func setStruct(dst, src reflect.Value) error {
}

// mapArgNamesToStructFields maps a slice of argument names to struct fields.
// first round: for each Exportable field that contains a `abi:""` tag
// and this field name exists in the given argument name list, pair them together.
// second round: for each argument name that has not been already linked,
// find what variable is expected to be mapped into, if it exists and has not been
// used, pair them.
//
// first round: for each Exportable field that contains a `abi:""` tag and this field name
// exists in the given argument name list, pair them together.
//
// second round: for each argument name that has not been already linked, find what
// variable is expected to be mapped into, if it exists and has not been used, pair them.
//
// Note this function assumes the given value is a struct value.
func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) {
Expand Down
5 changes: 2 additions & 3 deletions accounts/abi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ import "fmt"
// and struct definition) name will be converted to camelcase style which
// may eventually lead to name conflicts.
//
// Name conflicts are mostly resolved by adding number suffix.
// e.g. if the abi contains Methods send, send1
// ResolveNameConflict would return send2 for input send.
// Name conflicts are mostly resolved by adding number suffix. e.g. if the abi contains
// Methods "send" and "send1", ResolveNameConflict would return "send2" for input "send".
func ResolveNameConflict(rawName string, used func(string) bool) string {
name := rawName
ok := used(name)
Expand Down
Loading

0 comments on commit 481b3f9

Please sign in to comment.