github.com/koko1123/flow-go-1@v0.29.6/fvm/programs/programs.go (about) 1 package programs 2 3 import ( 4 "sync" 5 6 "github.com/onflow/cadence/runtime/common" 7 8 "github.com/koko1123/flow-go-1/fvm/derived" 9 "github.com/koko1123/flow-go-1/fvm/state" 10 ) 11 12 // TODO(patrick): rm after https://github.com/onflow/flow-emulator/pull/229 13 // is merged and integrated. 14 type Programs struct { 15 lock sync.RWMutex 16 17 block *derived.DerivedBlockData 18 currentTxn *derived.DerivedTransactionData 19 20 logicalTime derived.LogicalTime 21 } 22 23 func NewEmptyPrograms() *Programs { 24 block := derived.NewEmptyDerivedBlockData() 25 txn, err := block.NewDerivedTransactionData(0, 0) 26 if err != nil { 27 panic(err) 28 } 29 30 return &Programs{ 31 block: block, 32 currentTxn: txn, 33 logicalTime: 0, 34 } 35 } 36 37 func (p *Programs) ChildPrograms() *Programs { 38 p.lock.RLock() 39 defer p.lock.RUnlock() 40 41 childBlock := p.block.NewChildDerivedBlockData() 42 txn, err := childBlock.NewDerivedTransactionData(0, 0) 43 if err != nil { 44 panic(err) 45 } 46 47 return &Programs{ 48 block: childBlock, 49 currentTxn: txn, 50 logicalTime: 0, 51 } 52 } 53 54 func (p *Programs) NextTxIndexForTestingOnly() uint32 { 55 return p.block.NextTxIndexForTestingOnly() 56 } 57 58 func (p *Programs) GetForTestingOnly(location common.AddressLocation) (*derived.Program, *state.State, bool) { 59 return p.GetProgram(location) 60 } 61 62 func (p *Programs) GetProgram(location common.AddressLocation) (*derived.Program, *state.State, bool) { 63 p.lock.RLock() 64 defer p.lock.RUnlock() 65 66 return p.currentTxn.GetProgram(location) 67 } 68 69 func (p *Programs) SetProgram(location common.AddressLocation, program *derived.Program, state *state.State) { 70 p.lock.RLock() 71 defer p.lock.RUnlock() 72 73 p.currentTxn.SetProgram(location, program, state) 74 } 75 76 func (p *Programs) Cleanup(invalidator derived.TransactionInvalidator) { 77 p.lock.Lock() 78 defer p.lock.Unlock() 79 80 p.currentTxn.AddInvalidator(invalidator) 81 82 var err error 83 err = p.currentTxn.Commit() 84 if err != nil { 85 panic(err) 86 } 87 88 p.logicalTime++ 89 txn, err := p.block.NewDerivedTransactionData(p.logicalTime, p.logicalTime) 90 if err != nil { 91 panic(err) 92 } 93 94 p.currentTxn = txn 95 }