2023-03-14 21:26:20 +01:00
|
|
|
package ethapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2024-10-11 16:44:50 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-03-14 21:26:20 +01:00
|
|
|
)
|
|
|
|
|
2023-03-31 08:34:47 +02:00
|
|
|
const MaxNumberOfEntries = 1000
|
|
|
|
|
2024-10-11 16:44:50 +08:00
|
|
|
func TxOptsCheck(o types.TransactionOpts, blockNumber uint64, timeStamp uint64, statedb *state.StateDB) error {
|
2023-03-14 21:26:20 +01:00
|
|
|
if o.BlockNumberMin != nil && blockNumber < uint64(*o.BlockNumberMin) {
|
|
|
|
return errors.New("BlockNumberMin condition not met")
|
|
|
|
}
|
|
|
|
if o.BlockNumberMax != nil && blockNumber > uint64(*o.BlockNumberMax) {
|
|
|
|
return errors.New("BlockNumberMax condition not met")
|
|
|
|
}
|
|
|
|
if o.TimestampMin != nil && timeStamp < uint64(*o.TimestampMin) {
|
|
|
|
return errors.New("TimestampMin condition not met")
|
|
|
|
}
|
|
|
|
if o.TimestampMax != nil && timeStamp > uint64(*o.TimestampMax) {
|
|
|
|
return errors.New("TimestampMax condition not met")
|
|
|
|
}
|
2023-03-22 08:23:26 +01:00
|
|
|
counter := 0
|
|
|
|
for _, account := range o.KnownAccounts {
|
2023-03-29 21:55:41 +02:00
|
|
|
if account.StorageRoot != nil {
|
2023-03-22 08:23:26 +01:00
|
|
|
counter += 1
|
2023-03-29 21:55:41 +02:00
|
|
|
} else if account.StorageSlots != nil {
|
|
|
|
counter += len(account.StorageSlots)
|
2023-03-22 08:23:26 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 08:34:47 +02:00
|
|
|
if counter > MaxNumberOfEntries {
|
2023-03-14 21:26:20 +01:00
|
|
|
return errors.New("knownAccounts too large")
|
|
|
|
}
|
2024-10-11 16:44:50 +08:00
|
|
|
return TxOptsCheckStorage(o, statedb)
|
2023-03-14 21:26:20 +01:00
|
|
|
}
|
|
|
|
|
2024-10-11 16:44:50 +08:00
|
|
|
func TxOptsCheckStorage(o types.TransactionOpts, statedb *state.StateDB) error {
|
2023-03-14 21:26:20 +01:00
|
|
|
for address, accountStorage := range o.KnownAccounts {
|
2023-03-29 21:55:41 +02:00
|
|
|
if accountStorage.StorageRoot != nil {
|
2023-03-31 08:34:47 +02:00
|
|
|
rootHash := statedb.GetRoot(address)
|
|
|
|
if rootHash != *accountStorage.StorageRoot {
|
2023-03-14 21:26:20 +01:00
|
|
|
return errors.New("storage root hash condition not met")
|
|
|
|
}
|
2023-03-29 21:55:41 +02:00
|
|
|
} else if len(accountStorage.StorageSlots) > 0 {
|
|
|
|
for slot, value := range accountStorage.StorageSlots {
|
2023-03-14 21:26:20 +01:00
|
|
|
stored := statedb.GetState(address, slot)
|
|
|
|
if !bytes.Equal(stored.Bytes(), value.Bytes()) {
|
|
|
|
return errors.New("storage slot value condition not met")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|