本文整理匯總了Golang中github.com/NebulousLabs/Sia/encoding.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unmarshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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,
}
}
示例2: 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
}
示例3: dbGetTransactionIDSet
// dbGetTransactionIDSet returns a 'func(*bolt.Tx) error' that decodes a
// bucket of transaction IDs into a slice. If the bucket is nil,
// dbGetTransactionIDSet returns errNotExist.
func dbGetTransactionIDSet(bucket []byte, key interface{}, ids *[]types.TransactionID) func(*bolt.Tx) error {
return func(tx *bolt.Tx) error {
b := tx.Bucket(bucket).Bucket(encoding.Marshal(key))
if b == nil {
return errNotExist
}
// decode into a local slice
var txids []types.TransactionID
err := b.ForEach(func(txid, _ []byte) error {
var id types.TransactionID
err := encoding.Unmarshal(txid, &id)
if err != nil {
return err
}
txids = append(txids, id)
return nil
})
if err != nil {
return err
}
// set pointer
*ids = txids
return nil
}
}
示例4: findHostAnnouncements
// findHostAnnouncements returns a list of the host announcements found within
// a given block. No check is made to see that the ip address found in the
// announcement is actually a valid ip address.
func findHostAnnouncements(b types.Block) (announcements []modules.HostSettings) {
for _, t := range b.Transactions {
// the HostAnnouncement must be prefaced by the standard host
// announcement string
var prefix types.Specifier
for _, arb := range t.ArbitraryData {
copy(prefix[:], arb)
if prefix != modules.PrefixHostAnnouncement {
continue
}
// decode the HostAnnouncement
var ha modules.HostAnnouncement
err := encoding.Unmarshal(arb[types.SpecifierLen:], &ha)
if err != nil {
continue
}
// Add the announcement to the slice being returned.
announcements = append(announcements, modules.HostSettings{
NetAddress: ha.IPAddress,
})
}
}
return
}
示例5: applySiacoinInputs
// applySiacoinInputs takes all of the siacoin inputs in a transaction and
// applies them to the state, updating the diffs in the processed block.
func (cs *ConsensusSet) applySiacoinInputs(tx *bolt.Tx, pb *processedBlock, t types.Transaction) error {
// Remove all siacoin inputs from the unspent siacoin outputs list.
scoBucket := tx.Bucket(SiacoinOutputs)
for _, sci := range t.SiacoinInputs {
scoBytes := scoBucket.Get(sci.ParentID[:])
if build.DEBUG && scoBytes == nil {
panic(ErrMisuseApplySiacoinInput)
}
var sco types.SiacoinOutput
err := encoding.Unmarshal(scoBytes, &sco)
if err != nil {
return err
}
scod := modules.SiacoinOutputDiff{
Direction: modules.DiffRevert,
ID: sci.ParentID,
SiacoinOutput: sco,
}
pb.SiacoinOutputDiffs = append(pb.SiacoinOutputDiffs, scod)
err = cs.commitBucketSiacoinOutputDiff(scoBucket, scod, modules.DiffApply)
if err != nil {
return err
}
}
return nil
}
示例6: forEachDSCO
func forEachDSCO(tx *bolt.Tx, bh types.BlockHeight, fn func(id types.SiacoinOutputID, sco types.SiacoinOutput) error) error {
bucketID := append(prefix_dsco, encoding.Marshal(bh)...)
return forEach(tx, bucketID, func(kb, vb []byte) error {
var key types.SiacoinOutputID
var value types.SiacoinOutput
err := encoding.Unmarshal(kb, &key)
if err != nil {
return err
}
err = encoding.Unmarshal(vb, &value)
if err != nil {
return err
}
return fn(key, value)
})
}
示例7: forEachSiacoinOutputs
// forEachSiacoinOutputs applies a function to every siacoin output and ID
func (db *setDB) forEachSiacoinOutputs(fn func(k types.SiacoinOutputID, v types.SiacoinOutput)) {
db.forEachItem(SiacoinOutputs, func(kb, vb []byte) error {
var key types.SiacoinOutputID
var value types.SiacoinOutput
err := encoding.Unmarshal(kb, &key)
if err != nil {
return err
}
err = encoding.Unmarshal(vb, &value)
if err != nil {
return err
}
fn(key, value)
return nil
})
}
示例8: TestAnnouncement
// TestAnnouncement has a host announce itself to the blockchain and then
// checks that the announcement makes it correctly.
func TestAnnouncement(t *testing.T) {
ht := CreateHostTester("TestAnnouncement", t)
// Place the announcement.
err := ht.host.ForceAnnounce()
if err != nil {
t.Fatal(err)
}
ht.tpUpdateWait()
// Check that the announcement made it into the transaction pool correctly.
txns := ht.tpool.TransactionSet()
if len(txns) != 1 {
t.Error("Expecting 1 transaction in transaction pool, instead there was", len(txns))
}
encodedAnnouncement := txns[0].ArbitraryData[0][types.SpecifierLen:]
var ha modules.HostAnnouncement
err = encoding.Unmarshal([]byte(encodedAnnouncement), &ha)
if err != nil {
t.Error(err)
}
// TODO: Need to check that the host announcement gets the host into the
// hostdb.
}
示例9: validSiacoins
// validSiacoins checks that the siacoin inputs and outputs are valid in the
// context of the current consensus set.
func validSiacoins(tx *bolt.Tx, t types.Transaction) error {
scoBucket := tx.Bucket(SiacoinOutputs)
var inputSum types.Currency
for _, sci := range t.SiacoinInputs {
// Check that the input spends an existing output.
scoBytes := scoBucket.Get(sci.ParentID[:])
if scoBytes == nil {
return errMissingSiacoinOutput
}
// Check that the unlock conditions match the required unlock hash.
var sco types.SiacoinOutput
err := encoding.Unmarshal(scoBytes, &sco)
if build.DEBUG && err != nil {
panic(err)
}
if sci.UnlockConditions.UnlockHash() != sco.UnlockHash {
return errWrongUnlockConditions
}
inputSum = inputSum.Add(sco.Value)
}
if inputSum.Cmp(t.SiacoinOutputSum()) != 0 {
return errSiacoinInputOutputMismatch
}
return nil
}
示例10: GetHashInfo
// GetHashInfo returns sufficient data about the hash that was
// provided to do more extensive lookups
func (e *Explorer) GetHashInfo(hash []byte) (interface{}, error) {
if len(hash) < crypto.HashSize {
return nil, errors.New("requested hash not long enough")
}
lockID := e.mu.RLock()
defer e.mu.RUnlock(lockID)
// Perform a lookup to tell which type of hash it is
typeBytes, err := e.db.GetFromBucket("Hashes", hash[:crypto.HashSize])
if err != nil {
return nil, err
}
var hashType int
err = encoding.Unmarshal(typeBytes, &hashType)
if err != nil {
return nil, err
}
switch hashType {
case hashBlock:
var id types.BlockID
copy(id[:], hash[:])
return e.db.getBlock(types.BlockID(id))
case hashTransaction:
var id crypto.Hash
copy(id[:], hash[:])
return e.db.getTransaction(id)
case hashFilecontract:
var id types.FileContractID
copy(id[:], hash[:])
return e.db.getFileContract(id)
case hashCoinOutputID:
var id types.SiacoinOutputID
copy(id[:], hash[:])
return e.db.getSiacoinOutput(id)
case hashFundOutputID:
var id types.SiafundOutputID
copy(id[:], hash[:])
return e.db.getSiafundOutput(id)
case hashUnlockHash:
// Check that the address is valid before doing a lookup
if len(hash) != crypto.HashSize+types.UnlockHashChecksumSize {
return nil, errors.New("address does not have a valid checksum")
}
var id types.UnlockHash
copy(id[:], hash[:crypto.HashSize])
uhChecksum := crypto.HashObject(id)
givenChecksum := hash[crypto.HashSize : crypto.HashSize+types.UnlockHashChecksumSize]
if !bytes.Equal(givenChecksum, uhChecksum[:types.UnlockHashChecksumSize]) {
return nil, errors.New("address does not have a valid checksum")
}
return e.db.getAddressTransactions(id)
default:
return nil, errors.New("bad hash type")
}
}
示例11: findHostAnnouncements
// findHostAnnouncements returns a list of the host announcements found within
// a given block. No check is made to see that the ip address found in the
// announcement is actually a valid ip address.
func findHostAnnouncements(b types.Block) (announcements []modules.HostSettings) {
for _, t := range b.Transactions {
for _, data := range t.ArbitraryData {
// the HostAnnouncement must be prefaced by the standard host announcement string
if !strings.HasPrefix(data, modules.PrefixHostAnnouncement) {
continue
}
// decode the HostAnnouncement
var ha modules.HostAnnouncement
encAnnouncement := []byte(strings.TrimPrefix(data, modules.PrefixHostAnnouncement))
err := encoding.Unmarshal(encAnnouncement, &ha)
if err != nil {
continue
}
// Add the announcement to the slice being returned.
announcements = append(announcements, modules.HostSettings{
IPAddress: ha.IPAddress,
})
}
}
return
}
示例12: EarliestChildTimestamp
// EarliestChildTimestamp returns the earliest timestamp that the next block can
// have in order for it to be considered valid.
func (cs *ConsensusSet) EarliestChildTimestamp(bid types.BlockID) (timestamp types.Timestamp, exists bool) {
// Lock is not needed because the values being read will not change once
// they have been created.
err := cs.db.View(func(tx *bolt.Tx) error {
// Check that the parent exists.
blockMap := tx.Bucket(BlockMap)
// The identifier for the BlockMap is the sia encoding of the parent
// id. The sia encoding is the same as ParentID[:].
var parent processedBlock
parentBytes := blockMap.Get(bid[:])
if parentBytes == nil {
return ErrOrphan
}
err := encoding.Unmarshal(parentBytes, &parent)
if err != nil {
return err
}
timestamp = earliestChildTimestamp(blockMap, &parent)
return nil
})
if err != nil {
return 0, false
}
return timestamp, true
}
示例13: forEachFileContracts
// forEachFileContracts applies a function to each (file contract id, filecontract)
// pair in the consensus set
func (db *setDB) forEachFileContracts(fn func(k types.FileContractID, v types.FileContract)) {
db.forEachItem(FileContracts, func(kb, vb []byte) error {
var key types.FileContractID
var value types.FileContract
err := encoding.Unmarshal(kb, &key)
if err != nil {
return err
}
err = encoding.Unmarshal(vb, &value)
if err != nil {
return err
}
fn(key, value)
return nil
})
}
示例14: initUnseededKeys
// initUnseededKeys loads all of the unseeded keys into the wallet after the
// wallet gets unlocked.
func (w *Wallet) initUnseededKeys(masterKey crypto.TwofishKey) error {
for _, uk := range w.persist.UnseededKeys {
// Verify that the decryption key is correct.
encKey := uidEncryptionKey(masterKey, uk.UID)
expectedDecryptedVerification := make([]byte, crypto.EntropySize)
decryptedVerification, err := encKey.DecryptBytes(uk.EncryptionVerification)
if err != nil {
return err
}
if !bytes.Equal(expectedDecryptedVerification, decryptedVerification) {
return modules.ErrBadEncryptionKey
}
// Decrypt the spendable key and add it to the wallet.
encodedKey, err := encKey.DecryptBytes(uk.SpendableKey)
if err != nil {
return err
}
var sk spendableKey
err = encoding.Unmarshal(encodedKey, &sk)
if err != nil {
return err
}
w.keys[sk.UnlockConditions.UnlockHash()] = sk
}
return nil
}
示例15: dbBlockSummaries
// Returns an array of block summaries. Bounds checking should be done elsewhere
func (db *explorerDB) dbBlockSummaries(start types.BlockHeight, finish types.BlockHeight) ([]modules.ExplorerBlockData, error) {
summaries := make([]modules.ExplorerBlockData, int(finish-start))
err := db.View(func(tx *bolt.Tx) error {
heights := tx.Bucket([]byte("Heights"))
// Iterate over each block height, constructing a
// summary data structure for each block
for i := start; i < finish; i++ {
bSummaryBytes := heights.Get(encoding.Marshal(i))
if bSummaryBytes == nil {
return errors.New("Block not found in height bucket")
}
err := encoding.Unmarshal(bSummaryBytes, &summaries[i-start])
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
return summaries, nil
}