Compare commits

...

4 Commits

Author SHA1 Message Date
Potuz
353176cfc5 expose HdiffSerialized fields 2025-05-09 12:47:21 -03:00
Potuz
f1596b6c47 use pointers instead of structures 2025-05-08 17:27:16 -03:00
Potuz
49ff1ae771 Add function to compute diffs 2025-05-08 14:02:58 -03:00
potuz
c29a8708fb Add state diff native types
Adds a state diff native type and marshalling functions for
serialization.
2025-05-07 16:52:29 -03:00
7 changed files with 1678 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
### Added
- Add native state diff type and marshalling functions

View File

@@ -40,6 +40,12 @@ func NewWrappedExecutionData(v proto.Message) (interfaces.ExecutionData, error)
case *enginev1.ExecutionBundleElectra:
// note: no payload changes in electra so using deneb
return WrappedExecutionPayloadDeneb(pbStruct.Payload)
case *enginev1.ExecutionPayloadHeader:
return WrappedExecutionPayloadHeader(pbStruct)
case *enginev1.ExecutionPayloadHeaderCapella:
return WrappedExecutionPayloadHeaderCapella(pbStruct)
case *enginev1.ExecutionPayloadHeaderDeneb:
return WrappedExecutionPayloadHeaderDeneb(pbStruct)
default:
return nil, ErrUnsupportedVersion
}

View File

@@ -0,0 +1,33 @@
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["state_diff.go"],
importpath = "github.com/OffchainLabs/prysm/v6/consensus-types/hdiff",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/state:go_default_library",
"//config/fieldparams:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/helpers:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//proto/engine/v1:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_fastssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["state_diff_test.go"],
embed = [":go_default_library"],
deps = [
"//testing/require:go_default_library",
"//testing/util:go_default_library",
],
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,60 @@
package hdiff
import (
"testing"
"github.com/OffchainLabs/prysm/v6/testing/require"
"github.com/OffchainLabs/prysm/v6/testing/util"
)
func Test_diffToState(t *testing.T) {
source, _ := util.DeterministicGenesisStateElectra(t, 256)
target := source.Copy()
require.NoError(t, target.SetSlot(source.Slot()+1))
hdiff, err := diffToState(source, target)
require.NoError(t, err)
require.Equal(t, hdiff.slot, target.Slot())
require.Equal(t, hdiff.targetVersion, target.Version())
}
func Test_kmpIndex(t *testing.T) {
intSlice := make([]*int, 10)
for i := 0; i < len(intSlice); i++ {
intSlice[i] = new(int)
*intSlice[i] = i
}
integerEquals := func(a, b *int) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}
t.Run("integer entries match", func(t *testing.T) {
source := []*int{intSlice[0], intSlice[1], intSlice[2], intSlice[3], intSlice[4]}
target := []*int{intSlice[2], intSlice[3], intSlice[4], intSlice[5], intSlice[6], intSlice[7], nil}
target = append(target, source...)
require.Equal(t, 2, kmpIndex(len(source), target, integerEquals))
})
t.Run("integer entries skipped", func(t *testing.T) {
source := []*int{intSlice[0], intSlice[1], intSlice[2], intSlice[3], intSlice[4]}
target := []*int{intSlice[2], intSlice[3], intSlice[4], intSlice[0], intSlice[5], nil}
target = append(target, source...)
require.Equal(t, 2, kmpIndex(len(source), target, integerEquals))
})
t.Run("integer entries repetitions", func(t *testing.T) {
source := []*int{intSlice[0], intSlice[1], intSlice[0], intSlice[0], intSlice[0]}
target := []*int{intSlice[0], intSlice[0], intSlice[1], intSlice[2], intSlice[5], nil}
target = append(target, source...)
require.Equal(t, 3, kmpIndex(len(source), target, integerEquals))
})
t.Run("integer entries no match", func(t *testing.T) {
source := []*int{intSlice[0], intSlice[1], intSlice[2], intSlice[3]}
target := []*int{intSlice[4], intSlice[5], intSlice[6], nil}
target = append(target, source...)
require.Equal(t, len(source), kmpIndex(len(source), target, integerEquals))
})
}

View File

@@ -0,0 +1,9 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["comparisons.go"],
importpath = "github.com/OffchainLabs/prysm/v6/consensus-types/helpers",
visibility = ["//visibility:public"],
deps = ["//proto/prysm/v1alpha1:go_default_library"],
)

View File

@@ -0,0 +1,109 @@
package helpers
import (
"bytes"
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
)
func ForksEqual(s, t *ethpb.Fork) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
if s.Epoch != t.Epoch {
return false
}
if !bytes.Equal(s.PreviousVersion, t.PreviousVersion) {
return false
}
return bytes.Equal(s.CurrentVersion, t.CurrentVersion)
}
func BlockHeadersEqual(s, t *ethpb.BeaconBlockHeader) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
if s.Slot != t.Slot {
return false
}
if s.ProposerIndex != t.ProposerIndex {
return false
}
if !bytes.Equal(s.ParentRoot, t.ParentRoot) {
return false
}
if !bytes.Equal(s.StateRoot, t.StateRoot) {
return false
}
return bytes.Equal(s.BodyRoot, t.BodyRoot)
}
func Eth1DataEqual(s, t *ethpb.Eth1Data) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
if !bytes.Equal(s.DepositRoot, t.DepositRoot) {
return false
}
if s.DepositCount != t.DepositCount {
return false
}
return bytes.Equal(s.BlockHash, t.BlockHash)
}
func PendingDepositsEqual(s, t *ethpb.PendingDeposit) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
if !bytes.Equal(s.PublicKey, t.PublicKey) {
return false
}
if !bytes.Equal(s.WithdrawalCredentials, t.WithdrawalCredentials) {
return false
}
if s.Amount != t.Amount {
return false
}
if !bytes.Equal(s.Signature, t.Signature) {
return false
}
return s.Slot == t.Slot
}
func PendingPartialWithdrawalsEqual(s, t *ethpb.PendingPartialWithdrawal) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
if s.Index != t.Index {
return false
}
if s.Amount != t.Amount {
return false
}
return s.WithdrawableEpoch == t.WithdrawableEpoch
}
func PendingConsolidationsEqual(s, t *ethpb.PendingConsolidation) bool {
if s == nil && t == nil {
return true
}
if s == nil || t == nil {
return false
}
return s.SourceIndex == t.SourceIndex && s.TargetIndex == t.TargetIndex
}