当前位置: 首页>>代码示例>>Golang>>正文


Golang Transaction.UnmarshalBinary方法代码示例

本文整理汇总了Golang中github.com/FactomProject/factoid.Transaction.UnmarshalBinary方法的典型用法代码示例。如果您正苦于以下问题:Golang Transaction.UnmarshalBinary方法的具体用法?Golang Transaction.UnmarshalBinary怎么用?Golang Transaction.UnmarshalBinary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/FactomProject/factoid.Transaction的用法示例。


在下文中一共展示了Transaction.UnmarshalBinary方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: LoadState

// This is the Baby of Asset tracking!  We get the current height of our block
// and we go through each entry, unmarshalling it and building a "block".  We
// then process that block.
//
// We process transactions in our chain, and maintain a balance of our assets.
//
func (fs *AssetState) LoadState() error {

	// First we need to build our list of blocks going back.
	var blklist []factom.EBlock

	// Get me a chain head...
	blkmr, err := factom.GetChainHead(hex.EncodeToString(fs.ChainID()))
	if err != nil {
		return err
	}
	blk, err := factom.GetEBlock(blkmr.ChainHead)

	for {
		if blk.Header.BlockSequenceNumber < fs.nextblockheight {
			break
		}
		blklist = append(blklist, *blk)
		if blk.Header.BlockSequenceNumber == 0 {
			break
		}
		nblk, err := factom.GetEBlock(blk.Header.PrevKeyMR)
		if err != nil {
			fmt.Println("Error Reading Entry blocks")
			time.Sleep(time.Second)
			continue
		}
		blk = nblk
	}

	// Now process blocks forward

	for i := len(blklist) - 1; i >= 0; i-- {
		for _, entry := range blklist[i].EntryList {
			transEntry, err := factom.GetEntry(entry.EntryHash)
			t := new(fct.Transaction)
			transdata, err := hex.DecodeString(transEntry.Content)
			if err != nil {
				continue
			} // Ignore bad entries.
			err = t.UnmarshalBinary(transdata)
			if err != nil {
				continue
			} // Ignore bad entries.
			fs.AddTransaction(t)
		}
	}

	return nil
}
开发者ID:msgilligan,项目名称:Testing,代码行数:55,代码来源:AssetState.go

示例2: Execute

// New Transaction:  key --
// We create a new transaction, and track it with the user supplied key.  The
// user can then use this key to make subsequent calls to add inputs, outputs,
// and to sign. Then they can submit the transaction.
//
// When the transaction is submitted, we clear it from our working memory.
// Multiple transactions can be under construction at one time, but they need
// their own keys. Once a transaction is either submitted or deleted, the key
// can be reused.
func (Import) Execute(state IState, args []string) error {

	if len(args) != 3 {
		return fmt.Errorf("Invalid Parameters")
	}
	key := args[1]
	filename := args[2]

	if _, err := os.Stat(filename); err != nil {
		return fmt.Errorf("Could not find the input file %s", filename)
	}

	{ // Doing a bit of variable scope management here, since I want t later.
		t := state.GetFS().GetDB().GetRaw([]byte(fct.DB_BUILD_TRANS), []byte(key))
		if t != nil {
			return fmt.Errorf("That transaction already exists.  Specify a new one, or delete this one.")
		}
	}

	data, err := ioutil.ReadFile(filename)
	var hexdata []byte
	for _, b := range data {
		if b > 32 {
			hexdata = append(hexdata, b)
		}
	}

	bdata, err := hex.DecodeString(string(hexdata))
	if err != nil {
		return err
	}

	t := new(fct.Transaction)
	err = t.UnmarshalBinary(bdata)
	if err != nil {
		return err
	}

	state.GetFS().GetDB().PutRaw([]byte(fct.DB_BUILD_TRANS), []byte(key), t)

	fmt.Println("Transaction", filename, "has been imported")
	return nil
}
开发者ID:FactomProject,项目名称:walletapp,代码行数:52,代码来源:import.go

示例3: Test_create_genesis_FactoidState


//.........这里部分代码省略.........
						len(tx.GetInputs()), len(tx.GetOutputs()), len(tx.GetECOutputs())), // Msg
					0) // Expire
			}
			if len(m) < min {
				min = len(m)
				cp.CP.AddUpdate(
					"min transaction size", // tag
					"info",                 // Category
					fmt.Sprintf("Min Transaction Size %d", min), // Title
					fmt.Sprintf("<pre>#inputs = %-3d  #outputs = %-3d  #ecoutputs = %-3d<pre>",
						len(tx.GetInputs()), len(tx.GetOutputs()), len(tx.GetECOutputs())), // Msg
					0) // Expire
			}

			k := rand.Int() % (len(m) - 2)
			k++
			good := true
			flip = rand.Int() % 100
			// To simulate bad data, I mess up some of the data here.
			if rand.Int()%100 < 5 { // Mess up 5 percent of the transactions
				good = false
				if flip < 49 { // Flip a coin
					m = m[k:]
					fs.stats.errors["lost start of trans"] += 1
					fs.stats.full["lost start of trans"] = "lost start of trans"
				} else {
					m = m[:k]
					fs.stats.errors["lost end of trans"] += 1
					fs.stats.full["lost end of trans"] = "lost end of trans"
				}
			}

			t := new(fct.Transaction)
			err = t.UnmarshalBinary(m)

			if good && tx.IsEqual(t) != nil {
				fmt.Println("Fail valid Unmarshal")
				test.Fail()
				return
			}
			if err == nil {
				if good && err != nil {
					fmt.Println("Added a transaction that should have failed to be added")
					fmt.Println(err)
					test.Fail()
					return
				}
				if !good {
					fmt.Println("Failed to add a transaction that should have added")
					test.Fail()
					return
				}
			}

			if good {
				err = fs.AddTransaction(j+1, t)
			}
			if !addtest && err == nil {
				ts := int64(t.GetMilliTimestamp())
				bts := int64(fs.GetCurrentBlock().GetCoinbaseTimestamp())
				fmt.Println("timestamp failure ", ts, bts, ts-bts, fct.TRANSACTION_POST_LIMIT)
				test.Fail()
				return
			}
			if !addtest && err == nil {
				fmt.Println("failed to catch error")
开发者ID:FactomProject,项目名称:Testing,代码行数:67,代码来源:block_test.go

示例4: Test_create_genesis_FactoidState


//.........这里部分代码省略.........
					len(tx.GetOutputs()), "outputs and",
					len(tx.GetECOutputs()), "ecoutputs                       ")
				fmt.Print("\033[41;0H")
			}
			if len(m) < min {
				fmt.Print("\033[34;0H")
				min = len(m)
				fmt.Println("Min Transaction", cnt, "is", len(m), "Bytes long. ",
					len(tx.GetInputs()), "inputs and",
					len(tx.GetOutputs()), "outputs and",
					len(tx.GetECOutputs()), "ecoutputs                       ")
				fmt.Print("\033[41;0H")
			}

			k := rand.Int() % (len(m) - 2)
			k++
			good := true
			flip = rand.Int() % 100
			// To simulate bad data, I mess up some of the data here.
			if rand.Int()%100 < 5 { // Mess up 5 percent of the transactions
				good = false
				if flip < 49 { // Flip a coin
					m = m[k:]
					fs.stats.errors["lost start of trans"] += 1
					fs.stats.full["lost start of trans"] = "lost start of trans"
				} else {
					m = m[:k]
					fs.stats.errors["lost end of trans"] += 1
					fs.stats.full["lost end of trans"] = "lost end of trans"
				}
			}

			t := new(fct.Transaction)
			err = t.UnmarshalBinary(m)

			if good && tx.IsEqual(t) != nil {
				fmt.Println("\n\n\n\n\n\nFail valid Unmarshal")
				test.Fail()
				return
			}
			if err == nil {
				if good && err != nil {
					fmt.Println("\n\n\n\n\n\n\nAdded a transaction that should have failed to be added")
					fmt.Println(err)
					test.Fail()
					return
				}
				if !good {
					fmt.Println("\n\n\n\n\n\n\nFailed to add a transaction that should have added")
					test.Fail()
					return
				}
			}

			if good {
				err = fs.AddTransaction(t)
			}
			if !addtest && err == nil {
				ts := int64(t.GetMilliTimestamp())
				bts := int64(fs.GetCurrentBlock().GetCoinbaseTimestamp())
				fmt.Println("\n\n\n\n\n\n\ntimestamp failure ", ts, bts, ts-bts, fct.TRANSACTION_POST_LIMIT)
				test.Fail()
				return
			}
			if !addtest && err == nil {
				fmt.Println("\n\n\n\n\n\n\nfailed to catch error")
开发者ID:msgilligan,项目名称:Testing,代码行数:67,代码来源:block_test.go


注:本文中的github.com/FactomProject/factoid.Transaction.UnmarshalBinary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。