erigon-pulse/common/types.go
a 436493350e
Sentinel refactor (#8296)
1. changes sentinel to use an http-like interface

2. moves hexutil, crypto/blake2b, metrics packages to erigon-lib
2023-10-22 01:17:18 +02:00

178 lines
5.4 KiB
Go

// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package common
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"reflect"
"strings"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/length"
)
// Lengths of hashes and addresses in bytes.
const (
// BlockNumberLength length of uint64 big endian
BlockNumberLength = 8
// IncarnationLength length of uint64 for contract incarnations
IncarnationLength = 8
)
// UnprefixedHash allows marshaling a Hash without 0x prefix.
type UnprefixedHash libcommon.Hash
// UnmarshalText decodes the hash from hex. The 0x prefix is optional.
func (h *UnprefixedHash) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:])
}
// MarshalText encodes the hash as hex.
func (h UnprefixedHash) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h[:])), nil
}
/////////// Address
var addressT = reflect.TypeOf(libcommon.Address{})
// UnprefixedAddress allows marshaling an Address without 0x prefix.
type UnprefixedAddress libcommon.Address
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:])
}
// MarshalText encodes the address as hex.
func (a UnprefixedAddress) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(a[:])), nil
}
// MixedcaseAddress retains the original string, which may or may not be
// correctly checksummed
type MixedcaseAddress struct {
addr libcommon.Address
original string
}
// NewMixedcaseAddress constructor (mainly for testing)
func NewMixedcaseAddress(addr libcommon.Address) MixedcaseAddress {
return MixedcaseAddress{addr: addr, original: addr.Hex()}
}
// NewMixedcaseAddressFromString is mainly meant for unit-testing
func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
if !libcommon.IsHexAddress(hexaddr) {
return nil, errors.New("invalid address")
}
a := FromHex(hexaddr)
return &MixedcaseAddress{addr: libcommon.BytesToAddress(a), original: hexaddr}, nil
}
// UnmarshalJSON parses MixedcaseAddress
func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error {
if err := hexutility.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil {
return err
}
return json.Unmarshal(input, &ma.original)
}
// MarshalJSON marshals the original value
func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error) {
if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") {
return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:]))
}
return json.Marshal(fmt.Sprintf("0x%s", ma.original))
}
// Address returns the address
func (ma *MixedcaseAddress) Address() libcommon.Address {
return ma.addr
}
// String implements fmt.Stringer
func (ma *MixedcaseAddress) String() string {
if ma.ValidChecksum() {
return fmt.Sprintf("%s [chksum ok]", ma.original)
}
return fmt.Sprintf("%s [chksum INVALID]", ma.original)
}
// ValidChecksum returns true if the address has valid checksum
func (ma *MixedcaseAddress) ValidChecksum() bool {
return ma.original == ma.addr.Hex()
}
// Original returns the mixed-case input string
func (ma *MixedcaseAddress) Original() string {
return ma.original
}
// Addresses is a slice of libcommon.Address, implementing sort.Interface
type Addresses []libcommon.Address
func (addrs Addresses) Len() int {
return len(addrs)
}
func (addrs Addresses) Less(i, j int) bool {
return bytes.Compare(addrs[i][:], addrs[j][:]) == -1
}
func (addrs Addresses) Swap(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i]
}
// Hashes is a slice of libcommon.Hash, implementing sort.Interface
type Hashes []libcommon.Hash
func (hashes Hashes) Len() int {
return len(hashes)
}
func (hashes Hashes) Less(i, j int) bool {
return bytes.Compare(hashes[i][:], hashes[j][:]) == -1
}
func (hashes Hashes) Swap(i, j int) {
hashes[i], hashes[j] = hashes[j], hashes[i]
}
const StorageKeyLen = 2*length.Hash + IncarnationLength
// StorageKey is representation of address of a contract storage item
// It consists of two parts, each of which are 32-byte hashes:
// 1. Hash of the contract's address
// 2. Hash of the item's key
type StorageKey [StorageKeyLen]byte
// StorageKeys is a slice of StorageKey, implementing sort.Interface
type StorageKeys []StorageKey
func (keys StorageKeys) Len() int {
return len(keys)
}
func (keys StorageKeys) Less(i, j int) bool {
return bytes.Compare(keys[i][:], keys[j][:]) == -1
}
func (keys StorageKeys) Swap(i, j int) {
keys[i], keys[j] = keys[j], keys[i]
}