Skip to content

Commit

Permalink
Merge branch 'dev-debug' into unitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSnow committed Apr 22, 2017
2 parents bf6da97 + 5a2fc0e commit f558c2b
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 41 deletions.
35 changes: 0 additions & 35 deletions common/messages/dbstate.go
Expand Up @@ -140,30 +140,24 @@ func (m *DBStateMsg) Validate(state interfaces.IState) int {
if m.DirectoryBlock == nil || m.AdminBlock == nil || m.FactoidBlock == nil || m.EntryCreditBlock == nil {
state.AddStatus(fmt.Sprintf("DBStateMsg.Validate() Fail Doesn't have all the blocks"))
//We need the basic block types
fmt.Printf("vvv0 %s : INVALID : Missing a crucial block: D: %t, A:%t, F:%t, EC:%t\n", state.GetFactomNodeName(), m.DirectoryBlock != nil, m.AdminBlock != nil, m.FactoidBlock != nil, m.EntryCreditBlock != nil)
return -1
}

pre := fmt.Sprintf("%s Ht:%d", state.GetFactomNodeName(), m.DirectoryBlock.GetDatabaseHeight())

if m.IsInDB {
fmt.Printf("vvv %s : VALID : Is in DB\n", pre)
return 1
}

dbheight := m.DirectoryBlock.GetHeader().GetDBHeight()

// Just accept the genesis block
if dbheight == 0 {
fmt.Printf("vvv2 %s : VALID : Block is gensis\n", pre)
return 1
}

if state.GetNetworkID() != m.DirectoryBlock.GetHeader().GetNetworkID() {
state.AddStatus(fmt.Sprintf("DBStateMsg.Validate() Fail ht: %d Expecting NetworkID %x and found %x",
dbheight, state.GetNetworkID(), m.DirectoryBlock.GetHeader().GetNetworkID()))
//Wrong network ID
fmt.Printf("vvv1 %s : INVALID : Wrong networkID\n", pre)
return -1
}

Expand All @@ -173,7 +167,6 @@ func (m *DBStateMsg) Validate(state interfaces.IState) int {
if diff < -1 {
state.AddStatus(fmt.Sprintf("DBStateMsg.Validate() Fail dbstate dbht: %d Highest Saved %d diff %d",
dbheight, state.GetEntryDBHeightComplete(), diff))
fmt.Printf("vvv1 %s : INVALID : Diff < -1. DL%d, C:%d\n", pre, diff, state.GetEntryDBHeightComplete())
return -1
}

Expand All @@ -184,7 +177,6 @@ func (m *DBStateMsg) Validate(state interfaces.IState) int {
state.AddStatus(fmt.Sprintf("DBStateMsg.Validate() Fail ht: %d checkpoint failure. Had %s Expected %s",
dbheight, m.DirectoryBlock.DatabasePrimaryIndex().String(), key))
//Key does not match checkpoint
fmt.Printf("vvv1 %s : INVALID : Bad checkpoint\n", pre)
return -1
}
}
Expand Down Expand Up @@ -224,64 +216,46 @@ func (m *DBStateMsg) Validate(state interfaces.IState) int {
}

// It does not pass the signatures. Should we return -1?
fmt.Printf("vvv %s ::: UNSURE : F:%d, T:%d, S:%d\n", pre, fedCount, tally, len(m.SignatureList.List))
return 0
} else { // Alternative to signatures passing by checking our DB
// This block is not the next block we need. Check this block +1 and check it's prevKeyMr
next := state.GetDirectoryBlockByHeight(m.DirectoryBlock.GetDatabaseHeight() + 1)
if next == nil {
// Do not have the next directory block, so we cannot tell by this method
fmt.Printf("vvv1 %s : UNSURE : Next Dblock is nil\n", pre)
return 0
}
// If the prevKeyMr of the next matches this one, we know it is valid.
if next.GetHeader().GetPrevKeyMR().IsSameAs(m.DirectoryBlock.GetKeyMR()) {
goto ValidSignatures
} else {
// The KeyMR does not match, this block is invalid
fmt.Printf("vvv1 %s : INVALID : KeyMr does not match\n", pre)
return -1
}

}
ValidSignatures: // Goto here if signatures pass
fmt.Printf("vvv2 %s : VALID : End of Validate\n", pre)
return 1
}

func printoutsigstuff(m *DBStateMsg, s interfaces.IState, p bool, str string) {
if p {
fmt.Printf("sss %s Ht:%d :: %s\n", s.GetFactomNodeName(), m.DirectoryBlock.GetDatabaseHeight(),
str)
}
}

func (m *DBStateMsg) SigTally(state interfaces.IState) int {
printout := false
if m.DirectoryBlock.GetDatabaseHeight() == 75893 {
printout = true
}
dbheight := m.DirectoryBlock.GetHeader().GetDBHeight()

validSigCount := 0
validSigCount += m.checkpointFix()

data, err := m.DirectoryBlock.GetHeader().MarshalBinary()
if err != nil {
printoutsigstuff(m, state, printout, "Fail in marshal header. Valid: 0")
state.AddStatus(fmt.Sprint("Debug: DBState Signature Error, Marshal binary errored"))
return validSigCount
}

// Signatures that are not valid by current fed list
var remainingSig []interfaces.IFullSignature

sigs := ""
// If there is a repeat signature, we do not count it twice
sigmap := make(map[string]bool)
for _, sig := range m.SignatureList.List {
if sigmap[fmt.Sprintf("%x", sig.GetSignature()[:])] {
printoutsigstuff(m, state, printout, "Duplicate Signature Found")
continue // Toss duplicate signatures
}
sigmap[fmt.Sprintf("%x", sig.GetSignature()[:])] = true
Expand All @@ -303,13 +277,10 @@ func (m *DBStateMsg) SigTally(state interfaces.IState) int {
}

if sig.Verify(data) {
sigs += fmt.Sprintf("%x ", sig.GetSignature()[:])
remainingSig = append(remainingSig, sig)
}
}

printoutsigstuff(m, state, printout, fmt.Sprintf("Normal, Valid: %d, R:%d / %s", validSigCount, len(remainingSig), sigs))

// If promotions have occurred this block, we need to account for their signatures to be
// valid. We will only pay for this overhead if there are signatures left, meaning most blocks will
// not enter this loop
Expand Down Expand Up @@ -387,8 +358,6 @@ func (m *DBStateMsg) SigTally(state interfaces.IState) int {
}
}

printoutsigstuff(m, state, printout, fmt.Sprintf("Remaining Pre, Valid: %d, New:%d", validSigCount, len(newSigners)))

// These signatures that did not validate with current set of authorities
for _, sig := range remainingSig {
InnerSingerLoop:
Expand All @@ -399,10 +368,7 @@ func (m *DBStateMsg) SigTally(state interfaces.IState) int {
}
}
}
printoutsigstuff(m, state, printout, fmt.Sprintf("Remaining End, Valid: %d, R:%d", validSigCount, len(remainingSig)))
}
// End Temporary fix
printoutsigstuff(m, state, printout, fmt.Sprintf("Closeout, Valid: %d", validSigCount))
return validSigCount
}

Expand Down Expand Up @@ -463,7 +429,6 @@ func (e *DBStateMsg) JSONString() (string, error) {
func (m *DBStateMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("vvv3 Unmarshal Failed, hit panic recover()\nvvv %x\n", data)
err = fmt.Errorf("Error unmarshalling Directory Block State Message: %v", r)
}
}()
Expand Down
161 changes: 161 additions & 0 deletions common/messages/sig_test.gox
@@ -0,0 +1,161 @@
package messages_test

// Find bad sigs in a database

import (
//"encoding/hex"
"fmt"
"testing"

"github.com/FactomProject/factomd/common/adminBlock"
"github.com/FactomProject/factomd/testHelper"
//"github.com/FactomProject/factomd/common/entryBlock"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/database/databaseOverlay"
"github.com/FactomProject/factomd/database/leveldb"
)

var _ = fmt.Sprintf("")

func TestSig(t *testing.T) {
db, err := leveldb.NewLevelDB("/home/steven/.factom/m2/main-database/ldb/MAIN/factoid_level.db", false)
if err != nil {
panic(err)
}

start := 60000
end := 90000

var _, _ = start, end

for i := start; i < end; i++ {
// i := 75893
o := databaseOverlay.NewOverlay(db)
dblk, err := o.FetchDBlockByHeight(uint32(i))
if err != nil {
// panic(err)
return
}
if dblk == nil {
//panic("Nil")
return
}

sigsblock, err := o.FetchABlockByHeight(uint32(i + 1))
if err != nil {
//panic(err)
return
}
if sigsblock == nil {
//panic("Nil")
return
}

data, err := dblk.GetHeader().MarshalBinary()
if err != nil {
//panic(err)
return
}

/*publics := []string{
"e3b88b704533612f69b5d6390737481694d7d8acb71e532cac3e8dd2d11ca691",
"c2bbab9d274415765eae5c3ee3b94ff3c38dd5c9b02c8f842e2770a6de0b5068",
"646f6bf2eaa80a803f1ffd3286945c4d6ddfdf5974177a52141c6906153f5237",
"07f339e556ee999cc7e33500753ea0933381b09f5c2bca26e224d716e61a8862",
"6ceeb261cc19b14f6c89bb0bd937f195ffc9e6adaa5618e432752b01a00792c7",
}

sigs := []string{
"bdd779452c9060a677e0840d20bedf4a0b33a067a41f4abbb57d8e6bd9a1d850e9130393c425a1699d50919faf25a4285a16c8a9955207f3a7befed36da89e0d",
"b35598404950e25c69a290d1bd6807912aed7e882c9ff92aa38683657ce7c9e5874e12ea10e12f0174f2a84034d02e64b2dec388278b55e9972ac622e257300c",
"8066fc4222eff67470ffaca15bdb5d6d15b65daf3cc86c121b872d7485b388b3cb4b7bbbd0248076065262d54699bab68e7d5be96e137aa3428b903916e4180a",
"3ca389d8111dc232f0efdccb74734a7709b64b7d5698b32c012236526d9ab3eada38b410c01c0ecee54a4bbc0ab8b59f9a8dcdf2fafbd30a667400c13b66f905",
"00ba44dd5429b0e57069103e0095cab49fd1721078a6136fda231c24baccfd41be3b75a73a52bb01aa2c08c1e71ba4ba2036f9859b601835d69721310221c401",
}*/

var sigList []interfaces.IFullSignature
for _, a := range sigsblock.GetABEntries() {
dbs, ok := a.(*adminBlock.DBSignatureEntry)
if ok {
v := dbs.PrevDBSig.Verify(data)
if !v {
t.Errorf("H: %d, Invalid Sig. %x", i, dbs.PrevDBSig.GetSigBytes())
}
sigList = append(sigList, &dbs.PrevDBSig)
}
}

// Extra stuff to get sig tally
continue
a, err := o.FetchABlockByHeight(uint32(i))
if err != nil {
// panic(err)
return
}

f, err := o.FetchFBlockByHeight(uint32(i))
if err != nil {
// panic(err)
return
}

e, err := o.FetchECBlockByHeight(uint32(i))
if err != nil {
// panic(err)
return
}

var eblks []interfaces.IEntryBlock
for _, ents := range dblk.GetEBlockDBEntries() {
eb, err := o.FetchEBlock(ents.GetKeyMR())
if err != nil {
panic(err)
// return
}
eblks = append(eblks, eb)
}

var entries []interfaces.IEBEntry
for _, ent := range eblks {
hashes := ent.GetEntryHashes()
for _, h := range hashes {
if !h.IsMinuteMarker() {
e, err := o.FetchEntry(h)
if err != nil {
panic(err)
// return
}
entries = append(entries, e)
}
}
}

msg := messages.NewDBStateMsg(primitives.NewTimestampNow(), dblk, a, f, e, eblks, entries, sigList)
m := msg.(*messages.DBStateMsg)

s := testHelper.CreateEmptyTestState()

fmt.Println(m.SigTally(s))

/*for i := range publics {
pub, err := hex.DecodeString(publics[i])
if err != nil {
panic(err)
}

sig, err := hex.DecodeString(sigs[i])
if err != nil {
panic(err)
}

err = primitives.VerifySignature(data, pub, sig)
if err != nil {
fmt.Printf("%x\n", pub)
fmt.Printf("%x\n", sig)
// panic(err)
}
}*/
}
}
2 changes: 0 additions & 2 deletions state/authority.go
Expand Up @@ -100,7 +100,6 @@ func (st *State) VerifyAuthoritySignature(msg []byte, sig *[constants.SIGNATURE_
for _, fed := range feds {
auth, _ := st.GetAuthority(fed.GetChainID())
if auth == nil {
fmt.Printf("sss %s Ht:%d :: Fed %s not found\n", st.GetFactomNodeName(), dbheight, auth.AuthorityChainID.String())
continue
}
valid, err := auth.VerifySignature(msg, sig)
Expand All @@ -112,7 +111,6 @@ func (st *State) VerifyAuthoritySignature(msg []byte, sig *[constants.SIGNATURE_
for _, aud := range auds {
auth, _ := st.GetAuthority(aud.GetChainID())
if auth == nil {
fmt.Printf("sss %s Ht:%d :: Aud %s not found\n", st.GetFactomNodeName(), dbheight, auth.AuthorityChainID.String())
continue
}
valid, err := auth.VerifySignature(msg, sig)
Expand Down
4 changes: 0 additions & 4 deletions state/stateConsensus.go
Expand Up @@ -45,10 +45,6 @@ func (s *State) executeMsg(vm *VM, msg interfaces.IMsg) (ret bool) {
}
}

m, ok := msg.(*messages.DBStateMsg)
if ok {
fmt.Printf("mmm0 %s %d, V:%d\n", s.FactomNodeName, m.DirectoryBlock.GetDatabaseHeight(), msg.Validate(s))
}
switch msg.Validate(s) {
case 1:
if s.RunLeader &&
Expand Down

0 comments on commit f558c2b

Please sign in to comment.