forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
73 lines (62 loc) · 2.16 KB
/
utils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package core
import (
"fmt"
"math/big"
"github.com/ten-protocol/go-ten/go/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/measure"
)
// VerifySignature - Checks that the L2Tx has a valid signature.
func VerifySignature(chainID int64, tx *types.Transaction) error {
signer := types.NewLondonSigner(big.NewInt(chainID))
_, err := types.Sender(signer, tx)
return err
}
// GetAuthenticatedSender - Get sender and tx nonce from transaction
func GetAuthenticatedSender(chainID int64, tx *types.Transaction) (*gethcommon.Address, error) {
signer := types.NewLondonSigner(big.NewInt(chainID))
sender, err := types.Sender(signer, tx)
if err != nil {
return nil, err
}
return &sender, nil
}
const (
// log level for requests that take longer than this threshold in millis
_errorThreshold = 500
_warnThreshold = 200
_infoThreshold = 100
_debugThreshold = 50
)
// LogMethodDuration - call only with "defer"
func LogMethodDuration(logger gethlog.Logger, stopWatch *measure.Stopwatch, msg string, args ...any) {
var f func(msg string, ctx ...interface{})
durationMillis := stopWatch.Measure().Milliseconds()
// we adjust the logging level based on the time
switch {
case durationMillis > _errorThreshold:
f = logger.Error
case durationMillis > _warnThreshold:
f = logger.Warn
case durationMillis > _infoThreshold:
f = logger.Info
case durationMillis > _debugThreshold:
f = logger.Debug
default:
f = logger.Trace
}
newArgs := append([]any{log.DurationKey, stopWatch}, args...)
f(fmt.Sprintf("LogMethodDuration::%s", msg), newArgs...)
}
// GetTxSigner returns the address that signed a transaction
func GetTxSigner(tx *common.L2Tx) (gethcommon.Address, error) {
// TODO - Once the enclave's genesis.json is set, retrieve the signer type using `types.MakeSigner`.
from, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
return gethcommon.Address{}, fmt.Errorf("could not recover sender for transaction. Cause: %w", err)
}
return from, nil
}