2020-06-30 08:12:51 +00:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package t8ntool
|
|
|
|
|
|
|
|
import (
|
2022-06-15 20:17:16 +00:00
|
|
|
"encoding/binary"
|
2020-06-30 08:12:51 +00:00
|
|
|
"math/big"
|
2020-08-07 12:25:40 +00:00
|
|
|
|
|
|
|
"github.com/holiman/uint256"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-01-14 19:06:35 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
2022-07-07 11:47:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2020-06-30 08:12:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Prestate struct {
|
|
|
|
Env stEnv `json:"env"`
|
|
|
|
Pre core.GenesisAlloc `json:"pre"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ommer struct {
|
|
|
|
Delta uint64 `json:"delta"`
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
|
|
|
|
type stEnv struct {
|
2022-07-07 11:47:00 +00:00
|
|
|
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
|
|
|
|
Difficulty *big.Int `json:"currentDifficulty"`
|
|
|
|
Random *big.Int `json:"currentRandom"`
|
|
|
|
ParentDifficulty *big.Int `json:"parentDifficulty"`
|
|
|
|
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
|
|
|
|
Number uint64 `json:"currentNumber" gencodec:"required"`
|
|
|
|
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
|
|
|
|
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
|
|
|
|
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
|
|
|
|
Ommers []ommer `json:"ommers,omitempty"`
|
|
|
|
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
|
|
|
|
ParentUncleHash common.Hash `json:"parentUncleHash"`
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
type stEnvMarshaling struct {
|
2022-07-07 11:47:00 +00:00
|
|
|
Coinbase common.UnprefixedAddress
|
|
|
|
Difficulty *math.HexOrDecimal256
|
|
|
|
Random *math.HexOrDecimal256
|
|
|
|
ParentDifficulty *math.HexOrDecimal256
|
|
|
|
GasLimit math.HexOrDecimal64
|
|
|
|
Number math.HexOrDecimal64
|
|
|
|
Timestamp math.HexOrDecimal64
|
|
|
|
ParentTimestamp math.HexOrDecimal64
|
|
|
|
BaseFee *math.HexOrDecimal256
|
2020-06-30 08:12:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 11:47:00 +00:00
|
|
|
func MakePreState(chainRules *params.Rules, tx kv.RwTx, accounts core.GenesisAlloc) (*state.PlainStateReader, *state.PlainStateWriter) {
|
2021-05-16 05:21:29 +00:00
|
|
|
var blockNr uint64 = 0
|
2022-07-07 11:47:00 +00:00
|
|
|
stateReader, stateWriter := state.NewPlainStateReader(tx), state.NewPlainStateWriter(tx, tx, blockNr)
|
|
|
|
statedb := state.New(stateReader) //ibs
|
2020-06-30 08:12:51 +00:00
|
|
|
for addr, a := range accounts {
|
|
|
|
statedb.SetCode(addr, a.Code)
|
|
|
|
statedb.SetNonce(addr, a.Nonce)
|
2021-05-16 05:21:29 +00:00
|
|
|
balance, _ := uint256.FromBig(a.Balance)
|
|
|
|
statedb.SetBalance(addr, balance)
|
2020-06-30 08:12:51 +00:00
|
|
|
for k, v := range a.Storage {
|
2021-05-16 05:21:29 +00:00
|
|
|
key := k
|
2021-06-04 16:25:28 +00:00
|
|
|
val := uint256.NewInt(0).SetBytes(v.Bytes())
|
2021-05-16 05:21:29 +00:00
|
|
|
statedb.SetState(addr, &key, *val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.Code) > 0 || len(a.Storage) > 0 {
|
2022-06-15 20:17:16 +00:00
|
|
|
statedb.SetIncarnation(addr, state.FirstContractIncarnation)
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation)
|
|
|
|
tx.Put(kv.IncarnationMap, addr[:], b[:])
|
2020-06-30 08:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 05:21:29 +00:00
|
|
|
// Commit and re-open to start with a clean state.
|
2021-07-24 07:14:11 +00:00
|
|
|
if err := statedb.FinalizeTx(chainRules, state.NewPlainStateWriter(tx, tx, blockNr+1)); err != nil {
|
2020-08-07 12:25:40 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-24 07:14:11 +00:00
|
|
|
if err := statedb.CommitBlock(chainRules, state.NewPlainStateWriter(tx, tx, blockNr+1)); err != nil {
|
2020-08-07 12:25:40 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-07 11:47:00 +00:00
|
|
|
return stateReader, stateWriter
|
2020-06-30 08:12:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 11:47:00 +00:00
|
|
|
// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
|
|
|
|
// the caller does not provide an explicit difficulty, but instead provides only
|
|
|
|
// parent timestamp + difficulty.
|
|
|
|
// Note: this method only works for ethash engine.
|
|
|
|
func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64,
|
|
|
|
parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
|
|
|
|
uncleHash := parentUncleHash
|
|
|
|
if uncleHash == (common.Hash{}) {
|
|
|
|
uncleHash = types.EmptyUncleHash
|
|
|
|
}
|
|
|
|
parent := &types.Header{
|
|
|
|
ParentHash: common.Hash{},
|
|
|
|
UncleHash: uncleHash,
|
|
|
|
Difficulty: parentDifficulty,
|
|
|
|
Number: new(big.Int).SetUint64(number - 1),
|
|
|
|
Time: parentTime,
|
|
|
|
}
|
|
|
|
return ethash.CalcDifficulty(config, currentTime, parent.Time, parent.Difficulty, number-1, parent.UncleHash)
|
2020-06-30 08:12:51 +00:00
|
|
|
}
|