mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
merge
Merge branch 'bzz' of https://github.com/ethersphere/go-ethereum into bzz Conflicts: bzz/chunker.go bzz/memstore.go bzz/netstore.go
This commit is contained in:
commit
6baa73c24e
10 changed files with 79 additions and 31 deletions
|
|
@ -321,7 +321,7 @@ func (self *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error) {
|
||||||
dpaLogger.Debugf("chunk data received for %x", chunk.Key[:4])
|
dpaLogger.Debugf("chunk data received for %x", chunk.Key[:4])
|
||||||
}
|
}
|
||||||
if len(chunk.SData) == 0 {
|
if len(chunk.SData) == 0 {
|
||||||
dpaLogger.Debugf("No payload.")
|
dpaLogger.Debugf("No payload in %x.", chunk.Key)
|
||||||
return 0, notFound
|
return 0, notFound
|
||||||
}
|
}
|
||||||
chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8]))
|
chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8]))
|
||||||
|
|
|
||||||
|
|
@ -178,15 +178,25 @@ func readAll(reader SectionReader, result []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func benchReadAll(reader SectionReader) {
|
||||||
|
size := reader.Size()
|
||||||
|
output := make([]byte, 1000)
|
||||||
|
for pos := int64(0); pos < size; pos += 1000 {
|
||||||
|
reader.ReadAt(output, pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func benchmarkJoinRandomData(n int, chunks int, t *testing.B) {
|
func benchmarkJoinRandomData(n int, chunks int, t *testing.B) {
|
||||||
for i := 0; i < t.N; i++ {
|
|
||||||
t.StopTimer()
|
t.StopTimer()
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
// fmt.Printf("round %v\n", i)
|
||||||
chunker, tester := chunkerAndTester()
|
chunker, tester := chunkerAndTester()
|
||||||
key, _ := tester.Split(chunker, n)
|
key, _ := tester.Split(chunker, n)
|
||||||
|
// fmt.Printf("split done %v, joining...\n", i)
|
||||||
t.StartTimer()
|
t.StartTimer()
|
||||||
reader := tester.Join(chunker, key, i)
|
reader := tester.Join(chunker, key, i)
|
||||||
result := make([]byte, n)
|
// fmt.Printf("join done %v, reading...\n", i)
|
||||||
readAll(reader, result)
|
benchReadAll(reader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,12 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
|
||||||
size, err := manifestReader.Read(manifest)
|
size, err := manifestReader.Read(manifest)
|
||||||
if int64(size) < manifestReader.Size() {
|
if int64(size) < manifestReader.Size() {
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s not found.", name)
|
dpaLogger.Debugf("Swarm: Manifest %s not found.", name)
|
||||||
|
if err == nil {
|
||||||
|
http.Error(w, "Manifest retrieval cut short: "+string(size)+"<"+string(manifestReader.Size()),
|
||||||
|
http.StatusNotFound)
|
||||||
|
} else {
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s retrieved.", name)
|
dpaLogger.Debugf("Swarm: Manifest %s retrieved.", name)
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,7 @@ func (s *memStore) getEntryCnt() uint {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// entry (not its copy) is going to be in memStore
|
||||||
func (s *memStore) Put(entry *Chunk) {
|
func (s *memStore) Put(entry *Chunk) {
|
||||||
|
|
||||||
if s.capacity == 0 {
|
if s.capacity == 0 {
|
||||||
|
|
@ -193,13 +194,15 @@ func (s *memStore) Put(entry *Chunk) {
|
||||||
|
|
||||||
if node.entry.Key.isEqual(entry.Key) {
|
if node.entry.Key.isEqual(entry.Key) {
|
||||||
node.updateAccess(s.accessCnt)
|
node.updateAccess(s.accessCnt)
|
||||||
if node.entry.SData == nil {
|
if entry.SData == nil {
|
||||||
node.entry.Size = entry.Size
|
entry.Size = node.entry.Size
|
||||||
node.entry.SData = entry.SData
|
entry.SData = node.entry.SData
|
||||||
}
|
}
|
||||||
if node.entry.req == nil {
|
if entry.req == nil {
|
||||||
node.entry.req = entry.req
|
entry.req = node.entry.req
|
||||||
}
|
}
|
||||||
|
entry.C = node.entry.C
|
||||||
|
node.entry = entry
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -253,11 +256,7 @@ func (s *memStore) Get(hash Key) (chunk *Chunk, err error) {
|
||||||
if node.entry.Key.isEqual(hash) {
|
if node.entry.Key.isEqual(hash) {
|
||||||
s.accessCnt++
|
s.accessCnt++
|
||||||
node.updateAccess(s.accessCnt)
|
node.updateAccess(s.accessCnt)
|
||||||
chunk = &Chunk{
|
chunk = node.entry
|
||||||
Key: hash,
|
|
||||||
SData: node.entry.SData,
|
|
||||||
Size: node.entry.Size,
|
|
||||||
}
|
|
||||||
if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt {
|
if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt {
|
||||||
s.dbAccessCnt++
|
s.dbAccessCnt++
|
||||||
node.lastDBaccess = s.dbAccessCnt
|
node.lastDBaccess = s.dbAccessCnt
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,10 @@ func (self *NetStore) Put(entry *Chunk) {
|
||||||
|
|
||||||
func (self *NetStore) put(entry *Chunk) {
|
func (self *NetStore) put(entry *Chunk) {
|
||||||
self.localStore.Put(entry)
|
self.localStore.Put(entry)
|
||||||
dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed.", entry.Key)
|
dpaLogger.Debugf("NetStore.put: localStore.Put of %064x completed, %d bytes (%p).", entry.Key, len(entry.SData), entry)
|
||||||
self.store(entry)
|
go self.store(entry)
|
||||||
// only send responses once
|
// only send responses once
|
||||||
|
dpaLogger.Debugf("NetStore.put: req: %#v", entry.req)
|
||||||
if entry.req != nil && entry.req.status == reqSearching {
|
if entry.req != nil && entry.req.status == reqSearching {
|
||||||
entry.req.status = reqFound
|
entry.req.status = reqFound
|
||||||
close(entry.req.C)
|
close(entry.req.C)
|
||||||
|
|
@ -82,7 +83,9 @@ func (self *NetStore) put(entry *Chunk) {
|
||||||
func (self *NetStore) addStoreRequest(req *storeRequestMsgData) {
|
func (self *NetStore) addStoreRequest(req *storeRequestMsgData) {
|
||||||
self.lock.Lock()
|
self.lock.Lock()
|
||||||
defer self.lock.Unlock()
|
defer self.lock.Unlock()
|
||||||
|
dpaLogger.Debugf("NetStore.addStoreRequest: req = %#v", req)
|
||||||
chunk, err := self.localStore.Get(req.Key)
|
chunk, err := self.localStore.Get(req.Key)
|
||||||
|
dpaLogger.Debugf("NetStore.addStoreRequest: chunk reference %p", chunk)
|
||||||
// we assume that a returned chunk is the one stored in the memory cache
|
// we assume that a returned chunk is the one stored in the memory cache
|
||||||
if err != nil {
|
if err != nil {
|
||||||
chunk = &Chunk{
|
chunk = &Chunk{
|
||||||
|
|
@ -116,7 +119,7 @@ func (self *NetStore) Get(key Key) (chunk *Chunk, err error) {
|
||||||
dpaLogger.Debugf("NetStore.Get: %064x request time out ", key)
|
dpaLogger.Debugf("NetStore.Get: %064x request time out ", key)
|
||||||
err = notFound
|
err = notFound
|
||||||
case <-chunk.req.C:
|
case <-chunk.req.C:
|
||||||
dpaLogger.Debugf("NetStore.get: %064x retrieved", key)
|
dpaLogger.Debugf("NetStore.Get: %064x retrieved, %d bytes (%p)", key, len(chunk.SData), chunk)
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -137,6 +140,7 @@ func (self *NetStore) get(key Key) (chunk *Chunk) {
|
||||||
|
|
||||||
if chunk.req == nil {
|
if chunk.req == nil {
|
||||||
chunk.req = new(requestStatus)
|
chunk.req = new(requestStatus)
|
||||||
|
chunk.req.C = make(chan bool)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -158,8 +162,6 @@ func (self *NetStore) addRetrieveRequest(req *retrieveRequestMsgData) {
|
||||||
|
|
||||||
send, timeout := self.strategyUpdateRequest(chunk.req, req) // may change req status
|
send, timeout := self.strategyUpdateRequest(chunk.req, req) // may change req status
|
||||||
|
|
||||||
dpaLogger.Debugf("Is %v == %v?", send, storeRequestMsg)
|
|
||||||
|
|
||||||
if send == storeRequestMsg {
|
if send == storeRequestMsg {
|
||||||
dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - content found, delivering...", req.Key)
|
dpaLogger.Debugf("NetStore.addRetrieveRequest: %064x - content found, delivering...", req.Key)
|
||||||
self.deliver(req, chunk)
|
self.deliver(req, chunk)
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,7 @@ var (
|
||||||
Dial bool
|
Dial bool
|
||||||
PrintVersion bool
|
PrintVersion bool
|
||||||
Peers string
|
Peers string
|
||||||
|
Pull string
|
||||||
)
|
)
|
||||||
|
|
||||||
// flags specific to cli client
|
// flags specific to cli client
|
||||||
|
|
@ -105,6 +106,7 @@ func Init() {
|
||||||
flag.BoolVar(&Dial, "dial", true, "dial out connections (on)")
|
flag.BoolVar(&Dial, "dial", true, "dial out connections (on)")
|
||||||
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
|
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
|
||||||
flag.StringVar(&Peers, "peers", "", "imports the file given (hex or mnemonic formats)")
|
flag.StringVar(&Peers, "peers", "", "imports the file given (hex or mnemonic formats)")
|
||||||
|
flag.StringVar(&Pull, "pull", "", "swarm pull key")
|
||||||
flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
|
flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
|
||||||
flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
|
flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
|
||||||
flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)")
|
flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)")
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ func main() {
|
||||||
utils.StartWebSockets(ethereum)
|
utils.StartWebSockets(ethereum)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.StartEthereum(ethereum, UseSeed, Peers)
|
utils.StartEthereum(ethereum, UseSeed, Peers, Pull)
|
||||||
|
|
||||||
if StartJsConsole {
|
if StartJsConsole {
|
||||||
InitJsConsole(ethereum)
|
InitJsConsole(ethereum)
|
||||||
|
|
|
||||||
|
|
@ -120,9 +120,9 @@ func exit(err error) {
|
||||||
os.Exit(status)
|
os.Exit(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartEthereum(ethereum *eth.Ethereum, UseSeed bool, Peers string) {
|
func StartEthereum(ethereum *eth.Ethereum, UseSeed bool, Peers string, Pull string) {
|
||||||
clilogger.Infof("Starting %s", ethereum.ClientIdentity())
|
clilogger.Infof("Starting %s", ethereum.ClientIdentity())
|
||||||
err := ethereum.Start(UseSeed, Peers)
|
err := ethereum.Start(UseSeed, Peers, Pull)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ package eth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/bzz"
|
"github.com/ethereum/go-ethereum/bzz"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
|
@ -70,6 +72,7 @@ type Ethereum struct {
|
||||||
|
|
||||||
RpcServer *rpc.JsonRpcServer
|
RpcServer *rpc.JsonRpcServer
|
||||||
keyManager *crypto.KeyManager
|
keyManager *crypto.KeyManager
|
||||||
|
dpa *bzz.DPA
|
||||||
|
|
||||||
clientIdentity p2p.ClientIdentity
|
clientIdentity p2p.ClientIdentity
|
||||||
logger ethlogger.LogSystem
|
logger ethlogger.LogSystem
|
||||||
|
|
@ -144,12 +147,12 @@ func New(config *Config) (*Ethereum, error) {
|
||||||
}
|
}
|
||||||
chunker := &bzz.TreeChunker{}
|
chunker := &bzz.TreeChunker{}
|
||||||
chunker.Init()
|
chunker.Init()
|
||||||
dpa := &bzz.DPA{
|
eth.dpa = &bzz.DPA{
|
||||||
Chunker: chunker,
|
Chunker: chunker,
|
||||||
ChunkStore: netStore,
|
ChunkStore: netStore,
|
||||||
}
|
}
|
||||||
dpa.Start()
|
eth.dpa.Start()
|
||||||
go bzz.StartHttpServer(dpa)
|
go bzz.StartHttpServer(eth.dpa)
|
||||||
|
|
||||||
nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway)
|
nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -234,7 +237,7 @@ func (s *Ethereum) MaxPeers() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the ethereum
|
// Start the ethereum
|
||||||
func (s *Ethereum) Start(seed bool, p string) error {
|
func (s *Ethereum) Start(seed bool, p string, pull string) error {
|
||||||
err := s.net.Start()
|
err := s.net.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -264,6 +267,28 @@ func (s *Ethereum) Start(seed bool, p string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(pull) > 0 {
|
||||||
|
go func() {
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
key := ethutil.Hex2Bytes(pull)
|
||||||
|
reader := s.dpa.Retrieve(key)
|
||||||
|
logger.Debugf("retrieved reader for %064x", key)
|
||||||
|
b := make([]byte, 0x1000)
|
||||||
|
var length int64
|
||||||
|
for {
|
||||||
|
n, err := reader.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
logger.Debugf("read %v bytes. read error for %064x: %v", n, key, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
length += int64(n)
|
||||||
|
}
|
||||||
|
logger.Debugf("read %v bytes from %064x: %v", length, key)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: read peers here
|
// TODO: read peers here
|
||||||
if seed {
|
if seed {
|
||||||
logger.Infof("Connect to seed node %v", seedNodeAddress)
|
logger.Infof("Connect to seed node %v", seedNodeAddress)
|
||||||
|
|
|
||||||
|
|
@ -102,17 +102,22 @@ func writeMsg(w io.Writer, msg Msg) error {
|
||||||
copy(start, magicToken)
|
copy(start, magicToken)
|
||||||
binary.BigEndian.PutUint32(start[4:], payloadLen)
|
binary.BigEndian.PutUint32(start[4:], payloadLen)
|
||||||
|
|
||||||
srvlog.Debugf("Sending message:")
|
if msg.Size > 1 {
|
||||||
|
srvlog.Debugf("Sending message (size %v):", msg.Size)
|
||||||
|
}
|
||||||
for _, b := range [][]byte{start, listhdr, code} {
|
for _, b := range [][]byte{start, listhdr, code} {
|
||||||
|
if msg.Size > 1 {
|
||||||
srvlog.Debugf(" %x", b)
|
srvlog.Debugf(" %x", b)
|
||||||
|
}
|
||||||
if _, err := w.Write(b); err != nil {
|
if _, err := w.Write(b); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b := make([]byte, msg.Size)
|
b := make([]byte, msg.Size)
|
||||||
msg.Payload.Read(b)
|
msg.Payload.Read(b)
|
||||||
|
if msg.Size > 1 {
|
||||||
srvlog.Debugf(" %x", b)
|
srvlog.Debugf(" %x", b)
|
||||||
|
}
|
||||||
_, err := w.Write(b)
|
_, err := w.Write(b)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue