6 "github.com/bytom/consensus/segwit"
7 "github.com/bytom/crypto/sha3pool"
8 "github.com/bytom/errors"
9 "github.com/bytom/protocol/bc"
10 "github.com/bytom/protocol/vm"
13 // NewTxVMContext generates the vm.Context for BVM
14 func NewTxVMContext(vs *validationState, entry bc.Entry, prog *bc.Program, args [][]byte) *vm.Context {
17 blockHeight = vs.block.BlockHeader.GetHeight()
18 numResults = uint64(len(tx.ResultIds))
19 entryID = bc.EntryID(entry) // TODO(bobg): pass this in, don't recompute it
29 switch e := entry.(type) {
31 anchored := tx.Entries[*e.WitnessAnchoredId]
32 if iss, ok := anchored.(*bc.Issuance); ok {
33 a1 := iss.Value.AssetId.Bytes()
35 amount = &iss.Value.Amount
39 a1 := e.Value.AssetId.Bytes()
41 amount = &e.Value.Amount
42 destPos = &e.WitnessDestination.Position
45 a2 := e.AnchorId.Bytes()
49 spentOutput := tx.Entries[*e.SpentOutputId].(*bc.Output)
50 a1 := spentOutput.Source.Value.AssetId.Bytes()
52 amount = &spentOutput.Source.Value.Amount
53 destPos = &e.WitnessDestination.Position
56 s := e.SpentOutputId.Bytes()
69 txSigHashFn := func() []byte {
71 hasher := sha3pool.Get256()
72 defer sha3pool.Put256(hasher)
74 entryID.WriteTo(hasher)
79 hashBytes := hash.Bytes()
80 txSigHash = &hashBytes
90 result := &vm.Context{
91 VMVersion: prog.VmVersion,
92 Code: witnessProgram(prog.Code),
95 EntryID: entryID.Bytes(),
97 TxVersion: &tx.Version,
98 BlockHeight: &blockHeight,
100 TxSigHash: txSigHashFn,
101 NumResults: &numResults,
104 EntryData: entryData,
107 SpentOutputID: spentOutputID,
108 CheckOutput: ec.checkOutput,
114 func witnessProgram(prog []byte) []byte {
115 if segwit.IsP2WPKHScript(prog) {
116 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
119 } else if segwit.IsP2WSHScript(prog) {
120 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
127 type entryContext struct {
129 entries map[bc.Hash]bc.Entry
132 func (ec *entryContext) checkOutput(index uint64, data []byte, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
133 checkEntry := func(e bc.Entry) (bool, error) {
134 check := func(prog *bc.Program, value *bc.AssetAmount, dataHash *bc.Hash) bool {
135 return (prog.VmVersion == vmVersion &&
136 bytes.Equal(prog.Code, code) &&
137 bytes.Equal(value.AssetId.Bytes(), assetID) &&
138 value.Amount == amount &&
139 (len(data) == 0 || bytes.Equal(dataHash.Bytes(), data)))
142 switch e := e.(type) {
144 return check(e.ControlProgram, e.Source.Value, e.Data), nil
149 // The spec requires prog.Code to be the empty string only
150 // when !expansion. When expansion is true, we prepopulate
151 // prog.Code to give check() a freebie match.
153 // (The spec always requires prog.VmVersion to be zero.)
156 return check(&prog, e.Source.Value, e.Data), nil
159 return false, vm.ErrContext
162 checkMux := func(m *bc.Mux) (bool, error) {
163 if index >= uint64(len(m.WitnessDestinations)) {
164 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
166 eID := m.WitnessDestinations[index].Ref
167 e, ok := ec.entries[*eID]
169 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
174 switch e := ec.entry.(type) {
179 d, ok := ec.entries[*e.WitnessDestination.Ref]
181 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for issuance destination %x not found", e.WitnessDestination.Ref.Bytes())
183 if m, ok := d.(*bc.Mux); ok {
187 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
192 d, ok := ec.entries[*e.WitnessDestination.Ref]
194 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
196 if m, ok := d.(*bc.Mux); ok {
200 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
205 return false, vm.ErrContext