2023-06-21 23:25:27 +00:00
|
|
|
package chaingen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
2023-09-21 08:01:49 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/test_helpers"
|
2024-04-22 12:30:43 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/test_helpers/chaingen"
|
2023-06-21 23:25:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
|
|
|
|
"github.com/cerc-io/ipld-eth-db-validator/v5/internal/testdata"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bank, acct1, acct2 common.Address
|
|
|
|
contractAddr common.Address
|
|
|
|
contractDataRoot string
|
2024-04-22 12:30:43 +00:00
|
|
|
defaultContract *chaingen.ContractSpec
|
2023-06-21 23:25:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2024-04-22 12:30:43 +00:00
|
|
|
defaultContract, err = chaingen.ParseContract(testdata.TestContractABI, testdata.TestContractCode)
|
2023-06-21 23:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A GenContext which exactly replicates the chain generator used in existing tests
|
2024-04-22 12:30:43 +00:00
|
|
|
func DefaultGenContext(chainConfig *params.ChainConfig, db ethdb.Database) *chaingen.GenContext {
|
|
|
|
gen := chaingen.NewGenContext(chainConfig, db)
|
2023-06-21 23:25:27 +00:00
|
|
|
bank = gen.AddOwnedAccount(test_helpers.TestBankKey)
|
|
|
|
acct1 = gen.AddOwnedAccount(test_helpers.Account1Key)
|
|
|
|
acct2 = gen.AddOwnedAccount(test_helpers.Account2Key)
|
|
|
|
gen.AddContract("Test", defaultContract)
|
|
|
|
|
|
|
|
gen.AddFunction(func(i int, block *core.BlockGen) {
|
|
|
|
if err := defaultChainGen(gen, i, block); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
gen.Genesis = test_helpers.GenesisBlockForTesting(
|
|
|
|
db, bank, test_helpers.TestBankFunds, big.NewInt(params.InitialBaseFee), params.MaxGasLimit,
|
|
|
|
)
|
|
|
|
return gen
|
|
|
|
}
|
|
|
|
|
2024-04-22 12:30:43 +00:00
|
|
|
func defaultChainGen(gen *chaingen.GenContext, i int, block *core.BlockGen) error {
|
2023-06-21 23:25:27 +00:00
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
// In block 1, the test bank sends account #1 some ether.
|
|
|
|
tx, err := gen.CreateSendTx(bank, acct1, big.NewInt(10000))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx)
|
|
|
|
case 1:
|
|
|
|
// In block 2, the test bank sends some more ether to account #1.
|
|
|
|
// acct1 passes it on to account #2.
|
|
|
|
// acct1 creates a test contract.
|
|
|
|
tx1, err := gen.CreateSendTx(bank, acct1, big.NewInt(1000))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx1)
|
|
|
|
tx2, err := gen.CreateSendTx(acct1, acct2, big.NewInt(1000))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx2)
|
|
|
|
contractAddr, err = gen.DeployContract(acct1, "Test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
block.SetCoinbase(acct2)
|
|
|
|
tx, err := gen.CreateCallTx(bank, contractAddr, "Put", big.NewInt(3))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx)
|
|
|
|
case 3:
|
|
|
|
block.SetCoinbase(acct2)
|
|
|
|
tx, err := gen.CreateCallTx(bank, contractAddr, "Put", big.NewInt(9))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx)
|
|
|
|
case 4:
|
|
|
|
block.SetCoinbase(acct1)
|
|
|
|
tx, err := gen.CreateCallTx(bank, contractAddr, "Put", big.NewInt(0))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|