-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package contract | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type InternalOperation struct { | ||
Id int64 `json:"-"` | ||
Level int `json:"level"` | ||
Contract string `json:"contract"` | ||
Operation string `json:"operation"` | ||
Amount string `json:"amount"` | ||
Args []string `json:"args"` | ||
Results []string `json:"results"` | ||
} | ||
|
||
var ( | ||
internalOperations []InternalOperation | ||
txHash []byte | ||
opsLock sync.Mutex // To handle concurrent updates | ||
nextOpId int64 | ||
) | ||
|
||
func initInternalOps(newTxHash []byte) { | ||
opsLock.Lock() | ||
defer opsLock.Unlock() | ||
|
||
internalOperations = nil // Reinitialize the slice for new transaction | ||
txHash = newTxHash | ||
nextOpId = 1 // Reset nextOpId for each new transaction | ||
} | ||
|
||
func logOperation(ctx *vmContext, amount string, operation string, args ...string) int64 { | ||
opsLock.Lock() | ||
defer opsLock.Unlock() | ||
|
||
op := InternalOperation{ | ||
Id: nextOpId, | ||
Level: ctx.CallDepth(), | ||
Contract: ctx.Contract, | ||
Operation: operation, | ||
Amount: amount, | ||
Args: args, | ||
} | ||
internalOperations = append(internalOperations, op) | ||
nextOpId++ | ||
return op.Id | ||
} | ||
|
||
func logOperationResult(operationId int64, results ...string) { | ||
opsLock.Lock() | ||
defer opsLock.Unlock() | ||
|
||
for i, op := range internalOperations { | ||
if op.Id == operationId { | ||
internalOperations[i].Results = append(internalOperations[i].Results, results...) | ||
return | ||
} | ||
} | ||
log.Printf("No operation found with ID %d", operationId) | ||
} | ||
|
||
func saveOperations() { | ||
opsLock.Lock() | ||
defer opsLock.Unlock() | ||
|
||
data, err := json.Marshal(internalOperations) | ||
if err != nil { | ||
log.Fatal("Failed to marshal operations:", err) | ||
return | ||
} | ||
|
||
// Simulated database set function (adjust as needed for actual database implementation) | ||
err = db.Set(txHash, data) // Assuming db.Set exists and works with byte keys and value | ||
if err != nil { | ||
log.Fatal("Failed to save operations to database:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters