forked from Fantom-foundation/go-opera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_check.go
75 lines (65 loc) · 1.79 KB
/
basic_check.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
74
75
package basiccheck
import (
"errors"
"math"
base "github.com/Fantom-foundation/lachesis-base/eventcheck/basiccheck"
"github.com/ethereum/go-ethereum/core/types"
"github.com/frenchie-foundation/go-opera/evmcore"
"github.com/frenchie-foundation/go-opera/inter"
)
var (
ErrZeroTime = errors.New("event has zero timestamp")
ErrNegativeValue = errors.New("negative value")
ErrIntrinsicGas = errors.New("intrinsic gas too low")
)
type Checker struct {
base base.Checker
}
// New validator which performs checks which don't require anything except event
func New() *Checker {
return &Checker{
base: base.Checker{},
}
}
// validateTx checks whether a transaction is valid according to the consensus
// rules
func (v *Checker) validateTx(tx *types.Transaction) error {
// Transactions can't be negative. This may never happen using RLP decoded
// transactions but may occur if you create a transaction using the RPC.
if tx.Value().Sign() < 0 || tx.GasPrice().Sign() < 0 {
return ErrNegativeValue
}
// Ensure the transaction has more gas than the basic tx fee.
intrGas, err := evmcore.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil)
if err != nil {
return err
}
if tx.Gas() < intrGas {
return ErrIntrinsicGas
}
return nil
}
func (v *Checker) checkTxs(e inter.EventPayloadI) error {
for _, tx := range e.Txs() {
if err := v.validateTx(tx); err != nil {
return err
}
}
return nil
}
// Validate event
func (v *Checker) Validate(e inter.EventPayloadI) error {
if err := v.base.Validate(e); err != nil {
return err
}
if e.GasPowerUsed() >= math.MaxInt64-1 || e.GasPowerLeft().Max() >= math.MaxInt64-1 {
return base.ErrHugeValue
}
if e.CreationTime() <= 0 || e.MedianTime() <= 0 {
return ErrZeroTime
}
if err := v.checkTxs(e); err != nil {
return err
}
return nil
}