mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-30 00:23:46 +00:00
commit
74623db6ff
12 changed files with 515 additions and 3 deletions
52
beacon/light/api/portal_api.go
Normal file
52
beacon/light/api/portal_api.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/beacon/types"
|
||||
)
|
||||
|
||||
type PortalLightApi struct {
|
||||
}
|
||||
|
||||
func NewPortalLightApi() *PortalLightApi {
|
||||
return &PortalLightApi{}
|
||||
}
|
||||
|
||||
func (api *PortalLightApi) GetBestUpdatesAndCommittees(firstPeriod, count uint64) ([]*types.LightClientUpdate, []*types.SerializedSyncCommittee, error) {
|
||||
//contentKey := &beacon.LightClientUpdateKey{
|
||||
// StartPeriod: firstPeriod,
|
||||
// Count: count,
|
||||
//}
|
||||
|
||||
//resp, err := api.httpGetf("/eth/v1/beacon/light_client/updates?start_period=%d&count=%d", firstPeriod, count)
|
||||
//if err != nil {
|
||||
// return nil, nil, err
|
||||
//}
|
||||
//
|
||||
//var data []CommitteeUpdate
|
||||
//if err := json.Unmarshal(resp, &data); err != nil {
|
||||
// return nil, nil, err
|
||||
//}
|
||||
//if len(data) != int(count) {
|
||||
// return nil, nil, errors.New("invalid number of committee updates")
|
||||
//}
|
||||
//updates := make([]*types.LightClientUpdate, int(count))
|
||||
//committees := make([]*types.SerializedSyncCommittee, int(count))
|
||||
//for i, d := range data {
|
||||
// if d.Update.AttestedHeader.Header.SyncPeriod() != firstPeriod+uint64(i) {
|
||||
// return nil, nil, errors.New("wrong committee update header period")
|
||||
// }
|
||||
// if err := d.Update.Validate(); err != nil {
|
||||
// return nil, nil, err
|
||||
// }
|
||||
// if d.NextSyncCommittee.Root() != d.Update.NextSyncCommitteeRoot {
|
||||
// return nil, nil, errors.New("wrong sync committee root")
|
||||
// }
|
||||
// updates[i], committees[i] = new(types.LightClientUpdate), new(types.SerializedSyncCommittee)
|
||||
// *updates[i], *committees[i] = d.Update, d.NextSyncCommittee
|
||||
//}
|
||||
//return updates, committees, nil
|
||||
|
||||
return nil, nil, errors.New("not implemented")
|
||||
}
|
||||
4
go.mod
4
go.mod
|
|
@ -72,6 +72,7 @@ require (
|
|||
go.uber.org/automaxprocs v1.5.2
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/crypto v0.21.0
|
||||
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
|
||||
golang.org/x/sync v0.5.0
|
||||
golang.org/x/sys v0.18.0
|
||||
golang.org/x/text v0.14.0
|
||||
|
|
@ -145,10 +146,11 @@ require (
|
|||
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
|
||||
golang.org/x/mod v0.14.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
rsc.io/tmplfunc v0.0.3 // indirect
|
||||
)
|
||||
|
||||
replace github.com/protolambda/zrnt v0.32.2 => github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -431,6 +431,8 @@ github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsq
|
|||
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
|
||||
github.com/optimism-java/utp-go v0.0.0-20240309041853-b6b3a0dea581 h1:ZxgrtI0xIw+clB32iDDDWaiTcCizTeN7rNyzH9YorPI=
|
||||
github.com/optimism-java/utp-go v0.0.0-20240309041853-b6b3a0dea581/go.mod h1:DZ0jYzLzt4ZsCmhI/iqYgGFoNx45OfpEoKzXB8HVALQ=
|
||||
github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88 h1:hYCtTSAgXOtapscLmr6OgCQJulRjF3K7wZlvh9Zzrwk=
|
||||
github.com/optimism-java/zrnt v0.32.4-0.20240402113914-188558bfad88/go.mod h1:A0fezkp9Tt3GBLATSPIbuY4ywYESyAuc/FFmPKg8Lqs=
|
||||
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 h1:oYW+YCJ1pachXTQmzR3rNLYGGz4g/UgFcjb28p/viDM=
|
||||
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
|
||||
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
|
||||
|
|
@ -471,8 +473,6 @@ github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0
|
|||
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||
github.com/protolambda/bls12-381-util v0.1.0 h1:05DU2wJN7DTU7z28+Q+zejXkIsA/MF8JZQGhtBZZiWk=
|
||||
github.com/protolambda/bls12-381-util v0.1.0/go.mod h1:cdkysJTRpeFeuUVx/TXGDQNMTiRAalk1vQw3TYTHcE4=
|
||||
github.com/protolambda/zrnt v0.32.2 h1:KZ48T+3UhsPXNdtE/5QEvGc9DGjUaRI17nJaoznoIaM=
|
||||
github.com/protolambda/zrnt v0.32.2/go.mod h1:A0fezkp9Tt3GBLATSPIbuY4ywYESyAuc/FFmPKg8Lqs=
|
||||
github.com/protolambda/ztyp v0.2.2 h1:rVcL3vBu9W/aV646zF6caLS/dyn9BN8NYiuJzicLNyY=
|
||||
github.com/protolambda/ztyp v0.2.2/go.mod h1:9bYgKGqg3wJqT9ac1gI2hnVb0STQq7p/1lapqrqY1dU=
|
||||
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 h1:0tVE4tdWQK9ZpYygoV7+vS6QkDvQVySboMVEIxBJmXw=
|
||||
|
|
|
|||
|
|
@ -1 +1,63 @@
|
|||
package beacon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/beacon/types"
|
||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
||||
ssz "github.com/ferranbt/fastssz"
|
||||
)
|
||||
|
||||
const (
|
||||
LightClientBootstrap storage.ContentType = 0x10
|
||||
LightClientUpdate storage.ContentType = 0x11
|
||||
LightClientFinalityUpdate storage.ContentType = 0x12
|
||||
LightClientOptimisticUpdate storage.ContentType = 0x13
|
||||
HistoricalSummaries storage.ContentType = 0x14
|
||||
)
|
||||
|
||||
type BeaconNetwork struct {
|
||||
portalProtocol *discover.PortalProtocol
|
||||
}
|
||||
|
||||
func (bn *BeaconNetwork) GetBestUpdatesAndCommittees(firstPeriod, count uint64) ([]*types.LightClientUpdate, []*types.SerializedSyncCommittee, error) {
|
||||
lightClientUpdateKey := &LightClientUpdateKey{
|
||||
StartPeriod: firstPeriod,
|
||||
Count: count,
|
||||
}
|
||||
|
||||
_, err := bn.getContent(LightClientUpdate, lightClientUpdateKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
func (bn *BeaconNetwork) getContent(contentType storage.ContentType, beaconContentKey ssz.Marshaler) ([]byte, error) {
|
||||
contentKeyBytes, err := beaconContentKey.MarshalSSZ()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentKey := storage.NewContentKey(contentType, contentKeyBytes).Encode()
|
||||
contentId := bn.portalProtocol.ToContentId(contentKey)
|
||||
|
||||
res, err := bn.portalProtocol.Get(contentKey, contentId)
|
||||
// other error
|
||||
if err != nil && !errors.Is(err, storage.ErrContentNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
content, _, err := bn.portalProtocol.ContentLookup(contentKey, contentId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
|
|
|||
6
portalnetwork/beacon/testdata/light_client_bootstrap.json
vendored
Normal file
6
portalnetwork/beacon/testdata/light_client_bootstrap.json
vendored
Normal file
File diff suppressed because one or more lines are too long
6
portalnetwork/beacon/testdata/light_client_finality_update.json
vendored
Normal file
6
portalnetwork/beacon/testdata/light_client_finality_update.json
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"6718463": {
|
||||
"content_key": "0x020000000000000000",
|
||||
"content_value": "0xbba4da9670010000b30400001d34030000000000000000000000000000000000000000000000000000000000a4f8b2415bbca66d73597343afead504bd8282523df27153543b2de8973b7474ace652bb73991c3b63f7185a17333c8aea619a69b69863a5d79810dad6c363d3df460765de20d3c8f56bcd9490f15e2d9e122b3f6173cea76f6d0d16074ce0f0ecde5df1b81f36c08e91ce803c25add312bfd8cb759a81b7cfd158d724959cf22e83908e043f77bdc5992806617ef1fa0a4f1985d9aa1b67371d3580be8c2c64ffffffffffffffffffffffffffffbfffff7fffffffffffffffffffffffffffffffffffffffffffffffffffdefffffffffffffffffffeffffffffffffffffefffa480f81d481c5aa8439f8c65f86c69d3b1570cfd95b61300f7e29c5b0954c0c0fb080bba0e8ba60dead40c45355332ed06982fcaf53ad1ad4a7e6e39091ee7e5602d4a30b6d46e12047c31451f246bf43c4a3fbdb1d1de2f2180a776ef71fc3d0084660000000000ff83660000000000e5f4020000000000007b8211614b9c5331a1de2691ad7cdde1ca113b4e51b9f2757a4b502833f9234a06eb807d82b9175dd085748ade76aaa86fb4eca48bc8deb01b9c56a267a896d98469820ef7d34f8610e134c6eaf8d379e83cf6c3ab0663fba828edab344dc5f40000008e2a532cfdfdce86b27b26f611f1df0e2b00a54d18c6980e69ad28a4b70bf480336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0edb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71b4c4e01065cd22a0d2f27a544ff8315b9e28f6270a63f543831778f32648ac3a15ecbea44f91b9b4a28ec8308a30a4b6252f554a2088bb1c73ea01013ba4963dfeebabe6b0418ec13b30aadf129f5dcdd4f70ceaa56499df0b3ba9d8fe6ca410132a29cf387371294137003adb139ce9f06b6f7dbf32ee01da91016a485a8aab5669168efc3d3a200b2ee0f25d937a413dfc38e44e211f02432a01a83088e700808c302002139aad8496010300cd220b14288429d8a6049945000a3b60f00f00109019341a0129a08906314848650817002cb98894a328084521a87e6d0a64e8080004ac0805000800e29808294016508a2004001304002a02320c4705501ac008a62848701040012a08e5c8b69010f1009f80408894857053e1b940e2c9004912860e26112080610d002098242000ca001420209a42c141d0c86087509344ced8a32e0204410a0196025a12106104e20008ad4449d5043208090008810201820209108607a0fa1004aa323410050222867024e0d172a82821b02c00020513152006484c800b10f0420802c0244d0850288c6541dc34cabc32f5d355aef4017425cab95c1de0474d483623e23c9bf13d9a18f1597e930b010000000080c3c901000000000ca88800000000004b6094640000000038020000ab9bf66d04000000000000000000000000000000000000000000000000000000797be971185ac699fa7288c87457c3611b443fb98483fddf282dfa7c5b4b27058eddbcd8b5de9b747c57c827ceba820d19e987ff6740eed38ccbc947696d71e3e2c3b96c9200d7d5c66fb4e8458067d6420e64647682ca2f0798ae817d2bc19468747470733a2f2f6574682d6275696c6465722e636f6da08366000000000037b10700000000002a7315c8ddfc25dc2266a6b221cb8f9fdf641970ab1f65a2754df4e14c432b9c446c604131913bed45976c4a8ea27df843a72d622f80ef15da622f0d56ec1ffe3021190177b0405c4fa1a0311a493c4dd095f24d386be6a03f5838a6b1008665f4000000f5c98fc152bf40e1ce216c8839b8ddd42ea5b355f0b5c700fc9b2cb7c802e1c8336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0edb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71a533e05d648cb9635382f38b778dc3c76b18e4eab2d017fdb24c1c0e32d2991707a45ce3f26e443dafa697a76bf24122c9b19b57eebe798668dbd1a0da7c900795222290dd7278aa3ddd389cc1e1d165cc4bafe5c658153cdef5850f97c4d4fb6706b82310414a00fe7dc34b3043e0c7a8a24e810e3cec9de3e1dd7ce06bc191efd2273a357da4436fc4bf82069f6cb87d3d605aeda1336ac513f358b4296b8da51232896ad271063d93404eaa6b80960fa2d4b41d74a544eef94731f0b83ba2701e33b49e3191098c3369bc4658d82f536cb9d38285c51c7844292ced06dd2e8148f0bcd585dc1104e64aa930045e83d828ab436a0e587637e5ef83044d1c2918da284d7072847132885d443640979907485847368e32501345b0dc24c904c06732cc17d944b26181b0c05c6461bccbe4f7b2180a282b6871aa6472fa36cdc49ec50cf69d3eee4b26fe02bf48f39efb549d3963d19e4112c7250c580543516204ff1b0675680cfea02ac09f0b23d6e28c14b08421b3a10a9788442457d732b120c5c466c54ad938dedd3cd3701d9b32fa15dc8db3d6b1ea5d8495732477a72b34efcaf8c349aa40d8e1f21f226ca11182cff00921930b010000000080c3c901000000009104e90000000000d75b9464000000003802000076ab1c8604000000000000000000000000000000000000000000000000000000844f7a7e7585362114fc88a70654146215ab4eaad65ad54b64975840b78d365e8b75e78616a92294e747589c3ee4a845c2962e1cf6cd7fe8bfbd09439e5e6d5e4d4efdf181473803e734d97891388e1b50628658ac818599cf2cb069c8e6b38b6265617665726275696c642e6f7267"
|
||||
}
|
||||
}
|
||||
6
portalnetwork/beacon/testdata/light_client_optimistic_update.json
vendored
Normal file
6
portalnetwork/beacon/testdata/light_client_optimistic_update.json
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"6718463": {
|
||||
"content_key": "0x030000000000000000",
|
||||
"content_value": "0xbba4da96ac000000ffffffffffffffffffffffffffffbfffff7fffffffffffffffffffffffffffffffffffffffffffffffffffdefffffffffffffffffffeffffffffffffffffefffa480f81d481c5aa8439f8c65f86c69d3b1570cfd95b61300f7e29c5b0954c0c0fb080bba0e8ba60dead40c45355332ed06982fcaf53ad1ad4a7e6e39091ee7e5602d4a30b6d46e12047c31451f246bf43c4a3fbdb1d1de2f2180a776ef71fc3d0084660000000000ff83660000000000e5f4020000000000007b8211614b9c5331a1de2691ad7cdde1ca113b4e51b9f2757a4b502833f9234a06eb807d82b9175dd085748ade76aaa86fb4eca48bc8deb01b9c56a267a896d98469820ef7d34f8610e134c6eaf8d379e83cf6c3ab0663fba828edab344dc5f40000008e2a532cfdfdce86b27b26f611f1df0e2b00a54d18c6980e69ad28a4b70bf480336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0edb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71b4c4e01065cd22a0d2f27a544ff8315b9e28f6270a63f543831778f32648ac3a15ecbea44f91b9b4a28ec8308a30a4b6252f554a2088bb1c73ea01013ba4963dfeebabe6b0418ec13b30aadf129f5dcdd4f70ceaa56499df0b3ba9d8fe6ca410132a29cf387371294137003adb139ce9f06b6f7dbf32ee01da91016a485a8aab5669168efc3d3a200b2ee0f25d937a413dfc38e44e211f02432a01a83088e700808c302002139aad8496010300cd220b14288429d8a6049945000a3b60f00f00109019341a0129a08906314848650817002cb98894a328084521a87e6d0a64e8080004ac0805000800e29808294016508a2004001304002a02320c4705501ac008a62848701040012a08e5c8b69010f1009f80408894857053e1b940e2c9004912860e26112080610d002098242000ca001420209a42c141d0c86087509344ced8a32e0204410a0196025a12106104e20008ad4449d5043208090008810201820209108607a0fa1004aa323410050222867024e0d172a82821b02c00020513152006484c800b10f0420802c0244d0850288c6541dc34cabc32f5d355aef4017425cab95c1de0474d483623e23c9bf13d9a18f1597e930b010000000080c3c901000000000ca88800000000004b6094640000000038020000ab9bf66d04000000000000000000000000000000000000000000000000000000797be971185ac699fa7288c87457c3611b443fb98483fddf282dfa7c5b4b27058eddbcd8b5de9b747c57c827ceba820d19e987ff6740eed38ccbc947696d71e3e2c3b96c9200d7d5c66fb4e8458067d6420e64647682ca2f0798ae817d2bc19468747470733a2f2f6574682d6275696c6465722e636f6d"
|
||||
}
|
||||
}
|
||||
6
portalnetwork/beacon/testdata/light_client_updates_by_range.json
vendored
Normal file
6
portalnetwork/beacon/testdata/light_client_updates_by_range.json
vendored
Normal file
File diff suppressed because one or more lines are too long
66
portalnetwork/beacon/types.go
Normal file
66
portalnetwork/beacon/types.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package beacon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/protolambda/zrnt/eth2/beacon/altair"
|
||||
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
||||
"github.com/protolambda/zrnt/eth2/beacon/common"
|
||||
"github.com/protolambda/zrnt/eth2/configs"
|
||||
"github.com/protolambda/ztyp/codec"
|
||||
)
|
||||
|
||||
var (
|
||||
Bellatrix common.ForkDigest = [4]byte{0x0, 0x0, 0x0, 0x0}
|
||||
Capella common.ForkDigest = [4]byte{0xbb, 0xa4, 0xda, 0x96}
|
||||
)
|
||||
|
||||
//go:generate sszgen --path types.go
|
||||
|
||||
type LightClientUpdateKey struct {
|
||||
StartPeriod uint64
|
||||
Count uint64
|
||||
}
|
||||
|
||||
type LightClientBootstrapKey struct {
|
||||
BlockHash []byte `ssz-size:"32"`
|
||||
}
|
||||
|
||||
type LightClientFinalityUpdateKey struct {
|
||||
FinalizedSlot uint64
|
||||
}
|
||||
|
||||
type LightClientOptimisticUpdateKey struct {
|
||||
OptimisticSlot uint64
|
||||
}
|
||||
|
||||
type ForkedLightClientBootstrap struct {
|
||||
ForkDigest common.ForkDigest
|
||||
Bootstrap common.SpecObj
|
||||
}
|
||||
|
||||
func (flb *ForkedLightClientBootstrap) Deserialize(dr *codec.DecodingReader) error {
|
||||
_, err := dr.Read(flb.ForkDigest[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if flb.ForkDigest == Bellatrix {
|
||||
flb.Bootstrap = &altair.LightClientBootstrap{}
|
||||
} else if flb.ForkDigest == Capella {
|
||||
flb.Bootstrap = &capella.LightClientBootstrap{}
|
||||
} else {
|
||||
return errors.New("unknown fork digest")
|
||||
}
|
||||
|
||||
err = flb.Bootstrap.Deserialize(configs.Mainnet, dr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (flb *ForkedLightClientBootstrap) FixedLength() uint64 {
|
||||
return 0
|
||||
}
|
||||
252
portalnetwork/beacon/types_encoding.go
Normal file
252
portalnetwork/beacon/types_encoding.go
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
// Code generated by fastssz. DO NOT EDIT.
|
||||
// Hash: 7fed4f33c894dec224a58ca39c80acbd272f7fae47aa077a94e75a98a24804c5
|
||||
// Version: 0.1.3
|
||||
package beacon
|
||||
|
||||
import (
|
||||
ssz "github.com/ferranbt/fastssz"
|
||||
)
|
||||
|
||||
// MarshalSSZ ssz marshals the LightClientUpdateKey object
|
||||
func (l *LightClientUpdateKey) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(l)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the LightClientUpdateKey object to a target array
|
||||
func (l *LightClientUpdateKey) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
|
||||
// Field (0) 'StartPeriod'
|
||||
dst = ssz.MarshalUint64(dst, l.StartPeriod)
|
||||
|
||||
// Field (1) 'Count'
|
||||
dst = ssz.MarshalUint64(dst, l.Count)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the LightClientUpdateKey object
|
||||
func (l *LightClientUpdateKey) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size != 16 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
// Field (0) 'StartPeriod'
|
||||
l.StartPeriod = ssz.UnmarshallUint64(buf[0:8])
|
||||
|
||||
// Field (1) 'Count'
|
||||
l.Count = ssz.UnmarshallUint64(buf[8:16])
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the LightClientUpdateKey object
|
||||
func (l *LightClientUpdateKey) SizeSSZ() (size int) {
|
||||
size = 16
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the LightClientUpdateKey object
|
||||
func (l *LightClientUpdateKey) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(l)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the LightClientUpdateKey object with a hasher
|
||||
func (l *LightClientUpdateKey) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'StartPeriod'
|
||||
hh.PutUint64(l.StartPeriod)
|
||||
|
||||
// Field (1) 'Count'
|
||||
hh.PutUint64(l.Count)
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the LightClientUpdateKey object
|
||||
func (l *LightClientUpdateKey) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(l)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the LightClientBootstrapKey object
|
||||
func (l *LightClientBootstrapKey) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(l)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the LightClientBootstrapKey object to a target array
|
||||
func (l *LightClientBootstrapKey) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
|
||||
// Field (0) 'BlockHash'
|
||||
if size := len(l.BlockHash); size != 32 {
|
||||
err = ssz.ErrBytesLengthFn("LightClientBootstrapKey.BlockHash", size, 32)
|
||||
return
|
||||
}
|
||||
dst = append(dst, l.BlockHash...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the LightClientBootstrapKey object
|
||||
func (l *LightClientBootstrapKey) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size != 32 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
// Field (0) 'BlockHash'
|
||||
if cap(l.BlockHash) == 0 {
|
||||
l.BlockHash = make([]byte, 0, len(buf[0:32]))
|
||||
}
|
||||
l.BlockHash = append(l.BlockHash, buf[0:32]...)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the LightClientBootstrapKey object
|
||||
func (l *LightClientBootstrapKey) SizeSSZ() (size int) {
|
||||
size = 32
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the LightClientBootstrapKey object
|
||||
func (l *LightClientBootstrapKey) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(l)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the LightClientBootstrapKey object with a hasher
|
||||
func (l *LightClientBootstrapKey) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'BlockHash'
|
||||
if size := len(l.BlockHash); size != 32 {
|
||||
err = ssz.ErrBytesLengthFn("LightClientBootstrapKey.BlockHash", size, 32)
|
||||
return
|
||||
}
|
||||
hh.PutBytes(l.BlockHash)
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the LightClientBootstrapKey object
|
||||
func (l *LightClientBootstrapKey) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(l)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the LightClientFinalityUpdateKey object
|
||||
func (l *LightClientFinalityUpdateKey) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(l)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the LightClientFinalityUpdateKey object to a target array
|
||||
func (l *LightClientFinalityUpdateKey) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
|
||||
// Field (0) 'FinalizedSlot'
|
||||
dst = ssz.MarshalUint64(dst, l.FinalizedSlot)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the LightClientFinalityUpdateKey object
|
||||
func (l *LightClientFinalityUpdateKey) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size != 8 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
// Field (0) 'FinalizedSlot'
|
||||
l.FinalizedSlot = ssz.UnmarshallUint64(buf[0:8])
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the LightClientFinalityUpdateKey object
|
||||
func (l *LightClientFinalityUpdateKey) SizeSSZ() (size int) {
|
||||
size = 8
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the LightClientFinalityUpdateKey object
|
||||
func (l *LightClientFinalityUpdateKey) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(l)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the LightClientFinalityUpdateKey object with a hasher
|
||||
func (l *LightClientFinalityUpdateKey) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'FinalizedSlot'
|
||||
hh.PutUint64(l.FinalizedSlot)
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the LightClientFinalityUpdateKey object
|
||||
func (l *LightClientFinalityUpdateKey) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(l)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the LightClientOptimisticUpdateKey object
|
||||
func (l *LightClientOptimisticUpdateKey) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(l)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the LightClientOptimisticUpdateKey object to a target array
|
||||
func (l *LightClientOptimisticUpdateKey) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
|
||||
// Field (0) 'OptimisticSlot'
|
||||
dst = ssz.MarshalUint64(dst, l.OptimisticSlot)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the LightClientOptimisticUpdateKey object
|
||||
func (l *LightClientOptimisticUpdateKey) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size != 8 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
// Field (0) 'OptimisticSlot'
|
||||
l.OptimisticSlot = ssz.UnmarshallUint64(buf[0:8])
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the LightClientOptimisticUpdateKey object
|
||||
func (l *LightClientOptimisticUpdateKey) SizeSSZ() (size int) {
|
||||
size = 8
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the LightClientOptimisticUpdateKey object
|
||||
func (l *LightClientOptimisticUpdateKey) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(l)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the LightClientOptimisticUpdateKey object with a hasher
|
||||
func (l *LightClientOptimisticUpdateKey) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'OptimisticSlot'
|
||||
hh.PutUint64(l.OptimisticSlot)
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the LightClientOptimisticUpdateKey object
|
||||
func (l *LightClientOptimisticUpdateKey) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(l)
|
||||
}
|
||||
33
portalnetwork/beacon/types_test.go
Normal file
33
portalnetwork/beacon/types_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package beacon
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/protolambda/zrnt/eth2/beacon/capella"
|
||||
"github.com/protolambda/ztyp/codec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestForkedLightClientBootstrap(t *testing.T) {
|
||||
filePath := "testdata/light_client_bootstrap.json"
|
||||
|
||||
f, _ := os.Open(filePath)
|
||||
jsonStr, _ := io.ReadAll(f)
|
||||
|
||||
var result map[string]interface{}
|
||||
_ = json.Unmarshal(jsonStr, &result)
|
||||
|
||||
for k, v := range result {
|
||||
b, _ := hexutil.Decode(v.(map[string]interface{})["content_value"].(string))
|
||||
dec := codec.NewDecodingReader(bytes.NewReader(b), uint64(len(b)))
|
||||
var f ForkedLightClientBootstrap
|
||||
err := f.Deserialize(dec)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, k, f.Bootstrap.(*capella.LightClientBootstrap).Header.Beacon.Slot.String())
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,27 @@ import "fmt"
|
|||
|
||||
var ErrContentNotFound = fmt.Errorf("content not found")
|
||||
|
||||
type ContentType byte
|
||||
|
||||
type ContentKey struct {
|
||||
selector ContentType
|
||||
data []byte
|
||||
}
|
||||
|
||||
func NewContentKey(selector ContentType, hash []byte) *ContentKey {
|
||||
return &ContentKey{
|
||||
selector: selector,
|
||||
data: hash,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ContentKey) Encode() []byte {
|
||||
res := make([]byte, 0, len(c.data)+1)
|
||||
res = append(res, byte(c.selector))
|
||||
res = append(res, c.data...)
|
||||
return res
|
||||
}
|
||||
|
||||
type ContentStorage interface {
|
||||
Get(contentKey []byte, contentId []byte) ([]byte, error)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue