Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
VeerChaurasia committed Nov 2, 2024
1 parent 31e5e55 commit 8ac3173
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 42 deletions.
8 changes: 4 additions & 4 deletions execution/evm/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ type LegacyAnalyzedBytecode struct {
// JumpTable equivalent in Go, using a sync.Once pointer to simulate Arc and BitVec<u8>.
type JumpTable struct {
BitVector *Bitvector // Simulating BitVec<u8> as []byte
once sync.Once // Lazy initialization if needed
Once sync.Once // Lazy initialization if needed
}
type Bitvector struct {
bits []uint8
size int // Total number of bits represented
Bits []uint8
Size int // Total number of bits represented
}
type Opcode struct {
InitCode Eof `json:"initcode"`
Expand Down Expand Up @@ -161,4 +161,4 @@ type TypesSection struct {

func (t *TypesSection) UnmarshalJSON(data []byte) error {
return unmarshalJSON(data, t)
}
}
8 changes: 1 addition & 7 deletions execution/evm/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,19 @@ func TestOpcodeUnmarshalJSON(t *testing.T) {
}
}

// Helper function to convert hex string to byte slice
func hexToBytes(hexStr string) []byte {
bytes, err := hex.DecodeString(hexStr[2:])
if err != nil {
return nil
}
return bytes
}

// Helper function to parse hex address string to Address struct
func parseHexAddress(hexStr string) [20]byte {
var addr [20]byte
bytes, _ := hex.DecodeString(hexStr[2:])
copy(addr[:], bytes)
return addr
}

// Comparison function for Opcode struct
func compareOpcodes(a, b Opcode) bool {
return compareEof(a.InitCode, b.InitCode) && a.CreatedAddress == b.CreatedAddress && reflect.DeepEqual(a.Input, b.Input)
}
Expand All @@ -148,7 +143,6 @@ func compareEof(a, b Eof) bool {
reflect.DeepEqual(a.Raw, b.Raw)
}

// Comparison function for EofHeader struct
func compareEofHeader(a, b EofHeader) bool {
return a.TypesSize == b.TypesSize &&
reflect.DeepEqual(a.CodeSizes, b.CodeSizes) &&
Expand All @@ -165,4 +159,4 @@ func compareEofBody(a, b EofBody) bool {
reflect.DeepEqual(a.ContainerSection, b.ContainerSection) &&
reflect.DeepEqual(a.DataSection, b.DataSection) &&
a.IsDataFilled == b.IsDataFilled
}
}
40 changes: 10 additions & 30 deletions execution/evm/call_inputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"math/big"
"reflect"
"fmt"
"testing"
)

Expand All @@ -31,7 +30,7 @@ func TestCallInputsJSON(t *testing.T) {
"is_static": false,
"is_eof": false
}`,

expected: CallInputs{
Input: hexToBytes("0x1234"),
ReturnMemoryOffset: Range{Start: 0, End: 32},
Expand All @@ -41,7 +40,7 @@ func TestCallInputsJSON(t *testing.T) {
Caller: Address{Addr: parseHexAddress("0x9876543210fedcba9876543210fedcba98765432")},
Value: CallValue{
ValueType: "transfer",
Amount: parseU256("0xde0b6b3a7640000"),
Amount: parseU256("0xde0b6b3a7640000"),
},
Scheme: ICall,
IsStatic: false,
Expand All @@ -58,8 +57,6 @@ func TestCallInputsJSON(t *testing.T) {
expected: CallInputs{},
hasError: true,
},


}

for _, tc := range testCases {
Expand All @@ -77,11 +74,10 @@ func TestCallInputsJSON(t *testing.T) {
}
}
})

}
}


type TestCase2 struct {
name string
input string
Expand All @@ -99,7 +95,7 @@ func TestCallValueJSON(t *testing.T) {
}`,
expected: CallValue{
ValueType: "transfer",
Amount: parseU256("0xde0b6b3a7640000"),
Amount: parseU256("0xde0b6b3a7640000"),
},
hasError: false,
},
Expand All @@ -111,11 +107,10 @@ func TestCallValueJSON(t *testing.T) {
}`,
expected: CallValue{
ValueType: "apparent",
Amount: parseU256("0x6f05b59d3b20000"),
Amount: parseU256("0x6f05b59d3b20000"),
},
hasError: false,
},

}

for _, tc := range testCases {
Expand Down Expand Up @@ -163,7 +158,7 @@ func TestCreateInputsJSON(t *testing.T) {
SchemeType: SchemeTypeCreate,
Salt: parsePtrU256("0x1234567890abcdef1234567890abcdef12345678"),
},
Value: parseU256("0xde0b6b3a7640000"),
Value: parseU256("0xde0b6b3a7640000"),
InitCode: hexToBytes("0x010203"),
GasLimit: 100000,
},
Expand Down Expand Up @@ -276,26 +271,13 @@ func TestCreateSchemeJSON(t *testing.T) {
// return addr
// }

func parseUint64(hexStr string) (uint64, error) {
value, ok := new(big.Int).SetString(hexStr[2:], 16)
if !ok {
return 0, fmt.Errorf("invalid hex string: %s", hexStr)
}
return value.Uint64(), nil
}

func parseU256(hexStr string) U256 {
value, ok := new(big.Int).SetString(hexStr[2:], 16)
if !ok {
value, ok := new(big.Int).SetString(hexStr[2:], 16)
if !ok {
return U256(new(big.Int)) // Return a zero value or another default
}
return U256(value)
}
return U256(value)
}





func parsePtrU256(hexStr string) *U256 {
value, _ := new(big.Int).SetString(hexStr[2:], 16)
u256 := U256(value)
Expand Down Expand Up @@ -329,8 +311,6 @@ func compareCreateInputs(a, b *CreateInputs) bool {
a.GasLimit == b.GasLimit
}



// Comparison function for CreateSchemes
func compareCreateSchemes(a, b *CreateScheme) bool {
// Compare SchemeType
Expand Down
Loading

0 comments on commit 8ac3173

Please sign in to comment.