Skip to content

Commit

Permalink
Make IncrementBy method of Address. Reorganize functions to be groupe…
Browse files Browse the repository at this point in the history
…d by structs
  • Loading branch information
Stefan-Ethernal committed Sep 12, 2024
1 parent fc12483 commit cfaafe6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 77 deletions.
4 changes: 2 additions & 2 deletions command/bridge/deploy/internal_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ func preAllocateInternalPredicates(o command.OutputFormatter, internalContracts

// generateInternalContractAndProxyAddress generates the internal contract and proxy addresses
func generateInternalContractAndProxyAddress(lastAddress types.Address) (types.Address, types.Address) {
proxyAddress := types.IncrementAddressBy(lastAddress, 10)
implAddress := types.IncrementAddressBy(proxyAddress, 1)
proxyAddress := lastAddress.IncrementBy(10)
implAddress := lastAddress.IncrementBy(1)

return proxyAddress, implAddress
}
14 changes: 7 additions & 7 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func initBridgeStorageContract(cfg PolyBFTConfig, transition *state.Transition)
// initGatewayContract initializes Gateway contract on blade chain
func initGatewayContract(cfg PolyBFTConfig, bridgeCfg *BridgeConfig,
transition *state.Transition, alloc map[types.Address]*chain.GenesisAccount) error {
implementationAddr := types.IncrementAddressBy(bridgeCfg.InternalGatewayAddr, 1)
implementationAddr := bridgeCfg.InternalGatewayAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize gateway contract for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -413,7 +413,7 @@ func initERC20ACLPredicateContract(
contractAddr = bcfg.InternalERC20PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -447,7 +447,7 @@ func initERC721ACLPredicateContract(
contractAddr = bcfg.InternalERC721PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -481,7 +481,7 @@ func initERC1155ACLPredicateContract(
contractAddr = bcfg.InternalERC1155PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -514,7 +514,7 @@ func initERC20PredicateContract(
contractAddr = bcfg.InternalERC20PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -546,7 +546,7 @@ func initERC721PredicateContract(
contractAddr = bcfg.InternalERC721PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down Expand Up @@ -578,7 +578,7 @@ func initERC1155PredicateContract(
contractAddr = bcfg.InternalERC1155PredicateAddr
}

implementationAddr := types.IncrementAddressBy(contractAddr, 1)
implementationAddr := contractAddr.IncrementBy(1)
if _, exists := alloc[implementationAddr]; !exists {
// we do not initialize child predicates for bridges that were added
// after the genesis block
Expand Down
135 changes: 68 additions & 67 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ var (

type Hash [HashLength]byte

func (h Hash) Bytes() []byte {
return h[:]
}

func (h Hash) String() string {
return hex.EncodeToHex(h[:])
}

// UnmarshalText parses a hash in hex syntax.
func (h *Hash) UnmarshalText(input []byte) error {
*h = BytesToHash(StringToBytes(string(input)))

return nil
}

func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}

type Address [AddressLength]byte

func min(i, j int) int {
Expand All @@ -69,14 +88,6 @@ func BytesToHash(b []byte) Hash {
return h
}

func (h Hash) Bytes() []byte {
return h[:]
}

func (h Hash) String() string {
return hex.EncodeToHex(h[:])
}

// checksumEncode returns the checksummed address with 0x prefix, as by EIP-55
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
func (a Address) checksumEncode() string {
Expand Down Expand Up @@ -114,6 +125,55 @@ func (a Address) Bytes() []byte {
return a[:]
}

// IncrementBy increments the provided address by the given number.
//
// It does not mutate the original address, but returns altered copy instead.
func (a *Address) IncrementBy(increment uint64) Address {
// Convert Address to big.Int (20 bytes address space)
addrBigInt := new(big.Int).SetBytes(a[:])

// Increment by the provided number
addrBigInt.Add(addrBigInt, new(big.Int).SetUint64(increment))

var (
addrBytes = addrBigInt.Bytes()
newAddr Address
)

// Handle overflow by truncating to 20 bytes
if len(addrBytes) > AddressLength {
copy(newAddr[:], addrBytes[len(addrBytes)-AddressLength:])
} else {
copy(newAddr[AddressLength-len(addrBytes):], addrBytes)
}

return newAddr
}

// Compare returns 1 if addr1 is higher, -1 if addr2 is higher, and 0 if they are equal.
func (a Address) Compare(to Address) int {
addr1BigInt := new(big.Int).SetBytes(a[:])
addr2BigInt := new(big.Int).SetBytes(to[:])

return addr1BigInt.Cmp(addr2BigInt)
}

// UnmarshalText parses an address in hex syntax.
func (a *Address) UnmarshalText(input []byte) error {
buf := StringToBytes(string(input))
if len(buf) != AddressLength {
return fmt.Errorf("incorrect length")
}

*a = BytesToAddress(buf)

return nil
}

func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

func StringToHash(str string) Hash {
return BytesToHash(StringToBytes(str))
}
Expand Down Expand Up @@ -192,65 +252,6 @@ func IsValidAddress(address string, zeroAddressAllowed bool) (Address, error) {
return addr, nil
}

// IncrementAddressBy increments the provided address by the given number.
func IncrementAddressBy(addr Address, increment uint64) Address {
// Convert Address to big.Int
addrBigInt := new(big.Int).SetBytes(addr[:])

// Increment by the provided number
addrBigInt.Add(addrBigInt, big.NewInt(0).SetUint64(increment))

// Convert back to Address
var (
newAddr Address
addrBytes = addrBigInt.Bytes()
)

// Handle overflow by truncating to 20 bytes
if len(addrBytes) > 20 {
copy(newAddr[:], addrBytes[len(addrBytes)-20:])
} else {
copy(newAddr[20-len(addrBytes):], addrBytes)
}

return newAddr
}

// Compare returns 1 if addr1 is higher, -1 if addr2 is higher, and 0 if they are equal.
func (a Address) Compare(to Address) int {
addr1BigInt := new(big.Int).SetBytes(a[:])
addr2BigInt := new(big.Int).SetBytes(to[:])

return addr1BigInt.Cmp(addr2BigInt)
}

// UnmarshalText parses a hash in hex syntax.
func (h *Hash) UnmarshalText(input []byte) error {
*h = BytesToHash(StringToBytes(string(input)))

return nil
}

// UnmarshalText parses an address in hex syntax.
func (a *Address) UnmarshalText(input []byte) error {
buf := StringToBytes(string(input))
if len(buf) != AddressLength {
return fmt.Errorf("incorrect length")
}

*a = BytesToAddress(buf)

return nil
}

func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}

func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

type Proof struct {
Data []Hash // the proof himself
Metadata map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestIncrementAddressBy(t *testing.T) {

addr := StringToAddress(c.address)
expectedAddr := StringToAddress(c.expected)
incrementedAddr := IncrementAddressBy(addr, c.increment)
incrementedAddr := addr.IncrementBy(c.increment)

assert.Equal(t, expectedAddr, incrementedAddr, "expected address %s, got %s", expectedAddr.String(), incrementedAddr.String())
})
Expand Down

0 comments on commit cfaafe6

Please sign in to comment.