feat: add docker file and do some optimize

This commit is contained in:
fearlessfe 2024-02-03 23:26:43 +08:00 committed by Chen Kai
parent 2b2c241e7f
commit 35b3ac43af
4 changed files with 76 additions and 28 deletions

16
Dockerfile.portal Normal file
View file

@ -0,0 +1,16 @@
FROM golang:1.20.13 as builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build ./cmd/hive/main.go
FROM ubuntu:22.04
COPY --from=builder /app/main /usr/local/bin/app
EXPOSE 8545 9009/udp
ENTRYPOINT [ "app" ]

View file

@ -5,10 +5,10 @@ import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net/http"
"os"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
@ -17,22 +17,25 @@ import (
)
func main() {
privateKey, err := crypto.GenerateKey()
if err != nil {
panic(err)
var privateKey *ecdsa.PrivateKey
var err error
privateKeyHex := os.Getenv("HIVE_CLIENT_PRIVATE_KEY")
if privateKeyHex != "" {
keyBytes, err := hexutil.Decode("0x" + privateKeyHex)
if err != nil {
panic(err)
}
privateKey, err = crypto.ToECDSA(keyBytes)
if err != nil {
panic(err)
}
} else {
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")
@ -44,6 +47,12 @@ func main() {
}
config.BootstrapNodes = append(config.BootstrapNodes, bootNode)
}
udpPort := os.Getenv("UDP_PORT")
if udpPort != "" {
config.ListenAddr = ":" + udpPort
}
nodeId := enode.PubkeyToIDV4(&privateKey.PublicKey)
contentStorage, err := sqlite.NewContentStorage(1000*1000*1000, nodeId, "./")
if err != nil {
@ -70,13 +79,18 @@ func main() {
server.RegisterName("discv5", disv5)
server.RegisterName("portal", portal)
tcpPort := os.Getenv("TCP_PORT")
if tcpPort == "" {
tcpPort = "8545"
}
httpServer := &http.Server{
Addr: ":8545",
Addr: ":" + tcpPort,
Handler: server,
}
fmt.Printf("before http")
httpServer.ListenAndServe()
fmt.Printf("success")
}
func ReadKeyFromFile(name string) (*ecdsa.PrivateKey, error) {

View file

@ -133,7 +133,7 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
nodeRadius, _ := uint256.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
return &PortalProtocolConfig{
BootstrapNodes: make([]*enode.Node, 0),
ListenAddr: ":9000",
ListenAddr: ":9009",
NetRestrict: nil,
NodeRadius: nodeRadius,
RadiusCacheSize: 32 * 1024 * 1024,
@ -374,6 +374,7 @@ func (p *PortalProtocol) pingInner(node *enode.Node) (*portalwire.Pong, error) {
talkResp, err := p.DiscV5.TalkRequest(node, p.protocolId, talkRequestBytes)
if err != nil {
p.log.Error("ping error:", err)
p.replaceNode(node)
return nil, err
}

View file

@ -5,8 +5,10 @@ import (
"database/sql"
"errors"
"math/big"
"os"
"path"
"strings"
"sync"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
@ -41,6 +43,7 @@ var (
)
var _ storage.ContentStorage = &ContentStorage{}
var once sync.Once
type ContentStorage struct {
nodeId enode.ID
@ -81,14 +84,7 @@ func NewContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataD
func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataDir string) (*ContentStorage, error) {
// avoid repeated register in tests
registered := false
drives := sql.Drivers()
for _, v := range drives {
if v == "sqlite3_custom" {
registered = true
}
}
if !registered {
once.Do(func() {
sql.Register("sqlite3_custom", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
if err := conn.RegisterFunc("xor", xor, false); err != nil {
@ -100,8 +96,11 @@ func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataD
return nil
},
})
}
})
if err := createDir(nodeDataDir); err != nil {
return nil, err
}
sqlDb, err := sql.Open("sqlite3_custom", path.Join(nodeDataDir, sqliteName))
if err != nil {
@ -128,6 +127,24 @@ func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataD
return portalStorage, err
}
func createDir(dir string) error {
stat, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
return err
}
if !stat.IsDir() {
return errors.New("node dir should be a dir")
}
return nil
}
// Get the content according to the contentId
func (p *ContentStorage) Get(contentId []byte) ([]byte, error) {
var res []byte