本文整理匯總了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
}
示例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
}
示例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")
示例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")