feat: add main for hive

This commit is contained in:
fearlessfe 2024-01-27 13:06:48 +08:00 committed by Chen Kai
parent ca64ccc5dd
commit 975f60067b
3 changed files with 136 additions and 28 deletions

97
cmd/hive/main.go Normal file
View file

@ -0,0 +1,97 @@
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net/http"
"os"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage/sqlite"
"github.com/ethereum/go-ethereum/rpc"
)
func main() {
privateKey, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
// testStr := "enr:-Iq4QM89fOvgXRfNo429L4vD_qn3GEAQqelOQzMAJCBZs7oZZ3t6F07P-J2iNUMtyeDcvTzBqP4lJD0EllJdht7GOYqGAY1GIhc5gmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQM0MbiqlCBmJu-8T2tC4z9KqlcyB6HkdRsASowWTVrQAoN1ZHCCIyg"
// if err != nil {
// panic(err)
// }
// record := new(enr.Record)
// err = rlp.DecodeBytes([]byte(testStr), record)
// if err != nil {
// panic(err)
// }
config := discover.DefaultPortalProtocolConfig()
bootNodeStr := os.Getenv("HIVE_BOOTNODE")
if bootNodeStr != "" {
bootNode := new(enode.Node)
err = bootNode.UnmarshalText([]byte(bootNodeStr))
if err != nil {
panic(err)
}
config.BootstrapNodes = append(config.BootstrapNodes, bootNode)
}
nodeId := enode.PubkeyToIDV4(&privateKey.PublicKey)
contentStorage, err := sqlite.NewContentStorage(1000*1000*1000, nodeId, "./")
if err != nil {
panic(err)
}
contentQueue := make(chan *discover.ContentElement, 50)
protocol, err := discover.NewPortalProtocol(config, "history", privateKey, contentStorage, contentQueue)
if err != nil {
panic(err)
}
err = protocol.Start()
if err != nil {
panic(err)
}
disv5 := discover.NewAPI(protocol.DiscV5)
portal := discover.NewPortalAPI(protocol)
server := rpc.NewServer()
server.RegisterName("discv5", disv5)
server.RegisterName("portal", portal)
httpServer := &http.Server{
Addr: ":8545",
Handler: server,
}
fmt.Printf("before http")
httpServer.ListenAndServe()
fmt.Printf("success")
}
func ReadKeyFromFile(name string) (*ecdsa.PrivateKey, error) {
keyBytes, err := os.ReadFile(name)
if err != nil {
return nil, err
}
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, errors.New("failed to decode PEM block")
}
privateKey, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}

View file

@ -40,6 +40,8 @@ var (
maxDistance = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
)
var _ storage.ContentStorage = &ContentStorage{}
type ContentStorage struct {
nodeId enode.ID
nodeDataDir string
@ -73,7 +75,11 @@ func greater(a, b []byte) int {
return bytes.Compare(a, b)
}
func NewContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataDir string) (*ContentStorage, error) {
func NewContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataDir string) (storage.ContentStorage, error) {
return newContentStorage(storageCapacityInBytes, nodeId, nodeDataDir)
}
func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataDir string) (*ContentStorage, error) {
// avoid repeated register in tests
registered := false
drives := sql.Drivers()
@ -156,8 +162,13 @@ func newPutResultWithErr(err error) PutResult {
}
}
func (p *ContentStorage) Put(contentId []byte, content []byte) error {
res := p.put(contentId, content)
return res.Err()
}
// Put saves the contentId and content
func (p *ContentStorage) Put(contentId []byte, content []byte) PutResult {
func (p *ContentStorage) put(contentId []byte, content []byte) PutResult {
_, err := p.putStmt.Exec(contentId, content)
if err != nil {
return newPutResultWithErr(err)

View file

@ -28,7 +28,7 @@ func genBytes(length int) []byte {
func TestBasicStorage(t *testing.T) {
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := NewContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -39,7 +39,7 @@ func TestBasicStorage(t *testing.T) {
_, err = storage.Get(contentId)
assert.Equal(t, contentStorage.ErrContentNotFound, err)
pt := storage.Put(contentId, content)
pt := storage.put(contentId, content)
assert.NoError(t, pt.Err())
val, err := storage.Get(contentId)
@ -64,7 +64,7 @@ func TestBasicStorage(t *testing.T) {
func TestDBSize(t *testing.T) {
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := NewContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -73,17 +73,17 @@ func TestDBSize(t *testing.T) {
size1, err := storage.Size()
assert.NoError(t, err)
putResult := storage.Put(uint256.NewInt(1).Bytes(), genBytes(numBytes))
putResult := storage.put(uint256.NewInt(1).Bytes(), genBytes(numBytes))
assert.Nil(t, putResult.Err())
size2, err := storage.Size()
assert.NoError(t, err)
putResult = storage.Put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
putResult = storage.put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
assert.NoError(t, putResult.Err())
size3, err := storage.Size()
assert.NoError(t, err)
putResult = storage.Put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
putResult = storage.put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
assert.NoError(t, putResult.Err())
size4, err := storage.Size()
@ -125,7 +125,7 @@ func TestDBPruning(t *testing.T) {
storageCapacity := uint64(100_000)
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := NewContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -136,29 +136,29 @@ func TestDBPruning(t *testing.T) {
numBytes := 10_000
// test with private put method
pt1 := storage.Put(uint256.NewInt(1).Bytes(), genBytes(numBytes))
pt1 := storage.put(uint256.NewInt(1).Bytes(), genBytes(numBytes))
assert.NoError(t, pt1.Err())
pt2 := storage.Put(thirdFurthest.Bytes(), genBytes(numBytes))
pt2 := storage.put(thirdFurthest.Bytes(), genBytes(numBytes))
assert.NoError(t, pt2.Err())
pt3 := storage.Put(uint256.NewInt(3).Bytes(), genBytes(numBytes))
pt3 := storage.put(uint256.NewInt(3).Bytes(), genBytes(numBytes))
assert.NoError(t, pt3.Err())
pt4 := storage.Put(uint256.NewInt(10).Bytes(), genBytes(numBytes))
pt4 := storage.put(uint256.NewInt(10).Bytes(), genBytes(numBytes))
assert.NoError(t, pt4.Err())
pt5 := storage.Put(uint256.NewInt(5).Bytes(), genBytes(numBytes))
pt5 := storage.put(uint256.NewInt(5).Bytes(), genBytes(numBytes))
assert.NoError(t, pt5.Err())
pt6 := storage.Put(uint256.NewInt(11).Bytes(), genBytes(numBytes))
pt6 := storage.put(uint256.NewInt(11).Bytes(), genBytes(numBytes))
assert.NoError(t, pt6.Err())
pt7 := storage.Put(furthestElement.Bytes(), genBytes(4000))
pt7 := storage.put(furthestElement.Bytes(), genBytes(4000))
assert.NoError(t, pt7.Err())
pt8 := storage.Put(secondFurthest.Bytes(), genBytes(3000))
pt8 := storage.put(secondFurthest.Bytes(), genBytes(3000))
assert.NoError(t, pt8.Err())
pt9 := storage.Put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
pt9 := storage.put(uint256.NewInt(2).Bytes(), genBytes(numBytes))
assert.NoError(t, pt9.Err())
res, _ := storage.GetLargestDistance()
assert.Equal(t, res, uint256.NewInt(40))
pt10 := storage.Put(uint256.NewInt(4).Bytes(), genBytes(12000))
pt10 := storage.put(uint256.NewInt(4).Bytes(), genBytes(12000))
assert.NoError(t, pt10.Err())
assert.False(t, pt1.Pruned())
@ -192,7 +192,7 @@ func TestGetLargestDistance(t *testing.T) {
storageCapacity := uint64(100_000)
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := NewContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -200,13 +200,13 @@ func TestGetLargestDistance(t *testing.T) {
furthestElement := uint256.NewInt(40)
secondFurthest := uint256.NewInt(30)
pt7 := storage.Put(furthestElement.Bytes(), genBytes(2000))
pt7 := storage.put(furthestElement.Bytes(), genBytes(2000))
assert.NoError(t, pt7.Err())
val, err := storage.Get(furthestElement.Bytes())
assert.NoError(t, err)
assert.NotNil(t, val)
pt8 := storage.Put(secondFurthest.Bytes(), genBytes(2000))
pt8 := storage.put(secondFurthest.Bytes(), genBytes(2000))
assert.NoError(t, pt8.Err())
res, err := storage.GetLargestDistance()
assert.NoError(t, err)
@ -217,7 +217,7 @@ func TestSimpleForcePruning(t *testing.T) {
storageCapacity := uint64(100_000)
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := NewContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -226,13 +226,13 @@ func TestSimpleForcePruning(t *testing.T) {
secondFurthest := uint256.NewInt(30)
third := uint256.NewInt(10)
pt1 := storage.Put(furthestElement.Bytes(), genBytes(2000))
pt1 := storage.put(furthestElement.Bytes(), genBytes(2000))
assert.NoError(t, pt1.Err())
pt2 := storage.Put(secondFurthest.Bytes(), genBytes(2000))
pt2 := storage.put(secondFurthest.Bytes(), genBytes(2000))
assert.NoError(t, pt2.Err())
pt3 := storage.Put(third.Bytes(), genBytes(2000))
pt3 := storage.put(third.Bytes(), genBytes(2000))
assert.NoError(t, pt3.Err())
res, err := storage.GetLargestDistance()
assert.NoError(t, err)
@ -261,7 +261,7 @@ func TestForcePruning(t *testing.T) {
nodeId := uint256.MustFromHex("0x30994892f3e4889d99deb5340050510d1842778acc7a7948adffa475fed51d6e").Bytes()
content := genBytes(1000)
storage, err := NewContentStorage(startCap, enode.ID(nodeId), nodeDataDir)
storage, err := newContentStorage(startCap, enode.ID(nodeId), nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
@ -273,7 +273,7 @@ func TestForcePruning(t *testing.T) {
putCount := 0
// id < maxUint256 - remainder
for id.Cmp(uint256.NewInt(0).Sub(maxUint256, remainder)) == -1 {
res := storage.Put(id.Bytes(), content)
res := storage.put(id.Bytes(), content)
assert.NoError(t, res.Err())
id = id.Add(id, increment)
putCount++