Ejemplo n.º 1
0
func GenerateGenesisBlocks(networkID uint32) (interfaces.IDirectoryBlock, interfaces.IAdminBlock, interfaces.IFBlock, interfaces.IEntryCreditBlock) {
	dblk := directoryBlock.NewDirectoryBlock(nil)
	ablk := adminBlock.NewAdminBlock(nil)
	fblk := factoid.GetGenesisFBlock(networkID)
	ecblk := entryCreditBlock.NewECBlock()

	if networkID != constants.MAIN_NETWORK_ID {
		if networkID == constants.TEST_NETWORK_ID {
			ablk.AddFedServer(primitives.NewZeroHash())
		} else {
			ablk.AddFedServer(primitives.Sha([]byte("FNode0")))
		}
	} else {
		ecblk.GetBody().AddEntry(entryCreditBlock.NewServerIndexNumber())
		for i := 1; i < 11; i++ {
			minute := entryCreditBlock.NewMinuteNumber(uint8(i))
			ecblk.GetBody().AddEntry(minute)
		}
	}

	dblk.SetABlockHash(ablk)
	dblk.SetECBlockHash(ecblk)
	dblk.SetFBlockHash(fblk)
	dblk.GetHeader().SetNetworkID(networkID)

	dblk.GetHeader().SetTimestamp(primitives.NewTimestampFromMinutes(24018960))

	return dblk, ablk, fblk, ecblk
}
Ejemplo n.º 2
0
func createECEntriesfromBlocks(fBlock interfaces.IFBlock, eBlocks []*entryBlock.EBlock, height int) []interfaces.IECBlockEntry {
	ecEntries := []interfaces.IECBlockEntry{}
	ecEntries = append(ecEntries, entryCreditBlock.NewServerIndexNumber2(uint8(height%10+1)))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(0))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(1))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(2))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(3))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(4))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(5))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(6))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(7))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(8))
	ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(9))

	trans := fBlock.GetTransactions()
	for _, t := range trans {
		ecOut := t.GetECOutputs()
		for i, ec := range ecOut {
			increase := new(entryCreditBlock.IncreaseBalance)
			increase.ECPubKey = primitives.Byte32ToByteSlice32(ec.GetAddress().Fixed())
			increase.TXID = t.GetHash()
			increase.Index = uint64(i)
			increase.NumEC = ec.GetAmount() / fBlock.GetExchRate()
			ecEntries = append(ecEntries, increase)
		}
	}
	for _, eBlock := range eBlocks {
		if height == 0 {
			ecEntries = append(ecEntries, NewCommitChain(eBlock))
		} else {
			ecEntries = append(ecEntries, NewCommitEntry(eBlock))
		}
	}

	return ecEntries
}
Ejemplo n.º 3
0
// TODO: Should fault the server if we don't have the proper sequence of EOM messages.
func (s *State) ProcessEOM(dbheight uint32, msg interfaces.IMsg) bool {

	e := msg.(*messages.EOM)

	if s.Syncing && !s.EOM {
		return false
	}

	if s.EOM && int(e.Minute) > s.EOMMinute {
		return false
	}

	pl := s.ProcessLists.Get(dbheight)
	vm := s.ProcessLists.Get(dbheight).VMs[msg.GetVMIndex()]

	if uint32(pl.System.Height) >= e.SysHeight {
		s.EOMSys = true
	}

	// If I have done everything for all EOMs for all VMs, then and only then do I
	// let processing continue.
	if s.EOMDone && s.EOMSys {
		s.EOMProcessed--
		if s.EOMProcessed <= 0 {
			s.EOM = false
			s.EOMDone = false
			s.ReviewHolding()
			s.Syncing = false
		}
		s.SendHeartBeat()

		return true
	}

	// What I do once  for all VMs at the beginning of processing a particular EOM
	if !s.EOM {
		s.EOMSys = false
		s.Syncing = true
		s.EOM = true
		s.EOMLimit = len(s.LeaderPL.FedServers)
		s.EOMMinute = int(e.Minute)
		s.EOMsyncing = true
		s.EOMProcessed = 0
		s.Newblk = false

		for _, vm := range pl.VMs {
			vm.Synced = false
		}
		s.AddStatus("EOM Syncing")
		return false
	}

	// What I do for each EOM
	if !e.Processed {
		vm.LeaderMinute++
		s.EOMProcessed++
		e.Processed = true
		vm.Synced = true
		if s.LeaderPL.SysHighest < int(e.SysHeight) {
			s.LeaderPL.SysHighest = int(e.SysHeight)
		}
		s.AddStatus("EOM Processed")
		return false
	}

	allfaults := s.LeaderPL.System.Height >= s.LeaderPL.SysHighest

	// After all EOM markers are processed, Claim we are done.  Now we can unwind
	if allfaults && s.EOMProcessed == s.EOMLimit && !s.EOMDone {
		s.AddStatus("EOM All Done")

		s.EOMDone = true
		for _, eb := range pl.NewEBlocks {
			eb.AddEndOfMinuteMarker(byte(e.Minute + 1))
		}

		s.FactoidState.EndOfPeriod(int(e.Minute))

		ecblk := pl.EntryCreditBlock
		ecbody := ecblk.GetBody()
		mn := entryCreditBlock.NewMinuteNumber(e.Minute + 1)
		ecbody.AddEntry(mn)

		if !s.Leader {
			s.CurrentMinute = int(e.Minute)
		}

		s.CurrentMinute++

		switch {
		case s.CurrentMinute < 10:
			s.LeaderPL = s.ProcessLists.Get(s.LLeaderHeight)
			s.Leader, s.LeaderVMIndex = s.LeaderPL.GetVirtualServers(s.CurrentMinute, s.IdentityChainID)
		case s.CurrentMinute == 10:
			eBlocks := []interfaces.IEntryBlock{}
			entries := []interfaces.IEBEntry{}
			for _, v := range pl.NewEBlocks {
				eBlocks = append(eBlocks, v)
			}
			for _, v := range pl.NewEntries {
				entries = append(entries, v)
			}

			dbstate := s.AddDBState(true, s.LeaderPL.DirectoryBlock, s.LeaderPL.AdminBlock, s.GetFactoidState().GetCurrentBlock(), s.LeaderPL.EntryCreditBlock, eBlocks, entries)
			if dbstate == nil {
				dbstate = s.DBStates.Get(int(s.LeaderPL.DirectoryBlock.GetHeader().GetDBHeight()))
			}
			dbht := int(dbstate.DirectoryBlock.GetHeader().GetDBHeight())
			if dbht > 0 {
				prev := s.DBStates.Get(dbht - 1)
				s.DBStates.FixupLinks(prev, dbstate)
			}
			s.DBStates.ProcessBlocks(dbstate)

			s.CurrentMinute = 0
			s.LLeaderHeight++
			s.LeaderPL = s.ProcessLists.Get(s.LLeaderHeight)
			s.Leader, s.LeaderVMIndex = s.LeaderPL.GetVirtualServers(0, s.IdentityChainID)

			s.DBSigProcessed = 0

			// Note about dbsigs.... If we processed the previous minute, then we generate the DBSig for the next block.
			// But if we didn't process the preivious block, like we start from scratch, or we had to reset the entire
			// network, then no dbsig exists.  This code doesn't execute, and so we have no dbsig.  In that case, on
			// the next EOM, we see the block hasn't been signed, and we sign the block (Thats the call to SendDBSig()
			// above).
			if s.Leader {
				dbstate := s.DBStates.Get(int(s.LLeaderHeight - 1))
				dbs := new(messages.DirectoryBlockSignature)
				db := dbstate.DirectoryBlock
				dbs.DirectoryBlockHeader = db.GetHeader()
				//dbs.DirectoryBlockKeyMR = dbstate.DirectoryBlock.GetKeyMR()
				dbs.ServerIdentityChainID = s.GetIdentityChainID()
				dbs.DBHeight = s.LLeaderHeight
				dbs.Timestamp = s.GetTimestamp()
				dbs.SetVMHash(nil)
				dbs.SetVMIndex(s.LeaderVMIndex)
				dbs.SetLocal(true)
				dbs.Sign(s)
				err := dbs.Sign(s)
				if err != nil {
					panic(err)
				}
				dbs.LeaderExecute(s)

			}

			s.Saving = true
		}

		for k := range s.Commits {
			vs := s.Commits[k]
			if len(vs) == 0 {
				delete(s.Commits, k)
				continue
			}
			v, ok := vs[0].(interfaces.IMsg)
			if ok {
				_, ok := s.Replay.Valid(constants.TIME_TEST, v.GetRepeatHash().Fixed(), v.GetTimestamp(), s.GetTimestamp())
				if !ok {
					copy(vs, vs[1:])
					vs[len(vs)-1] = nil
					s.Commits[k] = vs[:len(vs)-1]
				}
			}
		}

		for k := range s.Acks {
			v := s.Acks[k].(*messages.Ack)
			if v.DBHeight < s.LLeaderHeight {
				delete(s.Acks, k)
			}
		}
	}

	return false
}