6 "github.com/vapor/consensus/segwit"
7 "github.com/vapor/crypto/sha3pool"
8 "github.com/vapor/errors"
9 "github.com/vapor/protocol/bc"
10 "github.com/vapor/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
27 switch e := entry.(type) {
29 switch spentOutput := tx.Entries[*e.SpentOutputId].(type) {
30 case *bc.IntraChainOutput:
31 a1 := spentOutput.Source.Value.AssetId.Bytes()
33 amount = &spentOutput.Source.Value.Amount
34 destPos = &e.WitnessDestination.Position
35 s := e.SpentOutputId.Bytes()
39 a1 := spentOutput.Source.Value.AssetId.Bytes()
41 amount = &spentOutput.Source.Value.Amount
42 destPos = &e.WitnessDestination.Position
43 s := e.SpentOutputId.Bytes()
49 txSigHashFn := func() []byte {
51 hasher := sha3pool.Get256()
52 defer sha3pool.Put256(hasher)
54 entryID.WriteTo(hasher)
59 hashBytes := hash.Bytes()
60 txSigHash = &hashBytes
70 result := &vm.Context{
71 VMVersion: prog.VmVersion,
72 Code: witnessProgram(prog.Code),
75 EntryID: entryID.Bytes(),
77 TxVersion: &tx.Version,
78 BlockHeight: &blockHeight,
80 TxSigHash: txSigHashFn,
81 NumResults: &numResults,
85 SpentOutputID: spentOutputID,
86 CheckOutput: ec.checkOutput,
92 func witnessProgram(prog []byte) []byte {
93 if segwit.IsP2WPKHScript(prog) {
94 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
97 } else if segwit.IsP2WSHScript(prog) {
98 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
105 type entryContext struct {
107 entries map[bc.Hash]bc.Entry
110 func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
111 checkEntry := func(e bc.Entry) (bool, error) {
112 check := func(prog *bc.Program, value *bc.AssetAmount) bool {
113 return (prog.VmVersion == vmVersion &&
114 bytes.Equal(prog.Code, code) &&
115 bytes.Equal(value.AssetId.Bytes(), assetID) &&
116 value.Amount == amount)
119 switch e := e.(type) {
120 case *bc.IntraChainOutput:
121 return check(e.ControlProgram, e.Source.Value), nil
124 return check(e.ControlProgram, e.Source.Value), nil
129 // The spec requires prog.Code to be the empty string only
130 // when !expansion. When expansion is true, we prepopulate
131 // prog.Code to give check() a freebie match.
133 // (The spec always requires prog.VmVersion to be zero.)
136 return check(&prog, e.Source.Value), nil
139 return false, vm.ErrContext
142 checkMux := func(m *bc.Mux) (bool, error) {
143 if index >= uint64(len(m.WitnessDestinations)) {
144 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
146 eID := m.WitnessDestinations[index].Ref
147 e, ok := ec.entries[*eID]
149 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
154 switch e := ec.entry.(type) {
159 d, ok := ec.entries[*e.WitnessDestination.Ref]
161 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
163 if m, ok := d.(*bc.Mux); ok {
167 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
172 return false, vm.ErrContext