-
Notifications
You must be signed in to change notification settings - Fork 8
/
eval.go
104 lines (89 loc) · 2.6 KB
/
eval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package dryrun
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"strings"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/protocol"
)
//go:generate sh ./bundle_sampletxn_json.sh
// Run bytecode using transaction data from txnFile file
func Run(bytecode []byte, txnFile string, sb *strings.Builder) (int, bool, error) {
txn, err := loadTxn(txnFile)
if err != nil {
return 0, false, err
}
stxn := transactions.SignedTxn{}
stxn.Txn = txn
proto := config.Consensus[protocol.ConsensusCurrentVersion]
ep := logic.EvalParams{Txn: &stxn, Proto: &proto}
cost, err := logic.Check(bytecode, ep)
if err != nil {
return 0, false, err
}
txgroup := make([]transactions.SignedTxn, 1)
txgroup[0] = stxn
ep = logic.EvalParams{
Txn: &stxn,
Proto: &proto,
Trace: sb,
TxnGroup: txgroup,
GroupIndex: 1,
}
pass, err := logic.Eval(bytecode, ep)
return cost, pass, err
}
func loadTxn(txnFile string) (txn transactions.Transaction, err error) {
var txnData []byte
if txnFile != "" {
txnData, err = ioutil.ReadFile(txnFile)
if err != nil {
return
}
} else {
txnData = sampleTxnData
}
var sampleTxn txnDesc
err = json.Unmarshal(txnData, &sampleTxn)
if err != nil {
return
}
txn.Type = protocol.TxType(sampleTxn.Type)
if txn.Sender, err = basics.UnmarshalChecksumAddress(sampleTxn.Sender); err != nil {
return
}
txn.Fee = basics.MicroAlgos{sampleTxn.Fee}
txn.FirstValid = basics.Round(sampleTxn.FirstValid)
txn.LastValid = basics.Round(sampleTxn.LastValid)
if txn.Note, err = base64.StdEncoding.DecodeString(sampleTxn.Note); err != nil {
return
}
var lease []byte
if lease, err = base64.StdEncoding.DecodeString(sampleTxn.Lease); err != nil {
return
}
copy(txn.Lease[:], lease)
txn.Amount = basics.MicroAlgos{sampleTxn.Amount}
if txn.Receiver, err = basics.UnmarshalChecksumAddress(sampleTxn.Receiver); err != nil {
return
}
if txn.CloseRemainderTo, err = basics.UnmarshalChecksumAddress(sampleTxn.CloseRemainderTo); err != nil {
return
}
txn.XferAsset = basics.AssetIndex(sampleTxn.XferAsset)
txn.AssetAmount = sampleTxn.AssetAmount
if txn.AssetSender, err = basics.UnmarshalChecksumAddress(sampleTxn.AssetSender); err != nil {
return
}
if txn.AssetReceiver, err = basics.UnmarshalChecksumAddress(sampleTxn.AssetReceiver); err != nil {
return
}
if txn.AssetCloseTo, err = basics.UnmarshalChecksumAddress(sampleTxn.AssetCloseTo); err != nil {
return
}
return txn, nil
}