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()
38 a1 := spentOutput.Source.Value.AssetId.Bytes()
40 amount = &spentOutput.Source.Value.Amount
41 destPos = &e.WitnessDestination.Position
42 s := e.SpentOutputId.Bytes()
48 txSigHashFn := func() []byte {
50 hasher := sha3pool.Get256()
51 defer sha3pool.Put256(hasher)
53 entryID.WriteTo(hasher)
58 hashBytes := hash.Bytes()
59 txSigHash = &hashBytes
69 result := &vm.Context{
70 VMVersion: prog.VmVersion,
71 Code: witnessProgram(prog.Code),
74 EntryID: entryID.Bytes(),
76 TxVersion: &tx.Version,
77 BlockHeight: &blockHeight,
79 TxSigHash: txSigHashFn,
80 NumResults: &numResults,
84 SpentOutputID: spentOutputID,
85 CheckOutput: ec.checkOutput,
91 func witnessProgram(prog []byte) []byte {
92 if segwit.IsP2WPKHScript(prog) {
93 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
96 } else if segwit.IsP2WSHScript(prog) {
97 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
104 type entryContext struct {
106 entries map[bc.Hash]bc.Entry
109 func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
110 checkEntry := func(e bc.Entry) (bool, error) {
111 check := func(prog *bc.Program, value *bc.AssetAmount) bool {
112 return (prog.VmVersion == vmVersion &&
113 bytes.Equal(prog.Code, code) &&
114 bytes.Equal(value.AssetId.Bytes(), assetID) &&
115 value.Amount == amount)
118 switch e := e.(type) {
119 case *bc.IntraChainOutput:
120 return check(e.ControlProgram, e.Source.Value), nil
123 return check(e.ControlProgram, e.Source.Value), nil
128 // The spec requires prog.Code to be the empty string only
129 // when !expansion. When expansion is true, we prepopulate
130 // prog.Code to give check() a freebie match.
132 // (The spec always requires prog.VmVersion to be zero.)
135 return check(&prog, e.Source.Value), nil
138 return false, vm.ErrContext
141 checkMux := func(m *bc.Mux) (bool, error) {
142 if index >= uint64(len(m.WitnessDestinations)) {
143 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
145 eID := m.WitnessDestinations[index].Ref
146 e, ok := ec.entries[*eID]
148 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
153 switch e := ec.entry.(type) {
158 d, ok := ec.entries[*e.WitnessDestination.Ref]
160 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
162 if m, ok := d.(*bc.Mux); ok {
166 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
171 return false, vm.ErrContext