feat: add gossip rpc and do some refactor

1. add gossip rpc method
2. use uint256.Ummarshal method to unmarshal uint256 bytes
This commit is contained in:
fearlessfe 2024-02-13 23:43:27 +08:00 committed by Chen Kai
parent 5e657f8423
commit bcb5d28724
7 changed files with 34 additions and 49 deletions

View file

@ -483,8 +483,17 @@ func (p *PortalAPI) HistoryStore(contentKeyHex string, contextHex string) (bool,
}
// TODO
func (p *PortalAPI) HistoryGossip() {
func (p *PortalAPI) HistoryGossip(contentKeyHex, contentHex string) (int, error) {
contentKey, err := hexutil.Decode(contentKeyHex)
if err != nil {
return 0, err
}
content, err := hexutil.Decode(contentHex)
if err != nil {
return 0, err
}
id := p.portalProtocol.Self().ID()
return p.portalProtocol.NeighborhoodGossip(&id, [][]byte{contentKey}, [][]byte{content})
}
// TODO

View file

@ -271,7 +271,7 @@ func (p *PortalProtocol) RoutingTableInfo() [][]string {
}
nodes = append(nodes, bucketNodes)
}
p.log.Trace("rouingTableInfo nodes:", nodes)
p.log.Trace("rouingTableInfo res:", "nodes", nodes)
return nodes
}

View file

@ -10,6 +10,7 @@ import (
ssz "github.com/ferranbt/fastssz"
"github.com/holiman/uint256"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var maxUint256 = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
@ -18,8 +19,8 @@ var maxUint256 = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffff
// we remove the message type here
func TestPingMessage(t *testing.T) {
dataRadius := maxUint256.Sub(maxUint256, uint256.NewInt(1))
reverseBytes := ReverseBytes(dataRadius.Bytes())
reverseBytes, err := dataRadius.MarshalSSZ()
require.NoError(t, err)
customData := &PingPongCustomData{
Radius: reverseBytes,
}
@ -39,7 +40,8 @@ func TestPingMessage(t *testing.T) {
func TestPongMessage(t *testing.T) {
dataRadius := maxUint256.Div(maxUint256, uint256.NewInt(2))
reverseBytes := ReverseBytes(dataRadius.Bytes())
reverseBytes, err := dataRadius.MarshalSSZ()
require.NoError(t, err)
customData := &PingPongCustomData{
Radius: reverseBytes,
}
@ -177,12 +179,3 @@ func TestOfferAndAcceptMessage(t *testing.T) {
// assert.NoError(t, err)
// assert.Equal(t, expected, fmt.Sprintf("0x%x", data))
}
func ReverseBytes(src []byte) []byte {
lenth := len(src)
dst := make([]byte, lenth)
for i := 0; i < len(src); i++ {
dst[lenth-1-i] = src[i]
}
return dst
}

View file

@ -8,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/portalnetwork/utils"
"github.com/ethereum/go-ethereum/rlp"
ssz "github.com/ferranbt/fastssz"
"github.com/holiman/uint256"
@ -48,13 +47,14 @@ func (e *epoch) add(header types.Header) error {
blockHash := header.Hash().Bytes()
difficulty := uint256.MustFromBig(header.Difficulty)
e.difficulty = uint256.NewInt(0).Add(e.difficulty, difficulty)
// big-endian
difficultyBytes := e.difficulty.Bytes32()
utils.ReverseBytesInPlace(difficultyBytes[:])
difficultyBytes, err := e.difficulty.MarshalSSZ()
if err != nil {
return err
}
record := HeaderRecord{
BlockHash: blockHash,
TotalDifficulty: difficultyBytes[:],
TotalDifficulty: difficultyBytes,
}
sszBytes, err := record.MarshalSSZ()
if err != nil {

View file

@ -4,17 +4,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"os"
"strconv"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/portalnetwork/utils"
"github.com/ethereum/go-ethereum/rlp"
"github.com/holiman/uint256"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVerifyHeaderWithProofs(t *testing.T) {
@ -68,8 +67,10 @@ func TestUpdate(t *testing.T) {
copy(tmp, epochAcc.HeaderRecords[i])
newEpochAcc.currentEpoch.records = append(newEpochAcc.currentEpoch.records, tmp)
}
startDifficulty := uint256.NewInt(0)
err = startDifficulty.UnmarshalSSZ(epochAcc.HeaderRecords[epochRecordIndex][32:])
startDifficulty := bytesToUint256(epochAcc.HeaderRecords[epochRecordIndex][32:])
require.NoError(t, err)
newEpochAcc.currentEpoch.difficulty = startDifficulty
@ -144,8 +145,3 @@ func getHeader(number uint64) (*types.Header, error) {
err = rlp.Decode(reader, head)
return head, err
}
func bytesToUint256(input []byte) *uint256.Int {
res := utils.ReverseBytes(input)
return uint256.MustFromBig(big.NewInt(0).SetBytes(res))
}

View file

@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/ethereum/go-ethereum/portalnetwork/utils"
"github.com/holiman/uint256"
sqlite3 "github.com/mattn/go-sqlite3"
)
@ -312,11 +311,15 @@ func (p *ContentStorage) GetLargestDistance() (*uint256.Int, error) {
if err != nil {
return nil, err
}
res := uint256.NewInt(0)
err = res.UnmarshalSSZ(distance)
return res, err
// reverse the distance, because big.SetBytes is big-endian
utils.ReverseBytesInPlace(distance)
bigNum := new(big.Int).SetBytes(distance)
res := uint256.MustFromBig(bigNum)
return res, nil
// utils.ReverseBytesInPlace(distance)
// bigNum := new(big.Int).SetBytes(distance)
// res := uint256.MustFromBig(bigNum)
// return res, nil
}
// EstimateNewRadius calculates an estimated new radius based on the current radius, used size, and storage capacity.

View file

@ -1,16 +0,0 @@
package utils
func ReverseBytesInPlace(src []byte) {
for i := 0; i < len(src)/2; i++ {
src[i], src[len(src)-i-1] = src[len(src)-i-1], src[i]
}
}
func ReverseBytes(src []byte) []byte {
lenth := len(src)
dst := make([]byte, lenth)
for i := 0; i < len(src); i++ {
dst[lenth-1-i] = src[i]
}
return dst
}