func newAck() *Ack { ack := new(Ack) ack.Timestamp = primitives.NewTimestampNow() hash, err := primitives.NewShaHashFromStr("cbd3d09db6defdc25dfc7d57f3479b339a077183cd67022e6d1ef6c041522b40") if err != nil { panic(err) } ack.MessageHash = hash hash, err = primitives.NewShaHashFromStr("bbd3d09db6defdc25dfc7d57f3479b339a077183cd67022e6d1ef6c041522b40") if err != nil { panic(err) } ack.MessageHash = hash ack.DBHeight = 123 ack.Height = 456 hash, err = primitives.NewShaHashFromStr("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") if err != nil { panic(err) } ack.SerialHash = hash hash, err = primitives.NewShaHashFromStr("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") if err != nil { panic(err) } ack.LeaderChainID = hash return ack }
func AnchorRecordToDirBlockInfo(ar *anchor.AnchorRecord) (*dbInfo.DirBlockInfo, error) { dbi := new(dbInfo.DirBlockInfo) var err error //TODO: fetch proper data //dbi.DBHash = dbi.DBHash, err = primitives.NewShaHashFromStr(ar.KeyMR) if err != nil { return nil, err } dbi.DBHeight = ar.DBHeight //dbi.Timestamp = dbi.BTCTxHash, err = primitives.NewShaHashFromStr(ar.Bitcoin.TXID) if err != nil { return nil, err } dbi.BTCTxOffset = ar.Bitcoin.Offset dbi.BTCBlockHeight = ar.Bitcoin.BlockHeight dbi.BTCBlockHash, err = primitives.NewShaHashFromStr(ar.Bitcoin.BlockHash) if err != nil { return nil, err } dbi.DBMerkleRoot, err = primitives.NewShaHashFromStr(ar.KeyMR) if err != nil { return nil, err } dbi.BTCConfirmed = true return dbi, nil }
func TestKeyMRs(t *testing.T) { entries := []string{"44c9f3a6d6f6b2ab5efb29e7d6159c4e3fca13fc5dd03b94ae3dea8bf30173cb", "41a36ab01a9b8e8d78d6b43b8e7e6671916a93b43b8fec48a627d0cb51f012f1", "905740850540f1d17fcb1fc7fd0c61a33150b2cdc0f88334f6a891ec34bd1cfc", "9c9610e09673c9136508112fe447c8b9c1e042a95bd140ec161ade4995cd0f73", "fbc3a4b40464049c999e99feff2bf36996f27869b045a0374bc47b7c2cda9e7c"} chainID, err := primitives.NewShaHashFromStr("0000000000000000000000000000000000000000000000000000000000000001") if err != nil { t.Error(err) } dbEntries := []interfaces.IDBEntry{} for _, e := range entries { h, err := primitives.NewShaHashFromStr(e) if err != nil { t.Error(err) } entry := new(DBEntry) entry.ChainID = chainID entry.KeyMR = h dbEntries = append(dbEntries, entry) } dBlock := NewDirectoryBlock(nil) err = dBlock.SetDBEntries(dbEntries) if err != nil { t.Error(err) } if dBlock.GetKeyMR().String() != "1710a017d0aaa29e03cdce767f2442a8519a512769777eb5c93d0167ad788104" { t.Error("Wrong DBlock KeyMR") } }
func newDirectoryBlockSignature() *DirectoryBlockSignature { dbs := new(DirectoryBlockSignature) dbs.DirectoryBlockHeight = 123456 hash, _ := primitives.NewShaHashFromStr("cbd3d09db6defdc25dfc7d57f3479b339a077183cd67022e6d1ef6c041522b40") dbs.DirectoryBlockKeyMR = hash hash, _ = primitives.NewShaHashFromStr("a077183cd67022e6d1ef6c041522b40cbd3d09db6defdc25dfc7d57f3479b339") dbs.ServerIdentityChainID = hash return dbs }
func (dbo *Overlay) FetchAllAnchorInfo() ([]*anchor.AnchorRecord, error) { chainID, err := primitives.NewShaHashFromStr(AnchorBlockID) if err != nil { panic(err) return nil, err } entries, err := dbo.FetchAllEntriesByChainID(chainID) if err != nil { panic(err) return nil, err } answer := []*anchor.AnchorRecord{} for _, entry := range entries { if entry.DatabasePrimaryIndex().String() == "24674e6bc3094eb773297de955ee095a05830e431da13a37382dcdc89d73c7d7" { continue } content := entry.GetContent() ar, err := anchor.UnmarshalAnchorRecord(content) if err != nil { panic(err) return nil, err } answer = append(answer, ar) } sort.Sort(ByAnchorDBHeightAccending(answer)) return answer, nil }
func newAck() *Ack { ack := new(Ack) ack.Timestamp.SetTimeNow() hash, _ := primitives.NewShaHashFromStr("cbd3d09db6defdc25dfc7d57f3479b339a077183cd67022e6d1ef6c041522b40") ack.MessageHash = hash return ack }
func ExportEChain(chainID string, db interfaces.DBOverlay) error { fmt.Printf("ExportEChain %v\n", chainID) id, err := primitives.NewShaHashFromStr(chainID) if err != nil { return err } eBlocks, err := db.FetchAllEBlocksByChain(id) if err != nil { return err } fmt.Printf("Fetched %v blocks\n", len(eBlocks)) sort.Sort(util.ByEBlockIDAccending(eBlocks)) for _, block := range eBlocks { SaveBinary(block.(interfaces.DatabaseBatchable)) SaveJSON(block.(interfaces.DatabaseBatchable)) height := block.GetDatabaseHeight() entryHashes := block.GetBody().GetEBEntries() for _, hash := range entryHashes { entry, err := db.FetchEntryByHash(hash) if err != nil { return err } err = ExportEntry(entry.(interfaces.DatabaseBatchable), height) if err != nil { return err } } } return nil }
func newHeartbeat() *Heartbeat { eom := new(Heartbeat) eom.Timestamp = primitives.NewTimestampNow() h, err := primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) } eom.DBlockHash = h h, err = primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) } eom.IdentityChainID = h return eom }
func newEOM() *EOM { eom := new(EOM) eom.Minute = 3 eom.DirectoryBlockHeight = 123456 eom.Timestamp.SetTime(0xFF22100122FF) hash, _ := primitives.NewShaHashFromStr("cbd3d09db6defdc25dfc7d57f3479b339a077183cd67022e6d1ef6c041522b40") eom.IdentityChainID = hash return eom }
func ExportEntryReceipt(entryID string, dbo interfaces.DBOverlay) error { h, err := primitives.NewShaHashFromStr(entryID) if err != nil { return err } receipt, err := CreateFullReceipt(dbo, h) if err != nil { return err } return Save(receipt) }
func newMissingData() *MissingData { msg := new(MissingData) msg.Timestamp = primitives.NewTimestampNow() h, err := primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) } msg.RequestHash = h return msg }
func newEOM() *EOM { eom := new(EOM) eom.Timestamp = primitives.NewTimestampFromMilliseconds(0xFF22100122FF) eom.Minute = 3 h, err := primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000") if err != nil { panic(err) } eom.ChainID = h eom.DBHeight = 123456 return eom }
func TestCreateFullReceipt(t *testing.T) { dbo := CreateAndPopulateTestDatabaseOverlay() hash, err := primitives.NewShaHashFromStr("be5fb8c3ba92c0436269fab394ff7277c67e9b2de4431b723ce5d89799c0b93a") if err != nil { t.Errorf("%v", err) } receipt, err := CreateFullReceipt(dbo, hash) if err != nil { t.Errorf("%v", err) } if receipt == nil { t.Errorf("Receipt is nil!") } //str, _ := receipt.JSONString() //t.Errorf("%v", str) }
func (e *Receipt) TrimReceipt() { if e == nil { return } entry, _ := primitives.NewShaHashFromStr(e.Entry.Key) for i := range e.MerkleBranch { if entry.IsSameAs(e.MerkleBranch[i].Left) { e.MerkleBranch[i].Left = nil } else { if entry.IsSameAs(e.MerkleBranch[i].Right) { e.MerkleBranch[i].Right = nil } } entry = e.MerkleBranch[i].Top e.MerkleBranch[i].Top = nil } }
func HandleV2EntryACK(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) { ackReq := new(AckRequest) err := MapToObject(params, ackReq) if err != nil { return nil, NewInvalidParamsError() } if ackReq.TxID == "" && ackReq.FullTransaction == "" { return nil, NewInvalidParamsError() } eTxID := "" ecTxID := "" if ackReq.TxID == "" { eTxID, ecTxID = DecodeTransactionToHashes(ackReq.FullTransaction) if ecTxID == "" && eTxID == "" { return nil, NewUnableToDecodeTransactionError() } } //TODO: fetch entries, ec TXs from state as well //We didn't receive a full transaction, but a transaction hash //We have to figure out which transaction hash we got if ecTxID == "" && eTxID == "" { h, err := primitives.NewShaHashFromStr(ackReq.TxID) if err != nil { return nil, NewInvalidParamsError() } entry, err := state.FetchEntryByHash(h) if err != nil { return nil, NewInternalError() } if entry != nil { eTxID = ackReq.TxID } else { ec, err := state.FetchECTransactionByHash(h) if err != nil { return nil, NewInternalError() } //fmt.Printf("ec - %v\n", ec) if ec != nil { ecTxID = ackReq.TxID eTxID = ec.GetEntryHash().String() } } } answer := new(EntryStatus) answer.CommitTxID = ecTxID answer.EntryHash = eTxID if answer.CommitTxID == "" && answer.EntryHash == "" { //We know nothing about the transaction, so we return unknown status answer.CommitData.Status = AckStatusUnknown answer.EntryData.Status = AckStatusUnknown return answer, nil } //Fetching the second part of the transaction pair if answer.EntryHash == "" { h, err := primitives.NewShaHashFromStr(answer.EntryHash) if err != nil { return nil, NewInvalidParamsError() } ec, err := state.FetchECTransactionByHash(h) if err != nil { return nil, NewInternalError() } if ec != nil { answer.EntryHash = ec.GetEntryHash().String() } } if answer.CommitTxID == "" { h, err := primitives.NewShaHashFromStr(answer.EntryHash) if err != nil { return nil, NewInvalidParamsError() } ec, err := state.FetchPaidFor(h) if err != nil { return nil, NewInternalError() } if ec != nil { answer.CommitTxID = ec.String() } } //Fetching statuses if answer.CommitTxID == "" { answer.CommitData.Status = AckStatusUnknown } else { h, err := primitives.NewShaHashFromStr(answer.CommitTxID) if err != nil { return nil, NewInvalidParamsError() } status, txid, txTime, blockTime, err := state.GetACKStatus(h) if err != nil { return nil, NewInternalError() } answer.CommitTxID = txid.String() if txTime != nil { answer.CommitData.TransactionDate = txTime.GetTimeMilli() if txTime.GetTimeMilli() > 0 { answer.CommitData.TransactionDateString = txTime.String() } } if blockTime != nil { answer.CommitData.BlockDate = blockTime.GetTimeMilli() if blockTime.GetTimeMilli() > 0 { answer.CommitData.BlockDateString = blockTime.String() } } switch status { case constants.AckStatusInvalid: answer.CommitData.Status = AckStatusInvalid break case constants.AckStatusUnknown: answer.CommitData.Status = AckStatusUnknown break case constants.AckStatusNotConfirmed: answer.CommitData.Status = AckStatusNotConfirmed break case constants.AckStatusACK: answer.CommitData.Status = AckStatusACK break case constants.AckStatus1Minute: answer.CommitData.Status = AckStatus1Minute break case constants.AckStatusDBlockConfirmed: answer.CommitData.Status = AckStatusDBlockConfirmed break default: return nil, NewInternalError() break } } if answer.EntryHash == "" { answer.EntryData.Status = AckStatusUnknown } else { h, err := primitives.NewShaHashFromStr(answer.EntryHash) if err != nil { return nil, NewInvalidParamsError() } status, txid, txTime, blockTime, err := state.GetACKStatus(h) if err != nil { return nil, NewInternalError() } answer.EntryHash = txid.String() if txTime != nil { answer.EntryData.TransactionDate = txTime.GetTimeMilli() if txTime.GetTimeMilli() > 0 { answer.EntryData.TransactionDateString = txTime.String() } } if blockTime != nil { answer.EntryData.BlockDate = blockTime.GetTimeMilli() if blockTime.GetTimeMilli() > 0 { answer.EntryData.BlockDateString = blockTime.String() } } switch status { case constants.AckStatusInvalid: answer.EntryData.Status = AckStatusInvalid break case constants.AckStatusUnknown: answer.EntryData.Status = AckStatusUnknown break case constants.AckStatusNotConfirmed: answer.EntryData.Status = AckStatusNotConfirmed break case constants.AckStatusACK: answer.EntryData.Status = AckStatusACK break case constants.AckStatus1Minute: answer.EntryData.Status = AckStatus1Minute break case constants.AckStatusDBlockConfirmed: answer.EntryData.Status = AckStatusDBlockConfirmed break default: return nil, NewInternalError() break } } return answer, nil }
func HandleV2FactoidACK(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) { ackReq := new(AckRequest) err := MapToObject(params, ackReq) if err != nil { return nil, NewInvalidParamsError() } if ackReq.TxID == "" && ackReq.FullTransaction == "" { return nil, NewInvalidParamsError() } txid := ackReq.TxID if txid == "" { b, err := hex.DecodeString(ackReq.FullTransaction) if err != nil { return nil, NewUnableToDecodeTransactionError() } tx := new(factoid.Transaction) err = tx.UnmarshalBinary(b) if err != nil { return nil, NewUnableToDecodeTransactionError() } txid = tx.GetSigHash().String() } txhash, err := primitives.NewShaHashFromStr(txid) if err != nil { return nil, NewInvalidParamsError() } status, h, txTime, blockTime, err := state.GetACKStatus(txhash) if err != nil { return nil, NewInternalError() } answer := new(FactoidTxStatus) answer.TxID = h.String() if txTime != nil { answer.TransactionDate = txTime.GetTimeMilli() if txTime.GetTimeMilli() > 0 { answer.TransactionDateString = txTime.String() } } if blockTime != nil { answer.BlockDate = blockTime.GetTimeMilli() if blockTime.GetTimeMilli() > 0 { answer.BlockDateString = blockTime.String() } } switch status { case constants.AckStatusInvalid: answer.Status = AckStatusInvalid break case constants.AckStatusUnknown: answer.Status = AckStatusUnknown break case constants.AckStatusNotConfirmed: answer.Status = AckStatusNotConfirmed break case constants.AckStatusACK: answer.Status = AckStatusACK break case constants.AckStatus1Minute: answer.Status = AckStatus1Minute break case constants.AckStatusDBlockConfirmed: answer.Status = AckStatusDBlockConfirmed break default: return nil, NewInternalError() break } return answer, nil }
func (e *Receipt) Validate() error { if e == nil { return fmt.Errorf("No receipt provided") } if e.Entry == nil { return fmt.Errorf("Receipt has no entry") } if e.MerkleBranch == nil { return fmt.Errorf("Receipt has no MerkleBranch") } if e.EntryBlockKeyMR == nil { return fmt.Errorf("Receipt has no EntryBlockKeyMR") } if e.DirectoryBlockKeyMR == nil { return fmt.Errorf("Receipt has no DirectoryBlockKeyMR") } entryHash, err := primitives.NewShaHashFromStr(e.Entry.Key) //TODO: validate entry hashes into EntryHash if err != nil { return err } var left interfaces.IHash var right interfaces.IHash var currentEntry interfaces.IHash currentEntry = entryHash eBlockFound := false dBlockFound := false for i, node := range e.MerkleBranch { if node.Left == nil { if node.Right == nil { return fmt.Errorf("Node %v/%v has two nil sides", i, len(e.MerkleBranch)) } left = currentEntry right = node.Right } else { left = node.Left if node.Right == nil { right = currentEntry } else { right = node.Right } } if left.IsSameAs(currentEntry) == false && left.IsSameAs(currentEntry) { return fmt.Errorf("Entry %v not found in node %v/%v", currentEntry, i, len(e.MerkleBranch)) } top := primitives.HashMerkleBranches(left, right) if node.Top != nil { if top.IsSameAs(node.Top) == false { return fmt.Errorf("Derived top %v is not the same as saved top in node %v/%v", top, i, len(e.MerkleBranch)) } } if top.IsSameAs(e.EntryBlockKeyMR) == true { eBlockFound = true } if top.IsSameAs(e.DirectoryBlockKeyMR) == true { dBlockFound = true } currentEntry = top } if eBlockFound == false { return fmt.Errorf("EntryBlockKeyMR not found in branch") } if dBlockFound == false { return fmt.Errorf("DirectoryBlockKeyMR not found in branch") } return nil }