func CreateTestDirectoryBlockHeader(prevBlock *directoryBlock.DirectoryBlock) *directoryBlock.DBlockHeader { header := new(directoryBlock.DBlockHeader) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(constants.MAIN_NETWORK_ID) if prevBlock == nil { header.SetDBHeight(0) header.SetPrevFullHash(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(primitives.NewTimestampFromMinutes(1234)) } else { header.SetDBHeight(prevBlock.Header.GetDBHeight() + 1) header.SetPrevFullHash(prevBlock.GetHash()) keyMR, err := prevBlock.BuildKeyMerkleRoot() if err != nil { panic(err) } header.SetPrevKeyMR(keyMR) header.SetTimestamp(primitives.NewTimestampFromMinutes(prevBlock.Header.GetTimestamp().GetTimeMinutesUInt32() + 1)) } header.SetVersion(1) return header }
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 }
func (h *DBlockHeader) GetTimestamp() interfaces.Timestamp { return primitives.NewTimestampFromMinutes(h.Timestamp) }
func TestAddServerFault(t *testing.T) { block := createTestAdminBlock().(*AdminBlock) for i := 0; i < 5; i++ { block.ABEntries = append(block.ABEntries, new(AddFederatedServer)) block.ABEntries = append(block.ABEntries, new(DBSignatureEntry)) block.ABEntries = append(block.ABEntries, new(EndOfMinuteEntry)) block.ABEntries = append(block.ABEntries, new(IncreaseServerCount)) block.ABEntries = append(block.ABEntries, new(AddFederatedServerBitcoinAnchorKey)) } for i := 0; i < 10; i++ { sf := new(ServerFault) sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i * 2)) sf.ServerID = testHelper.NewRepeatingHash(1) sf.AuditServerID = testHelper.NewRepeatingHash(2) sf.VMIndex = 5 sf.DBHeight = 0x44556677 sf.Height = 0x88990011 block.AddServerFault(sf) } for i := 0; i < 10; i++ { sf := new(ServerFault) sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i*2 + 1)) sf.ServerID = testHelper.NewRepeatingHash(1) sf.AuditServerID = testHelper.NewRepeatingHash(2) sf.VMIndex = 5 sf.DBHeight = 0x44556677 sf.Height = 0x88990011 block.AddServerFault(sf) } for i := 0; i < 20; i++ { sf := new(ServerFault) sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i)) sf.ServerID = testHelper.NewRepeatingHash(1) sf.AuditServerID = testHelper.NewRepeatingHash(2) sf.VMIndex = byte(i) sf.DBHeight = 0x44556677 sf.Height = 0x88990011 block.AddServerFault(sf) } if len(block.ABEntries) != 5*5+10+10+20 { t.Errorf("Wrong length of ABEntries - %v", len(block.ABEntries)) } sfFound := false for i := range block.ABEntries { if block.ABEntries[i].Type() != constants.TYPE_SERVER_FAULT { if sfFound { t.Errorf("Non-SF entry between SF entries at position %v", i) } continue } if i == 0 { t.Errorf("SF entry is at position 0 when it shouldn't be") continue } if block.ABEntries[i-1].Type() != constants.TYPE_SERVER_FAULT { continue } prev := block.ABEntries[i-1].(*ServerFault) cur := block.ABEntries[i].(*ServerFault) if prev.Timestamp.GetTimeMilliUInt64() > cur.Timestamp.GetTimeMilliUInt64() { t.Errorf("Wrong order by Timestamp") continue } if prev.Timestamp.GetTimeMilliUInt64() < cur.Timestamp.GetTimeMilliUInt64() { continue } if prev.VMIndex > cur.VMIndex { t.Errorf("Wrong order by VMIndex") continue } } }