Merge pull request #92 from 0glabs/readonly

fix: read only
This commit is contained in:
MiniFrenchBread 2024-12-26 18:33:53 +08:00 committed by GitHub
commit 559d1beb03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -4,4 +4,5 @@ const (
ErrGetStateDB = "get EVM StateDB failed"
ErrInvalidNumberOfArgs = "invalid number of arguments; expected %d; got: %d"
ErrSenderNotOrigin = "msg.sender is not from tx origin"
ErrWriteOnReadOnly = "read only call to write functions"
)

View File

@ -92,6 +92,17 @@ func (d *DASignersPrecompile) RequiredGas(input []byte) uint64 {
return RequiredGasMax
}
func (d *DASignersPrecompile) IsTx(method string) bool {
switch method {
case DASignersFunctionUpdateSocket,
DASignersFunctionRegisterSigner,
DASignersFunctionRegisterNextEpoch:
return true
default:
return false
}
}
// Run implements vm.PrecompiledContract.
func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
// parse input
@ -106,6 +117,10 @@ func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly b
if err != nil {
return nil, err
}
// readonly check
if readonly && d.IsTx(method.Name) {
return nil, fmt.Errorf(precopmiles_common.ErrWriteOnReadOnly)
}
// get state db and context
stateDB, ok := evm.StateDB.(*statedb.StateDB)
if !ok {

View File

@ -67,6 +67,20 @@ func (s *StakingPrecompile) RequiredGas(input []byte) uint64 {
return 0
}
func (s *StakingPrecompile) IsTx(method string) bool {
switch method {
case StakingFunctionCreateValidator,
StakingFunctionEditValidator,
StakingFunctionDelegate,
StakingFunctionBeginRedelegate,
StakingFunctionUndelegate,
StakingFunctionCancelUnbondingDelegation:
return true
default:
return false
}
}
// Run implements vm.PrecompiledContract.
func (s *StakingPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
// parse input
@ -81,6 +95,10 @@ func (s *StakingPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly boo
if err != nil {
return nil, err
}
// readonly check
if readonly && s.IsTx(method.Name) {
return nil, fmt.Errorf(precopmiles_common.ErrWriteOnReadOnly)
}
// get state db and context
stateDB, ok := evm.StateDB.(*statedb.StateDB)
if !ok {