-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/ethapi: implement API for EIP-4337
- Loading branch information
1 parent
0d87a13
commit 973a830
Showing
7 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ethapi | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
) | ||
|
||
type AccountStorage struct { | ||
RootHash *common.Hash | ||
SlotValue map[common.Hash]common.Hash | ||
} | ||
|
||
func (r *AccountStorage) UnmarshalJSON(data []byte) error { | ||
var hash common.Hash | ||
if err := json.Unmarshal(data, &hash); err == nil { | ||
r.RootHash = &hash | ||
return nil | ||
} | ||
return json.Unmarshal(data, &r.SlotValue) | ||
} | ||
|
||
func (r AccountStorage) MarshalJSON() ([]byte, error) { | ||
if r.RootHash != nil { | ||
return json.Marshal(*r.RootHash) | ||
} | ||
return json.Marshal(r.SlotValue) | ||
} | ||
|
||
type TransactionOpts struct { | ||
KnownAccounts map[common.Address]AccountStorage `json:"knownAccounts"` | ||
BlockNumberMin *hexutil.Uint64 `json:"blockNumberMin,omitempty"` | ||
BlockNumberMax *hexutil.Uint64 `json:"blockNumberMax,omitempty"` | ||
TimestampMin *hexutil.Uint64 `json:"timestampMin,omitempty"` | ||
TimestampMax *hexutil.Uint64 `json:"timestampMax,omitempty"` | ||
} | ||
|
||
func (o *TransactionOpts) Check(blockNumber uint64, timeStamp uint64, statedb *state.StateDB) error { | ||
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") | ||
} | ||
if len(o.KnownAccounts) > 1000 { | ||
return errors.New("knownAccounts too large") | ||
} | ||
return o.CheckOnlyStorage(statedb) | ||
} | ||
|
||
func (o *TransactionOpts) CheckOnlyStorage(statedb *state.StateDB) error { | ||
for address, AccountStorage := range o.KnownAccounts { | ||
if AccountStorage.RootHash != nil { | ||
trie := statedb.StorageTrie(address) | ||
if trie == nil { | ||
return errors.New("Storage trie not found for address key in knownAccounts option") | ||
} | ||
if trie.Hash() != *AccountStorage.RootHash { | ||
return errors.New("Storage root hash condition not met") | ||
} | ||
} else if len(AccountStorage.SlotValue) > 0 { | ||
for slot, value := range AccountStorage.SlotValue { | ||
stored := statedb.GetState(address, slot) | ||
if !bytes.Equal(stored.Bytes(), value.Bytes()) { | ||
return errors.New("Storage slot value condition not met") | ||
} | ||
} | ||
} // else AccountStorage.SlotValue is empty - ignore it and check the rest of conditions | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters