mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 22:43:47 +00:00
feat: add docker file and do some optimize
This commit is contained in:
parent
2b2c241e7f
commit
35b3ac43af
4 changed files with 76 additions and 28 deletions
16
Dockerfile.portal
Normal file
16
Dockerfile.portal
Normal 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" ]
|
||||||
|
|
@ -5,10 +5,10 @@ import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
|
@ -17,22 +17,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
privateKey, err := crypto.GenerateKey()
|
var privateKey *ecdsa.PrivateKey
|
||||||
if err != nil {
|
var err error
|
||||||
panic(err)
|
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()
|
config := discover.DefaultPortalProtocolConfig()
|
||||||
|
|
||||||
bootNodeStr := os.Getenv("HIVE_BOOTNODE")
|
bootNodeStr := os.Getenv("HIVE_BOOTNODE")
|
||||||
|
|
@ -44,6 +47,12 @@ func main() {
|
||||||
}
|
}
|
||||||
config.BootstrapNodes = append(config.BootstrapNodes, bootNode)
|
config.BootstrapNodes = append(config.BootstrapNodes, bootNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
udpPort := os.Getenv("UDP_PORT")
|
||||||
|
|
||||||
|
if udpPort != "" {
|
||||||
|
config.ListenAddr = ":" + udpPort
|
||||||
|
}
|
||||||
nodeId := enode.PubkeyToIDV4(&privateKey.PublicKey)
|
nodeId := enode.PubkeyToIDV4(&privateKey.PublicKey)
|
||||||
contentStorage, err := sqlite.NewContentStorage(1000*1000*1000, nodeId, "./")
|
contentStorage, err := sqlite.NewContentStorage(1000*1000*1000, nodeId, "./")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -70,13 +79,18 @@ func main() {
|
||||||
server.RegisterName("discv5", disv5)
|
server.RegisterName("discv5", disv5)
|
||||||
server.RegisterName("portal", portal)
|
server.RegisterName("portal", portal)
|
||||||
|
|
||||||
|
tcpPort := os.Getenv("TCP_PORT")
|
||||||
|
|
||||||
|
if tcpPort == "" {
|
||||||
|
tcpPort = "8545"
|
||||||
|
}
|
||||||
|
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: ":8545",
|
Addr: ":" + tcpPort,
|
||||||
Handler: server,
|
Handler: server,
|
||||||
}
|
}
|
||||||
fmt.Printf("before http")
|
|
||||||
httpServer.ListenAndServe()
|
httpServer.ListenAndServe()
|
||||||
fmt.Printf("success")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadKeyFromFile(name string) (*ecdsa.PrivateKey, error) {
|
func ReadKeyFromFile(name string) (*ecdsa.PrivateKey, error) {
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ func DefaultPortalProtocolConfig() *PortalProtocolConfig {
|
||||||
nodeRadius, _ := uint256.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
nodeRadius, _ := uint256.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
||||||
return &PortalProtocolConfig{
|
return &PortalProtocolConfig{
|
||||||
BootstrapNodes: make([]*enode.Node, 0),
|
BootstrapNodes: make([]*enode.Node, 0),
|
||||||
ListenAddr: ":9000",
|
ListenAddr: ":9009",
|
||||||
NetRestrict: nil,
|
NetRestrict: nil,
|
||||||
NodeRadius: nodeRadius,
|
NodeRadius: nodeRadius,
|
||||||
RadiusCacheSize: 32 * 1024 * 1024,
|
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)
|
talkResp, err := p.DiscV5.TalkRequest(node, p.protocolId, talkRequestBytes)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.log.Error("ping error:", err)
|
||||||
p.replaceNode(node)
|
p.replaceNode(node)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,10 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
|
@ -41,6 +43,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ storage.ContentStorage = &ContentStorage{}
|
var _ storage.ContentStorage = &ContentStorage{}
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
type ContentStorage struct {
|
type ContentStorage struct {
|
||||||
nodeId enode.ID
|
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) {
|
func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataDir string) (*ContentStorage, error) {
|
||||||
// avoid repeated register in tests
|
// avoid repeated register in tests
|
||||||
registered := false
|
once.Do(func() {
|
||||||
drives := sql.Drivers()
|
|
||||||
for _, v := range drives {
|
|
||||||
if v == "sqlite3_custom" {
|
|
||||||
registered = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !registered {
|
|
||||||
sql.Register("sqlite3_custom", &sqlite3.SQLiteDriver{
|
sql.Register("sqlite3_custom", &sqlite3.SQLiteDriver{
|
||||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||||
if err := conn.RegisterFunc("xor", xor, false); err != nil {
|
if err := conn.RegisterFunc("xor", xor, false); err != nil {
|
||||||
|
|
@ -100,8 +96,11 @@ func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataD
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
|
|
||||||
|
if err := createDir(nodeDataDir); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
sqlDb, err := sql.Open("sqlite3_custom", path.Join(nodeDataDir, sqliteName))
|
sqlDb, err := sql.Open("sqlite3_custom", path.Join(nodeDataDir, sqliteName))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -128,6 +127,24 @@ func newContentStorage(storageCapacityInBytes uint64, nodeId enode.ID, nodeDataD
|
||||||
return portalStorage, err
|
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
|
// Get the content according to the contentId
|
||||||
func (p *ContentStorage) Get(contentId []byte) ([]byte, error) {
|
func (p *ContentStorage) Get(contentId []byte) ([]byte, error) {
|
||||||
var res []byte
|
var res []byte
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue