func CreateTestDirectoryBlockHeader(prevBlock *directoryBlock.DirectoryBlock) *directoryBlock.DBlockHeader { header := new(directoryBlock.DBlockHeader) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(0xffff) if prevBlock == nil { header.SetDBHeight(0) header.SetPrevLedgerKeyMR(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(1234) } else { header.SetDBHeight(prevBlock.Header.GetDBHeight() + 1) header.SetPrevLedgerKeyMR(prevBlock.GetHash()) keyMR, err := prevBlock.BuildKeyMerkleRoot() if err != nil { panic(err) } header.SetPrevKeyMR(keyMR) header.SetTimestamp(prevBlock.Header.GetTimestamp() + 1) } header.SetVersion(1) return header }
func (c *DirectoryBlock) New() interfaces.BinaryMarshallableAndCopyable { dBlock := new(DirectoryBlock) dBlock.Header = NewDBlockHeader() dBlock.DBHash = primitives.NewZeroHash() dBlock.KeyMR = primitives.NewZeroHash() return dBlock }
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 TestAuthoritySignature(t *testing.T) { s := testHelper.CreateAndPopulateTestState() idindex := s.CreateBlankFactomIdentity(primitives.NewZeroHash()) s.Identities[idindex].ManagementChainID = primitives.NewZeroHash() index := s.AddAuthorityFromChainID(primitives.NewZeroHash()) s.Authorities[index].SigningKey = *(s.GetServerPublicKey()) s.Authorities[index].Status = 1 ack := new(messages.Ack) ack.DBHeight = s.LLeaderHeight ack.VMIndex = 1 ack.Minute = byte(5) ack.Timestamp = s.GetTimestamp() ack.MessageHash = primitives.NewZeroHash() ack.LeaderChainID = s.IdentityChainID ack.SerialHash = primitives.NewZeroHash() err := ack.Sign(s) if err != nil { t.Error("Authority Test Failed when signing message") } msg, err := ack.MarshalForSignature() if err != nil { t.Error("Authority Test Failed when marshalling for sig") } sig := ack.GetSignature() server, err := s.Authorities[0].VerifySignature(msg, sig.GetSignature()) if !server || err != nil { t.Error("Authority Test Failed when checking sigs") } }
// // UnmarshalBinaryData for FullServerFault // func (m *FullServerFault) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling With Signatures Invalid Server Fault: %v", r) } }() newData = data if newData[0] != m.Type() { return nil, fmt.Errorf("Invalid Message type") } newData = newData[1:] if m.ServerID == nil { m.ServerID = primitives.NewZeroHash() } newData, err = Unmarshall(m.ServerID, err, newData) if m.AuditServerID == nil { m.AuditServerID = primitives.NewZeroHash() } newData, err = m.AuditServerID.UnmarshalBinaryData(newData) if err != nil { return nil, err } m.VMIndex, newData = newData[0], newData[1:] m.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] m.Height, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] m.SystemHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] m.Timestamp = new(primitives.Timestamp) newData, err = m.Timestamp.UnmarshalBinaryData(newData) if err != nil { return nil, err } m.SSerialHash = primitives.NewZeroHash() newData, err = m.SSerialHash.UnmarshalBinaryData(newData) if err != nil { return nil, err } newData, err = m.SignatureList.UnmarshalBinaryData(newData) if err != nil { return nil, err } if len(newData) > 0 { m.Signature = new(primitives.Signature) newData, err = m.Signature.UnmarshalBinaryData(newData) if err != nil { return nil, err } } return newData, nil }
func NewECBlockHeader() *ECBlockHeader { h := new(ECBlockHeader) h.BodyHash = primitives.NewZeroHash() h.PrevHeaderHash = primitives.NewZeroHash() h.PrevFullHash = primitives.NewZeroHash() h.HeaderExpansionArea = make([]byte, 0) return h }
func NewDBlockHeader() *DBlockHeader { d := new(DBlockHeader) d.BodyMR = primitives.NewZeroHash() d.PrevKeyMR = primitives.NewZeroHash() d.PrevLedgerKeyMR = primitives.NewZeroHash() return d }
// NewEBlockHeader initializes a new empty Entry Block Header. func NewEBlockHeader() *EBlockHeader { e := new(EBlockHeader) e.ChainID = primitives.NewZeroHash() e.BodyMR = primitives.NewZeroHash() e.PrevKeyMR = primitives.NewZeroHash() e.PrevLedgerKeyMR = primitives.NewZeroHash() return e }
func NewDirBlockInfo() *DirBlockInfo { dbi := new(DirBlockInfo) dbi.DBHash = primitives.NewZeroHash() dbi.BTCTxHash = primitives.NewZeroHash() dbi.BTCBlockHash = primitives.NewZeroHash() dbi.DBMerkleRoot = primitives.NewZeroHash() return dbi }
func newDirBlockInfoCopy() *dirBlockInfoCopy { dbi := new(dirBlockInfoCopy) dbi.DBHash = primitives.NewZeroHash() dbi.BTCTxHash = primitives.NewZeroHash() dbi.BTCBlockHash = primitives.NewZeroHash() dbi.DBMerkleRoot = primitives.NewZeroHash() return dbi }
func NewECBlockHeader() *ECBlockHeader { h := new(ECBlockHeader) h.ECChainID = primitives.NewZeroHash() h.ECChainID.SetBytes(constants.EC_CHAINID) h.BodyHash = primitives.NewZeroHash() h.PrevHeaderHash = primitives.NewZeroHash() h.PrevLedgerKeyMR = primitives.NewZeroHash() h.HeaderExpansionArea = make([]byte, 0) return h }
func NewAdminBlock(prev interfaces.IAdminBlock) interfaces.IAdminBlock { block := new(AdminBlock) block.Header = new(ABlockHeader) if prev != nil { block.Header.SetPrevBackRefHash(primitives.NewZeroHash()) block.Header.SetDBHeight(prev.GetDBHeight() + 1) } else { block.Header.SetPrevBackRefHash(primitives.NewZeroHash()) } return block }
func UnmarshalDBlock(data []byte) (interfaces.IDirectoryBlock, error) { dBlock := new(DirectoryBlock) dBlock.Header = NewDBlockHeader() dBlock.DBHash = primitives.NewZeroHash() dBlock.KeyMR = primitives.NewZeroHash() err := dBlock.UnmarshalBinary(data) if err != nil { return nil, err } return dBlock, nil }
// NewDirBlockInfoFromDirBlock creates a DirDirBlockInfo from DirectoryBlock func NewDirBlockInfoFromDirBlock(dirBlock interfaces.IDirectoryBlock) *DirBlockInfo { dbic := new(DirBlockInfo) dbic.DBHash = dirBlock.GetHash() dbic.DBHeight = dirBlock.GetDatabaseHeight() dbic.DBMerkleRoot = dirBlock.GetKeyMR() dbic.Timestamp = int64(dirBlock.GetHeader().GetTimestamp()) // * 60 ??? dbic.BTCTxHash = primitives.NewZeroHash() dbic.BTCBlockHash = primitives.NewZeroHash() dbic.BTCConfirmed = false return dbic }
func NewCommitChain() *CommitChain { c := new(CommitChain) c.Version = 0 c.MilliTime = new(primitives.ByteSlice6) c.ChainIDHash = primitives.NewZeroHash() c.Weld = primitives.NewZeroHash() c.EntryHash = primitives.NewZeroHash() c.Credits = 0 c.ECPubKey = new(primitives.ByteSlice32) c.Sig = new(primitives.ByteSlice64) return c }
func createTestDirectoryBlockHeader() *DBlockHeader { header := new(DBlockHeader) header.SetDBHeight(1) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(constants.MAIN_NETWORK_ID) header.SetPrevFullHash(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(primitives.NewTimestampFromSeconds(1234)) header.SetVersion(1) return header }
func setupFirstAuthority(s *state.State) { var id state.Identity if networkIdentity := s.GetNetworkBootStrapIdentity(); networkIdentity != nil { id.IdentityChainID = networkIdentity } else { id.IdentityChainID = primitives.NewZeroHash() } id.ManagementChainID, _ = primitives.HexToHash("88888800000000000000000000000000") if pub := s.GetNetworkBootStrapKey(); pub != nil { id.SigningKey = pub } else { id.SigningKey = primitives.NewZeroHash() } id.MatryoshkaHash = primitives.NewZeroHash() id.ManagementCreated = 0 id.ManagementRegistered = 0 id.IdentityCreated = 0 id.IdentityRegistered = 0 id.Key1 = primitives.NewZeroHash() id.Key2 = primitives.NewZeroHash() id.Key3 = primitives.NewZeroHash() id.Key4 = primitives.NewZeroHash() id.Status = 1 s.Identities = append(s.Identities, &id) var auth state.Authority auth.Status = 1 auth.SigningKey = primitives.PubKeyFromString(id.SigningKey.String()) auth.MatryoshkaHash = primitives.NewZeroHash() auth.AuthorityChainID = id.IdentityChainID auth.ManagementChainID, _ = primitives.HexToHash("88888800000000000000000000000000") s.Authorities = append(s.Authorities, &auth) }
func createTestDirectoryBlockHeader() *DBlockHeader { header := new(DBlockHeader) header.SetDBHeight(1) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(0xffff) header.SetPrevLedgerKeyMR(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(1234) header.SetVersion(1) return header }
func TestServerFaultUpdateState(t *testing.T) { sigs := 10 sf := new(ServerFault) sf.Timestamp = primitives.NewTimestampNow() sf.ServerID = testHelper.NewRepeatingHash(1) sf.AuditServerID = testHelper.NewRepeatingHash(2) sf.VMIndex = 0x33 sf.DBHeight = 0x44556677 sf.Height = 0x88990011 core, err := sf.MarshalCore() if err != nil { t.Errorf("%v", err) } for i := 0; i < sigs; i++ { priv := testHelper.NewPrimitivesPrivateKey(uint64(i)) sig := priv.Sign(core) sf.SignatureList.List = append(sf.SignatureList.List, sig) } sf.SignatureList.Length = uint32(len(sf.SignatureList.List)) s := testHelper.CreateAndPopulateTestState() idindex := s.CreateBlankFactomIdentity(primitives.NewZeroHash()) s.Identities[idindex].ManagementChainID = primitives.NewZeroHash() for i := 0; i < sigs; i++ { //Federated Server index := s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i))) s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i)).Pub s.Authorities[index].Status = 1 s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i))) //Audit Server index = s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i + sigs))) s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i + sigs)).Pub s.Authorities[index].Status = 0 s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i+sigs))) } err = sf.UpdateState(s) if err != nil { t.Errorf("%v", err) } }
func (id *Identity) IsFull() bool { zero := primitives.NewZeroHash() if id.IdentityChainID.IsSameAs(zero) { return false } if id.ManagementChainID.IsSameAs(zero) { return false } if id.MatryoshkaHash.IsSameAs(zero) { return false } if id.Key1.IsSameAs(zero) { return false } if id.Key2.IsSameAs(zero) { return false } if id.Key3.IsSameAs(zero) { return false } if id.Key4.IsSameAs(zero) { return false } if id.SigningKey.IsSameAs(zero) { return false } if len(id.AnchorKeys) == 0 { return false } return true }
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 NewEntry() *Entry { e := new(Entry) e.ChainID = primitives.NewZeroHash() e.ExtIDs = make([][]byte, 0) e.Content = make([]byte, 0) return e }
func (a *Anchor) checkMissingDirBlockInfo() { anchorLog.Debug("checkMissingDirBlockInfo for those unsaved DirBlocks in database") dblocks, _ := a.db.FetchAllDBlocks() dirBlockInfos, _ := a.db.FetchAllDirBlockInfos() //FetchAllDirBlockInfos() for _, dblock := range dblocks { var found = false for i, dbinfo := range dirBlockInfos { if dbinfo.GetDatabaseHeight() == dblock.GetDatabaseHeight() { dirBlockInfos = append(dirBlockInfos[:i], dirBlockInfos[i+1:]...) found = true break } } if !found { if dblock.GetKeyMR() == nil || bytes.Compare(dblock.GetKeyMR().Bytes(), primitives.NewZeroHash().Bytes()) == 0 { dblock.BuildKeyMerkleRoot() } dirBlockInfo := dbInfo.NewDirBlockInfoFromDirBlock(dblock) dirBlockInfo.SetTimestamp(primitives.NewTimestampNow()) anchorLog.Debug("add missing dirBlockInfo to map: ", spew.Sdump(dirBlockInfo)) a.db.SaveDirBlockInfo(dirBlockInfo) a.dirBlockInfoSlice = append(a.dirBlockInfoSlice, dirBlockInfo) } } }
func getECblock(hash string) *ECBlockHolder { mr, err := primitives.HexToHash(hash) if err != nil { return nil } dbase := StatePointer.GetAndLockDB() ecblk, err := dbase.FetchECBlock(mr) StatePointer.UnlockDB() if ecblk == nil || err != nil { return nil } if ecblk.GetHeader() == nil { return nil } holder := new(ECBlockHolder) holder.ECBlock = ecblk length := 0 zero := primitives.NewZeroHash() for _, e := range ecblk.GetEntryHashes() { if e != nil && !e.IsSameAs(zero) { length++ } } holder.Length = length return holder }
func (db *Overlay) SaveIncludedInMulti(entries []interfaces.IHash, block interfaces.IHash, checkForDuplicateEntries bool) error { if entries == nil || block == nil { return nil } batch := []interfaces.Record{} for _, entry := range entries { if checkForDuplicateEntries == true { loaded, err := db.Get(INCLUDED_IN, entry.Bytes(), primitives.NewZeroHash()) if err != nil { return err } if loaded != nil { continue } } batch = append(batch, interfaces.Record{INCLUDED_IN, entry.Bytes(), block}) } err := db.DB.PutInBatch(batch) if err != nil { return err } return nil }
func (b *IncreaseBalance) UnmarshalBinaryData(data []byte) (newData []byte, err error) { buf := bytes.NewBuffer(data) hash := make([]byte, 32) _, err = buf.Read(hash) if err != nil { return } b.ECPubKey = new(primitives.ByteSlice32) copy(b.ECPubKey[:], hash) _, err = buf.Read(hash) if err != nil { return } if b.TXID == nil { b.TXID = primitives.NewZeroHash() } b.TXID.SetBytes(hash) tmp := make([]byte, 0) b.Index, tmp = primitives.DecodeVarInt(buf.Bytes()) b.NumEC, tmp = primitives.DecodeVarInt(tmp) newData = tmp return }
func (b *IncreaseBalance) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling IncreaseBalance: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) _, err = buf.Read(hash) if err != nil { return } b.ECPubKey = new(primitives.ByteSlice32) copy(b.ECPubKey[:], hash) _, err = buf.Read(hash) if err != nil { return } if b.TXID == nil { b.TXID = primitives.NewZeroHash() } b.TXID.SetBytes(hash) tmp := make([]byte, 0) b.Index, tmp = primitives.DecodeVarInt(buf.DeepCopyBytes()) b.NumEC, tmp = primitives.DecodeVarInt(tmp) newData = tmp return }
// AddEndOfMinuteMarker adds the End of Minute to the Entry Block. The End of // Minut byte becomes the last byte in a 32 byte slice that is added to the // Entry Block Body as an Entry Block Entry. func (e *EBlock) AddEndOfMinuteMarker(m byte) { h := make([]byte, 32) h[len(h)-1] = m hash := primitives.NewZeroHash() hash.SetBytes(h) e.Body.EBEntries = append(e.Body.EBEntries, hash) }
func (a *Anchor) checkTxMalleation(transaction *btcutil.Tx, details *btcjson.BlockDetails) { anchorLog.Debug("in checkTxMalleation") dirBlockInfos := make([]interfaces.IDirBlockInfo, 0, len(a.dirBlockInfoSlice)) for _, v := range a.dirBlockInfoSlice { // find those already anchored but no call back yet if v.GetBTCBlockHeight() == 0 && bytes.Compare(v.GetBTCTxHash().Bytes(), primitives.NewZeroHash().Bytes()) != 0 { dirBlockInfos = append(dirBlockInfos, v) } } sort.Sort(util.ByDirBlockInfoTimestamp(dirBlockInfos)) anchorLog.Debugf("malleated tx candidate count=%d, dirBlockInfo list=%s\n", len(dirBlockInfos), spew.Sdump(dirBlockInfos)) for _, dirBlockInfo := range dirBlockInfos { tx, err := a.wclient.GetRawTransaction(toShaHash(dirBlockInfo.GetBTCTxHash())) if err != nil { anchorLog.Debugf(err.Error()) continue } anchorLog.Debugf("GetRawTransaction=%s, dirBlockInfo=%s\n", spew.Sdump(tx), spew.Sdump(dirBlockInfo)) // compare OP_RETURN if reflect.DeepEqual(transaction.MsgTx().TxOut[0], tx.MsgTx().TxOut[0]) { anchorLog.Debugf("Tx Malleated: original.txid=%s, malleated.txid=%s\n", dirBlockInfo.GetBTCTxHash().(*primitives.Hash).BTCString(), transaction.Sha().String()) a.doSaveDirBlockInfo(transaction, details, dirBlockInfo.(*dbInfo.DirBlockInfo), true) break } } }
func CreateTestDirectoryBlock(prevBlock *directoryBlock.DirectoryBlock) *directoryBlock.DirectoryBlock { dblock := new(directoryBlock.DirectoryBlock) dblock.SetHeader(CreateTestDirectoryBlockHeader(prevBlock)) dblock.SetDBEntries(make([]interfaces.IDBEntry, 0, 5)) de := new(directoryBlock.DBEntry) de.ChainID = primitives.NewZeroHash() de.KeyMR = primitives.NewZeroHash() dblock.SetDBEntries(append(dblock.GetDBEntries(), de)) //dblock.GetHeader().SetBlockCount(uint32(len(dblock.GetDBEntries()))) return dblock }