forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_context.go
100 lines (79 loc) · 1.72 KB
/
vm_context.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
package protocol
import (
"errors"
)
type Context struct {
Account
changes []Change
FundsTx
}
type Change struct {
index int
value []byte
}
func NewChange(index int, value []byte) Change {
return Change{index, value}
}
func (c *Change) GetChange() (int, []byte) {
return c.index, c.value
}
func NewContext(account Account, fundsTx FundsTx) *Context {
newContext := Context{
Account: account,
changes: []Change{},
FundsTx: fundsTx,
}
return &newContext
}
func (c *Context) GetContract() []byte {
return c.Contract
}
func (c *Context) GetContractVariable(index int) ([]byte, error) {
if index >= len(c.ContractVariables) || index < 0 {
return []byte{}, errors.New("Index out of bounds")
}
variable := []byte(c.ContractVariables[index])
cp := make([]byte, len(variable))
copy(cp, variable)
return cp, nil
}
func (c *Context) SetContractVariable(index int, value []byte) error {
if len(c.ContractVariables) <= index {
return errors.New("Index out of bounds")
}
cp := make([]byte, len(value))
copy(cp, value)
change := NewChange(index, cp)
c.changes = append(c.changes, change)
return nil
}
func (c *Context) PersistChanges() {
for _, change := range c.changes {
i, value := change.GetChange()
c.ContractVariables[i] = value
}
}
func (c *Context) GetAddress() [64]byte {
return c.Address
}
func (c *Context) GetIssuer() [32]byte {
return c.Issuer
}
func (c *Context) GetBalance() uint64 {
return c.Balance
}
func (c *Context) GetSender() [32]byte {
return c.From
}
func (c *Context) GetAmount() uint64 {
return c.Amount
}
func (c *Context) GetTransactionData() []byte {
return c.Data
}
func (c *Context) GetFee() uint64 {
return c.Fee
}
func (c *Context) GetSig1() [64]byte {
return c.Sig1
}