From 11e2ebdfab6295dcaeaa8c7788a5107493ed90be Mon Sep 17 00:00:00 2001 From: Jose Hugo De la cruz Romero Date: Tue, 20 Apr 2021 10:49:59 -0600 Subject: [PATCH 1/5] Ignore PUSH data bytes, detect and ignore metadata corresponding to the contract swarm address --- codetrie/codetrie.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/codetrie/codetrie.go b/codetrie/codetrie.go index 293d2fbbb4f3..fd5a2cbb809f 100644 --- a/codetrie/codetrie.go +++ b/codetrie/codetrie.go @@ -20,6 +20,7 @@ var ( versionValue = []byte{0x00} codeLengthKey = []byte{0xff, 0xfe} codeHashKey = []byte{0xff, 0xff} + swarmIdent = []byte{0xa1, 0x65, 0x62, 0x7a, 0x7a, 0x72, 0x30} // a165 bzzr0 ) type Trie interface { @@ -217,19 +218,46 @@ func setFIO(chunks []*Chunk) { } chunkSize := len(chunks[0].code) + swarmMatch := 0 + swarmLeft := 0 for i, chunk := range chunks { if i == len(chunks)-1 { break } + pushDataLeft := 0 for j, op := range chunk.code { + + if swarmLeft > 0 { + swarmLeft -= 1 // skip bytes corresponding to swarm metadata + continue + } + + if op == swarmIdent[swarmMatch] { + swarmMatch += 1 + if swarmMatch == len(swarmIdent) { + swarmLeft = 36 + } + } else { + swarmMatch = 0 + } + + if uint8(j) < chunk.fio { // FIO already set in previous chunk analysis + continue + } + if pushDataLeft > 0 { // current byte corresponds to PUSH data + pushDataLeft -= 1 + continue + } + opcode := OpCode(op) // Push is the only opcode with immediate if !opcode.IsPush() { continue } size := getPushSize(opcode) + pushDataLeft = size // Fits within chunk if j+size < chunkSize { continue From 7896c66e977e48dacbd05b9b67ef25e2a0a6efc3 Mon Sep 17 00:00:00 2001 From: Jose Hugo De la cruz Romero Date: Tue, 20 Apr 2021 21:19:01 -0600 Subject: [PATCH 2/5] fix, reset swarmMatch to 0 --- codetrie/codetrie.go | 1 + 1 file changed, 1 insertion(+) diff --git a/codetrie/codetrie.go b/codetrie/codetrie.go index fd5a2cbb809f..8c63236a5aab 100644 --- a/codetrie/codetrie.go +++ b/codetrie/codetrie.go @@ -238,6 +238,7 @@ func setFIO(chunks []*Chunk) { swarmMatch += 1 if swarmMatch == len(swarmIdent) { swarmLeft = 36 + swarmMatch = 0 } } else { swarmMatch = 0 From 903dd76dee3071061734f1b4d466f40d7ac69798 Mon Sep 17 00:00:00 2001 From: Jose Hugo De la cruz Romero Date: Wed, 21 Apr 2021 17:03:33 -0600 Subject: [PATCH 3/5] Validate swarm metadata starting with 0xa2 --- codetrie/codetrie.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codetrie/codetrie.go b/codetrie/codetrie.go index 8c63236a5aab..3fe0b798c741 100644 --- a/codetrie/codetrie.go +++ b/codetrie/codetrie.go @@ -234,7 +234,7 @@ func setFIO(chunks []*Chunk) { continue } - if op == swarmIdent[swarmMatch] { + if op == swarmIdent[swarmMatch] || (swarmMatch == 0 && op == 0xa2) { swarmMatch += 1 if swarmMatch == len(swarmIdent) { swarmLeft = 36 From d3c7b7e43ad586046c95a32d66f88c4661a47b02 Mon Sep 17 00:00:00 2001 From: Jose Hugo De la cruz Romero Date: Thu, 22 Apr 2021 12:07:43 -0600 Subject: [PATCH 4/5] Ingnore the rest of the metadata --- codetrie/codetrie.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/codetrie/codetrie.go b/codetrie/codetrie.go index 3fe0b798c741..0e0571651d9f 100644 --- a/codetrie/codetrie.go +++ b/codetrie/codetrie.go @@ -219,7 +219,6 @@ func setFIO(chunks []*Chunk) { chunkSize := len(chunks[0].code) swarmMatch := 0 - swarmLeft := 0 for i, chunk := range chunks { if i == len(chunks)-1 { @@ -229,17 +228,12 @@ func setFIO(chunks []*Chunk) { pushDataLeft := 0 for j, op := range chunk.code { - if swarmLeft > 0 { - swarmLeft -= 1 // skip bytes corresponding to swarm metadata - continue + if swarmMatch == len(swarmIdent) { + continue // skip bytes corresponding to swarm metadata } if op == swarmIdent[swarmMatch] || (swarmMatch == 0 && op == 0xa2) { swarmMatch += 1 - if swarmMatch == len(swarmIdent) { - swarmLeft = 36 - swarmMatch = 0 - } } else { swarmMatch = 0 } From f1fcb259a06eb2f5309b758e82830c086fdb08fc Mon Sep 17 00:00:00 2001 From: Jose Hugo De la cruz Romero Date: Thu, 22 Apr 2021 14:05:06 -0600 Subject: [PATCH 5/5] Different bzzr versions --- codetrie/codetrie.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codetrie/codetrie.go b/codetrie/codetrie.go index 0e0571651d9f..30382f1593df 100644 --- a/codetrie/codetrie.go +++ b/codetrie/codetrie.go @@ -20,7 +20,7 @@ var ( versionValue = []byte{0x00} codeLengthKey = []byte{0xff, 0xfe} codeHashKey = []byte{0xff, 0xff} - swarmIdent = []byte{0xa1, 0x65, 0x62, 0x7a, 0x7a, 0x72, 0x30} // a165 bzzr0 + swarmIdent = []byte{0xa1, 0x65, 0x62, 0x7a, 0x7a, 0x72} // a165 bzzr ) type Trie interface {