Skip to content

Commit

Permalink
Merge pull request #167 from evanlinjin/master
Browse files Browse the repository at this point in the history
root check/fix improvements and rebuilt static files
  • Loading branch information
Evan Lin committed Oct 30, 2017
2 parents 16fc3db + 0a58679 commit ba07124
Show file tree
Hide file tree
Showing 26 changed files with 1,908 additions and 2,157 deletions.
8 changes: 3 additions & 5 deletions cmd/autoposter/autoposter.go
Expand Up @@ -12,15 +12,13 @@ func main() {
app.Usage = "Used for testing Skycoin BBS"
app.Flags = cli.FlagsByName{
cli.IntFlag{
Name: "http-port,p",
Name: "http-port,p",
Destination: &HTTPPort,
Value: HTTPPort,
Value: HTTPPort,
},
}
app.Commands = cli.Commands{
{

},
{},
}
app.Action = cli.ActionFunc(func(ctx *cli.Context) error {
return nil
Expand Down
Empty file modified run.sh 100644 → 100755
Empty file.
30 changes: 15 additions & 15 deletions src/msgs/discoverer.go
@@ -1,16 +1,16 @@
package msgs

import (
"github.com/skycoin/skycoin/src/cipher"
"sync"
"github.com/skycoin/bbs/src/misc/tag"
"github.com/skycoin/bbs/src/misc/boo"
"time"
"github.com/skycoin/skycoin/src/cipher/encoder"
"github.com/skycoin/bbs/src/misc/inform"
"github.com/skycoin/bbs/src/misc/tag"
"github.com/skycoin/bbs/src/store/object"
"github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/cipher/encoder"
"log"
"github.com/skycoin/bbs/src/misc/inform"
"os"
"sync"
"time"
)

// pkMap stores public keys in order and avoids duplicates.
Expand Down Expand Up @@ -140,7 +140,7 @@ func (b DiscoveredBoard) Verify(ts int64) error {
type DiscovererMsg struct {
Type DiscovererType
TimeStamp int64
Boards []DiscoveredBoard
Boards []DiscoveredBoard
}

// Add adds a Board to 'DiscovererMsg.Boards'. This is only relevant if
Expand Down Expand Up @@ -182,16 +182,16 @@ type BoardDiscoverer struct {
doRange RangeFunc

tsMux sync.RWMutex
ts int64
ts int64
}

// NewBoardDiscoverer creates a new BoardDiscoverer.
func NewBoardDiscoverer(doSend SendFunc, doRange RangeFunc) *BoardDiscoverer {
return &BoardDiscoverer{
l: inform.NewLogger(true, os.Stdout, "BOARD_DISCOVERER"),
nodes: newPKMap(),
boards: newPKMap(),
doSend: doSend,
l: inform.NewLogger(true, os.Stdout, "BOARD_DISCOVERER"),
nodes: newPKMap(),
boards: newPKMap(),
doSend: doSend,
doRange: doRange,
}
}
Expand All @@ -211,7 +211,7 @@ func (bd *BoardDiscoverer) SendAsk() {

bd.ts = int64(time.Now().UnixNano())
outMsg := &DiscovererMsg{
Type: DiscovererAsk,
Type: DiscovererAsk,
TimeStamp: bd.ts,
}

Expand All @@ -224,7 +224,7 @@ func (bd *BoardDiscoverer) SendAsk() {
func (bd *BoardDiscoverer) SendResponse(nodeKey cipher.PubKey, ts int64) {

respMsg := &DiscovererMsg{
Type: DiscovererResponse,
Type: DiscovererResponse,
TimeStamp: ts,
}

Expand Down Expand Up @@ -268,4 +268,4 @@ func (bd *BoardDiscoverer) setTimeStamp(ts int64) {
bd.tsMux.Lock()
defer bd.tsMux.Unlock()
bd.ts = ts
}
}
30 changes: 15 additions & 15 deletions src/msgs/relay.go
Expand Up @@ -54,11 +54,11 @@ type RelayConfig struct {
}

type Relay struct {
c *RelayConfig
l *log.Logger
factory *factory.MessengerFactory
compiler *state.Compiler
incomplete *Incomplete
c *RelayConfig
l *log.Logger
factory *factory.MessengerFactory
compiler *state.Compiler
incomplete *Incomplete
//discoverer *BoardDiscoverer
in chan *BBSMessage
onConnected chan struct{}
Expand Down Expand Up @@ -208,16 +208,16 @@ func (r *Relay) service() {
}

//case <-nodeRefreshTicker.C:
//r.discoverer.ClearNodes()
//r.factory.ForEachConn(func(conn *factory.Connection) {
// if e := conn.FindServiceNodesByAttributes(ServiceName); e != nil {
// r.l.Printf("failed to find services of '%s'", ServiceName)
// }
//})
//go func() {
// time.Sleep(nodeInterval/2)
//r.discoverer.SendAsk()
//}()
//r.discoverer.ClearNodes()
//r.factory.ForEachConn(func(conn *factory.Connection) {
// if e := conn.FindServiceNodesByAttributes(ServiceName); e != nil {
// r.l.Printf("failed to find services of '%s'", ServiceName)
// }
//})
//go func() {
// time.Sleep(nodeInterval/2)
//r.discoverer.SendAsk()
//}()

case <-r.onConnected:
if e := r.compiler.EnsureSubmissionKeys(r.GetKeys()); e != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/store/cxo/manager.go
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/skycoin/bbs/src/misc/boo"
"github.com/skycoin/bbs/src/misc/inform"
"github.com/skycoin/bbs/src/msgs"
"github.com/skycoin/bbs/src/store/cxo/setup"
"github.com/skycoin/bbs/src/store/object"
"github.com/skycoin/bbs/src/store/object/revisions/r0"
"github.com/skycoin/bbs/src/store/object/transfer"
Expand All @@ -25,7 +26,6 @@ import (
"strconv"
"sync"
"time"
"github.com/skycoin/bbs/src/store/cxo/setup"
)

const (
Expand Down Expand Up @@ -470,7 +470,7 @@ func (m *Manager) NewBoard(in *object.NewBoardIO) error {
<<< DISCOVERER >>>
*/

func (m *Manager) GetDiscoveredBoards() ([]string) {
func (m *Manager) GetDiscoveredBoards() []string {
return m.relay.GetBoards()
}

Expand Down
8 changes: 4 additions & 4 deletions src/store/cxo/setup/setup.go
@@ -1,11 +1,11 @@
package setup

import (
"github.com/skycoin/cxo/skyobject"
"github.com/skycoin/bbs/src/store/object/revisions/r0"
"github.com/skycoin/bbs/src/misc/boo"
"github.com/skycoin/bbs/src/store/object"
"github.com/skycoin/bbs/src/store/object/revisions/r0"
"github.com/skycoin/cxo/node"
"github.com/skycoin/bbs/src/misc/boo"
"github.com/skycoin/cxo/skyobject"
)

// PrepareRegistry sets up the CXO Registry.
Expand Down Expand Up @@ -60,4 +60,4 @@ func NewBoard(node *node.Node, in *object.NewBoardIO) (*skyobject.Root, error) {
} else {
return root, nil
}
}
}
90 changes: 48 additions & 42 deletions src/store/state/board_instance.go
Expand Up @@ -98,59 +98,23 @@ func (bi *BoardInstance) UpdateWithReceived(r *skyobject.Root, sk cipher.SecKey)
pFlags |= skyobject.ViewOnly
}

var (
oldHas bool
oldSeq uint64
)

// Find old.
if bi.p != nil {
oldHas = true
oldSeq = bi.h.GetRootSeq()
bi.l.Printf("was ready with seq(%d), hence closed.", oldSeq)
bi.p.Close()
}

newPack, e := ct.Unpack(r, pFlags, ct.CoreRegistry().Types(), sk)
if e != nil {
bi.l.Println(" - root unpack failed with error:", e)

if oldHas && bi.isMaster() {
var goal = r.Seq
bi.l.Printf("\t- invalid root is of seq %d, attempting to surpass.", goal)

r, e := bi.n.Container().Root(r.Pub, oldSeq)
if e != nil {
bi.l.Println("\t- FAILED:", e)
return e
}

newPack, e = ct.Unpack(r, pFlags, ct.CoreRegistry().Types(), sk)
if e != nil {
bi.l.Println("\t- FAILED:", e)
return e
}

for i := oldSeq; i < goal; i++ {
if e := newPack.Save(); e != nil {
bi.l.Println("\t- FAILED:", e)
return e
}
}
if newPack, e = bi.fixRoot(firstRun, pFlags, r.Seq, r.Pub, sk); e != nil {
bi.l.Println("\t- FAILED:", e)
return e
} else {
bi.l.Println("\t- SUCCESS!", newPack.Root().Seq)
bi.needPublish.Set()

} else {
bi.l.Println("\t- unable to fix, returning...")
return e
}
}

bi.l.Println(" - root unpack succeeded.")
bi.p = newPack

newHeaders, e := pack.NewHeaders(bi.h, bi.p);
if e != nil {
newHeaders, e := pack.NewHeaders(bi.h, bi.p)
if e != nil {
bi.l.Println(" - failed to generate new headers:", e)
return e
}
Expand Down Expand Up @@ -184,6 +148,48 @@ func (bi *BoardInstance) UpdateWithReceived(r *skyobject.Root, sk cipher.SecKey)
return nil
}

func (bi *BoardInstance) fixRoot(firstRun bool, flags skyobject.Flag, goal uint64, pk cipher.PubKey, sk cipher.SecKey) (*skyobject.Pack, error) {
var (
ct = bi.n.Container()
isMaster = sk != (cipher.SecKey{})
validPack *skyobject.Pack
)

// If we don't have old, find it.
if firstRun == false {
for i := goal; i >= 0; i-- {
if tempRoot, e := ct.Root(pk, i); e != nil || len(tempRoot.Refs) != r0.RootChildrenCount {
continue
} else if tempPack, e := ct.Unpack(tempRoot, flags, ct.CoreRegistry().Types(), sk); e != nil {
continue
} else {
// TODO (evanlinjin) : Need to check root.
validPack = tempPack
break
}
}
}
if validPack == nil {
return nil, boo.New(boo.InvalidRead,
"failed to find a valid root that can represent a board")
}

// Return if we are unable to change most recent root.
if isMaster == false {
return validPack, nil
}

// Surpass sequence.
oldSeq := validPack.Root().Seq
for i := oldSeq; i < goal; i++ {
if e := validPack.Save(); e != nil {
return nil, boo.WrapTypef(e, boo.Internal, "failed to surpass seq(%d)", oldSeq)
}
}

return validPack, nil
}

// PublishChanges publishes changes to CXO.
// Only use if instance is initialised, changes were made, and the node owns the board.
// Should be triggered by compiler based on an interval.
Expand Down
14 changes: 7 additions & 7 deletions src/store/state/board_instance_add_test.go
@@ -1,17 +1,17 @@
package state

import (
"testing"
"github.com/skycoin/skycoin/src/cipher"
"math/rand"
"sync"
"testing"
"time"
"math/rand"
"github.com/skycoin/skycoin/src/cipher"
)

func TestBoardInstance_NewThread(t *testing.T) {
const (
boardSeed = "a" // Seed for board creation.
userSeed = "b" // Seed for user generation.
userSeed = "b" // Seed for user generation.
)

bi, quit := initInstance(t, boardSeed)
Expand Down Expand Up @@ -113,8 +113,8 @@ func TestBoardInstance_NewThread(t *testing.T) {

t.Run("add content while modifying board", func(t *testing.T) {
const (
threadCount = 30
postCount = 100
threadCount = 30
postCount = 100
maxSubPKCount = 5
)

Expand Down Expand Up @@ -186,4 +186,4 @@ func TestBoardInstance_NewThread(t *testing.T) {

wg.Wait()
})
}
}

0 comments on commit ba07124

Please sign in to comment.