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

faet: add update test #16

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions portalnetwork/history/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"

"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"
Expand Down Expand Up @@ -43,11 +44,15 @@ func newEpoch() *epoch {

func (e *epoch) add(header types.Header) error {
blockHash := header.Hash().Bytes()
difficulty := uint256.MustFromBig(header.Number)
difficulty := uint256.MustFromBig(header.Difficulty)
e.difficulty = uint256.NewInt(0).Add(e.difficulty, difficulty)
// big-endian
difficultyBytes := e.difficulty.Bytes32()
utils.ReverseBytesInPlace(difficultyBytes[:])

record := HeaderRecord{
BlockHash: blockHash,
TotalDifficulty: e.difficulty.Bytes(),
TotalDifficulty: difficultyBytes[:],
}
sszBytes, err := record.MarshalSSZ()
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions portalnetwork/history/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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"
)

Expand Down Expand Up @@ -51,6 +54,35 @@ func TestBuildAndVerifyProof(t *testing.T) {
}
}

func TestUpdate(t *testing.T) {
epochAcc, err := getEpochAccu("0xcddbda3fd6f764602c06803ff083dbfc73f2bb396df17a31e5457329b9a0f38d")
assert.NoError(t, err)

startNumber := 1000000
epochRecordIndex := GetHeaderRecordIndex(uint64(startNumber))

newEpochAcc := NewAccumulator()

for i := 0; i <= int(epochRecordIndex); i++ {
tmp := make([]byte, 64)
copy(tmp, epochAcc.HeaderRecords[i])
newEpochAcc.currentEpoch.records = append(newEpochAcc.currentEpoch.records, tmp)
}

startDifficulty := bytesToUint256(epochAcc.HeaderRecords[epochRecordIndex][32:])

newEpochAcc.currentEpoch.difficulty = startDifficulty

for i := startNumber + 1; i <= 1000010; i++ {
header, err := getHeader(uint64(i))
assert.NoError(t, err)
err = newEpochAcc.Update(*header)
assert.NoError(t, err)
currIndex := GetHeaderRecordIndex(uint64(i))
assert.True(t, bytes.Equal(newEpochAcc.currentEpoch.records[currIndex], epochAcc.HeaderRecords[currIndex]))
}
}

// all test blocks are in the same epoch
func parseHeaderWithProof() ([]BlockHeaderWithProof, error) {
headWithProofBytes, err := os.ReadFile("./testdata/header_with_proofs.json")
Expand Down Expand Up @@ -112,3 +144,8 @@ 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))
}
9 changes: 2 additions & 7 deletions portalnetwork/storage/content_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/utils"
"github.com/holiman/uint256"
sqlite3 "github.com/mattn/go-sqlite3"
)
Expand Down Expand Up @@ -285,7 +286,7 @@ func (p *ContentStorage) GetLargestDistance() (*uint256.Int, error) {
return nil, err
}
// reverse the distance, because big.SetBytes is big-endian
reverseBytes(distance)
utils.ReverseBytesInPlace(distance)
bigNum := new(big.Int).SetBytes(distance)
res := uint256.MustFromBig(bigNum)
return res, nil
Expand Down Expand Up @@ -385,9 +386,3 @@ func (p *ContentStorage) deleteContentOutOfRadius(radius *uint256.Int) error {
func (p *ContentStorage) ForcePrune(radius *uint256.Int) error {
return p.deleteContentOutOfRadius(radius)
}

func reverseBytes(src []byte) {
for i := 0; i < len(src)/2; i++ {
src[i], src[len(src)-i-1] = src[len(src)-i-1], src[i]
}
}
16 changes: 16 additions & 0 deletions portalnetwork/utils/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
}