This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 361
/
vmcontext.go
179 lines (153 loc) · 4.49 KB
/
vmcontext.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package validation
import (
"bytes"
"github.com/bytom/consensus/segwit"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/errors"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/vm"
)
// NewTxVMContext generates the vm.Context for BVM
func NewTxVMContext(vs *validationState, entry bc.Entry, prog *bc.Program, args [][]byte) *vm.Context {
var (
tx = vs.tx
blockHeight = vs.block.BlockHeader.GetHeight()
numResults = uint64(len(tx.ResultIds))
entryID = bc.EntryID(entry) // TODO(bobg): pass this in, don't recompute it
assetID *[]byte
amount *uint64
destPos *uint64
spentOutputID *[]byte
)
switch e := entry.(type) {
case *bc.Issuance:
a1 := e.Value.AssetId.Bytes()
assetID = &a1
amount = &e.Value.Amount
destPos = &e.WitnessDestination.Position
case *bc.Spend:
spentOutput := tx.Entries[*e.SpentOutputId].(*bc.Output)
a1 := spentOutput.Source.Value.AssetId.Bytes()
assetID = &a1
amount = &spentOutput.Source.Value.Amount
destPos = &e.WitnessDestination.Position
s := e.SpentOutputId.Bytes()
spentOutputID = &s
}
var txSigHash *[]byte
txSigHashFn := func() []byte {
if txSigHash == nil {
hasher := sha3pool.Get256()
defer sha3pool.Put256(hasher)
entryID.WriteTo(hasher)
tx.ID.WriteTo(hasher)
var hash bc.Hash
hash.ReadFrom(hasher)
hashBytes := hash.Bytes()
txSigHash = &hashBytes
}
return *txSigHash
}
ec := &entryContext{
entry: entry,
entries: tx.Entries,
}
result := &vm.Context{
VMVersion: prog.VmVersion,
Code: witnessProgram(prog.Code),
Arguments: args,
EntryID: entryID.Bytes(),
TxVersion: &tx.Version,
BlockHeight: &blockHeight,
TxSigHash: txSigHashFn,
NumResults: &numResults,
AssetID: assetID,
Amount: amount,
DestPos: destPos,
SpentOutputID: spentOutputID,
CheckOutput: ec.checkOutput,
}
return result
}
func witnessProgram(prog []byte) []byte {
if segwit.IsP2WPKHScript(prog) {
if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
return witnessProg
}
} else if segwit.IsP2WSHScript(prog) {
if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
return witnessProg
}
}
return prog
}
type entryContext struct {
entry bc.Entry
entries map[bc.Hash]bc.Entry
}
func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
checkEntry := func(e bc.Entry) (bool, error) {
check := func(prog *bc.Program, value *bc.AssetAmount) bool {
return (prog.VmVersion == vmVersion &&
bytes.Equal(prog.Code, code) &&
bytes.Equal(value.AssetId.Bytes(), assetID) &&
value.Amount == amount)
}
switch e := e.(type) {
case *bc.Output:
return check(e.ControlProgram, e.Source.Value), nil
case *bc.Retirement:
var prog bc.Program
if expansion {
// The spec requires prog.Code to be the empty string only
// when !expansion. When expansion is true, we prepopulate
// prog.Code to give check() a freebie match.
//
// (The spec always requires prog.VmVersion to be zero.)
prog.Code = code
}
return check(&prog, e.Source.Value), nil
}
return false, vm.ErrContext
}
checkMux := func(m *bc.Mux) (bool, error) {
if index >= uint64(len(m.WitnessDestinations)) {
return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
}
eID := m.WitnessDestinations[index].Ref
e, ok := ec.entries[*eID]
if !ok {
return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
}
return checkEntry(e)
}
switch e := ec.entry.(type) {
case *bc.Mux:
return checkMux(e)
case *bc.Issuance:
d, ok := ec.entries[*e.WitnessDestination.Ref]
if !ok {
return false, errors.Wrapf(bc.ErrMissingEntry, "entry for issuance destination %x not found", e.WitnessDestination.Ref.Bytes())
}
if m, ok := d.(*bc.Mux); ok {
return checkMux(m)
}
if index != 0 {
return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
}
return checkEntry(d)
case *bc.Spend:
d, ok := ec.entries[*e.WitnessDestination.Ref]
if !ok {
return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
}
if m, ok := d.(*bc.Mux); ok {
return checkMux(m)
}
if index != 0 {
return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
}
return checkEntry(d)
}
return false, vm.ErrContext
}