forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implemented changes for EIP-2929 (access list) * added missing files * fixed unit test Test_opCall * populate access_list with precompiles only if eip-2929 is activated * added address to access list for Balance opcode * added test cases and for EIP-2929 and also call stack output * changed structure of access list and removed some comments * fixed linting errors * fixed AddSlot method * added test cases for access list * removed call stack output for Test_Transition_EIP2929 * corrected indentation * fixed linting errors * fixed linting errors * fixed Test_opReturnDataCopy * removed ActivePrecompile Address slice * changed the const name * changed the const name in comments * change the AddAddress function to a variadic function * refactored * added test cases for SLOAD opcode * fixed linting errors * refactored code * EIP-2565 Implementation (0xPolygon#1704) * implemented EIP-2565 * fixed linting error * fixed linting error * fixed TestModExpWithEIP2565 * resolved comments * resolved comments * resolved merge conflicts * resolved merge conflicts * fixed EIP-2929 test
- Loading branch information
Showing
14 changed files
with
638 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package runtime | ||
|
||
import ( | ||
"github.com/0xPolygon/polygon-edge/types" | ||
) | ||
|
||
type AccessList map[types.Address]map[types.Hash]struct{} | ||
|
||
func NewAccessList() *AccessList { | ||
al := make(AccessList) | ||
|
||
return &al | ||
} | ||
|
||
// Checks if address is present within the access list. | ||
func (al *AccessList) ContainsAddress(address types.Address) bool { | ||
_, ok := (*al)[address] | ||
|
||
return ok | ||
} | ||
|
||
// Contains checks if a slot is present in an account. | ||
// Returns two boolean flags: `accountPresent` and `slotPresent`. | ||
func (al *AccessList) Contains(address types.Address, slot types.Hash) (bool, bool) { | ||
_, addrPresent := (*al)[address] | ||
_, slotPresent := (*al)[address][slot] | ||
|
||
return addrPresent, slotPresent | ||
} | ||
|
||
// Copy creates an deep copy of provided AccessList. | ||
func (al *AccessList) Copy() *AccessList { | ||
cp := make(AccessList, len(*al)) | ||
|
||
for addr, slotMap := range *al { | ||
cpSlotMap := make(map[types.Hash]struct{}, len(slotMap)) | ||
for slotHash := range slotMap { | ||
cpSlotMap[slotHash] = struct{}{} | ||
} | ||
|
||
cp[addr] = cpSlotMap | ||
} | ||
|
||
return &cp | ||
} | ||
|
||
// AddAddress adds an address to the access list | ||
// returns 'true' if the operation results in a change (i.e., the address was not already present in the list). | ||
func (al *AccessList) AddAddress(address ...types.Address) { | ||
for _, addr := range address { | ||
if _, exists := (*al)[addr]; exists { | ||
continue | ||
} | ||
|
||
(*al)[addr] = make(map[types.Hash]struct{}) | ||
} | ||
} | ||
|
||
// This function adds the specified address and slot pair to the access list. | ||
// The return values indicate whether the address was newly added and whether the slot was newly added. | ||
func (al *AccessList) AddSlot(address types.Address, slot types.Hash) (addrChange bool, slotChange bool) { | ||
slotMap, addressExists := (*al)[address] | ||
if !addressExists { | ||
slotMap = make(map[types.Hash]struct{}) | ||
(*al)[address] = slotMap | ||
} | ||
|
||
_, slotPresent := slotMap[slot] | ||
if !slotPresent { | ||
slotMap[slot] = struct{}{} | ||
|
||
return !addressExists, true | ||
} | ||
|
||
// slot and address were already present in access list | ||
return false, false | ||
} |
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,99 @@ | ||
package runtime | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/0xPolygon/polygon-edge/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
address1 = types.BytesToAddress([]byte("address1")) | ||
address2 = types.BytesToAddress([]byte("address2")) | ||
address3 = types.BytesToAddress([]byte("address3")) | ||
slotHash = types.BytesToHash([]byte("slot")) | ||
) | ||
|
||
func createInitialAccessList() *AccessList { | ||
initialAccessList := NewAccessList() | ||
(*initialAccessList)[address1] = make(map[types.Hash]struct{}) | ||
(*initialAccessList)[address1][slotHash] = struct{}{} | ||
(*initialAccessList)[address2] = make(map[types.Hash]struct{}) | ||
|
||
return initialAccessList | ||
} | ||
|
||
func TestContainsAddress(t *testing.T) { | ||
t.Parallel() | ||
|
||
initialAccessList := createInitialAccessList() | ||
assert.True(t, initialAccessList.ContainsAddress(address1)) | ||
assert.False(t, initialAccessList.ContainsAddress(address3)) | ||
} | ||
|
||
func TestContains(t *testing.T) { | ||
t.Parallel() | ||
|
||
initialAccessList := createInitialAccessList() | ||
|
||
address1Present, slotHashPresent := initialAccessList.Contains(address1, slotHash) | ||
assert.True(t, address1Present) | ||
assert.True(t, slotHashPresent) | ||
|
||
address3Present, slotHashPresent := initialAccessList.Contains(address3, slotHash) | ||
assert.False(t, address3Present) | ||
assert.False(t, slotHashPresent) | ||
} | ||
|
||
func TestCopy(t *testing.T) { | ||
t.Parallel() | ||
|
||
initialAccessList := createInitialAccessList() | ||
copiedAccessList := initialAccessList.Copy() | ||
assert.Equal(t, initialAccessList, copiedAccessList) | ||
} | ||
|
||
func TestAddAddress(t *testing.T) { | ||
t.Parallel() | ||
|
||
initialAccessList := createInitialAccessList() | ||
finalAccessList := createInitialAccessList() | ||
|
||
initialAccessList.AddAddress(address1) | ||
assert.Equal(t, finalAccessList, initialAccessList) | ||
|
||
initialAccessList.AddAddress(address3) | ||
|
||
(*finalAccessList)[address3] = make(map[types.Hash]struct{}) | ||
assert.Equal(t, finalAccessList, initialAccessList) | ||
} | ||
|
||
func TestAddSlot(t *testing.T) { | ||
t.Parallel() | ||
|
||
initialAccessList := createInitialAccessList() | ||
finalAccessList := createInitialAccessList() | ||
|
||
// add address1 and slotHash | ||
addr1Exists, slot1Exists := initialAccessList.AddSlot(address1, slotHash) | ||
assert.False(t, addr1Exists) | ||
assert.False(t, slot1Exists) | ||
assert.Equal(t, finalAccessList, initialAccessList) | ||
|
||
// add address2 and slotHash | ||
addr2Exists, slot2Exists := initialAccessList.AddSlot(address2, slotHash) | ||
assert.False(t, addr2Exists) | ||
assert.True(t, slot2Exists) | ||
|
||
(*finalAccessList)[address2][slotHash] = struct{}{} | ||
assert.Equal(t, finalAccessList, initialAccessList) | ||
|
||
// add address3 and slotHash | ||
addr3Exists, slot3Exists := initialAccessList.AddSlot(address3, slotHash) | ||
assert.True(t, addr3Exists) | ||
assert.True(t, slot3Exists) | ||
|
||
(*finalAccessList)[address3] = make(map[types.Hash]struct{}) | ||
(*finalAccessList)[address3][slotHash] = struct{}{} | ||
assert.Equal(t, finalAccessList, initialAccessList) | ||
} |
Oops, something went wrong.