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 mainchainOutput := tx.Entries[*e.MainchainOutputId].(*bc.IntraChainOutput)
30 a1 := mainchainOutput.Source.Value.AssetId.Bytes()
32 amount = &mainchainOutput.Source.Value.Amount
33 destPos = &e.WitnessDestination.Position
34 s := e.MainchainOutputId.Bytes()
38 spentOutput := tx.Entries[*e.SpentOutputId].(*bc.IntraChainOutput)
39 a1 := spentOutput.Source.Value.AssetId.Bytes()
41 amount = &spentOutput.Source.Value.Amount
42 destPos = &e.WitnessDestination.Position
43 s := e.SpentOutputId.Bytes()
47 voteOutput := tx.Entries[*e.SpentOutputId].(*bc.VoteOutput)
48 a1 := voteOutput.Source.Value.AssetId.Bytes()
50 amount = &voteOutput.Source.Value.Amount
51 destPos = &e.WitnessDestination.Position
52 s := e.SpentOutputId.Bytes()
57 txSigHashFn := func() []byte {
59 hasher := sha3pool.Get256()
60 defer sha3pool.Put256(hasher)
62 entryID.WriteTo(hasher)
67 hashBytes := hash.Bytes()
68 txSigHash = &hashBytes
78 result := &vm.Context{
79 VMVersion: prog.VmVersion,
80 Code: witnessProgram(prog.Code),
83 EntryID: entryID.Bytes(),
85 TxVersion: &tx.Version,
86 BlockHeight: &blockHeight,
88 TxSigHash: txSigHashFn,
89 NumResults: &numResults,
93 SpentOutputID: spentOutputID,
94 CheckOutput: ec.checkOutput,
100 func witnessProgram(prog []byte) []byte {
101 if segwit.IsP2WPKHScript(prog) {
102 if witnessProg, err := segwit.ConvertP2PKHSigProgram([]byte(prog)); err == nil {
105 } else if segwit.IsP2WSHScript(prog) {
106 if witnessProg, err := segwit.ConvertP2SHProgram([]byte(prog)); err == nil {
113 type entryContext struct {
115 entries map[bc.Hash]bc.Entry
118 func (ec *entryContext) checkOutput(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, expansion bool) (bool, error) {
119 checkEntry := func(e bc.Entry) (bool, error) {
120 check := func(prog *bc.Program, value *bc.AssetAmount) bool {
121 return (prog.VmVersion == vmVersion &&
122 bytes.Equal(prog.Code, code) &&
123 bytes.Equal(value.AssetId.Bytes(), assetID) &&
124 value.Amount == amount)
127 switch e := e.(type) {
128 case *bc.IntraChainOutput:
129 return check(e.ControlProgram, e.Source.Value), nil
132 return check(e.ControlProgram, e.Source.Value), nil
137 // The spec requires prog.Code to be the empty string only
138 // when !expansion. When expansion is true, we prepopulate
139 // prog.Code to give check() a freebie match.
141 // (The spec always requires prog.VmVersion to be zero.)
144 return check(&prog, e.Source.Value), nil
147 return false, vm.ErrContext
150 checkMux := func(m *bc.Mux) (bool, error) {
151 if index >= uint64(len(m.WitnessDestinations)) {
152 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= %d", index, len(m.WitnessDestinations))
154 eID := m.WitnessDestinations[index].Ref
155 e, ok := ec.entries[*eID]
157 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for mux destination %d, id %x, not found", index, eID.Bytes())
162 switch e := ec.entry.(type) {
167 d, ok := ec.entries[*e.WitnessDestination.Ref]
169 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for spend destination %x not found", e.WitnessDestination.Ref.Bytes())
171 if m, ok := d.(*bc.Mux); ok {
175 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
180 d, ok := ec.entries[*e.WitnessDestination.Ref]
182 return false, errors.Wrapf(bc.ErrMissingEntry, "entry for vetoInput destination %x not found", e.WitnessDestination.Ref.Bytes())
184 if m, ok := d.(*bc.Mux); ok {
188 return false, errors.Wrapf(vm.ErrBadValue, "index %d >= 1", index)
193 return false, vm.ErrContext