diff --git a/p2p/discover/api.go b/p2p/discover/api.go index 7bb983b07d..8e2b49d5bd 100644 --- a/p2p/discover/api.go +++ b/p2p/discover/api.go @@ -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 diff --git a/p2p/discover/portal_protocol.go b/p2p/discover/portal_protocol.go index 220a9728fd..d2d80e31e7 100644 --- a/p2p/discover/portal_protocol.go +++ b/p2p/discover/portal_protocol.go @@ -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 } diff --git a/p2p/discover/portalwire/messages_test.go b/p2p/discover/portalwire/messages_test.go index d7b412605a..a83a6dcffb 100644 --- a/p2p/discover/portalwire/messages_test.go +++ b/p2p/discover/portalwire/messages_test.go @@ -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 -} diff --git a/portalnetwork/history/accumulator.go b/portalnetwork/history/accumulator.go index f70ad6c666..610a5d3cb3 100644 --- a/portalnetwork/history/accumulator.go +++ b/portalnetwork/history/accumulator.go @@ -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 { diff --git a/portalnetwork/history/accumulator_test.go b/portalnetwork/history/accumulator_test.go index 9d66a06b3a..82876d3eb8 100644 --- a/portalnetwork/history/accumulator_test.go +++ b/portalnetwork/history/accumulator_test.go @@ -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)) -} diff --git a/portalnetwork/storage/sqlite/content_storage.go b/portalnetwork/storage/sqlite/content_storage.go index c8dadc3317..113be22ad6 100644 --- a/portalnetwork/storage/sqlite/content_storage.go +++ b/portalnetwork/storage/sqlite/content_storage.go @@ -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. diff --git a/portalnetwork/utils/bytes.go b/portalnetwork/utils/bytes.go deleted file mode 100644 index 0205d31ca7..0000000000 --- a/portalnetwork/utils/bytes.go +++ /dev/null @@ -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 -}