func IsMinuteMarker(hash string) bool {
	h, err := common.HexToHash(hash)
	if err != nil {
		panic(err)
	}
	return h.IsMinuteMarker()
}
Exemplo n.º 2
0
func handleGetRaw(ctx *web.Context, hashkey string) {
	type rawData struct {
		Data string
	}
	//TODO: var block common.BinaryMarshallable
	d := new(rawData)

	h, err := common.HexToHash(hashkey)
	if err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	}

	// try to find the block data in db and return the first one found
	if block, _ := dbase.FetchFBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchDBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchABlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchDBlockByMR(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEBlockByMR(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchECBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEntryByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	}

	if p, err := json.Marshal(d); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		ctx.Write(p)
	}

	//	ctx.WriteHeader(httpOK)
}
Exemplo n.º 3
0
// DirBlockLocatorFromHash returns a block locator for the passed block hash.
// See BlockLocator for details on the algotirhm used to create a block locator.
//
// In addition to the general algorithm referenced above, there are a couple of
// special cases which are handled:
//
//  - If the genesis hash is passed, there are no previous hashes to add and
//    therefore the block locator will only consist of the genesis hash
//  - If the passed hash is not currently known, the block locator will only
//    consist of the passed hash
func DirBlockLocatorFromHash(hash *wire.ShaHash) blockchain.BlockLocator {
	// The locator contains the requested hash at the very least.
	locator := make(blockchain.BlockLocator, 0, wire.MaxBlockLocatorsPerMsg)
	locator = append(locator, hash)

	h, _ := common.HexToHash(common.GENESIS_DIR_BLOCK_HASH)
	genesisHash := wire.FactomHashToShaHash(h)
	// Nothing more to do if a locator for the genesis hash was requested.
	if genesisHash.IsEqual(hash) {
		return locator
	}

	// Attempt to find the height of the block that corresponds to the
	// passed hash, and if it's on a side chain, also find the height at
	// which it forks from the main chain.
	blockHeight := int64(-1)

	// Generate the block locators according to the algorithm described in
	// in the BlockLocator comment and make sure to leave room for the
	// final genesis hash.

	dblock, _ := db.FetchDBlockByHash(hash.ToFactomHash())
	if dblock != nil {
		blockHeight = int64(dblock.Header.DBHeight)
	}
	increment := int64(1)
	for len(locator) < wire.MaxBlockLocatorsPerMsg-1 {
		// Once there are 10 locators, exponentially increase the
		// distance between each block locator.
		if len(locator) > 10 {
			increment *= 2
		}
		blockHeight -= increment
		if blockHeight < 1 {
			break
		}

		blk, _ := db.FetchDBlockByHeight(uint32(blockHeight))
		if blk == nil {
			continue
		} else if blk.DBHash == nil {
			blk.DBHash, _ = common.CreateHash(blk)
		}

		locator = append(locator, wire.FactomHashToShaHash(blk.DBHash))
	}

	// Append the appropriate genesis block.
	locator = append(locator, genesisHash)
	return locator
}
Exemplo n.º 4
0
func main() {
	cfg = util.ReadConfig()
	ldbpath := cfg.App.LdbPath
	initDB(ldbpath)

	anchorChainID, _ := common.HexToHash(cfg.Anchor.AnchorChainID)
	//fmt.Println("anchorChainID: ", cfg.Anchor.AnchorChainID)

	processAnchorChain(anchorChainID)

	//initDB("/home/bw/.factom/ldb.prd")
	//dirBlockInfoMap, _ := db.FetchAllDirBlockInfo() // map[string]*common.DirBlockInfo
	//for _, dirBlockInfo := range dirBlockInfoMap {
	//fmt.Printf("dirBlockInfo: %s\n", spew.Sdump(dirBlockInfo))
	//}
}
Exemplo n.º 5
0
func sanityCheck() {
	cfg = util.ReadConfig()
	ldbpath := cfg.App.LdbPath
	initDB(ldbpath)
	dirBlockInfoMap = make(map[uint32]*common.DirBlockInfo)
	anchorChainID, _ = common.HexToHash(cfg.Anchor.AnchorChainID)
	fmt.Printf("ldbPath=%s, anchorChainID=%s\n", ldbpath, cfg.Anchor.AnchorChainID)

	dblocks, _ = db.FetchAllDBlocks()
	dirBlockInfoMap, _ := db.FetchAllDirBlockInfo()
	eblocks, _ = db.FetchAllEBlocksByChain(anchorChainID)
	fmt.Printf("There are %d directory blocks, %d DirBlockInfos, and %d anchor chain blocks in this database.\n",
		len(dblocks), len(dirBlockInfoMap), len(*eblocks))

	if len(dblocks) == len(dirBlockInfoMap) {
		fmt.Println("All dir blocks have dirBlockInfo. All good and done!")
		os.Exit(0)
	}
}
Exemplo n.º 6
0
// InitRPCClient is used to create rpc client for btcd and btcwallet
// and it can be used to test connecting to btcd / btcwallet servers
// running in different machine.
func InitRPCClient() error {
	anchorLog.Debug("init RPC client")
	cfg = util.ReadConfig()
	certHomePath := cfg.Btc.CertHomePath
	rpcClientHost := cfg.Btc.RpcClientHost
	rpcClientEndpoint := cfg.Btc.RpcClientEndpoint
	rpcClientUser := cfg.Btc.RpcClientUser
	rpcClientPass := cfg.Btc.RpcClientPass
	certHomePathBtcd := cfg.Btc.CertHomePathBtcd
	rpcBtcdHost := cfg.Btc.RpcBtcdHost
	confirmationsNeeded = cfg.Anchor.ConfirmationsNeeded

	//Added anchor parameters
	var err error
	serverECKey, err = common.NewPrivateKeyFromHex(cfg.Anchor.ServerECKey)
	if err != nil {
		panic("Cannot parse Server EC Key from configuration file: " + err.Error())
	}
	anchorChainID, err = common.HexToHash(cfg.Anchor.AnchorChainID)
	anchorLog.Debug("anchorChainID: ", anchorChainID)
	if err != nil || anchorChainID == nil {
		panic("Cannot parse Server AnchorChainID from configuration file: " + err.Error())
	}

	// Connect to local btcwallet RPC server using websockets.
	ntfnHandlers := createBtcwalletNotificationHandlers()
	certHomeDir := btcutil.AppDataDir(certHomePath, false)
	anchorLog.Debug("btcwallet.cert.home=", certHomeDir)
	certs, err := ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
	if err != nil {
		return fmt.Errorf("cannot read rpc.cert file: %s\n", err)
	}
	connCfg := &btcrpcclient.ConnConfig{
		Host:         rpcClientHost,
		Endpoint:     rpcClientEndpoint,
		User:         rpcClientUser,
		Pass:         rpcClientPass,
		Certificates: certs,
	}
	wclient, err = btcrpcclient.New(connCfg, &ntfnHandlers)
	if err != nil {
		return fmt.Errorf("cannot create rpc client for btcwallet: %s\n", err)
	}
	anchorLog.Debug("successfully created rpc client for btcwallet")

	// Connect to local btcd RPC server using websockets.
	dntfnHandlers := createBtcdNotificationHandlers()
	certHomeDir = btcutil.AppDataDir(certHomePathBtcd, false)
	anchorLog.Debug("btcd.cert.home=", certHomeDir)
	certs, err = ioutil.ReadFile(filepath.Join(certHomeDir, "rpc.cert"))
	if err != nil {
		return fmt.Errorf("cannot read rpc.cert file for btcd rpc server: %s\n", err)
	}
	dconnCfg := &btcrpcclient.ConnConfig{
		Host:         rpcBtcdHost,
		Endpoint:     rpcClientEndpoint,
		User:         rpcClientUser,
		Pass:         rpcClientPass,
		Certificates: certs,
	}
	dclient, err = btcrpcclient.New(dconnCfg, &dntfnHandlers)
	if err != nil {
		return fmt.Errorf("cannot create rpc client for btcd: %s\n", err)
	}
	anchorLog.Debug("successfully created rpc client for btcd")

	return nil
}