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 a1 := e.Value.AssetId.Bytes()
31 amount = &e.Value.Amount
32 destPos = &e.WitnessDestination.Position
35 spentOutput := tx.Entries[*e.SpentOutputId].(*bc.Output)
36 a1 := spentOutput.Source.Value.AssetId.Bytes()
38 amount = &spentOutput.Source.Value.Amount
39 destPos = &e.WitnessDestination.Position
40 s := e.SpentOutputId.Bytes()
45 txSigHashFn := func() []byte {
47 hasher := sha3pool.Get256()
48 defer sha3pool.Put256(hasher)
50 entryID.WriteTo(hasher)
55 hashBytes := hash.Bytes()
56 txSigHash = &hashBytes
66 result := &vm.Context{
67 VMVersion: prog.VmVersion,
68 Code: witnessProgram(prog.Code),
71 EntryID: entryID.Bytes(),
73 TxVersion: &tx.Version,
74 BlockHeight: &blockHeight,
76 TxSigHash: txSigHashFn,
77 NumResults: &numResults,
81 SpentOutputID: spentOutputID,
82 CheckOutput: ec.checkOutput,
88 func witnessProgram(prog []byte) []byte {
89 if segwit.IsP2WPKHScript(prog) {
90 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
93 } else if segwit.IsP2WSHScript(prog) {
94 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
101 type entryContext struct {
103 entries map[bc.Hash]bc.Entry
106 func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
107 checkEntry := func(e bc.Entry) (bool, error) {
108 check := func(prog *bc.Program, value *bc.AssetAmount) bool {
109 return (prog.VmVersion == vmVersion &&
110 bytes.Equal(prog.Code, code) &&
111 bytes.Equal(value.AssetId.Bytes(), assetID) &&
112 value.Amount == amount)
115 switch e := e.(type) {
117 return check(e.ControlProgram, e.Source.Value), nil
122 // The spec requires prog.Code to be the empty string only
123 // when !expansion. When expansion is true, we prepopulate
124 // prog.Code to give check() a freebie match.
126 // (The spec always requires prog.VmVersion to be zero.)
129 return check(&prog, e.Source.Value), nil
132 return false, vm.ErrContext
135 checkMux := func(m *bc.Mux) (bool, error) {
136 if index >= uint64(len(m.WitnessDestinations)) {
137 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
139 eID := m.WitnessDestinations[index].Ref
140 e, ok := ec.entries[*eID]
142 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
147 switch e := ec.entry.(type) {
152 d, ok := ec.entries[*e.WitnessDestination.Ref]
154 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for issuance destination %x not found", e.WitnessDestination.Ref.Bytes())
156 if m, ok := d.(*bc.Mux); ok {
160 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
165 d, ok := ec.entries[*e.WitnessDestination.Ref]
167 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
169 if m, ok := d.(*bc.Mux); ok {
173 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
178 return false, vm.ErrContext