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) {
28 case *bc.CrossChainInput:
29 a1 := e.Value.AssetId.Bytes()
31 amount = &e.Value.Amount
32 destPos = &e.WitnessDestination.Position
35 switch spentOutput := tx.Entries[*e.SpentOutputId].(type) {
36 case *bc.IntraChainOutput:
37 a1 := spentOutput.Source.Value.AssetId.Bytes()
39 amount = &spentOutput.Source.Value.Amount
40 destPos = &e.WitnessDestination.Position
41 s := e.SpentOutputId.Bytes()
45 a1 := spentOutput.Source.Value.AssetId.Bytes()
47 amount = &spentOutput.Source.Value.Amount
48 destPos = &e.WitnessDestination.Position
49 s := e.SpentOutputId.Bytes()
55 txSigHashFn := func() []byte {
57 hasher := sha3pool.Get256()
58 defer sha3pool.Put256(hasher)
60 entryID.WriteTo(hasher)
65 hashBytes := hash.Bytes()
66 txSigHash = &hashBytes
76 result := &vm.Context{
77 VMVersion: prog.VmVersion,
78 Code: witnessProgram(prog.Code),
81 EntryID: entryID.Bytes(),
83 TxVersion: &tx.Version,
84 BlockHeight: &blockHeight,
86 TxSigHash: txSigHashFn,
87 NumResults: &numResults,
91 SpentOutputID: spentOutputID,
92 CheckOutput: ec.checkOutput,
98 func witnessProgram(prog []byte) []byte {
99 if segwit.IsP2WPKHScript(prog) {
100 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
103 } else if segwit.IsP2WSHScript(prog) {
104 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
111 type entryContext struct {
113 entries map[bc.Hash]bc.Entry
116 func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
117 checkEntry := func(e bc.Entry) (bool, error) {
118 check := func(prog *bc.Program, value *bc.AssetAmount) bool {
119 return (prog.VmVersion == vmVersion &&
120 bytes.Equal(prog.Code, code) &&
121 bytes.Equal(value.AssetId.Bytes(), assetID) &&
122 value.Amount == amount)
125 switch e := e.(type) {
126 case *bc.IntraChainOutput:
127 return check(e.ControlProgram, e.Source.Value), nil
130 return check(e.ControlProgram, e.Source.Value), nil
135 // The spec requires prog.Code to be the empty string only
136 // when !expansion. When expansion is true, we prepopulate
137 // prog.Code to give check() a freebie match.
139 // (The spec always requires prog.VmVersion to be zero.)
142 return check(&prog, e.Source.Value), nil
145 return false, vm.ErrContext
148 checkMux := func(m *bc.Mux) (bool, error) {
149 if index >= uint64(len(m.WitnessDestinations)) {
150 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
152 eID := m.WitnessDestinations[index].Ref
153 e, ok := ec.entries[*eID]
155 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
160 switch e := ec.entry.(type) {
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