Skip to content

Commit

Permalink
update parsing to fix condition OP_RETURN splits not detected
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Mar 29, 2023
1 parent fc70ed6 commit 8ff4833
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__debug_bin.exe
__debug_bin.exe
.DS_Store
test/.DS_Store
53 changes: 30 additions & 23 deletions bpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,38 +165,44 @@ func (x *XPut) fromScript(config ParseConfig, script *bscript.Script, idx uint8)
if err != nil {
return err
}

var requireMet = false
var reqOmitted = true
var splitterRequirementMet bool
requireMet := make(map[int]bool)
splitterRequirementMet := make(map[int]bool)
var prevSplitter bool
var isSplitter bool
for cIdx, part := range parts {
for _, req := range config.SplitConfig {
for configIndex, req := range config.SplitConfig {
var reqOmitted = true
if req.Require != nil {
reqOmitted = false

// Look through previous parts to see if the required token is found
chunksToCheck := parts[cIdx:]
chunksToCheck := parts[:cIdx]

for _, c := range chunksToCheck {
if len(c) == 1 && c[0] == *req.Require {
requireMet = true
requireMet[configIndex] = true
break
}
}
}
splitterRequirementMet[configIndex] = reqOmitted || requireMet[configIndex]
}

splitterRequirementMet = reqOmitted || requireMet

tape_i, cell_i, _, err = x.processChunk(part, config, uint8(cIdx), idx, splitterRequirementMet, tape_i, cell_i)
if prevSplitter {
cell_i = 0
tape_i++
prevSplitter = false
}
tape_i, cell_i, isSplitter, err = x.processChunk(part, config, uint8(cIdx), idx, splitterRequirementMet, tape_i, cell_i)
if err != nil {
return err
}
prevSplitter = isSplitter
}
}
return nil
}

func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx uint8, requireMet bool, tape_i, cell_i uint8) (uint8, uint8, bool, error) {
func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx uint8, requireMet map[int]bool, tape_i, cell_i uint8) (uint8, uint8, bool, error) {

if x.Tape == nil {
x.Tape = make([]Tape, 0)
Expand Down Expand Up @@ -234,7 +240,7 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u

// Split config provided
if o.SplitConfig != nil {
for _, setting := range o.SplitConfig {
for configIndex, setting := range o.SplitConfig {
if ops != nil {
// Check if this is a manual seperator that happens to also be an opcode
var splitOpStrPtr *string
Expand All @@ -244,16 +250,19 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u
}
// or an actual op splitter
if setting.Token != nil && (setting.Token.Op != nil && *setting.Token.Op == *op) || (setting.Token.Ops != nil && setting.Token.Ops == ops) || (setting.Token.S != nil && splitOpStrPtr != nil && *setting.Token.S == *splitOpStrPtr) {
if setting.Require == nil || requireMet {
if setting.Require == nil || requireMet[configIndex] {
splitter = setting.Include
isSplitter = true
}
}
} else {
// Script type
if setting.Token != nil && (s != nil && setting.Token.S != nil && *setting.Token.S == *s) || (b != nil && setting.Token.B != nil && *setting.Token.B == *b) {
splitter = setting.Include
isSplitter = true
if setting.Require == nil || requireMet[configIndex] {
splitter = setting.Include

isSplitter = true
}
}
}
}
Expand All @@ -269,7 +278,7 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u
// Don't include the seperator by default, just make a new tape and reset cell
cell = make([]Cell, 0)
cell_i = 0
tape_i++
// tape_i++

} else if *splitter == IncludeL {
if isOpType {
Expand Down Expand Up @@ -306,7 +315,7 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u
// otherwise make a new tape
outTapes := append(x.Tape, Tape{Cell: cell, I: tape_i})
x.Tape = outTapes
tape_i++
// tape_i++
}

cell_i = 0
Expand All @@ -330,7 +339,6 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u

cell = []Cell{*item}
cell_i = 1

} else if *splitter == IncludeR {
outTapes := append(x.Tape, Tape{Cell: cell, I: tape_i})
x.Tape = outTapes
Expand All @@ -355,7 +363,6 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u
cell = make([]Cell, 0)
cell_i = 0
}

} else {
if o.Transform != nil {
t := *o.Transform
Expand Down Expand Up @@ -390,15 +397,15 @@ func (x *XPut) processChunk(chunk []byte, o ParseConfig, chunkIndex uint8, idx u
// create new tape if needed
if len(x.Tape) == int(tape_i) {
x.Tape = append(x.Tape, Tape{
I: tape_i,
I: tape_i,
Cell: make([]Cell, 0),
})
}

cell = append(x.Tape[tape_i].Cell, *item)

// add the cell to the tape
x.Tape[tape_i].Cell = cell

// reset cell
}

}
Expand Down
32 changes: 24 additions & 8 deletions bpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
"github.com/stretchr/testify/assert"
)

var sampleTx, bitchatTx, bpuBuster, boostTx string
var sampleTx, bitchatTx, bpuBuster, boostTx, bigOrdTx string

func init() {
sampleTx = test.GetTestHex("./test/data/98a5f6ef18eaea188bdfdc048f89a48af82627a15a76fd53584975f28ab3cc39.hex")
bitchatTx = test.GetTestHex("./test/data/653947cee3268c26efdcc97ef4e775d990e49daf81ecd2555127bda22fe5a21f.hex")
bpuBuster = test.GetTestHex("./test/data/58d8d8407ceb37c4a04bd76ea4c78c504c4692647ad5646ecfc9bd3187cb7266.hex")
boostTx = test.GetTestHex("./test/data/c5c7248302683107aa91014fd955908a7c572296e803512e497ddf7d1f458bd3.hex")
bigOrdTx = test.GetTestHex("./test/data/c8cd6ff398d23e12e65ab065757fe6caf2d74b5e214b638365d61583030aa069.hex")
}

var seperator = "|"
var l = IncludeL
var opReturn = uint8(106)
var opFalse = uint8(0)

var splitConfig = []SplitConfig{
{
Expand All @@ -31,12 +31,6 @@ var splitConfig = []SplitConfig{
},
Include: &l,
},
{
Token: &Token{
Op: &opFalse,
},
Include: &l,
},
{
Token: &Token{
S: &seperator,
Expand Down Expand Up @@ -193,4 +187,26 @@ func TestBoost(t *testing.T) {
})
}

func TestOrd(t *testing.T) {

t.Run("bpu.Parse ord", func(t *testing.T) {
bpuTx, err := Parse(ParseConfig{RawTxHex: &bigOrdTx, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
assert.Nil(t, err)

assert.NotNil(t, bpuTx)

assert.Equal(t, 1, len(bpuTx.In))
assert.Equal(t, 1437, len(bpuTx.Out))

assert.NotNil(t, bpuTx.Out[0].Tape)
assert.NotNil(t, bpuTx.Out[0].XPut.Tape)
assert.Equal(t, 2, len(bpuTx.Out[0].Tape))
assert.Equal(t, 14, len(bpuTx.Out[0].Tape[0].Cell))
assert.Equal(t, 8, len(bpuTx.Out[0].Tape[1].Cell))
})
}

// TODO: Split tests

Large diffs are not rendered by default.

0 comments on commit 8ff4833

Please sign in to comment.