예제 #1
0
func anchorChainToDirBlockInfo(aRecord *anchor.AnchorRecord) (*common.DirBlockInfo, error) {
	dirBlockInfo := new(common.DirBlockInfo)
	dirBlockInfo.DBHeight = aRecord.DBHeight
	dirBlockInfo.BTCTxOffset = aRecord.Bitcoin.Offset
	dirBlockInfo.BTCBlockHeight = aRecord.Bitcoin.BlockHeight
	mrBytes, _ := hex.DecodeString(aRecord.KeyMR)
	dirBlockInfo.DBMerkleRoot, _ = common.NewShaHash(mrBytes)
	dirBlockInfo.BTCConfirmed = true

	txSha, _ := wire.NewShaHashFromStr(aRecord.Bitcoin.TXID)
	dirBlockInfo.BTCTxHash = toHash(txSha)
	blkSha, _ := wire.NewShaHashFromStr(aRecord.Bitcoin.BlockHash)
	dirBlockInfo.BTCBlockHash = toHash(blkSha)

	dblock, err := db.FetchDBlockByHeight(aRecord.DBHeight)
	if err != nil {
		fmt.Printf("err in FetchDBlockByHeight: %d\n", aRecord.DBHeight)
		dirBlockInfo.DBHash = new(common.Hash)
	} else {
		dirBlockInfo.Timestamp = int64(dblock.Header.Timestamp * 60)
		dirBlockInfo.DBHash = dblock.DBHash
	}
	fmt.Printf("dirBlockInfo: %s\n", spew.Sdump(dirBlockInfo))
	return dirBlockInfo, nil
}
예제 #2
0
func addTxIn(msgtx *wire.MsgTx, b balance) error {
	output := b.unspentResult
	anchorLog.Info("unspentResult: %#v\n", output)
	prevTxHash, err := wire.NewShaHashFromStr(output.TxID)
	if err != nil {
		return fmt.Errorf("cannot get sha hash from str: %s", err)
	}

	outPoint := wire.NewOutPoint(prevTxHash, output.Vout)
	msgtx.AddTxIn(wire.NewTxIn(outPoint, nil))

	// OnRedeemingTx
	err = dclient.NotifySpent([]*wire.OutPoint{outPoint})
	if err != nil {
		anchorLog.Error("NotifySpent err: ", err.Error())
	}

	subscript, err := hex.DecodeString(output.ScriptPubKey)
	if err != nil {
		return fmt.Errorf("cannot decode scriptPubKey: %s", err)
	}

	sigScript, err := txscript.SignatureScript(msgtx, 0, subscript,
		txscript.SigHashAll, b.wif.PrivKey, true) //.ToECDSA(), true)
	if err != nil {
		return fmt.Errorf("cannot create scriptSig: %s", err)
	}
	msgtx.TxIn[0].SignatureScript = sigScript

	return nil
}
예제 #3
0
func (a *Anchor) doSaveDirBlockInfo(transaction *btcutil.Tx, details *btcjson.BlockDetails, dirBlockInfo *dbInfo.DirBlockInfo, replace bool) {
	if replace {
		dirBlockInfo.BTCTxHash = toHash(transaction.Sha()) // in case of tx being malleated
	}
	dirBlockInfo.BTCTxOffset = int32(details.Index)
	dirBlockInfo.BTCBlockHeight = details.Height
	btcBlockHash, _ := wire.NewShaHashFromStr(details.Hash)
	dirBlockInfo.BTCBlockHash = toHash(btcBlockHash)
	dirBlockInfo.SetTimestamp(primitives.NewTimestampNow())
	a.db.SaveDirBlockInfo(dirBlockInfo)
	anchorLog.Infof("In doSaveDirBlockInfo, dirBlockInfo:%s saved to db\n", spew.Sdump(dirBlockInfo))

	// to make factom / explorer more user friendly, instead of waiting for
	// over 2 hours to know it's anchored, we can create the anchor chain instantly
	// then change it when the btc main chain re-org happens.
	a.saveToAnchorChain(dirBlockInfo)
}
예제 #4
0
파일: anchor.go 프로젝트: Kalipsol/factomd
func saveDirBlockInfo(transaction *btcutil.Tx, details *btcjson.BlockDetails) {
	anchorLog.Debug("in saveDirBlockInfo")
	var saved = false
	for _, dirBlockInfo := range dirBlockInfoMap {
		if dirBlockInfo.BTCTxHash != nil &&
			bytes.Compare(dirBlockInfo.BTCTxHash.Bytes(), transaction.Sha().Bytes()) == 0 {
			dirBlockInfo.BTCTxOffset = int32(details.Index)
			dirBlockInfo.BTCBlockHeight = details.Height
			btcBlockHash, _ := wire.NewShaHashFromStr(details.Hash)
			dirBlockInfo.BTCBlockHash = toHash(btcBlockHash)
			dirBlockInfo.BTCConfirmed = true
			db.InsertDirBlockInfo(dirBlockInfo)
			delete(dirBlockInfoMap, dirBlockInfo.DBMerkleRoot.String())
			anchorLog.Infof("In saveDirBlockInfo, dirBlockInfo:%s saved to db\n", spew.Sdump(dirBlockInfo))
			saved = true

			anchorRec := new(AnchorRecord)
			anchorRec.AnchorRecordVer = 1
			anchorRec.DBHeight = dirBlockInfo.DBHeight
			anchorRec.KeyMR = dirBlockInfo.DBMerkleRoot.String()
			_, recordHeight, _ := db.FetchBlockHeightCache()
			anchorRec.RecordHeight = uint32(recordHeight)
			anchorRec.Bitcoin.Address = defaultAddress.String()
			anchorRec.Bitcoin.TXID = transaction.Sha().String()
			anchorRec.Bitcoin.BlockHeight = details.Height
			anchorRec.Bitcoin.BlockHash = details.Hash
			anchorRec.Bitcoin.Offset = int32(details.Index)
			anchorLog.Info("anchor.record saved: " + spew.Sdump(anchorRec))

			//jsonARecord, _ := json.Marshal(anchorRec)
			//anchorLog.Debug("jsonAnchorRecord: ", string(jsonARecord))

			//Submit the anchor record to the anchor chain (entry chain)
			err := submitEntryToAnchorChain(anchorRec)
			if err != nil {
				anchorLog.Error("Error in writing anchor into anchor chain: ", err.Error())
			}

			break
		}
	}
	// should not happen at all?
	if !saved {
		anchorLog.Info("Not saved to db: ")
	}
}
예제 #5
0
func saveDirBlockInfo(transaction *btcutil.Tx, details *btcjson.BlockDetails) {
	anchorLog.Debug("in saveDirBlockInfo")
	var saved = false
	for _, dirBlockInfo := range dirBlockInfoMap {
		if bytes.Compare(dirBlockInfo.BTCTxHash.Bytes(), transaction.Sha().Bytes()) == 0 {
			dirBlockInfo.BTCTxOffset = int32(details.Index)
			dirBlockInfo.BTCBlockHeight = details.Height
			btcBlockHash, _ := wire.NewShaHashFromStr(details.Hash)
			dirBlockInfo.BTCBlockHash = toHash(btcBlockHash)
			dirBlockInfo.BTCConfirmed = true
			db.InsertDirBlockInfo(dirBlockInfo)
			delete(dirBlockInfoMap, dirBlockInfo.DBMerkleRoot.String())
			anchorLog.Infof("In saveDirBlockInfo, dirBlockInfo:%s saved to db\n", spew.Sdump(dirBlockInfo))
			saved = true

			anchorRec := new(AnchorRecord)
			anchorRec.AnchorRecordVer = 1
			anchorRec.DBHeight = dirBlockInfo.DBHeight
			anchorRec.KeyMR = dirBlockInfo.DBMerkleRoot.String()
			_, recordHeight, _ := db.FetchBlockHeightCache()
			anchorRec.RecordHeight = uint32(recordHeight)
			anchorRec.Bitcoin.Address = defaultAddress.String()
			anchorRec.Bitcoin.TXID = transaction.Sha().String()
			anchorRec.Bitcoin.BlockHeight = details.Height
			anchorRec.Bitcoin.BlockHash = details.Hash
			anchorRec.Bitcoin.Offset = int32(details.Index)
			anchorLog.Info("anchor.record saved: " + spew.Sdump(anchorRec))

			err := submitEntryToAnchorChain(anchorRec)
			if err != nil {
				anchorLog.Error("Error in writing anchor into anchor chain: ", err.Error())
			}
			break
		}
	}
	// This happends when there's a double spending (for dir block 122 and its btc tx)
	// (see https://www.blocktrail.com/BTC/tx/ac82f4173259494b22f4987f1e18608f38f1ff756fb4a3c637dfb5565aa5e6cf)
	// or tx mutation / malleated
	// In this case, it will end up being re-anchored.
	if !saved {
		anchorLog.Info("Not saved to db: btc.tx=%s\n blockDetails=%s\n", spew.Sdump(transaction), spew.Sdump(details))
	}
}
예제 #6
0
func (a *Anchor) checkConfirmations(dirBlockInfo *dbInfo.DirBlockInfo, index int) error {
	anchorLog.Debug("check Confirmations for btc tx: ", toShaHash(dirBlockInfo.GetBTCTxHash()).String())
	txResult, err := a.wclient.GetTransaction(toShaHash(dirBlockInfo.GetBTCTxHash()))
	if err != nil {
		anchorLog.Debugf(err.Error())
		return err
	}
	anchorLog.Debugf("GetTransactionResult: %s\n", spew.Sdump(txResult))
	if txResult.Confirmations >= int64(a.confirmationsNeeded) {
		btcBlockHash, _ := wire.NewShaHashFromStr(txResult.BlockHash)
		var rewrite = false
		// Either the call back is not recorded in case of BTCBlockHash is zero hash,
		// or bad things like re-organization of btc main chain happened
		if bytes.Compare(dirBlockInfo.BTCBlockHash.Bytes(), btcBlockHash.Bytes()) != 0 {
			anchorLog.Debugf("BTCBlockHash changed: original BTCBlockHeight=%d, original BTCBlockHash=%s, original tx offset=%d\n", dirBlockInfo.BTCBlockHeight, toShaHash(dirBlockInfo.BTCBlockHash).String(), dirBlockInfo.BTCTxOffset)
			dirBlockInfo.BTCBlockHash = toHash(btcBlockHash)
			btcBlock, err := a.wclient.GetBlockVerbose(btcBlockHash, true)
			if err != nil {
				anchorLog.Debugf(err.Error())
			}
			if btcBlock.Height > 0 {
				dirBlockInfo.BTCBlockHeight = int32(btcBlock.Height)
			}
			anchorLog.Debugf("BTCBlockHash changed: new BTCBlockHeight=%d, new BTCBlockHash=%s, btcBlockVerbose.Height=%d\n", dirBlockInfo.BTCBlockHeight, btcBlockHash.String(), btcBlock.Height)
			rewrite = true
		}
		dirBlockInfo.BTCConfirmed = true // needs confirmationsNeeded (20) to be confirmed.
		dirBlockInfo.SetTimestamp(primitives.NewTimestampNow())
		a.db.SaveDirBlockInfo(dirBlockInfo)
		a.dirBlockInfoSlice = append(a.dirBlockInfoSlice[:index], a.dirBlockInfoSlice[index+1:]...) //delete it
		anchorLog.Debugf("Fully confirmed %d times. txid=%s, dirblockInfo=%s\n", txResult.Confirmations, txResult.TxID, spew.Sdump(dirBlockInfo))
		if rewrite {
			anchorLog.Debug("rewrite to anchor chain: ", spew.Sdump(dirBlockInfo))
			a.saveToAnchorChain(dirBlockInfo)
		}
	}
	return nil
}