-
Notifications
You must be signed in to change notification settings - Fork 5
/
panic.go
54 lines (46 loc) · 1.48 KB
/
panic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package abi
import (
"fmt"
"math/big"
)
// Panic is the Error instance for panic responses.
var Panic = NewError("Panic", NewTupleType(TupleTypeElem{Name: "error", Type: NewUintType(256)}))
// panicPrefix is the prefix of panic messages. It is the first 4 bytes of the
// keccak256 hash of the string "Panic(uint256)".
var panicPrefix = FourBytes{0x4e, 0x48, 0x7b, 0x71}
// PanicError represents an error returned by contract calls when the call
// panics.
type PanicError struct {
Code *big.Int
}
// Error implements the error interface.
func (e PanicError) Error() string {
return fmt.Sprintf("panic: %s", e.Code.String())
}
// IsPanic returns true if the data has the panic prefix.
func IsPanic(data []byte) bool {
return panicPrefix.Match(data) && len(data) == 36
}
// DecodePanic decodes the panic data returned by contract calls.
// If the data is not a valid panic message, it returns nil.
func DecodePanic(data []byte) *big.Int {
// The code below is a slightly optimized version of
// Panic.DecodeValues(data).
if !IsPanic(data) {
return nil
}
s := &UintValue{Size: 256}
t := TupleValue{TupleValueElem{Value: s}}
if _, err := t.DecodeABI(BytesToWords(data[4:])); err != nil {
return nil
}
return &s.Int
}
// ToPanicError converts the panic data returned by contract calls into a PanicError.
// If the data does not contain a valid panic message, it returns nil.
func ToPanicError(data []byte) error {
if !IsPanic(data) {
return nil
}
return PanicError{Code: DecodePanic(data)}
}