本文整理匯總了Golang中github.com/NebulousLabs/Sia/encoding.Marshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Marshal函數的具體用法?Golang Marshal怎麽用?Golang Marshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Marshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getTransaction
// Returns the transaction with the given id
func (db *explorerDB) getTransaction(id crypto.Hash) (modules.TransactionResponse, error) {
var tr modules.TransactionResponse
// Look up the transaction's location
tBytes, err := db.GetFromBucket("Transactions", encoding.Marshal(id))
if err != nil {
return tr, err
}
var tLocation txInfo
err = encoding.Unmarshal(tBytes, &tLocation)
if err != nil {
return tr, err
}
// Look up the block specified by the location and extract the transaction
bBytes, err := db.GetFromBucket("Blocks", encoding.Marshal(tLocation.BlockID))
if err != nil {
return tr, err
}
var block types.Block
err = encoding.Unmarshal(bBytes, &block)
if err != nil {
return tr, err
}
tr.Tx = block.Transactions[tLocation.TxNum]
tr.ParentID = tLocation.BlockID
tr.TxNum = tLocation.TxNum
tr.ResponseType = responseTransaction
return tr, nil
}
示例2: RegisterTransaction
// RegisterTransaction takes a transaction and its parents and returns a
// TransactionBuilder which can be used to expand the transaction. The most
// typical call is 'RegisterTransaction(types.Transaction{}, nil)', which
// registers a new transaction without parents.
func (w *Wallet) RegisterTransaction(t types.Transaction, parents []types.Transaction) modules.TransactionBuilder {
// Create a deep copy of the transaction and parents by encoding them. A
// deep copy ensures that there are no pointer or slice related errors -
// the builder will be working directly on the transaction, and the
// transaction may be in use elsewhere (in this case, the host is using the
// transaction.
pBytes := encoding.Marshal(parents)
var pCopy []types.Transaction
err := encoding.Unmarshal(pBytes, &pCopy)
if err != nil {
panic(err)
}
tBytes := encoding.Marshal(t)
var tCopy types.Transaction
err = encoding.Unmarshal(tBytes, &tCopy)
if err != nil {
panic(err)
}
return &transactionBuilder{
parents: pCopy,
transaction: tCopy,
wallet: w,
}
}
示例3: addFileContract
// addFileContract adds a file contract to the database. An error is returned
// if the file contract is already in the database.
func addFileContract(tx *bolt.Tx, id types.FileContractID, fc types.FileContract) {
// Add the file contract to the database.
fcBucket := tx.Bucket(FileContracts)
// Sanity check - should not be adding a zero-payout file contract.
if build.DEBUG && fc.Payout.IsZero() {
panic("adding zero-payout file contract")
}
// Sanity check - should not be adding a file contract already in the db.
if build.DEBUG && fcBucket.Get(id[:]) != nil {
panic("repeat file contract")
}
err := fcBucket.Put(id[:], encoding.Marshal(fc))
if build.DEBUG && err != nil {
panic(err)
}
// Add an entry for when the file contract expires.
expirationBucketID := append(prefixFCEX, encoding.Marshal(fc.WindowEnd)...)
expirationBucket, err := tx.CreateBucketIfNotExists(expirationBucketID)
if build.DEBUG && err != nil {
panic(err)
}
err = expirationBucket.Put(id[:], []byte{})
if build.DEBUG && err != nil {
panic(err)
}
}
示例4: newChild
// newChild creates a blockNode from a block and adds it to the parent's set of
// children. The new node is also returned. It necessairly modifies the database
//
// TODO: newChild has a fair amount of room for optimization.
func (cs *ConsensusSet) newChild(pb *processedBlock, b types.Block) *processedBlock {
// Create the child node.
childID := b.ID()
child := &processedBlock{
Block: b,
Parent: b.ParentID,
Height: pb.Height + 1,
Depth: pb.childDepth(),
}
err := cs.db.Update(func(tx *bolt.Tx) error {
blockMap := tx.Bucket(BlockMap)
err := cs.setChildTarget(blockMap, child)
if err != nil {
return err
}
pb.Children = append(pb.Children, childID)
err = blockMap.Put(child.Block.ParentID[:], encoding.Marshal(*pb))
if err != nil {
return err
}
return blockMap.Put(childID[:], encoding.Marshal(*child))
})
if build.DEBUG && err != nil {
panic(err)
}
return child
}
示例5: BenchmarkEncodeBlock
// BenchmarkEncodeEmptyBlock benchmarks encoding an empty block.
//
// i5-4670K, 9a90f86: 48 MB/s
func BenchmarkEncodeBlock(b *testing.B) {
var block Block
b.SetBytes(int64(len(encoding.Marshal(block))))
for i := 0; i < b.N; i++ {
encoding.Marshal(block)
}
}
示例6: createDSCOBucket
// createDSCOBucket creates a bucket for the delayed siacoin outputs at the
// input height.
func createDSCOBucket(tx *bolt.Tx, bh types.BlockHeight) error {
bucketID := append(prefix_dsco, encoding.Marshal(bh)...)
dscoBuckets := tx.Bucket(DSCOBuckets)
err := dscoBuckets.Put(encoding.Marshal(bh), encoding.Marshal(bucketID))
if err != nil {
panic(err)
}
_, err = tx.CreateBucket(bucketID)
return err
}
示例7: SigHash
// SigHash returns the hash of the fields in a transaction covered by a given
// signature. See CoveredFields for more details.
func (t Transaction) SigHash(i int) crypto.Hash {
cf := t.TransactionSignatures[i].CoveredFields
var signedData []byte
if cf.WholeTransaction {
signedData = encoding.MarshalAll(
t.SiacoinInputs,
t.SiacoinOutputs,
t.FileContracts,
t.FileContractRevisions,
t.StorageProofs,
t.SiafundInputs,
t.SiafundOutputs,
t.MinerFees,
t.ArbitraryData,
t.TransactionSignatures[i].ParentID,
t.TransactionSignatures[i].PublicKeyIndex,
t.TransactionSignatures[i].Timelock,
)
} else {
for _, input := range cf.SiacoinInputs {
signedData = append(signedData, encoding.Marshal(t.SiacoinInputs[input])...)
}
for _, output := range cf.SiacoinOutputs {
signedData = append(signedData, encoding.Marshal(t.SiacoinOutputs[output])...)
}
for _, contract := range cf.FileContracts {
signedData = append(signedData, encoding.Marshal(t.FileContracts[contract])...)
}
for _, revision := range cf.FileContractRevisions {
signedData = append(signedData, encoding.Marshal(t.FileContractRevisions[revision])...)
}
for _, storageProof := range cf.StorageProofs {
signedData = append(signedData, encoding.Marshal(t.StorageProofs[storageProof])...)
}
for _, siafundInput := range cf.SiafundInputs {
signedData = append(signedData, encoding.Marshal(t.SiafundInputs[siafundInput])...)
}
for _, siafundOutput := range cf.SiafundOutputs {
signedData = append(signedData, encoding.Marshal(t.SiafundOutputs[siafundOutput])...)
}
for _, minerFee := range cf.MinerFees {
signedData = append(signedData, encoding.Marshal(t.MinerFees[minerFee])...)
}
for _, arbData := range cf.ArbitraryData {
signedData = append(signedData, encoding.Marshal(t.ArbitraryData[arbData])...)
}
}
for _, sig := range cf.TransactionSignatures {
signedData = append(signedData, encoding.Marshal(t.TransactionSignatures[sig])...)
}
return crypto.HashBytes(signedData)
}
示例8: insertItem
// insertItem inserts an item to a bucket. In debug mode, a panic is thrown if
// the bucket does not exist or if the item is already in the bucket.
func insertItem(tx *bolt.Tx, bucket []byte, key, value interface{}) error {
b := tx.Bucket(bucket)
if build.DEBUG && b == nil {
panic(errNilBucket)
}
k := encoding.Marshal(key)
v := encoding.Marshal(value)
if build.DEBUG && b.Get(k) != nil {
panic(errRepeatInsert)
}
return b.Put(k, v)
}
示例9: putObject
func (tx *boltTx) putObject(bucket string, key, val interface{}) {
// if an error has already be encountered, do nothing
if tx.err != nil {
return
}
b := tx.Bucket([]byte(bucket))
if b == nil {
tx.err = errors.New("bucket does not exist: " + bucket)
return
}
tx.err = b.Put(encoding.Marshal(key), encoding.Marshal(val))
return
}
示例10: BenchmarkStandaloneValid
// BenchmarkStandaloneValid times how long it takes to verify a single
// large transaction, with a certain number of signatures
func BenchmarkStandaloneValid(b *testing.B) {
numSigs := 7
// make a transaction numSigs with valid inputs with valid signatures
b.ReportAllocs()
txn := Transaction{}
sk := make([]crypto.SecretKey, numSigs)
pk := make([]crypto.PublicKey, numSigs)
for i := 0; i < numSigs; i++ {
s, p, err := crypto.GenerateKeyPair()
if err != nil {
b.Fatal(err)
}
sk[i] = s
pk[i] = p
uc := UnlockConditions{
PublicKeys: []SiaPublicKey{
{Algorithm: SignatureEd25519, Key: pk[i][:]},
},
SignaturesRequired: 1,
}
txn.SiacoinInputs = append(txn.SiacoinInputs, SiacoinInput{
UnlockConditions: uc,
})
copy(txn.SiacoinInputs[i].ParentID[:], encoding.Marshal(i))
txn.TransactionSignatures = append(txn.TransactionSignatures, TransactionSignature{
CoveredFields: CoveredFields{WholeTransaction: true},
})
copy(txn.TransactionSignatures[i].ParentID[:], encoding.Marshal(i))
}
// Transaction must be constructed before signing
for i := 0; i < numSigs; i++ {
sigHash := txn.SigHash(i)
sig0, err := crypto.SignHash(sigHash, sk[i])
if err != nil {
b.Fatal(err)
}
txn.TransactionSignatures[i].Signature = sig0[:]
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := txn.StandaloneValid(10)
if err != nil {
b.Fatal(err)
}
}
}
示例11: setSiafundPool
// setSiafundPool updates the saved siafund pool on disk
func setSiafundPool(tx *bolt.Tx, c types.Currency) {
bucket := tx.Bucket(SiafundPool)
err := bucket.Put(SiafundPool, encoding.Marshal(c))
if build.DEBUG && err != nil {
panic(err)
}
}
示例12: applyFileContractMaintenance
// applyFileContractMaintenance looks for all of the file contracts that have
// expired without an appropriate storage proof, and calls 'applyMissedProof'
// for the file contract.
func applyFileContractMaintenance(tx *bolt.Tx, pb *processedBlock) {
// Get the bucket pointing to all of the expiring file contracts.
fceBucketID := append(prefixFCEX, encoding.Marshal(pb.Height)...)
fceBucket := tx.Bucket(fceBucketID)
// Finish if there are no expiring file contracts.
if fceBucket == nil {
return
}
var dscods []modules.DelayedSiacoinOutputDiff
var fcds []modules.FileContractDiff
err := fceBucket.ForEach(func(keyBytes, valBytes []byte) error {
var id types.FileContractID
copy(id[:], keyBytes)
amspDSCODS, fcd := applyMissedStorageProof(tx, pb, id)
fcds = append(fcds, fcd)
dscods = append(dscods, amspDSCODS...)
return nil
})
if build.DEBUG && err != nil {
panic(err)
}
for _, dscod := range dscods {
pb.DelayedSiacoinOutputDiffs = append(pb.DelayedSiacoinOutputDiffs, dscod)
commitDelayedSiacoinOutputDiff(tx, dscod, modules.DiffApply)
}
for _, fcd := range fcds {
pb.FileContractDiffs = append(pb.FileContractDiffs, fcd)
commitFileContractDiff(tx, fcd, modules.DiffApply)
}
err = tx.DeleteBucket(fceBucketID)
if build.DEBUG && err != nil {
panic(err)
}
}
示例13: TestLowFeeTransaction
func TestLowFeeTransaction(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// Initialize variables to populate transaction pool
tpt := newTpoolTester("TestLowFeeTransaction", t)
emptyData := make([]byte, 15e3-16)
randData := make([]byte, 16) // not yet random
emptyTxn := types.Transaction{
ArbitraryData: []string{"NonSia" + string(append(emptyData, randData...))},
}
transSize := len(encoding.Marshal(emptyTxn))
// Fill it to 20 MB
for i := 0; i <= (TransactionPoolSizeForFee / transSize); i++ {
// Make a unique transaction to accept
rand.Read(randData)
uniqueTxn := types.Transaction{
ArbitraryData: []string{"NonSia" + string(append(emptyData, randData...))},
}
// Accept said transaction
err := tpt.tpool.AcceptTransaction(uniqueTxn)
if err != nil {
t.Error(err)
}
}
// Should be the straw to break the camel's back (i.e. the transaction at >20 MB)
err := tpt.tpool.AcceptTransaction(emptyTxn)
if err != ErrLowMinerFees {
t.Fatal("expecting ErrLowMinerFees got:", err)
}
}
示例14: createDSCOBucket
// createDSCOBucket creates a bucket for the delayed siacoin outputs at the
// input height.
func createDSCOBucket(tx *bolt.Tx, bh types.BlockHeight) {
bucketID := append(prefixDSCO, encoding.Marshal(bh)...)
_, err := tx.CreateBucket(bucketID)
if build.DEBUG && err != nil {
panic(err)
}
}
示例15: TestFindHostAnnouncements
// TestFindHostAnnouncements probes the findHostAnnouncements function
func TestFindHostAnnouncements(t *testing.T) {
// Create a block with a host announcement.
announcement := modules.PrefixHostAnnouncement + string(encoding.Marshal(modules.HostAnnouncement{}))
b := types.Block{
Transactions: []types.Transaction{
types.Transaction{
ArbitraryData: []string{announcement},
},
},
}
announcements := findHostAnnouncements(b)
if len(announcements) != 1 {
t.Error("host announcement not found in block")
}
// Try with an altered prefix
b.Transactions[0].ArbitraryData[0] = "bad" + b.Transactions[0].ArbitraryData[0]
announcements = findHostAnnouncements(b)
if len(announcements) != 0 {
t.Error("host announcement found when there was an invalid prefix")
}
// Try with an invalid host encoding.
b.Transactions[0].ArbitraryData[0] = modules.PrefixHostAnnouncement + "bad"
announcements = findHostAnnouncements(b)
if len(announcements) != 0 {
t.Error("host announcement found when there was an invalid encoding of a host announcement")
}
}