本文整理匯總了Golang中github.com/NebulousLabs/Sia/crypto.RandBytes函數的典型用法代碼示例。如果您正苦於以下問題:Golang RandBytes函數的具體用法?Golang RandBytes怎麽用?Golang RandBytes使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了RandBytes函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createRandFile
// createRandFile creates a file on disk and fills it with random bytes.
func createRandFile(path string, size int) error {
data, err := crypto.RandBytes(size)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, 0600)
}
示例2: TestIntegrationUploadDownload
// TestIntegrationUploadDownload tests that the contractor can upload data to
// a host and download it intact.
func TestIntegrationUploadDownload(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
// create testing trio
h, c, _, err := newTestingTrio("TestIntegrationUploadDownload")
if err != nil {
t.Fatal(err)
}
// get the host's entry from the db
hostEntry, ok := c.hdb.Host(h.ExternalSettings().NetAddress)
if !ok {
t.Fatal("no entry for host in db")
}
// form a contract with the host
contract, err := c.managedNewContract(hostEntry, 10, c.blockHeight+100)
if err != nil {
t.Fatal(err)
}
// revise the contract
editor, err := c.Editor(contract)
if err != nil {
t.Fatal(err)
}
data, err := crypto.RandBytes(int(modules.SectorSize))
if err != nil {
t.Fatal(err)
}
root, err := editor.Upload(data)
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
// download the data
contract = c.contracts[contract.ID]
downloader, err := c.Downloader(contract)
if err != nil {
t.Fatal(err)
}
retrieved, err := downloader.Sector(root)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, retrieved) {
t.Fatal("downloaded data does not match original")
}
err = downloader.Close()
if err != nil {
t.Fatal(err)
}
}
示例3: randSector
// randSector creates a random sector, returning the sector along with the
// Merkle root of the sector.
func randSector() (crypto.Hash, []byte, error) {
sectorData, err := crypto.RandBytes(int(modules.SectorSize))
if err != nil {
return crypto.Hash{}, nil, err
}
sectorRoot := crypto.MerkleRoot(sectorData)
return sectorRoot, sectorData, nil
}
示例4: createSector
// createSector makes a random, unique sector that can be inserted into the
// storage manager.
func createSector() (sectorRoot crypto.Hash, sectorData []byte, err error) {
sectorData, err = crypto.RandBytes(int(modules.SectorSize))
if err != nil {
return crypto.Hash{}, nil, err
}
sectorRoot = crypto.MerkleRoot(sectorData)
return sectorRoot, sectorData, nil
}
示例5: TestIntegrationDelete
// TestIntegrationDelete tests that the contractor can delete a sector from a
// contract previously formed with a host.
func TestIntegrationDelete(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
// create testing trio
h, c, _, err := newTestingTrio("TestIntegrationDelete")
if err != nil {
t.Fatal(err)
}
// get the host's entry from the db
hostEntry, ok := c.hdb.Host(h.ExternalSettings().NetAddress)
if !ok {
t.Fatal("no entry for host in db")
}
// form a contract with the host
contract, err := c.managedNewContract(hostEntry, 10, c.blockHeight+100)
if err != nil {
t.Fatal(err)
}
// revise the contract
editor, err := c.Editor(contract)
if err != nil {
t.Fatal(err)
}
data, err := crypto.RandBytes(int(modules.SectorSize))
if err != nil {
t.Fatal(err)
}
_, err = editor.Upload(data)
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
// delete the sector
contract = c.contracts[contract.ID]
editor, err = c.Editor(contract)
if err != nil {
t.Fatal(err)
}
err = editor.Delete(contract.MerkleRoots[0])
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
}
示例6: HeaderForWork
// HeaderForWork returns a block that is ready for nonce grinding, along with
// the root hash of the block.
func (m *Miner) HeaderForWork() (types.BlockHeader, types.Target, error) {
if !m.wallet.Unlocked() {
return types.BlockHeader{}, types.Target{}, modules.ErrLockedWallet
}
lockID := m.mu.Lock()
defer m.mu.Unlock(lockID)
err := m.checkAddress()
if err != nil {
return types.BlockHeader{}, types.Target{}, err
}
if time.Since(m.lastBlock).Seconds() > secondsBetweenBlocks {
m.prepareNewBlock()
}
// The header that will be returned for nonce grinding.
// The header is constructed from a block and some arbitrary data. The
// arbitrary data allows for multiple unique blocks to be generated from
// a single block in memory. A block pointer is used in order to avoid
// storing multiple copies of the same block in memory
var header types.BlockHeader
var arbData []byte
var block *types.Block
if m.memProgress%(headerForWorkMemory/blockForWorkMemory) == 0 {
// Grab a new block. Allocate space for the pointer to store it as well
block = new(types.Block)
*block, _ = m.blockForWork()
header = block.Header()
arbData = block.Transactions[0].ArbitraryData[0]
m.lastBlock = time.Now()
} else {
// Set block to previous block, but create new arbData
block = m.blockMem[m.headerMem[m.memProgress-1]]
arbData, _ = crypto.RandBytes(types.SpecifierLen)
block.Transactions[0].ArbitraryData[0] = arbData
header = block.Header()
}
// Save a mapping from the header to its block as well as from the
// header to its arbitrary data, replacing the block that was
// stored 'headerForWorkMemory' requests ago.
delete(m.blockMem, m.headerMem[m.memProgress])
delete(m.arbDataMem, m.headerMem[m.memProgress])
m.blockMem[header] = block
m.arbDataMem[header] = arbData
m.headerMem[m.memProgress] = header
m.memProgress++
if m.memProgress == headerForWorkMemory {
m.memProgress = 0
}
// Return the header and target.
return header, m.target, nil
}
示例7: newTestingFile
// newTestingFile initializes a file object with random parameters.
func newTestingFile() *file {
key, _ := crypto.GenerateTwofishKey()
data, _ := crypto.RandBytes(8)
nData, _ := crypto.RandIntn(10)
nParity, _ := crypto.RandIntn(10)
rsc, _ := NewRSCode(nData+1, nParity+1)
return &file{
name: "testfile-" + strconv.Itoa(int(data[0])),
size: encoding.DecUint64(data[1:5]),
masterKey: key,
erasureCode: rsc,
pieceSize: encoding.DecUint64(data[6:8]),
}
}
示例8: blockForWork
// blockForWork returns a block that is ready for nonce grinding, including
// correct miner payouts and a random transaction to prevent collisions and
// overlapping work with other blocks being mined in parallel or for different
// forks (during testing).
func (m *Miner) blockForWork() types.Block {
b := m.unsolvedBlock
// Update the timestmap.
if b.Timestamp < types.CurrentTimestamp() {
b.Timestamp = types.CurrentTimestamp()
}
// Update the address + payouts.
_ = m.checkAddress() // Err is ignored - address generation failed but can't do anything about it (log maybe).
b.MinerPayouts = []types.SiacoinOutput{{Value: b.CalculateSubsidy(m.height + 1), UnlockHash: m.address}}
// Add an arb-data txn to the block to create a unique merkle root.
randBytes, _ := crypto.RandBytes(types.SpecifierLen)
randTxn := types.Transaction{
ArbitraryData: [][]byte{append(modules.PrefixNonSia[:], randBytes...)},
}
b.Transactions = append([]types.Transaction{randTxn}, b.Transactions...)
return b
}
示例9: blockForWork
// Creates a block ready for nonce grinding, also returning the MerkleRoot of
// the block. Getting the MerkleRoot of a block requires encoding and hashing
// in a specific way, which are implementation details we didn't want to
// require external miners to need to worry about. All blocks returned are
// unique, which means all miners can safely start at the '0' nonce.
func (m *Miner) blockForWork() (types.Block, types.Target) {
// Determine the timestamp.
blockTimestamp := types.CurrentTimestamp()
if blockTimestamp < m.earliestTimestamp {
blockTimestamp = m.earliestTimestamp
}
// Create the miner payouts.
subsidy := types.CalculateCoinbase(m.height)
for _, txn := range m.transactions {
for _, fee := range txn.MinerFees {
subsidy = subsidy.Add(fee)
}
}
blockPayouts := []types.SiacoinOutput{types.SiacoinOutput{Value: subsidy, UnlockHash: m.address}}
// Create the list of transacitons, including the randomized transaction.
// The transactions are assembled by calling append(singleElem,
// existingSlic) because doing it the reverse way has some side effects,
// creating a race condition and ultimately changing the block hash for
// other parts of the program. This is related to the fact that slices are
// pointers, and not immutable objects. Use of the builtin `copy` function
// when passing objects like blocks around may fix this problem.
randBytes, _ := crypto.RandBytes(types.SpecifierLen)
randTxn := types.Transaction{
ArbitraryData: [][]byte{append(modules.PrefixNonSia[:], randBytes...)},
}
blockTransactions := append([]types.Transaction{randTxn}, m.transactions...)
// Assemble the block
b := types.Block{
ParentID: m.parent,
Timestamp: blockTimestamp,
MinerPayouts: blockPayouts,
Transactions: blockTransactions,
}
return b, m.target
}
示例10: blockForWork
// blockForWork returns a block that is ready for nonce grinding, including
// correct miner payouts and a random transaction to prevent collisions and
// overlapping work with other blocks being mined in parallel or for different
// forks (during testing).
func (m *Miner) blockForWork() types.Block {
b := m.persist.UnsolvedBlock
// Update the timestmap.
if b.Timestamp < types.CurrentTimestamp() {
b.Timestamp = types.CurrentTimestamp()
}
// Update the address + payouts.
err := m.checkAddress()
if err != nil {
m.log.Println(err)
}
b.MinerPayouts = []types.SiacoinOutput{{Value: b.CalculateSubsidy(m.persist.Height + 1), UnlockHash: m.persist.Address}}
// Add an arb-data txn to the block to create a unique merkle root.
randBytes, _ := crypto.RandBytes(types.SpecifierLen)
randTxn := types.Transaction{
ArbitraryData: [][]byte{append(modules.PrefixNonSia[:], randBytes...)},
}
b.Transactions = append([]types.Transaction{randTxn}, b.Transactions...)
return b
}
示例11: TestUploadDownload
// TestUploadDownload tests the Upload and Download methods using a mock
// contractor.
func TestUploadDownload(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// create renter with mocked contractor
hc := &uploadDownloadContractor{
sectors: make(map[crypto.Hash][]byte),
}
rt, err := newContractorTester("TestUploadDownload", nil, hc)
if err != nil {
t.Fatal(err)
}
// create a file
data, err := crypto.RandBytes(777)
if err != nil {
t.Fatal(err)
}
source := filepath.Join(build.SiaTestingDir, "renter", "TestUploadDownload", "test.dat")
err = ioutil.WriteFile(source, data, 0600)
if err != nil {
t.Fatal(err)
}
// use 1-1 erasure code, because we'll only have one host
rsc, _ := NewRSCode(1, 1)
// upload file
err = rt.renter.Upload(modules.FileUploadParams{
Source: source,
SiaPath: "foo",
ErasureCode: rsc,
// Upload will use sane defaults for other params
})
if err != nil {
t.Fatal(err)
}
files := rt.renter.FileList()
if len(files) != 1 {
t.Fatal("expected 1 file, got", len(files))
}
// wait for repair loop for fully upload file
for i := 0; i < 10 && !files[0].Available; i++ {
files = rt.renter.FileList()
time.Sleep(time.Second)
}
if !files[0].Available {
t.Fatal("file did not reach full availability:", files[0].UploadProgress)
}
// download the file
dest := filepath.Join(build.SiaTestingDir, "renter", "TestUploadDownload", "test.dat")
err = rt.renter.Download("foo", dest)
if err != nil {
t.Fatal(err)
}
downData, err := ioutil.ReadFile(dest)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(downData, data) {
t.Fatal("recovered data does not match original")
}
}
示例12: TestIntegrationModify
// TestIntegrationModify tests that the contractor can modify a previously-
// uploaded sector.
func TestIntegrationModify(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
// create testing trio
h, c, _, err := newTestingTrio("TestIntegrationModify")
if err != nil {
t.Fatal(err)
}
// get the host's entry from the db
hostEntry, ok := c.hdb.Host(h.ExternalSettings().NetAddress)
if !ok {
t.Fatal("no entry for host in db")
}
// form a contract with the host
contract, err := c.managedNewContract(hostEntry, 10, c.blockHeight+100)
if err != nil {
t.Fatal(err)
}
// revise the contract
editor, err := c.Editor(contract)
if err != nil {
t.Fatal(err)
}
data, err := crypto.RandBytes(int(modules.SectorSize))
if err != nil {
t.Fatal(err)
}
// insert the sector
_, err = editor.Upload(data)
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
// modify the sector
oldRoot := crypto.MerkleRoot(data)
offset, newData := uint64(10), []byte{1, 2, 3, 4, 5}
copy(data[offset:], newData)
newRoot := crypto.MerkleRoot(data)
contract = c.contracts[contract.ID]
editor, err = c.Editor(contract)
if err != nil {
t.Fatal(err)
}
err = editor.Modify(oldRoot, newRoot, offset, newData)
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
}
示例13: uploadFile
// uploadFile uploads a file to the host from the tester's renter. The data
// used to make the file is returned. The nickname of the file in the renter is
// the same as the name provided as input.
func (ht *hostTester) uploadFile(path string, renew bool) ([]byte, error) {
// Check that renting is initialized properly.
err := ht.initRenting()
if err != nil {
return nil, err
}
// Create a file to upload to the host.
source := filepath.Join(ht.persistDir, path+".testfile")
datasize := uint64(1024)
data, err := crypto.RandBytes(int(datasize))
if err != nil {
return nil, err
}
dataMerkleRoot, err := crypto.ReaderMerkleRoot(bytes.NewReader(data))
if err != nil {
return nil, err
}
err = ioutil.WriteFile(source, data, 0600)
if err != nil {
return nil, err
}
// Have the renter upload to the host.
rsc, err := renter.NewRSCode(1, 1)
if err != nil {
return nil, err
}
fup := modules.FileUploadParams{
Source: source,
SiaPath: path,
Duration: testUploadDuration,
Renew: renew,
ErasureCode: rsc,
PieceSize: 0,
}
err = ht.renter.Upload(fup)
if err != nil {
return nil, err
}
// Wait until the upload has finished.
for i := 0; i < 100; i++ {
time.Sleep(time.Millisecond * 100)
// Asynchronous processes in the host access obligations by id,
// therefore a lock is required to scan the set of obligations.
if func() bool {
ht.host.mu.Lock()
defer ht.host.mu.Unlock()
for _, ob := range ht.host.obligationsByID {
if dataMerkleRoot == ob.merkleRoot() {
return true
}
}
return false
}() {
break
}
}
// Block until the renter is at 50 upload progress - it takes time for the
// contract to confirm renter-side.
complete := false
for i := 0; i < 50 && !complete; i++ {
fileInfos := ht.renter.FileList()
for _, fileInfo := range fileInfos {
if fileInfo.UploadProgress >= 50 {
complete = true
}
}
if complete {
break
}
time.Sleep(time.Millisecond * 50)
}
if !complete {
return nil, errors.New("renter never recognized that the upload completed")
}
// The rest of the upload can be performed under lock.
ht.host.mu.Lock()
defer ht.host.mu.Unlock()
if len(ht.host.obligationsByID) != 1 {
return nil, errors.New("expecting a single obligation")
}
for _, ob := range ht.host.obligationsByID {
if ob.fileSize() >= datasize {
return data, nil
}
}
return nil, errors.New("ht.uploadFile: upload failed")
}
示例14: TestIntegrationAutoRenew
// TestIntegrationAutoRenew tests that contracts are automatically renwed at
// the expected block height.
func TestIntegrationAutoRenew(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
// create testing trio
_, c, m, err := newTestingTrio("TestIntegrationAutoRenew")
if err != nil {
t.Fatal(err)
}
// form a contract with the host
a := modules.Allowance{
Funds: types.SiacoinPrecision.Mul64(100), // 100 SC
Hosts: 1,
Period: 50,
RenewWindow: 10,
}
err = c.SetAllowance(a)
if err != nil {
t.Fatal(err)
}
contract := c.Contracts()[0]
// revise the contract
editor, err := c.Editor(contract)
if err != nil {
t.Fatal(err)
}
data, err := crypto.RandBytes(int(modules.SectorSize))
if err != nil {
t.Fatal(err)
}
// insert the sector
root, err := editor.Upload(data)
if err != nil {
t.Fatal(err)
}
err = editor.Close()
if err != nil {
t.Fatal(err)
}
// set allowance to a lower period; Contractor will auto-renew when
// current contract expires
a.Period--
err = c.SetAllowance(a)
if err != nil {
t.Fatal(err)
}
// mine until we enter the renew window
renewHeight := contract.EndHeight() - c.allowance.RenewWindow
for c.blockHeight < renewHeight {
_, err := m.AddBlock()
if err != nil {
t.Fatal(err)
}
}
// check renewed contract
contract = c.Contracts()[0]
if contract.FileContract.FileMerkleRoot != root {
t.Fatal(contract.FileContract.FileMerkleRoot)
} else if contract.FileContract.FileSize != modules.SectorSize {
t.Fatal(contract.FileContract.FileSize)
} else if contract.FileContract.RevisionNumber != 0 {
t.Fatal(contract.FileContract.RevisionNumber)
} else if contract.FileContract.WindowStart != c.blockHeight+c.allowance.Period {
t.Fatal(contract.FileContract.WindowStart)
}
}
示例15: TestErasureDownload
// TestErasureDownload tests parallel downloading of erasure-coded data. It
// mocks the fetcher interface in order to directly test the downloading
// algorithm.
func TestErasureDownload(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// generate data
const dataSize = 777
data, err := crypto.RandBytes(dataSize)
if err != nil {
t.Fatal(err)
}
// create Reed-Solomon encoder
rsc, err := NewRSCode(2, 10)
if err != nil {
t.Fatal(err)
}
// create hosts
const pieceSize = 10
hosts := make([]fetcher, rsc.NumPieces())
for i := range hosts {
hosts[i] = &testFetcher{
sectors: make(map[crypto.Hash][]byte),
pieceMap: make(map[uint64][]pieceData),
pieceSize: pieceSize,
delay: time.Millisecond,
failRate: 5, // 20% failure rate
}
}
// make one host really slow
hosts[0].(*testFetcher).delay = 100 * time.Millisecond
// make one host always fail
hosts[1].(*testFetcher).failRate = 1
// upload data to hosts
r := bytes.NewReader(data) // makes chunking easier
chunk := make([]byte, pieceSize*rsc.MinPieces())
var i uint64
for i = uint64(0); ; i++ {
_, err := io.ReadFull(r, chunk)
if err == io.EOF {
break
} else if err != nil && err != io.ErrUnexpectedEOF {
t.Fatal(err)
}
pieces, err := rsc.Encode(chunk)
if err != nil {
t.Fatal(err)
}
for j, p := range pieces {
root := crypto.MerkleRoot(p)
host := hosts[j%len(hosts)].(*testFetcher) // distribute evenly
host.pieceMap[i] = append(host.pieceMap[i], pieceData{
Chunk: uint64(i),
Piece: uint64(j),
MerkleRoot: root,
})
host.sectors[root] = p
}
}
// check hosts (not strictly necessary)
err = checkHosts(hosts, rsc.MinPieces(), i)
if err != nil {
t.Fatal(err)
}
// download data
d := newFile("foo", rsc, pieceSize, dataSize).newDownload(hosts, "")
buf := new(bytes.Buffer)
err = d.run(buf)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf.Bytes(), data) {
t.Fatal("recovered data does not match original")
}
/*
// These metrics can be used to assess the efficiency of the download
// algorithm.
totFetch := 0
for i, h := range hosts {
h := h.(*testHost)
t.Logf("Host %2d: Fetched: %v/%v", i, h.nFetch, h.nAttempt)
totFetch += h.nAttempt
}
t.Log("Optimal fetches:", i*uint64(rsc.MinPieces()))
t.Log("Total fetches: ", totFetch)
*/
}