Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): Custom RPC types #1893

Draft
wants to merge 59 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
0eec5fd
bet
itsdevbear Aug 14, 2024
d3907f4
bet
itsdevbear Aug 14, 2024
d828672
bet
itsdevbear Aug 14, 2024
17a7b79
bet
itsdevbear Aug 14, 2024
a2547f1
bet
itsdevbear Aug 14, 2024
4ca0c57
bet
itsdevbear Aug 14, 2024
39c3e7d
bet
itsdevbear Aug 14, 2024
441a038
bet
itsdevbear Aug 14, 2024
fe9fd58
bet
itsdevbear Aug 14, 2024
5f56fe7
bet
itsdevbear Aug 14, 2024
57a3bf3
bet
itsdevbear Aug 14, 2024
fbcf372
bet
itsdevbear Aug 14, 2024
85e1837
bet
itsdevbear Aug 14, 2024
cd8e51d
bet
itsdevbear Aug 14, 2024
799feb1
bet
itsdevbear Aug 14, 2024
569a674
bet
itsdevbear Aug 14, 2024
f1a2945
bet
itsdevbear Aug 14, 2024
92b3b45
bet
itsdevbear Aug 14, 2024
079fcdc
bet
itsdevbear Aug 14, 2024
31cf017
bet
itsdevbear Aug 15, 2024
cbfac1e
bet
itsdevbear Aug 15, 2024
18237a1
bet
itsdevbear Aug 15, 2024
9c7f44d
bet
itsdevbear Aug 15, 2024
fd90876
bet
itsdevbear Aug 15, 2024
59a20d0
bet
itsdevbear Aug 15, 2024
50b4f0e
Merge branch 'main' into custom-rpc-types
itsdevbear Aug 15, 2024
e919d88
bet
itsdevbear Aug 15, 2024
a767000
Merge branch 'main' into custom-rpc-types
itsdevbear Aug 15, 2024
3fc5350
bet
itsdevbear Aug 15, 2024
939f14b
bet
itsdevbear Aug 15, 2024
53a7ef1
bet
itsdevbear Aug 15, 2024
9f22f3d
bet
itsdevbear Aug 15, 2024
5d9ff65
bet
itsdevbear Aug 15, 2024
347c1d2
Merge branch 'main' into custom-rpc-types
itsdevbear Aug 15, 2024
72ae104
bet
itsdevbear Aug 15, 2024
d7ffc21
merge
itsdevbear Aug 15, 2024
248286c
bet
itsdevbear Aug 15, 2024
19306ce
bet
itsdevbear Aug 15, 2024
a264441
bet
itsdevbear Aug 15, 2024
3dc62f0
bet
itsdevbear Aug 15, 2024
fd0a805
bet
itsdevbear Aug 15, 2024
43799a4
bet
itsdevbear Aug 15, 2024
9b08e28
bet
itsdevbear Aug 15, 2024
af0c586
bet
itsdevbear Aug 15, 2024
9a8fdb4
bet
itsdevbear Aug 15, 2024
88b6899
bet
itsdevbear Aug 15, 2024
70a74c8
Merge branch 'main' into custom-rpc-types
itsdevbear Aug 15, 2024
d198db5
bet
itsdevbear Aug 15, 2024
257e8ba
Merge branch 'main' into custom-rpc-types
itsdevbear Aug 16, 2024
20f7634
Merge branch 'main' of github.com:berachain/beacon-kit into custom-rp…
calbera Oct 1, 2024
ab27640
log generic
calbera Oct 1, 2024
be73fd8
bet
calbera Oct 1, 2024
ce4ad41
Merge branch 'main' into custom-rpc-types
calbera Oct 1, 2024
8e30442
bet
calbera Oct 2, 2024
c528d7e
nits
calbera Oct 2, 2024
0a30dcc
gen
calbera Oct 2, 2024
0486a82
tests
calbera Oct 2, 2024
62d6d23
bet
calbera Oct 2, 2024
a795152
Wipg
calbera Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions mod/execution/pkg/client/ethclient/types/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package types

import (
"errors"

"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
)

// FilterArgs is a map of string to any, used to configure a filter query.
type FilterArgs map[string]any

// New creates a new FilterArgs with the given addresses and topics.
func (f FilterArgs) New(
addrs []common.ExecutionAddress, topics []common.ExecutionHash,
) FilterArgs {
return FilterArgs{
"address": addrs,
"topics": topics,
}
}

// SetFromBlock sets the "fromBlock" field in the FilterArgs, ensuring that
// BlockHash is not also set.
func (f FilterArgs) SetFromBlock(block BlockNumber) error {
var err error
if f["fromBlock"], err = block.MarshalText(); err != nil {
return err
}
if f["blockHash"] != nil {
return errors.New("cannot specify both BlockHash and FromBlock/ToBlock")
}
return nil
}

// SetToBlock sets the "toBlock" field in the FilterArgs, ensuring that
// BlockHash is not also set.
func (f FilterArgs) SetToBlock(block BlockNumber) error {
var err error
if f["toBlock"], err = block.MarshalText(); err != nil {
return err
}
if f["blockHash"] != nil {
return errors.New("cannot specify both BlockHash and FromBlock/ToBlock")
}
return nil
}

// SetBlockHash sets the "blockHash" field in the FilterArgs, ensuring that
// FromBlock and ToBlock are not also set.
func (f FilterArgs) SetBlockHash(hash common.ExecutionHash) error {
if f["fromBlock"] != nil || f["toBlock"] != nil {
return errors.New("cannot specify both BlockHash and FromBlock/ToBlock")
}
f["blockHash"] = hash.Hex()
return nil
}
116 changes: 116 additions & 0 deletions mod/execution/pkg/client/ethclient/types/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package types

import (
"errors"
"fmt"
stdmath "math"
"strings"

"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

const (
SafeBlockNumbeStr = "safe"
FinalizedBlockNumStr = "finalized"
LatestBlockNumStr = "latest"
PendingBlockNumStr = "pending"
EarliestBlockNumberStr = "earliest"
)

const (
SafeBlockNumber = BlockNumber(-4)
FinalizedBlockNumber = BlockNumber(-3)
LatestBlockNumber = BlockNumber(-2)
PendingBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
)

type BlockNumber int64

// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
// - "safe", "finalized", "latest", "earliest" or "pending" as string arguments
// - the block number
// Returned errors:
// - an invalid block number error when the given argument isn't a known strings
// - an out of range error when the given block number is either too little or too large
func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
input := strings.TrimSpace(string(data))

if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}

switch input {
case EarliestBlockNumberStr:
*bn = EarliestBlockNumber
return nil
case LatestBlockNumStr:
*bn = LatestBlockNumber
return nil
case PendingBlockNumStr:
*bn = PendingBlockNumber
return nil
case FinalizedBlockNumStr:
*bn = FinalizedBlockNumber
return nil
case SafeBlockNumbeStr:
*bn = SafeBlockNumber
return nil
}

result := new(math.U64)
result.UnmarshalJSON([]byte(input))
if *result > math.U64(uint64(stdmath.MaxInt64)) {
return errors.New("block number larger than int64")
}
//#nosec:G701 // handled by the guard above.
*bn = BlockNumber(*result.UnwrapPtr())
return nil
}

// MarshalText implements encoding.TextMarshaler. It marshals:
// - "safe", "finalized", "latest", "earliest" or "pending" as strings
// - other numbers as hex
func (bn BlockNumber) MarshalText() ([]byte, error) {
return []byte(bn.String()), nil
}

func (bn BlockNumber) String() string {
switch bn {
case EarliestBlockNumber:
return EarliestBlockNumberStr
case LatestBlockNumber:
return LatestBlockNumStr
case PendingBlockNumber:
return PendingBlockNumStr
case FinalizedBlockNumber:
return FinalizedBlockNumStr
case SafeBlockNumber:
return SafeBlockNumbeStr
default:
if bn < 0 {
return fmt.Sprintf("<invalid %d>", bn)
}
return math.U64(bn).Hex()
}
}
7 changes: 7 additions & 0 deletions mod/primitives/pkg/math/u64.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func (u U64) Base10() string {
return strconv.FormatUint(uint64(u), 10)
}

// Hex returns the hex representation of the U64.
func (u U64) Hex() string {
//#nosec:G703 // its okay.
val, _ := hex.MarshalText(u.Unwrap())
return string(val)
}

// ----------------------- U64 Mathematical Methods -----------------------

// Unwrap returns a copy of the underlying uint64 value of U64.
Expand Down
Loading