-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add barebones builder with mempool validation facilities
- Loading branch information
1 parent
63f66ac
commit caab622
Showing
2 changed files
with
131 additions
and
0 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,68 @@ | ||
package builder | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/NethermindEth/juno/blockchain" | ||
"github.com/NethermindEth/juno/core" | ||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/db" | ||
"github.com/NethermindEth/juno/mempool" | ||
"github.com/NethermindEth/juno/utils" | ||
"github.com/NethermindEth/juno/vm" | ||
) | ||
|
||
type Builder struct { | ||
ownAddress felt.Felt | ||
|
||
bc *blockchain.Blockchain | ||
vm vm.VM | ||
|
||
log utils.Logger | ||
} | ||
|
||
func New(ownAddr *felt.Felt, bc *blockchain.Blockchain, builderVM vm.VM, log utils.Logger) *Builder { | ||
return &Builder{ | ||
ownAddress: *ownAddr, | ||
|
||
bc: bc, | ||
vm: builderVM, | ||
} | ||
} | ||
|
||
// ValidateAgainstPendingState validates a user transaction against the pending state | ||
// only hard-failures result in an error, reverts are not reported back to caller | ||
func (b *Builder) ValidateAgainstPendingState(userTxn *mempool.BroadcastedTransaction) error { | ||
declaredClasses := []core.Class{} | ||
if userTxn.DeclaredClass != nil { | ||
declaredClasses = []core.Class{userTxn.DeclaredClass} | ||
} | ||
|
||
nextHeight := uint64(0) | ||
if height, err := b.bc.Height(); err == nil { | ||
nextHeight = height + 1 | ||
} else if err != nil && !errors.Is(err, db.ErrKeyNotFound) { | ||
return err | ||
} | ||
|
||
pendingBlock, err := b.bc.Pending() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
state, stateCloser, err := b.bc.PendingState() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if err = stateCloser(); err != nil { | ||
b.log.Errorw("closing state in ValidateAgainstPendingState", "err", err) | ||
} | ||
}() | ||
|
||
_, _, err = b.vm.Execute([]core.Transaction{userTxn.Transaction}, declaredClasses, nextHeight, | ||
pendingBlock.Block.Timestamp, &b.ownAddress, state, b.bc.Network(), []*felt.Felt{}, | ||
false, false, false, pendingBlock.Block.GasPrice, pendingBlock.Block.GasPriceSTRK, false) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package builder_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/NethermindEth/juno/blockchain" | ||
"github.com/NethermindEth/juno/builder" | ||
"github.com/NethermindEth/juno/clients/feeder" | ||
"github.com/NethermindEth/juno/core" | ||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/db/pebble" | ||
"github.com/NethermindEth/juno/mempool" | ||
"github.com/NethermindEth/juno/mocks" | ||
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" | ||
"github.com/NethermindEth/juno/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestValidateAgainstPendingState(t *testing.T) { | ||
testDB := pebble.NewMemTest(t) | ||
mockCtrl := gomock.NewController(t) | ||
mockVM := mocks.NewMockVM(mockCtrl) | ||
bc := blockchain.New(testDB, utils.Integration, utils.NewNopZapLogger()) | ||
seqAddr := utils.HexToFelt(t, "0xDEADBEEF") | ||
testBuilder := builder.New(seqAddr, bc, mockVM, utils.NewNopZapLogger()) | ||
|
||
client := feeder.NewTestClient(t, utils.Integration) | ||
gw := adaptfeeder.New(client) | ||
|
||
su, b, err := gw.StateUpdateWithBlock(context.Background(), 0) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, bc.StorePending(&blockchain.Pending{ | ||
Block: b, | ||
StateUpdate: su, | ||
})) | ||
|
||
userTxn := mempool.BroadcastedTransaction{ | ||
Transaction: &core.InvokeTransaction{ | ||
TransactionHash: utils.HexToFelt(t, "0x1337"), | ||
}, | ||
DeclaredClass: &core.Cairo0Class{ | ||
Program: "best program", | ||
}, | ||
} | ||
|
||
mockVM.EXPECT().Execute([]core.Transaction{userTxn.Transaction}, | ||
[]core.Class{userTxn.DeclaredClass}, uint64(0), b.Timestamp, seqAddr, | ||
gomock.Any(), utils.Integration, []*felt.Felt{}, false, false, | ||
false, b.GasPrice, b.GasPriceSTRK, false).Return(nil, nil, nil) | ||
assert.NoError(t, testBuilder.ValidateAgainstPendingState(&userTxn)) | ||
|
||
require.NoError(t, bc.Store(b, &core.BlockCommitments{}, su, nil)) | ||
mockVM.EXPECT().Execute([]core.Transaction{userTxn.Transaction}, | ||
[]core.Class{userTxn.DeclaredClass}, uint64(1), b.Timestamp+1, seqAddr, | ||
gomock.Any(), utils.Integration, []*felt.Felt{}, false, false, | ||
false, b.GasPrice, b.GasPriceSTRK, false).Return(nil, nil, errors.New("oops")) | ||
assert.EqualError(t, testBuilder.ValidateAgainstPendingState(&userTxn), "oops") | ||
} |