Skip to content

Commit c77ba56

Browse files
author
Darioush Jalali
authored
all changes from v.13.2-x branch (#1141)
1 parent 82aed45 commit c77ba56

File tree

245 files changed

+6307
-4504
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+6307
-4504
lines changed

accounts/abi/abi.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"errors"
3333
"fmt"
3434
"io"
35+
"math/big"
3536

3637
"github.com/ethereum/go-ethereum/common"
3738
"github.com/ethereum/go-ethereum/crypto"
@@ -372,24 +373,65 @@ func (abi *ABI) HasReceive() bool {
372373
// revertSelector is a special function selector for revert reason unpacking.
373374
var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4]
374375

376+
// panicSelector is a special function selector for panic reason unpacking.
377+
var panicSelector = crypto.Keccak256([]byte("Panic(uint256)"))[:4]
378+
379+
// panicReasons map is for readable panic codes
380+
// see this linkage for the deails
381+
// https://docs.soliditylang.org/en/v0.8.21/control-structures.html#panic-via-assert-and-error-via-require
382+
// the reason string list is copied from ether.js
383+
// https://github.com/ethers-io/ethers.js/blob/fa3a883ff7c88611ce766f58bdd4b8ac90814470/src.ts/abi/interface.ts#L207-L218
384+
var panicReasons = map[uint64]string{
385+
0x00: "generic panic",
386+
0x01: "assert(false)",
387+
0x11: "arithmetic underflow or overflow",
388+
0x12: "division or modulo by zero",
389+
0x21: "enum overflow",
390+
0x22: "invalid encoded storage byte array accessed",
391+
0x31: "out-of-bounds array access; popping on an empty array",
392+
0x32: "out-of-bounds access of an array or bytesN",
393+
0x41: "out of memory",
394+
0x51: "uninitialized function",
395+
}
396+
375397
// UnpackRevert resolves the abi-encoded revert reason. According to the solidity
376398
// spec https://solidity.readthedocs.io/en/latest/control-structures.html#revert,
377-
// the provided revert reason is abi-encoded as if it were a call to a function
378-
// `Error(string)`. So it's a special tool for it.
399+
// the provided revert reason is abi-encoded as if it were a call to function
400+
// `Error(string)` or `Panic(uint256)`. So it's a special tool for it.
379401
func UnpackRevert(data []byte) (string, error) {
380402
if len(data) < 4 {
381403
return "", errors.New("invalid data for unpacking")
382404
}
383-
if !bytes.Equal(data[:4], revertSelector) {
405+
switch {
406+
case bytes.Equal(data[:4], revertSelector):
407+
typ, err := NewType("string", "", nil)
408+
if err != nil {
409+
return "", err
410+
}
411+
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
412+
if err != nil {
413+
return "", err
414+
}
415+
return unpacked[0].(string), nil
416+
case bytes.Equal(data[:4], panicSelector):
417+
typ, err := NewType("uint256", "", nil)
418+
if err != nil {
419+
return "", err
420+
}
421+
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
422+
if err != nil {
423+
return "", err
424+
}
425+
pCode := unpacked[0].(*big.Int)
426+
// uint64 safety check for future
427+
// but the code is not bigger than MAX(uint64) now
428+
if pCode.IsUint64() {
429+
if reason, ok := panicReasons[pCode.Uint64()]; ok {
430+
return reason, nil
431+
}
432+
}
433+
return fmt.Sprintf("unknown panic code: %#x", pCode), nil
434+
default:
384435
return "", errors.New("invalid data for unpacking")
385436
}
386-
typ, err := NewType("string", "", nil)
387-
if err != nil {
388-
return "", err
389-
}
390-
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:])
391-
if err != nil {
392-
return "", err
393-
}
394-
return unpacked[0].(string), nil
395437
}

accounts/abi/abi_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,8 @@ func TestUnpackRevert(t *testing.T) {
11841184
{"", "", errors.New("invalid data for unpacking")},
11851185
{"08c379a1", "", errors.New("invalid data for unpacking")},
11861186
{"08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000", "revert reason", nil},
1187+
{"4e487b710000000000000000000000000000000000000000000000000000000000000000", "generic panic", nil},
1188+
{"4e487b7100000000000000000000000000000000000000000000000000000000000000ff", "unknown panic code: 0xff", nil},
11871189
}
11881190
for index, c := range cases {
11891191
t.Run(fmt.Sprintf("case %d", index), func(t *testing.T) {

accounts/abi/bind/backends/simulated.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address,
235235
if err != nil {
236236
return nil, err
237237
}
238-
239238
return stateDB.GetCode(contract), nil
240239
}
241240

@@ -248,7 +247,6 @@ func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Addres
248247
if err != nil {
249248
return nil, err
250249
}
251-
252250
return stateDB.GetBalance(contract), nil
253251
}
254252

@@ -261,7 +259,6 @@ func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address,
261259
if err != nil {
262260
return 0, err
263261
}
264-
265262
return stateDB.GetNonce(contract), nil
266263
}
267264

@@ -274,7 +271,6 @@ func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Addres
274271
if err != nil {
275272
return nil, err
276273
}
277-
278274
val := stateDB.GetState(contract, key)
279275
return val[:], nil
280276
}
@@ -739,8 +735,10 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
739735
if err != nil {
740736
return err
741737
}
742-
stateDB, _ := b.blockchain.State()
743-
738+
stateDB, err := b.blockchain.State()
739+
if err != nil {
740+
return err
741+
}
744742
b.acceptedBlock = blocks[0]
745743
b.acceptedState, _ = state.New(b.acceptedBlock.Root(), stateDB.Database(), nil)
746744
return nil
@@ -858,11 +856,12 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
858856
blocks, _, _ := core.GenerateChain(b.config, block, dummy.NewFaker(), b.database, 1, 10, func(number int, block *core.BlockGen) {
859857
block.OffsetTime(int64(adjustment.Seconds()))
860858
})
861-
stateDB, _ := b.blockchain.State()
862-
859+
stateDB, err := b.blockchain.State()
860+
if err != nil {
861+
return err
862+
}
863863
b.acceptedBlock = blocks[0]
864864
b.acceptedState, _ = state.New(b.acceptedBlock.Root(), stateDB.Database(), nil)
865-
866865
return nil
867866
}
868867

accounts/abi/method.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ func NewMethod(name string, rawName string, funType FunctionType, mutability str
137137
state = state + " "
138138
}
139139
identity := fmt.Sprintf("function %v", rawName)
140-
if funType == Fallback {
140+
switch funType {
141+
case Fallback:
141142
identity = "fallback"
142-
} else if funType == Receive {
143+
case Receive:
143144
identity = "receive"
144-
} else if funType == Constructor {
145+
case Constructor:
145146
identity = "constructor"
146147
}
147148
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", "))

accounts/abi/method_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ func TestMethodString(t *testing.T) {
9494

9595
for _, test := range table {
9696
var got string
97-
if test.method == "fallback" {
97+
switch test.method {
98+
case "fallback":
9899
got = abi.Fallback.String()
99-
} else if test.method == "receive" {
100+
case "receive":
100101
got = abi.Receive.String()
101-
} else {
102+
default:
102103
got = abi.Methods[test.method].String()
103104
}
104105
if got != test.expectation {

accounts/abi/unpack.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
170170
// this value will become our slice or our array, depending on the type
171171
var refSlice reflect.Value
172172

173-
if t.T == SliceTy {
173+
switch t.T {
174+
case SliceTy:
174175
// declare our slice
175176
refSlice = reflect.MakeSlice(t.GetType(), size, size)
176-
} else if t.T == ArrayTy {
177+
case ArrayTy:
177178
// declare our array
178179
refSlice = reflect.New(t.GetType()).Elem()
179-
} else {
180+
default:
180181
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
181182
}
182183

accounts/keystore/watch.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package keystore
3131

3232
import (
33+
"os"
3334
"time"
3435

3536
"github.com/ethereum/go-ethereum/log"
@@ -87,7 +88,9 @@ func (w *watcher) loop() {
8788
}
8889
defer watcher.Close()
8990
if err := watcher.Add(w.ac.keydir); err != nil {
90-
logger.Warn("Failed to watch keystore folder", "err", err)
91+
if !os.IsNotExist(err) {
92+
logger.Warn("Failed to watch keystore folder", "err", err)
93+
}
9194
return
9295
}
9396

cmd/evm/internal/t8ntool/execution.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,26 @@ type ommer struct {
7676

7777
//go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
7878
type stEnv struct {
79-
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
80-
Difficulty *big.Int `json:"currentDifficulty"`
81-
Random *big.Int `json:"currentRandom"`
82-
ParentDifficulty *big.Int `json:"parentDifficulty"`
83-
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
84-
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
85-
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
86-
MinBaseFee *big.Int `json:"minBaseFee,omitempty"`
87-
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
88-
Number uint64 `json:"currentNumber" gencodec:"required"`
89-
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
90-
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
91-
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
92-
Ommers []ommer `json:"ommers,omitempty"`
93-
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
94-
ParentUncleHash common.Hash `json:"parentUncleHash"`
95-
ExcessBlobGas *uint64 `json:"excessBlobGas,omitempty"`
96-
ParentExcessBlobGas *uint64 `json:"parentExcessBlobGas,omitempty"`
97-
ParentBlobGasUsed *uint64 `json:"parentBlobGasUsed,omitempty"`
79+
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
80+
Difficulty *big.Int `json:"currentDifficulty"`
81+
Random *big.Int `json:"currentRandom"`
82+
ParentDifficulty *big.Int `json:"parentDifficulty"`
83+
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
84+
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
85+
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
86+
MinBaseFee *big.Int `json:"minBaseFee,omitempty"`
87+
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
88+
Number uint64 `json:"currentNumber" gencodec:"required"`
89+
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
90+
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
91+
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
92+
Ommers []ommer `json:"ommers,omitempty"`
93+
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
94+
ParentUncleHash common.Hash `json:"parentUncleHash"`
95+
ExcessBlobGas *uint64 `json:"excessBlobGas,omitempty"`
96+
ParentExcessBlobGas *uint64 `json:"parentExcessBlobGas,omitempty"`
97+
ParentBlobGasUsed *uint64 `json:"parentBlobGasUsed,omitempty"`
98+
ParentBeaconBlockRoot *common.Hash `json:"parentBeaconBlockRoot"`
9899
}
99100

100101
type stEnvMarshaling struct {
@@ -191,6 +192,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
191192
// chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
192193
// misc.ApplyDAOHardFork(statedb)
193194
// }
195+
if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
196+
evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
197+
core.ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
198+
}
194199
var blobGasUsed uint64
195200
for i, tx := range txs {
196201
if tx.Type() == types.BlobTxType && vmContext.ExcessBlobGas == nil {

cmd/evm/internal/t8ntool/gen_stenv.go

Lines changed: 44 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)