• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unpackdev / solgo / 9149422546

19 May 2024 05:37PM UTC coverage: 64.249% (-0.5%) from 64.782%
9149422546

Pull #214

github

0x19
Cleanup
Pull Request #214: Overall improvements

27 of 427 new or added lines in 20 files covered. (6.32%)

14 existing lines in 4 files now uncovered.

27494 of 42793 relevant lines covered (64.25%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/opcode/log.go
1
package opcode
2

3
import (
4
        "github.com/ethereum/go-ethereum/common"
5
)
6

7
// DecodeLOG1 returns all instructions with the LOG1 OpCode and decodes their arguments.
NEW
8
func (d *Decompiler) DecodeLOG1() []Instruction {
×
NEW
9
        return d.decodeLogInstructions(LOG1, 1)
×
NEW
10
}
×
11

12
// DecodeLOG2 returns all instructions with the LOG2 OpCode and decodes their arguments.
NEW
13
func (d *Decompiler) DecodeLOG2() []Instruction {
×
NEW
14
        return d.decodeLogInstructions(LOG2, 2)
×
NEW
15
}
×
16

17
// DecodeLOG3 returns all instructions with the LOG3 OpCode and decodes their arguments.
NEW
18
func (d *Decompiler) DecodeLOG3() []Instruction {
×
NEW
19
        return d.decodeLogInstructions(LOG3, 3)
×
NEW
20
}
×
21

22
// DecodeLOG4 returns all instructions with the LOG4 OpCode and decodes their arguments.
NEW
23
func (d *Decompiler) DecodeLOG4() []Instruction {
×
NEW
24
        return d.decodeLogInstructions(LOG4, 4)
×
NEW
25
}
×
26

27
// decodeLogInstructions processes the LOG instructions and decodes their arguments.
NEW
28
func (d *Decompiler) decodeLogInstructions(opCode OpCode, topicCount int) []Instruction {
×
NEW
29
        instructions := d.GetInstructionsByOpCode(opCode)
×
NEW
30

×
NEW
31
        for _, instruction := range instructions {
×
NEW
32
                data, topics := d.decodeLogArgs(instruction.Offset, topicCount)
×
NEW
33
                instruction.Data = data
×
NEW
34
                _ = topics
×
NEW
35
        }
×
36

NEW
37
        return instructions
×
38
}
39

40
// decodeLogArgs decodes the arguments for a LOG instruction.
NEW
41
func (d *Decompiler) decodeLogArgs(offset, topicCount int) ([]byte, []string) {
×
NEW
42
        var stack [][]byte
×
NEW
43
        currentIndex := -1
×
NEW
44

×
NEW
45
        // Find the index of the LOG instruction
×
NEW
46
        for i, instr := range d.instructions {
×
NEW
47
                if instr.Offset == offset {
×
NEW
48
                        currentIndex = i
×
NEW
49
                        break
×
50
                }
51
        }
52

NEW
53
        if currentIndex == -1 {
×
NEW
54
                return nil, nil
×
NEW
55
        }
×
56

57
        // Process instructions to reconstruct the stack state
NEW
58
        for i := 0; i < currentIndex; i++ {
×
NEW
59
                instr := d.instructions[i]
×
NEW
60
                switch {
×
NEW
61
                case instr.OpCode.IsPush():
×
NEW
62
                        stack = append(stack, instr.Args)
×
NEW
63
                case instr.OpCode == SWAP1:
×
NEW
64
                        if len(stack) >= 2 {
×
NEW
65
                                stack[len(stack)-1], stack[len(stack)-2] = stack[len(stack)-2], stack[len(stack)-1]
×
NEW
66
                        }
×
NEW
67
                case instr.OpCode == SWAP2:
×
NEW
68
                        if len(stack) >= 3 {
×
NEW
69
                                stack[len(stack)-1], stack[len(stack)-3] = stack[len(stack)-3], stack[len(stack)-1]
×
NEW
70
                        }
×
NEW
71
                case instr.OpCode == SWAP3:
×
NEW
72
                        if len(stack) >= 4 {
×
NEW
73
                                stack[len(stack)-1], stack[len(stack)-4] = stack[len(stack)-4], stack[len(stack)-1]
×
NEW
74
                        }
×
NEW
75
                case instr.OpCode == SWAP4:
×
NEW
76
                        if len(stack) >= 5 {
×
NEW
77
                                stack[len(stack)-1], stack[len(stack)-5] = stack[len(stack)-5], stack[len(stack)-1]
×
NEW
78
                        }
×
NEW
79
                case instr.OpCode == DUP1:
×
NEW
80
                        if len(stack) >= 1 {
×
NEW
81
                                stack = append(stack, stack[len(stack)-1])
×
NEW
82
                        }
×
NEW
83
                case instr.OpCode == DUP2:
×
NEW
84
                        if len(stack) >= 2 {
×
NEW
85
                                stack = append(stack, stack[len(stack)-2])
×
NEW
86
                        }
×
NEW
87
                case instr.OpCode == DUP3:
×
NEW
88
                        if len(stack) >= 3 {
×
NEW
89
                                stack = append(stack, stack[len(stack)-3])
×
NEW
90
                        }
×
NEW
91
                case instr.OpCode == DUP4:
×
NEW
92
                        if len(stack) >= 4 {
×
NEW
93
                                stack = append(stack, stack[len(stack)-4])
×
NEW
94
                        }
×
NEW
95
                case instr.OpCode == POP:
×
NEW
96
                        if len(stack) > 0 {
×
NEW
97
                                stack = stack[:len(stack)-1]
×
NEW
98
                        }
×
99
                }
100
        }
101

102
        // Collect the data and topics for the LOG instruction
NEW
103
        if len(stack) < topicCount+1 {
×
NEW
104
                return nil, nil
×
NEW
105
        }
×
106

NEW
107
        topics := make([]string, 0)
×
NEW
108
        var data []byte
×
NEW
109
        topicCountFound := 0
×
NEW
110

×
NEW
111
        // Iterate from the end of the stack to find topics and data
×
NEW
112
        for i := len(stack) - 1; i >= 0; i-- {
×
NEW
113
                if len(stack[i]) == 32 && topicCountFound < topicCount {
×
NEW
114
                        topics = append([]string{common.Bytes2Hex(stack[i])}, topics...) // Prepend to maintain order
×
NEW
115
                        topicCountFound++
×
NEW
116
                } else if data == nil {
×
NEW
117
                        data = stack[i]
×
NEW
118
                }
×
119

120
                // Break if we have found all topics and data
NEW
121
                if topicCountFound == topicCount && data != nil {
×
NEW
122
                        break
×
123
                }
124
        }
125

NEW
126
        if data == nil {
×
NEW
127
                return nil, nil
×
NEW
128
        }
×
129

NEW
130
        return data, topics
×
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc